Coverage for hdl_registers/constant/test/test_bit_vector_constant.py: 100%

69 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-11 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# -------------------------------------------------------------------------------------------------- 

9 

10from copy import copy 

11 

12import pytest 

13 

14from hdl_registers.constant.bit_vector_constant import BitVectorConstant, UnsignedVectorConstant 

15 

16 

17def test_unsigned_hexadecimal(): 

18 constant = UnsignedVectorConstant(name="apa", value="0x10a_BCdef", description="hest") 

19 

20 assert constant.name == "apa" 

21 assert constant.prefix == "0x" 

22 assert constant.value == "10a_BCdef" 

23 assert constant.value_without_separator == "10aBCdef" 

24 assert constant.description == "hest" 

25 assert constant.is_hexadecimal_not_binary 

26 assert constant.width == 32 

27 

28 

29def test_unsigned_binary(): 

30 constant = UnsignedVectorConstant(name="apa", value="0b10_01", description="hest") 

31 

32 assert constant.name == "apa" 

33 assert constant.prefix == "0b" 

34 assert constant.value == "10_01" 

35 assert constant.value_without_separator == "1001" 

36 assert constant.description == "hest" 

37 assert not constant.is_hexadecimal_not_binary 

38 assert constant.width == 4 

39 

40 

41def test_illegal_prefix_should_raise_exception(): 

42 with pytest.raises(ValueError) as exception_info: 

43 UnsignedVectorConstant(name="apa", value="123") 

44 assert ( 

45 str(exception_info.value) 

46 == 'Constant "apa" value must start with a correct prefix. Value: "123".' 

47 ) 

48 

49 with pytest.raises(ValueError) as exception_info: 

50 UnsignedVectorConstant(name="apa", value="0b") 

51 assert ( 

52 str(exception_info.value) 

53 == 'Constant "apa" value must start with a correct prefix. Value: "0b".' 

54 ) 

55 

56 with pytest.raises(ValueError) as exception_info: 

57 UnsignedVectorConstant(name="apa", value="0x") 

58 assert ( 

59 str(exception_info.value) 

60 == 'Constant "apa" value must start with a correct prefix. Value: "0x".' 

61 ) 

62 

63 # Check also via setter 

64 constant = UnsignedVectorConstant(name="apa", value="0b11") 

65 

66 with pytest.raises(ValueError) as exception_info: 

67 constant.value = "456" 

68 assert ( 

69 str(exception_info.value) 

70 == 'Constant "apa" value must start with a correct prefix. Value: "456".' 

71 ) 

72 

73 with pytest.raises(ValueError) as exception_info: 

74 constant.value = "0b" 

75 assert ( 

76 str(exception_info.value) 

77 == 'Constant "apa" value must start with a correct prefix. Value: "0b".' 

78 ) 

79 

80 with pytest.raises(ValueError) as exception_info: 

81 constant.value = "0x" 

82 assert ( 

83 str(exception_info.value) 

84 == 'Constant "apa" value must start with a correct prefix. Value: "0x".' 

85 ) 

86 

87 

88def test_illegal_value_type_should_raise_exception(): 

89 with pytest.raises(TypeError) as exception_info: 

90 UnsignedVectorConstant(name="apa", value=123) 

91 assert ( 

92 str(exception_info.value) 

93 == 'Constant "apa" has invalid data type "<class \'int\'>". Value: "123".' 

94 ) 

95 

96 constant = UnsignedVectorConstant(name="apa", value="0b11") 

97 with pytest.raises(TypeError) as exception_info: 

98 constant.value = 456 

99 assert ( 

100 str(exception_info.value) 

101 == 'Constant "apa" has invalid data type "<class \'int\'>". Value: "456".' 

102 ) 

103 

104 

105def test_illegal_hexadecimal_character_should_raise_exception(): 

106 with pytest.raises(ValueError) as exception_info: 

107 UnsignedVectorConstant(name="apa", value="0xABC01X") 

108 assert ( 

109 str(exception_info.value) 

110 == 'Constant "apa" contains illegal character "X". Value: "0xABC01X".' 

111 ) 

112 

113 constant = UnsignedVectorConstant(name="apa", value="0x123") 

114 with pytest.raises(ValueError) as exception_info: 

115 constant.value = "0x1230Z" 

116 assert ( 

117 str(exception_info.value) 

118 == 'Constant "apa" contains illegal character "Z". Value: "0x1230Z".' 

119 ) 

120 

121 

122def test_repr(): 

123 data = BitVectorConstant(name="apa", value="0b00") 

124 

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

126 assert "apa" in repr(data) 

127 assert repr(data) == repr(copy(data)) 

128 

129 # Different name 

130 other = BitVectorConstant(name="hest", value="0b00") 

131 assert repr(data) != repr(other) 

132 

133 # Different value 

134 other = BitVectorConstant(name="apa", value="0xff") 

135 assert repr(data) != repr(other) 

136 

137 # Different prefix 

138 other = BitVectorConstant(name="apa", value="0x00") 

139 assert repr(data) != repr(other) 

140 

141 # Different description 

142 data = BitVectorConstant(name="apa", value="0b00", description="X") 

143 assert repr(data) != repr(other)