Coverage for hdl_registers/constant/test/test_bit_vector_constant.py: 100%
69 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
11from copy import copy
13# Third party libraries
14import pytest
16# First party libraries
17from hdl_registers.constant.bit_vector_constant import BitVectorConstant, UnsignedVectorConstant
20def test_unsigned_hexadecimal():
21 constant = UnsignedVectorConstant(name="apa", value="0x10a_BCdef", description="hest")
23 assert constant.name == "apa"
24 assert constant.prefix == "0x"
25 assert constant.value == "10a_BCdef"
26 assert constant.value_without_separator == "10aBCdef"
27 assert constant.description == "hest"
28 assert constant.is_hexadecimal_not_binary
29 assert constant.width == 32
32def test_unsigned_binary():
33 constant = UnsignedVectorConstant(name="apa", value="0b10_01", description="hest")
35 assert constant.name == "apa"
36 assert constant.prefix == "0b"
37 assert constant.value == "10_01"
38 assert constant.value_without_separator == "1001"
39 assert constant.description == "hest"
40 assert not constant.is_hexadecimal_not_binary
41 assert constant.width == 4
44def test_illegal_prefix_should_raise_exception():
45 with pytest.raises(ValueError) as exception_info:
46 UnsignedVectorConstant(name="apa", value="123")
47 assert (
48 str(exception_info.value)
49 == 'Constant "apa" value must start with a correct prefix. Value: "123".'
50 )
52 with pytest.raises(ValueError) as exception_info:
53 UnsignedVectorConstant(name="apa", value="0b")
54 assert (
55 str(exception_info.value)
56 == 'Constant "apa" value must start with a correct prefix. Value: "0b".'
57 )
59 with pytest.raises(ValueError) as exception_info:
60 UnsignedVectorConstant(name="apa", value="0x")
61 assert (
62 str(exception_info.value)
63 == 'Constant "apa" value must start with a correct prefix. Value: "0x".'
64 )
66 # Check also via setter
67 constant = UnsignedVectorConstant(name="apa", value="0b11")
69 with pytest.raises(ValueError) as exception_info:
70 constant.value = "456"
71 assert (
72 str(exception_info.value)
73 == 'Constant "apa" value must start with a correct prefix. Value: "456".'
74 )
76 with pytest.raises(ValueError) as exception_info:
77 constant.value = "0b"
78 assert (
79 str(exception_info.value)
80 == 'Constant "apa" value must start with a correct prefix. Value: "0b".'
81 )
83 with pytest.raises(ValueError) as exception_info:
84 constant.value = "0x"
85 assert (
86 str(exception_info.value)
87 == 'Constant "apa" value must start with a correct prefix. Value: "0x".'
88 )
91def test_illegal_value_type_should_raise_exception():
92 with pytest.raises(TypeError) as exception_info:
93 UnsignedVectorConstant(name="apa", value=123)
94 assert (
95 str(exception_info.value)
96 == 'Constant "apa" has invalid data type "<class \'int\'>". Value: "123".'
97 )
99 constant = UnsignedVectorConstant(name="apa", value="0b11")
100 with pytest.raises(TypeError) as exception_info:
101 constant.value = 456
102 assert (
103 str(exception_info.value)
104 == 'Constant "apa" has invalid data type "<class \'int\'>". Value: "456".'
105 )
108def test_illegal_hexadecimal_character_should_raise_exception():
109 with pytest.raises(ValueError) as exception_info:
110 UnsignedVectorConstant(name="apa", value="0xABC01X")
111 assert (
112 str(exception_info.value)
113 == 'Constant "apa" contains illegal character "X". Value: "0xABC01X".'
114 )
116 constant = UnsignedVectorConstant(name="apa", value="0x123")
117 with pytest.raises(ValueError) as exception_info:
118 constant.value = "0x1230Z"
119 assert (
120 str(exception_info.value)
121 == 'Constant "apa" contains illegal character "Z". Value: "0x1230Z".'
122 )
125def test_repr():
126 data = BitVectorConstant(name="apa", value="0b00")
128 # Check that repr is an actual representation, not just "X object at 0xABCDEF"
129 assert "apa" in repr(data)
130 assert repr(data) == repr(copy(data))
132 # Different name
133 other = BitVectorConstant(name="hest", value="0b00")
134 assert repr(data) != repr(other)
136 # Different value
137 other = BitVectorConstant(name="apa", value="0xff")
138 assert repr(data) != repr(other)
140 # Different prefix
141 other = BitVectorConstant(name="apa", value="0x00")
142 assert repr(data) != repr(other)
144 # Different description
145 data = BitVectorConstant(name="apa", value="0b00", description="X")
146 assert repr(data) != repr(other)