Coverage for hdl_registers/test/test_register_array.py: 100%
71 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-01 20:50 +0000
« 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# --------------------------------------------------------------------------------------------------
10# Third party libraries
11import pytest
13# First party libraries
14from hdl_registers.register_array import RegisterArray
15from hdl_registers.register_modes import REGISTER_MODES
18def test_registers_are_appended_properly_and_can_be_edited_in_place():
19 register_array = RegisterArray(name="apa", base_index=0, length=4, description="")
21 register_hest = register_array.append_register(
22 name="hest", mode=REGISTER_MODES["r"], description=""
23 )
24 assert register_hest.index == 0
26 register_zebra = register_array.append_register(
27 name="zebra", mode=REGISTER_MODES["r"], description=""
28 )
29 assert register_zebra.index == 1
31 register_hest.description = "new desc"
32 assert register_array.registers[0].description == "new desc"
35def test_get_register():
36 register_array = RegisterArray(name="apa", base_index=0, length=3, description="")
37 hest = register_array.append_register(name="hest", mode=REGISTER_MODES["w"], description="")
38 zebra = register_array.append_register(name="zebra", mode=REGISTER_MODES["r"], description="")
40 assert register_array.get_register("hest") is hest
41 assert register_array.get_register("zebra") is zebra
43 with pytest.raises(ValueError) as exception_info:
44 assert register_array.get_register("non existing") is None
45 assert (
46 str(exception_info.value)
47 == 'Could not find register "non existing" within register array "apa"'
48 )
51def test_repr_basic():
52 # Check that repr is an actual representation, not just "X object at 0xABCDEF"
53 assert "apa" in repr(RegisterArray(name="apa", base_index=0, length=4, description=""))
55 # Different name
56 assert repr(RegisterArray(name="apa", base_index=0, length=4, description="")) != repr(
57 RegisterArray(name="hest", base_index=0, length=4, description="")
58 )
60 # Different base_index
61 assert repr(RegisterArray(name="apa", base_index=0, length=4, description="")) != repr(
62 RegisterArray(name="apa", base_index=8, length=4, description="")
63 )
65 # Different length
66 assert repr(RegisterArray(name="apa", base_index=0, length=4, description="")) != repr(
67 RegisterArray(name="apa", base_index=0, length=8, description="")
68 )
70 # Different description
71 assert repr(RegisterArray(name="apa", base_index=0, length=4, description="hest")) != repr(
72 RegisterArray(name="apa", base_index=0, length=4, description="zebra")
73 )
76def test_repr_with_registers_appended():
77 register_array_a = RegisterArray(name="apa", base_index=0, length=4, description="")
78 register_array_a.append_register(name="hest", mode=REGISTER_MODES["r"], description="")
80 register_array_b = RegisterArray(name="apa", base_index=0, length=4, description="")
81 register_array_b.append_register(name="hest", mode=REGISTER_MODES["r"], description="")
83 assert repr(register_array_a) == repr(register_array_b)
85 register_array_a.append_register(name="zebra", mode=REGISTER_MODES["w"], description="")
86 register_array_b.append_register(name="zebra", mode=REGISTER_MODES["r_w"], description="")
88 assert repr(register_array_a) != repr(register_array_b)
91def test_invalid_length_should_raise_exception():
92 with pytest.raises(ValueError) as exception_info:
93 RegisterArray(name="apa", base_index=0, length=0, description="")
94 assert (
95 str(exception_info.value) == 'Register array "apa" length must be greater than 0. Got "0".'
96 )
99def test_index():
100 register_array = RegisterArray(name="apa", base_index=0, length=4, description="")
101 register_array.append_register(name="hest", mode=REGISTER_MODES["r"], description="")
102 assert register_array.index == 3
104 register_array.length = 5
105 assert register_array.index == 4
107 register_array.append_register(name="zebra", mode=REGISTER_MODES["r"], description="")
108 assert register_array.index == 9
111def test_index_for_array_without_registers_should_raise_exception():
112 register_array = RegisterArray(name="apa", base_index=0, length=4, description="")
114 with pytest.raises(ValueError) as exception_info:
115 print(register_array.index)
116 assert str(exception_info.value) == 'Register array "apa" must contain at least one register.'
119def test_start_index():
120 register_array = RegisterArray(name="apa", base_index=10, length=4, description="")
121 register_array.append_register(name="hest", mode=REGISTER_MODES["r"], description="")
122 assert register_array.get_start_index(0) == 10
123 assert register_array.get_start_index(1) == 11
124 assert register_array.get_start_index(2) == 12
126 register_array.append_register(name="zebra", mode=REGISTER_MODES["r"], description="")
127 assert register_array.get_start_index(0) == 10
128 assert register_array.get_start_index(1) == 12
129 assert register_array.get_start_index(2) == 14
132def test_start_index_with_argument_outside_of_length_should_raise_exception():
133 register_array = RegisterArray(name="apa", base_index=0, length=4, description="")
134 register_array.append_register(name="hest", mode=REGISTER_MODES["r"], description="")
136 with pytest.raises(ValueError) as exception_info:
137 register_array.get_start_index(4)
138 assert str(exception_info.value) == 'Index 4 out of range for register array "apa" of length 4.'
140 with pytest.raises(ValueError) as exception_info:
141 register_array.get_start_index(-1)
142 assert (
143 str(exception_info.value) == 'Index -1 out of range for register array "apa" of length 4.'
144 )