Coverage for hdl_registers/test/test_register.py: 100%
83 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# Third party libraries
11import pytest
13# First party libraries
14from hdl_registers.register import Register
15from hdl_registers.register_modes import REGISTER_MODES
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=""))
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 )
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 )
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 )
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"))
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")
50 register_b = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="")
51 register_b.append_bit(name="hest", description="", default_value="0")
53 assert repr(register_a) == repr(register_b)
55 register_a.append_bit(name="zebra", description="", default_value="0")
56 register_b.append_bit(name="foo", description="", default_value="0")
58 assert repr(register_a) != repr(register_b)
61def test_bits_are_appended_properly_and_can_be_edited_in_place():
62 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="")
64 bit_hest = register.append_bit(name="hest", description="abc", default_value="0")
65 assert bit_hest.base_index == 0
67 bit_zebra = register.append_bit(name="zebra", description="def", default_value="0")
68 assert bit_zebra.base_index == 1
70 bit_hest.description = "new desc"
71 assert register.fields[0].description == "new desc"
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="")
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
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
87 bit_vector_hest.description = "new desc"
88 assert register.fields[0].description == "new desc"
91def test_integers_are_appended_properly_and_can_be_edited_in_place():
92 register = Register(name="apa", index=0, mode=REGISTER_MODES["r"], description="")
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
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
112 integer_hest.description = "new desc"
113 assert register.fields[0].description == "new desc"
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)
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".'
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)
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".'
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)
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".'
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")
157 assert register.default_value == 1 * 2**0 + 1 * 2**2 + 6 * 2**3 + 5 * 2**7
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")
164 assert register.default_value == 1
166 register.fields[0].default_value = "0"
167 assert register.default_value == 0
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")
175 assert register.get_field("hest") is hest
176 assert register.get_field("zebra") is zebra
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"'