Coverage for hdl_registers/test/test_register.py: 100%

83 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-01 20:50 +0000

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

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

3# 

4# This file is part of the hdl-registers project, an HDL register generator fast enough to run 

5# in real time. 

6# https://hdl-registers.com 

7# https://github.com/hdl-registers/hdl-registers 

8# -------------------------------------------------------------------------------------------------- 

9 

10# Third party libraries 

11import pytest 

12 

13# First party libraries 

14from hdl_registers.register import Register 

15from hdl_registers.register_modes import REGISTER_MODES 

16 

17 

18def test_repr_basic(): 

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

20 assert "apa" in repr(Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="")) 

21 

22 # Different name 

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

24 Register(name="hest", index=0, mode=REGISTER_MODES["r"], description="") 

25 ) 

26 

27 # Different index 

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

29 Register(name="apa", index=1, mode=REGISTER_MODES["r"], description="") 

30 ) 

31 

32 # Different mode 

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

34 Register(name="apa", index=0, mode=REGISTER_MODES["w"], description="") 

35 ) 

36 

37 # Different description 

38 assert repr( 

39 Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="Blah") 

40 ) != repr(Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="Gah")) 

41 

42 

43def test_repr_with_bits_appended(): 

44 """ 

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

46 """ 

47 register_a = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

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

49 

50 register_b = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

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

52 

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

54 

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

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

57 

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

59 

60 

61def test_bits_are_appended_properly_and_can_be_edited_in_place(): 

62 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

63 

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

65 assert bit_hest.base_index == 0 

66 

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

68 assert bit_zebra.base_index == 1 

69 

70 bit_hest.description = "new desc" 

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

72 

73 

74def test_bit_vectors_are_appended_properly_and_can_be_edited_in_place(): 

75 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

76 

77 bit_vector_hest = register.append_bit_vector( 

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

79 ) 

80 assert bit_vector_hest.base_index == 0 

81 

82 bit_vector_zebra = register.append_bit_vector( 

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

84 ) 

85 assert bit_vector_zebra.base_index == 3 

86 

87 bit_vector_hest.description = "new desc" 

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

89 

90 

91def test_integers_are_appended_properly_and_can_be_edited_in_place(): 

92 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

93 

94 integer_hest = register.append_integer( 

95 name="hest", 

96 description="", 

97 min_value=0, 

98 max_value=10, 

99 default_value=0, 

100 ) 

101 assert integer_hest.base_index == 0 

102 

103 integer_zebra = register.append_integer( 

104 name="zebra", 

105 description="", 

106 min_value=0, 

107 max_value=20, 

108 default_value=0, 

109 ) 

110 assert integer_zebra.base_index == 4 

111 

112 integer_hest.description = "new desc" 

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

114 

115 

116def test_appending_bit_to_full_register(): 

117 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

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

119 

120 with pytest.raises(ValueError) as exception_info: 

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

122 assert str(exception_info.value) == 'Maximum width exceeded for register "apa".' 

123 

124 

125def test_appending_bit_vector_to_full_register(): 

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

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

128 

129 with pytest.raises(ValueError) as exception_info: 

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

131 assert str(exception_info.value) == 'Maximum width exceeded for register "apa".' 

132 

133 

134def test_appending_integer_to_full_register(): 

135 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

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

137 

138 with pytest.raises(ValueError) as exception_info: 

139 register.append_integer( 

140 name="zebra", 

141 description="", 

142 min_value=0, 

143 max_value=4, 

144 default_value=0, 

145 ) 

146 assert str(exception_info.value) == 'Maximum width exceeded for register "apa".' 

147 

148 

149def test_default_value(): 

150 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

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

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

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

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

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

156 

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

158 

159 

160def test_default_value_can_be_updated(): 

161 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

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

163 

164 assert register.default_value == 1 

165 

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

167 assert register.default_value == 0 

168 

169 

170def test_get_field(): 

171 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="") 

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

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

174 

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

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

177 

178 with pytest.raises(ValueError) as exception_info: 

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

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