Coverage for hdl_registers/test/unit/test_bit.py: 100%

55 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-29 22:03 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the hdl_registers project, a HDL register generator fast enough to be run 

5# in real time. 

6# https://hdl-registers.com 

7# https://gitlab.com/hdl_registers/hdl_registers 

8# -------------------------------------------------------------------------------------------------- 

9 

10# Third party libraries 

11import pytest 

12 

13# First party libraries 

14from hdl_registers.bit import Bit 

15from hdl_registers.register_field_type import Unsigned 

16 

17 

18def test_get_value(): 

19 bit = Bit(name="", index=2, description="", default_value="1") 

20 

21 register_value = int("1111011", base=2) 

22 assert bit.get_value(register_value) == 0 

23 

24 register_value = int("0000100", base=2) 

25 assert bit.get_value(register_value) == 1 

26 

27 

28def test_max_binary_value(): 

29 bit = Bit(name="", index=0, description="", default_value="1") 

30 assert bit.max_binary_value == 0b1 

31 

32 bit = Bit(name="", index=4, description="", default_value="0") 

33 assert bit.max_binary_value == 0b1 

34 

35 

36def test_set_value(): 

37 bit = Bit(name="", index=0, description="", default_value="1") 

38 assert bit.set_value(0b1) == 0b1 

39 

40 with pytest.raises(ValueError): 

41 bit.set_value(0b11) 

42 

43 bit = Bit(name="", index=4, description="", default_value="0") 

44 assert bit.set_value(0b1) == 0b10000 

45 

46 with pytest.raises(ValueError): 

47 bit.set_value(0b11) 

48 

49 

50def test_repr(): 

51 # Check that repr is an actual representation, not just "X object at 0xABCDEF" 

52 assert "apa" in repr(Bit(name="apa", index=0, description="", default_value="0")) 

53 

54 # Different name 

55 assert repr(Bit(name="apa", index=0, description="", default_value="0")) != repr( 

56 Bit(name="hest", index=0, description="", default_value="0") 

57 ) 

58 

59 # Different index 

60 assert repr(Bit(name="apa", index=0, description="", default_value="0")) != repr( 

61 Bit(name="apa", index=1, description="", default_value="0") 

62 ) 

63 

64 # Different description 

65 assert repr(Bit(name="apa", index=0, description="Blah", default_value="0")) != repr( 

66 Bit(name="apa", index=0, description="Gaah", default_value="0") 

67 ) 

68 

69 # Different default_value 

70 assert repr(Bit(name="apa", index=0, description="", default_value="1")) != repr( 

71 Bit(name="apa", index=0, description="", default_value="0") 

72 ) 

73 

74 

75def test_default_value_uint(): 

76 assert Bit(name="apa", index=0, description="", default_value="1").default_value_uint == 1 

77 assert Bit(name="apa", index=0, description="", default_value="0").default_value_uint == 0 

78 

79 

80def test_invalid_default_value_should_raise_exception(): 

81 with pytest.raises(ValueError) as exception_info: 

82 Bit(name="hest", index=0, description="", default_value=1) 

83 assert str(exception_info.value) == ( 

84 'Bit "hest" should have string value for "default_value". Got "1".' 

85 ) 

86 

87 with pytest.raises(ValueError) as exception_info: 

88 Bit(name="hest", index=0, description="", default_value="11") 

89 assert str(exception_info.value) == ( 

90 'Bit "hest" invalid binary value for "default_value". Got: "11".' 

91 ) 

92 

93 with pytest.raises(ValueError) as exception_info: 

94 Bit(name="hest", index=0, description="", default_value="2") 

95 assert str(exception_info.value) == ( 

96 'Bit "hest" invalid binary value for "default_value". Got: "2".' 

97 ) 

98 

99 

100def test_can_update_default_value(): 

101 bit = Bit(name="hest", index=0, description="", default_value="1") 

102 assert bit.default_value == "1" 

103 

104 bit.default_value = "0" 

105 assert bit.default_value == "0" 

106 

107 

108def test_updating_to_invalid_default_value_should_raise_exception(): 

109 # Create with a valid default_value 

110 bit = Bit(name="hest", index=0, description="", default_value="1") 

111 

112 # Update to an invalid value 

113 with pytest.raises(ValueError) as exception_info: 

114 bit.default_value = "2" 

115 assert str(exception_info.value) == ( 

116 'Bit "hest" invalid binary value for "default_value". Got: "2".' 

117 ) 

118 

119 

120def test_field_type(): 

121 bit = Bit(name="test", index=0, description="", default_value="1") 

122 assert isinstance(bit.field_type, Unsigned)