Coverage for hdl_registers/field/test/test_enumeration.py: 100%
81 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.field.enumeration import Enumeration, EnumerationElement
17def test_enumeration_element():
18 enumeration_element = EnumerationElement(name="apa", value=3, description="hest")
20 assert enumeration_element.name == "apa"
21 assert enumeration_element.value == 3
22 assert enumeration_element.description == "hest"
24 assert repr(enumeration_element) == "EnumerationElement(_name=apa,_value=3,description=hest,)"
27def test_enumeration_basics():
28 enumeration = Enumeration(
29 name="apa",
30 base_index=3,
31 description="hest",
32 elements={
33 "element0": "desc0",
34 "element1": "desc1",
35 "element2": "desc2",
36 "element3": "desc3",
37 "element4": "desc4",
38 },
39 default_value="element2",
40 )
42 assert enumeration.name == "apa"
43 assert enumeration.base_index == 3
44 assert enumeration.description == "hest"
46 for idx, element in enumerate(enumeration.elements):
47 assert element.name == f"element{idx}"
48 assert element.value == idx
49 assert element.description == f"desc{idx}"
51 assert enumeration.width == 3
53 assert enumeration.default_value is enumeration.elements[2]
54 assert enumeration.default_value_uint == 2
57def test_no_elements_should_raise_exception():
58 with pytest.raises(ValueError) as exception_info:
59 Enumeration(
60 name="apa",
61 base_index=3,
62 description="",
63 elements=dict(),
64 default_value="element0",
65 )
66 assert str(exception_info.value) == 'Enumeration "apa", must have at least one element.'
69def test_get_element_by_name():
70 enumeration = Enumeration(
71 name="apa",
72 base_index=3,
73 description="",
74 elements={"element0": "", "element1": ""},
75 default_value="element0",
76 )
78 assert enumeration.get_element_by_name("element0") is enumeration.elements[0]
79 assert enumeration.get_element_by_name("element1") is enumeration.elements[1]
82def test_get_element_by_name_with_invalid_name_should_raise_exception():
83 enumeration = Enumeration(
84 name="apa",
85 base_index=3,
86 description="",
87 elements={"element0": ""},
88 default_value="element0",
89 )
91 with pytest.raises(ValueError) as exception_info:
92 enumeration.get_element_by_name("element1")
93 assert (
94 str(exception_info.value)
95 == 'Enumeration "apa", requested element name does not exist. Got: "element1".'
96 )
99def test_get_element_by_value():
100 enumeration = Enumeration(
101 name="apa",
102 base_index=3,
103 description="",
104 elements={"element0": "", "element1": ""},
105 default_value="element0",
106 )
108 assert enumeration.get_element_by_value(0) is enumeration.elements[0]
109 assert enumeration.get_element_by_value(1) is enumeration.elements[1]
112def test_get_element_by_value_with_invalid_value_should_raise_exception():
113 enumeration = Enumeration(
114 name="apa",
115 base_index=3,
116 description="",
117 elements={"element0": ""},
118 default_value="element0",
119 )
121 with pytest.raises(ValueError) as exception_info:
122 enumeration.get_element_by_value(1)
123 assert (
124 str(exception_info.value)
125 == 'Enumeration "apa", requested element value does not exist. Got: "1".'
126 )
129def test_setting_default_value():
130 enumeration = Enumeration(
131 name="apa",
132 base_index=3,
133 description="",
134 elements={"element0": "", "element1": ""},
135 default_value="element0",
136 )
137 assert enumeration.default_value is enumeration.elements[0]
139 enumeration.set_default_value("element1")
140 assert enumeration.default_value is enumeration.elements[1]
143def test_setting_default_value_that_does_not_exist_should_raise_exception():
144 # Invalid value to constructor.
145 with pytest.raises(ValueError) as exception_info:
146 Enumeration(
147 name="apa",
148 base_index=3,
149 description="",
150 elements={"element0": ""},
151 default_value="element1",
152 )
153 assert (
154 str(exception_info.value)
155 ) == 'Enumeration "apa", requested element name does not exist. Got: "element1".'
157 # Valid value to constructor but then an invalid update of the value.
158 enumeration = Enumeration(
159 name="apa",
160 base_index=3,
161 description="",
162 elements={"element0": ""},
163 default_value="element0",
164 )
165 with pytest.raises(ValueError) as exception_info:
166 enumeration.set_default_value("element1")
167 assert (
168 str(exception_info.value)
169 ) == 'Enumeration "apa", requested element name does not exist. Got: "element1".'
172def test_get_value():
173 base_index = 3
175 enumeration = Enumeration(
176 name="apa",
177 base_index=base_index,
178 description="",
179 elements={
180 "element0": "",
181 "element1": "",
182 "element2": "",
183 },
184 default_value="element0",
185 )
187 assert enumeration.width == 2
189 # Ones outside of this field. Should be masked out when getting value.
190 register_base_value = 0b111_00_111
192 register_value = register_base_value
193 assert enumeration.get_value(register_value=register_value) is enumeration.elements[0]
195 register_value = register_base_value + (1 << base_index)
196 assert enumeration.get_value(register_value=register_value) is enumeration.elements[1]
198 register_value = register_base_value + (2 << base_index)
199 assert enumeration.get_value(register_value=register_value) is enumeration.elements[2]
201 register_value = register_base_value + (3 << base_index)
202 with pytest.raises(ValueError) as exception_info:
203 enumeration.get_value(register_value=register_value)
204 assert (
205 str(exception_info.value)
206 == 'Enumeration "apa", requested element value does not exist. Got: "3".'
207 )
210def test_set_value():
211 base_index = 4
213 enumeration = Enumeration(
214 name="apa",
215 base_index=base_index,
216 description="",
217 elements={
218 "element0": "",
219 "element1": "",
220 "element2": "",
221 },
222 default_value="element0",
223 )
225 assert enumeration.width == 2
227 assert enumeration.set_value(field_value=enumeration.elements[0]) == 0
228 assert enumeration.set_value(field_value=enumeration.elements[1]) == 1 << base_index
229 assert enumeration.set_value(field_value=enumeration.elements[2]) == 2 << base_index
232def test_repr():
233 enumeration = Enumeration(
234 name="apa",
235 base_index=3,
236 description="hest",
237 elements={"element0": "element0 description"},
238 default_value="element0",
239 )
240 element = "EnumerationElement(_name=element0,_value=0,description=element0 description,)"
241 assert repr(enumeration) == (
242 "Enumeration(name=apa,_base_index=3,description=hest,"
243 f"_elements=[{element}],"
244 f"_default_value={element},)"
245 )