Coverage for hdl_registers/field/test/test_bit.py: 100%
47 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.field.bit import Bit
17def test_get_value():
18 bit = Bit(name="", index=2, description="", default_value="1")
20 register_value = int("11110_11", base=2)
21 assert bit.get_value(register_value) == 0
23 register_value = int("00001_00", base=2)
24 assert bit.get_value(register_value) == 1
27def test_set_value():
28 bit = Bit(name="", index=0, description="", default_value="1")
29 assert bit.set_value(0b1) == 0b1
31 with pytest.raises(ValueError):
32 bit.set_value(0b11)
34 bit = Bit(name="", index=4, description="", default_value="0")
35 assert bit.set_value(0b0) == 0b0
36 assert bit.set_value(0b1) == 0b1_0000
38 with pytest.raises(ValueError):
39 bit.set_value(0b11)
42def test_repr():
43 # Check that repr is an actual representation, not just "X object at 0xABCDEF"
44 assert "apa" in repr(Bit(name="apa", index=0, description="", default_value="0"))
46 # Different name
47 assert repr(Bit(name="apa", index=0, description="", default_value="0")) != repr(
48 Bit(name="hest", index=0, description="", default_value="0")
49 )
51 # Different index
52 assert repr(Bit(name="apa", index=0, description="", default_value="0")) != repr(
53 Bit(name="apa", index=1, description="", default_value="0")
54 )
56 # Different description
57 assert repr(Bit(name="apa", index=0, description="Blah", default_value="0")) != repr(
58 Bit(name="apa", index=0, description="Gaah", default_value="0")
59 )
61 # Different default_value
62 assert repr(Bit(name="apa", index=0, description="", default_value="1")) != repr(
63 Bit(name="apa", index=0, description="", default_value="0")
64 )
67def test_default_value_uint():
68 assert Bit(name="apa", index=0, description="", default_value="1").default_value_uint == 1
69 assert Bit(name="apa", index=0, description="", default_value="0").default_value_uint == 0
72def test_invalid_default_value_should_raise_exception():
73 with pytest.raises(ValueError) as exception_info:
74 Bit(name="hest", index=0, description="", default_value=1)
75 assert str(exception_info.value) == (
76 'Bit "hest" should have string value for "default_value". Got "1".'
77 )
79 with pytest.raises(ValueError) as exception_info:
80 Bit(name="hest", index=0, description="", default_value="11")
81 assert str(exception_info.value) == (
82 'Bit "hest" invalid binary value for "default_value". Got: "11".'
83 )
85 with pytest.raises(ValueError) as exception_info:
86 Bit(name="hest", index=0, description="", default_value="2")
87 assert str(exception_info.value) == (
88 'Bit "hest" invalid binary value for "default_value". Got: "2".'
89 )
92def test_can_update_default_value():
93 bit = Bit(name="hest", index=0, description="", default_value="1")
94 assert bit.default_value == "1"
96 bit.default_value = "0"
97 assert bit.default_value == "0"
100def test_updating_to_invalid_default_value_should_raise_exception():
101 # Create with a valid default_value
102 bit = Bit(name="hest", index=0, description="", default_value="1")
104 # Update to an invalid value
105 with pytest.raises(ValueError) as exception_info:
106 bit.default_value = "2"
107 assert str(exception_info.value) == (
108 'Bit "hest" invalid binary value for "default_value". Got: "2".'
109 )