Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9import pytest 

10from tsfpga.registers.register import Register 

11 

12 

13def test_repr_basic(): 

14 # Check that repr is an actual representation, not just "X object at 0xABCDEF" 

15 assert "apa" in repr(Register(name="apa", index=0, mode="r", description="")) 

16 

17 # Different name 

18 assert repr(Register(name="apa", index=0, mode="r", description="")) != repr( 

19 Register(name="hest", index=0, mode="r", description="") 

20 ) 

21 

22 # Different index 

23 assert repr(Register(name="apa", index=0, mode="r", description="")) != repr( 

24 Register(name="apa", index=1, mode="r", description="") 

25 ) 

26 

27 # Different mode 

28 assert repr(Register(name="apa", index=0, mode="r", description="")) != repr( 

29 Register(name="apa", index=0, mode="w", description="") 

30 ) 

31 

32 # Different description 

33 assert repr(Register(name="apa", index=0, mode="r", description="Blaah")) != repr( 

34 Register(name="apa", index=0, mode="r", description="Gaah") 

35 ) 

36 

37 

38def test_repr_with_bits_appended(): 

39 """ 

40 Shows that the ``fields`` impact the repr. Do not need to test with other fields than bits. 

41 """ 

42 register_a = Register(name="apa", index=0, mode="r", description="") 

43 register_a.append_bit(name="hest", description="", default_value="0") 

44 

45 register_b = Register(name="apa", index=0, mode="r", description="") 

46 register_b.append_bit(name="hest", description="", default_value="0") 

47 

48 assert repr(register_a) == repr(register_b) 

49 

50 register_a.append_bit(name="zebra", description="", default_value="0") 

51 register_b.append_bit(name="foo", description="", default_value="0") 

52 

53 assert repr(register_a) != repr(register_b) 

54 

55 

56def test_bits_are_appended_properly_and_can_be_edited_in_place(): 

57 register = Register(name="apa", index=0, mode="r", description="") 

58 

59 bit_hest = register.append_bit(name="hest", description="abc", default_value="0") 

60 assert bit_hest.base_index == 0 

61 

62 bit_zebra = register.append_bit(name="zebra", description="def", default_value="0") 

63 assert bit_zebra.base_index == 1 

64 

65 bit_hest.description = "new desc" 

66 assert register.fields[0].description == "new desc" 

67 

68 

69def test_bit_vectors_are_appended_properly_and_can_be_edited_in_place(): 

70 register = Register(name="apa", index=0, mode="r", description="") 

71 

72 bit_vector_hest = register.append_bit_vector( 

73 name="hest", description="abc", width=3, default_value="000" 

74 ) 

75 assert bit_vector_hest.base_index == 0 

76 

77 bit_vector_zebra = register.append_bit_vector( 

78 name="zebra", description="def", width=5, default_value="00000" 

79 ) 

80 assert bit_vector_zebra.base_index == 3 

81 

82 bit_vector_hest.description = "new desc" 

83 assert register.fields[0].description == "new desc" 

84 

85 

86def test_appending_bit_to_full_register(): 

87 register = Register(name="apa", index=0, mode="r", description="") 

88 register.append_bit_vector(name="foo", width=32, description="", default_value="0" * 32) 

89 

90 with pytest.raises(ValueError) as exception_info: 

91 register.append_bit(name="bar", description="", default_value="0") 

92 assert str(exception_info.value).startswith('Maximum width exceeded for register "apa"') 

93 

94 

95def test_appending_bit_vector_to_full_register(): 

96 register = Register(name="apa", index=0, mode="r", description="") 

97 register.append_bit_vector(name="foo", width=30, description="", default_value="0" * 30) 

98 

99 with pytest.raises(ValueError) as exception_info: 

100 register.append_bit_vector(name="bar", description="", width=3, default_value="000") 

101 assert str(exception_info.value).startswith('Maximum width exceeded for register "apa"') 

102 

103 

104def test_default_value(): 

105 register = Register(name="apa", index=0, mode="r", description="") 

106 register.append_bit(name="foo", description="", default_value="1") 

107 register.append_bit(name="foo", description="", default_value="0") 

108 register.append_bit(name="foo", description="", default_value="1") 

109 register.append_bit_vector(name="foo", description="", width=4, default_value="0110") 

110 register.append_bit_vector(name="foo", description="", width=4, default_value="0101") 

111 

112 assert register.default_value == 1 * 2 ** 0 + 1 * 2 ** 2 + 6 * 2 ** 3 + 5 * 2 ** 7 

113 

114 

115def test_default_value_can_be_updated(): 

116 register = Register(name="apa", index=0, mode="r", description="") 

117 register.append_bit(name="foo", description="", default_value="1") 

118 

119 assert register.default_value == 1 

120 

121 register.fields[0].default_value = "0" 

122 assert register.default_value == 0 

123 

124 

125def test_get_field(): 

126 register = Register(name="apa", index=0, mode="r", description="") 

127 hest = register.append_bit(name="hest", description="", default_value="1") 

128 zebra = register.append_bit(name="zebra", description="", default_value="1") 

129 

130 assert register.get_field("hest") is hest 

131 assert register.get_field("zebra") is zebra 

132 

133 with pytest.raises(ValueError) as exception_info: 

134 assert register.get_field("non existing") is None 

135 assert str(exception_info.value) == 'Could not find field "non existing" within register "apa"'