Coverage for hdl_registers/test/test_register_list.py: 100%
146 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 20:51 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 20:51 +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# --------------------------------------------------------------------------------------------------
10# Standard libraries
11import copy
12from pathlib import Path
14# Third party libraries
15import pytest
17# First party libraries
18from hdl_registers.register import Register
19from hdl_registers.register_list import RegisterList
20from hdl_registers.register_modes import REGISTER_MODES
23def test_from_default_registers():
24 register_a = Register(name="a", index=0, mode=REGISTER_MODES["r"], description="AA")
25 register_b = Register(name="b", index=1, mode=REGISTER_MODES["w"], description="BB")
26 default_registers = [register_a, register_b]
28 register_list = RegisterList.from_default_registers(
29 name="apa", source_definition_file=None, default_registers=default_registers
30 )
32 # Change some things in the register objects to show that they are copied
33 default_registers.append(
34 Register(name="c", index=2, mode=REGISTER_MODES["r_w"], description="CC")
35 )
36 register_a.mode = REGISTER_MODES["w"]
37 register_b.name = "x"
39 print(register_list.get_register("a").mode)
40 print(REGISTER_MODES["r"])
42 assert len(register_list.register_objects) == 2
43 assert register_list.get_register("a").mode == REGISTER_MODES["r"]
44 assert register_list.get_register("b").name == "b"
47def test_from_default_registers_with_bad_indexes_should_raise_exception():
48 register_a = Register(name="a", index=0, mode=REGISTER_MODES["r"], description="")
49 register_b = Register(name="b", index=0, mode=REGISTER_MODES["w"], description="")
50 default_registers = [register_a, register_b]
52 with pytest.raises(ValueError) as exception_info:
53 RegisterList.from_default_registers(
54 name="apa", source_definition_file=None, default_registers=default_registers
55 )
56 assert (
57 str(exception_info.value)
58 == 'Default register index mismatch for "b". Got "0", expected "1".'
59 )
62def test_header_constants():
63 registers = RegisterList(name="apa", source_definition_file=None)
64 hest = registers.add_constant("hest", 123, "")
65 zebra = registers.add_constant("zebra", 456, "description")
67 assert len(registers.constants) == 2
69 assert registers.get_constant("hest") == hest
70 assert registers.get_constant("zebra") == zebra
72 with pytest.raises(ValueError) as exception_info:
73 assert registers.get_constant("non existing") is None
74 assert (
75 str(exception_info.value)
76 == 'Could not find constant "non existing" within register list "apa"'
77 )
79 zebra.value = -5
80 assert registers.get_constant("zebra").value == -5
83def test_registers_are_appended_properly_and_can_be_edited_in_place():
84 register_array = RegisterList(name="apa", source_definition_file=Path("."))
86 register_hest = register_array.append_register(
87 name="hest", mode=REGISTER_MODES["r"], description=""
88 )
89 assert register_hest.index == 0
91 register_zebra = register_array.append_register(
92 name="zebra", mode=REGISTER_MODES["r"], description=""
93 )
94 assert register_zebra.index == 1
96 register_hest.description = "new desc"
97 assert register_array.register_objects[0].description == "new desc"
100def test_register_arrays_are_appended_properly_and_can_be_edited_in_place():
101 register_array = RegisterList(name="apa", source_definition_file=Path("."))
103 register_array_hest = register_array.append_register_array(
104 name="hest", length=4, description=""
105 )
106 assert register_array_hest.base_index == 0
107 register_array_hest.append_register(name="foo", mode=REGISTER_MODES["r"], description="")
108 register_array_hest.append_register(name="bar", mode=REGISTER_MODES["w"], description="")
110 register_array_zebra = register_array.append_register_array(
111 name="zebra", length=2, description=""
112 )
113 assert register_array_zebra.base_index == 8
116def test_get_register():
117 register_list = RegisterList(name="apa", source_definition_file=None)
118 apa = register_list.append_register(name="apa", mode=REGISTER_MODES["r"], description="")
119 hest = register_list.append_register(name="hest", mode=REGISTER_MODES["r"], description="")
120 register_array = register_list.append_register_array(
121 name="register_array", length=3, description=""
122 )
123 zebra = register_array.append_register(name="zebra", mode=REGISTER_MODES["r"], description="")
125 assert register_list.get_register(register_name="apa") is apa
126 assert register_list.get_register(register_name="hest") is hest
128 with pytest.raises(ValueError) as exception_info:
129 assert register_list.get_register(register_name="non existing") is None
130 assert (
131 str(exception_info.value)
132 == 'Could not find register "non existing" within register list "apa"'
133 )
135 with pytest.raises(ValueError) as exception_info:
136 register_list.get_register(register_name="register_array")
137 assert (
138 str(exception_info.value)
139 == 'Could not find register "register_array" within register list "apa"'
140 )
141 register_list.get_register_array("register_array")
143 with pytest.raises(ValueError) as exception_info:
144 register_list.get_register(register_name="zebra")
145 assert str(exception_info.value) == 'Could not find register "zebra" within register list "apa"'
147 assert (
148 register_list.get_register(register_name="zebra", register_array_name="register_array")
149 is zebra
150 )
152 with pytest.raises(ValueError) as exception_info:
153 register_list.get_register(register_name="hest", register_array_name="register_array")
154 assert (
155 str(exception_info.value)
156 == 'Could not find register "hest" within register array "register_array"'
157 )
160def test_get_register_array():
161 register_list = RegisterList(name="apa", source_definition_file=None)
163 hest = register_list.append_register_array(name="hest", length=3, description="")
164 hest.append_register(name="foo", mode=REGISTER_MODES["r"], description="")
166 zebra = register_list.append_register_array(name="zebra", length=2, description="")
167 zebra.append_register(name="bar", mode=REGISTER_MODES["r"], description="")
169 register_list.append_register(name="register", mode=REGISTER_MODES["r"], description="")
171 assert register_list.get_register_array("hest") is hest
172 assert register_list.get_register_array("zebra") is zebra
174 with pytest.raises(ValueError) as exception_info:
175 register_list.get_register_array("non existing")
176 assert (
177 str(exception_info.value)
178 == 'Could not find register array "non existing" within register list "apa"'
179 )
181 with pytest.raises(ValueError) as exception_info:
182 register_list.get_register_array("register")
183 assert (
184 str(exception_info.value)
185 == 'Could not find register array "register" within register list "apa"'
186 )
187 register_list.get_register("register")
190def test_get_register_index():
191 register_list = RegisterList(name=None, source_definition_file=None)
193 register_list.append_register(name="apa", mode=REGISTER_MODES["r"], description="")
194 register_list.append_register(name="hest", mode=REGISTER_MODES["r"], description="")
196 zebra = register_list.append_register_array(name="zebra", length=2, description="")
197 zebra.append_register(name="bar", mode=REGISTER_MODES["r"], description="")
198 zebra.append_register(name="baz", mode=REGISTER_MODES["r"], description="")
200 assert register_list.get_register_index(register_name="apa") == 0
201 assert register_list.get_register_index(register_name="hest") == 1
202 assert (
203 register_list.get_register_index(
204 register_name="bar", register_array_name="zebra", register_array_index=0
205 )
206 == 2
207 )
208 assert (
209 register_list.get_register_index(
210 register_name="baz", register_array_name="zebra", register_array_index=1
211 )
212 == 5
213 )
216def test_repr_basic():
217 # Check that repr is an actual representation, not just "X object at 0xABCDEF"
218 assert "apa" in repr(RegisterList(name="apa", source_definition_file=Path(".")))
220 # Different name
221 assert repr(RegisterList(name="apa", source_definition_file=Path("."))) != repr(
222 RegisterList(name="hest", source_definition_file=Path("."))
223 )
225 # Different source_definition_file
226 assert repr(RegisterList(name="apa", source_definition_file=Path("."))) != repr(
227 RegisterList(name="apa", source_definition_file=Path("./zebra"))
228 )
231def test_repr_with_constant_added():
232 register_list_a = RegisterList(name="apa", source_definition_file=Path("."))
233 register_list_b = RegisterList(name="apa", source_definition_file=Path("."))
234 assert repr(register_list_a) == repr(register_list_b)
236 register_list_a.add_constant(name="zebra", value=3, description="")
238 assert repr(register_list_a) != repr(register_list_b)
241def test_repr_with_register_appended():
242 register_list_a = RegisterList(name="apa", source_definition_file=Path("."))
243 register_list_b = RegisterList(name="apa", source_definition_file=Path("."))
244 assert repr(register_list_a) == repr(register_list_b)
246 register_list_a.append_register(name="zebra", mode=REGISTER_MODES["w"], description="")
248 assert repr(register_list_a) != repr(register_list_b)
251def test_repr_with_register_array_appended():
252 register_list_a = RegisterList(name="apa", source_definition_file=Path("."))
253 register_list_b = RegisterList(name="apa", source_definition_file=Path("."))
254 assert repr(register_list_a) == repr(register_list_b)
256 register_list_a.append_register_array(name="zebra", length=4, description="")
258 assert repr(register_list_a) != repr(register_list_b)
261def test_deep_copy_of_register_list_actually_copies_everything():
262 original_list = RegisterList("original", Path("/original_file.txt"))
263 original_list.add_constant("original_constant", value=2, description="original constant")
264 original_list.append_register(
265 "original_register", REGISTER_MODES["w"], description="original register"
266 )
267 original_array = original_list.append_register_array("original_array", length=4, description="")
268 original_array.append_register(
269 name="original_register_in_array", mode=REGISTER_MODES["r"], description=""
270 )
272 copied_list = copy.deepcopy(original_list)
274 assert copied_list.constants is not original_list.constants
275 assert copied_list.constants[0] is not original_list.constants[0]
277 copied_list.add_constant(name="new_constant", value=5, description="")
278 assert len(copied_list.constants) == 2 and len(original_list.constants) == 1
280 assert copied_list.register_objects is not original_list.register_objects
281 assert copied_list.register_objects[0] is not original_list.register_objects[0]
283 # Original register in position 0, original register array in position 1, new register in 2
284 copied_list.append_register(name="new_register", mode=REGISTER_MODES["r"], description="")
285 assert len(copied_list.register_objects) == 3 and len(original_list.register_objects) == 2
287 assert copied_list.register_objects[1] is not original_list.register_objects[1]
288 assert (
289 copied_list.register_objects[1].registers is not original_list.register_objects[1].registers
290 )
291 assert (
292 copied_list.register_objects[1].registers[0]
293 is not original_list.register_objects[1].registers[0]
294 )
295 copied_list.register_objects[1].append_register(
296 name="new_register_in_array", mode=REGISTER_MODES["r_w"], description=""
297 )
298 assert len(copied_list.register_objects[1].registers) == 2
299 assert len(original_list.register_objects[1].registers) == 1