Coverage for tsfpga/test/test_math_utils.py: 100%

99 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 20:52 +0000

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

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

3# 

4# This file is part of the tsfpga project, a project platform for modern FPGA development. 

5# https://tsfpga.com 

6# https://github.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9# Third party libraries 

10import pytest 

11 

12# First party libraries 

13from tsfpga.math_utils import ( 

14 to_binary_nibble_string, 

15 to_binary_string, 

16 to_hex_byte_string, 

17 to_hex_string, 

18) 

19 

20 

21def test_to_binary_string(): 

22 assert to_binary_string(value=0, result_width=1) == "0" 

23 assert to_binary_string(value=1, result_width=1) == "1" 

24 assert to_binary_string(value=2, result_width=3) == "010" 

25 assert to_binary_string(value=15, result_width=4) == "1111" 

26 assert to_binary_string(value=0, result_width=5) == "00000" 

27 

28 

29def test_to_binary_string_width_out_of_range_should_raise_exception(): 

30 with pytest.raises(ValueError) as exception_info: 

31 to_binary_string(value=15, result_width=0) 

32 assert str(exception_info.value) == 'Invalid result width "0".' 

33 

34 with pytest.raises(ValueError) as exception_info: 

35 to_binary_string(value=-7, result_width=-3) 

36 assert str(exception_info.value) == 'Invalid result width "-3".' 

37 

38 

39def test_to_binary_string_value_out_of_range_should_raise_exception(): 

40 with pytest.raises(ValueError) as exception_info: 

41 to_binary_string(value=15, result_width=3) 

42 assert str(exception_info.value) == 'Value "15" out of 3-bit range.' 

43 

44 with pytest.raises(ValueError) as exception_info: 

45 to_binary_string(value=-7, result_width=10) 

46 assert str(exception_info.value) == 'Value "-7" out of 10-bit range.' 

47 

48 

49def test_to_binary_nibble_string(): 

50 assert to_binary_nibble_string(value=0, result_width_bits=1) == "0" 

51 assert to_binary_nibble_string(value=1, result_width_bits=1) == "1" 

52 

53 assert to_binary_nibble_string(value=2, result_width_bits=3) == "010" 

54 assert to_binary_nibble_string(value=7, result_width_bits=3) == "111" 

55 

56 assert to_binary_nibble_string(value=15, result_width_bits=4) == "1111" 

57 

58 assert to_binary_nibble_string(value=0, result_width_bits=5) == "0_0000" 

59 assert to_binary_nibble_string(value=16, result_width_bits=5) == "1_0000" 

60 

61 assert to_binary_nibble_string(value=265, result_width_bits=9) == "1_0000_1001" 

62 assert to_binary_nibble_string(value=565, result_width_bits=10) == "10_0011_0101" 

63 assert to_binary_nibble_string(value=565, result_width_bits=12) == "0010_0011_0101" 

64 assert to_binary_nibble_string(value=2345, result_width_bits=12) == "1001_0010_1001" 

65 

66 assert to_binary_nibble_string(value=5345, result_width_bits=13) == "1_0100_1110_0001" 

67 

68 

69def test_to_binary_nibble_string_width_out_of_range_should_raise_exception(): 

70 with pytest.raises(ValueError) as exception_info: 

71 to_binary_nibble_string(value=15, result_width_bits=0) 

72 assert str(exception_info.value) == 'Invalid result width "0".' 

73 

74 with pytest.raises(ValueError) as exception_info: 

75 to_binary_nibble_string(value=-7, result_width_bits=-3) 

76 assert str(exception_info.value) == 'Invalid result width "-3".' 

77 

78 

79def test_to_binary_nibble_string_value_out_of_range_should_raise_exception(): 

80 with pytest.raises(ValueError) as exception_info: 

81 to_binary_nibble_string(value=15, result_width_bits=3) 

82 assert str(exception_info.value) == 'Value "15" out of 3-bit range.' 

83 

84 with pytest.raises(ValueError) as exception_info: 

85 to_binary_nibble_string(value=-7, result_width_bits=10) 

86 assert str(exception_info.value) == 'Value "-7" out of 10-bit range.' 

87 

88 

89def test_to_hex_string(): 

90 assert to_hex_string(value=0, result_width_bits=1) == "0" 

91 assert to_hex_string(value=1, result_width_bits=1) == "1" 

92 assert to_hex_string(value=2, result_width_bits=3) == "2" 

93 assert to_hex_string(value=2, result_width_bits=4) == "2" 

94 assert to_hex_string(value=2, result_width_bits=5) == "02" 

95 assert to_hex_string(value=15, result_width_bits=4) == "F" 

96 assert to_hex_string(value=0, result_width_bits=5) == "00" 

97 

98 

99def test_to_hex_string_width_out_of_range_should_raise_exception(): 

100 with pytest.raises(ValueError) as exception_info: 

101 to_hex_string(value=15, result_width_bits=0) 

102 assert str(exception_info.value) == 'Invalid result width "0".' 

103 

104 with pytest.raises(ValueError) as exception_info: 

105 to_hex_string(value=-7, result_width_bits=-3) 

106 assert str(exception_info.value) == 'Invalid result width "-3".' 

107 

108 

109def test_to_hex_string_value_out_of_range_should_raise_exception(): 

110 with pytest.raises(ValueError) as exception_info: 

111 to_hex_string(value=15, result_width_bits=3) 

112 assert str(exception_info.value) == 'Value "15" out of 3-bit range.' 

113 

114 with pytest.raises(ValueError) as exception_info: 

115 to_hex_string(value=-7, result_width_bits=10) 

116 assert str(exception_info.value) == 'Value "-7" out of 10-bit range.' 

117 

118 

119def test_to_hex_byte_string(): 

120 assert to_hex_byte_string(value=0, result_width_bits=1) == "0" 

121 assert to_hex_byte_string(value=1, result_width_bits=1) == "1" 

122 

123 assert to_hex_byte_string(value=2, result_width_bits=3) == "2" 

124 assert to_hex_byte_string(value=7, result_width_bits=3) == "7" 

125 

126 assert to_hex_byte_string(value=15, result_width_bits=4) == "F" 

127 

128 assert to_hex_byte_string(value=0, result_width_bits=5) == "00" 

129 assert to_hex_byte_string(value=15, result_width_bits=5) == "0F" 

130 assert to_hex_byte_string(value=16, result_width_bits=5) == "10" 

131 

132 assert to_hex_byte_string(value=265, result_width_bits=9) == "1_09" 

133 assert to_hex_byte_string(value=565, result_width_bits=10) == "2_35" 

134 assert to_hex_byte_string(value=565, result_width_bits=12) == "2_35" 

135 assert to_hex_byte_string(value=2345, result_width_bits=12) == "9_29" 

136 

137 assert to_hex_byte_string(value=5345, result_width_bits=13) == "14_E1" 

138 

139 

140def test_to_hex_byte_string_width_out_of_range_should_raise_exception(): 

141 with pytest.raises(ValueError) as exception_info: 

142 to_hex_byte_string(value=15, result_width_bits=0) 

143 assert str(exception_info.value) == 'Invalid result width "0".' 

144 

145 with pytest.raises(ValueError) as exception_info: 

146 to_hex_byte_string(value=-7, result_width_bits=-3) 

147 assert str(exception_info.value) == 'Invalid result width "-3".' 

148 

149 

150def test_to_hex_byte_string_value_out_of_range_should_raise_exception(): 

151 with pytest.raises(ValueError) as exception_info: 

152 to_hex_byte_string(value=15, result_width_bits=3) 

153 assert str(exception_info.value) == 'Value "15" out of 3-bit range.' 

154 

155 with pytest.raises(ValueError) as exception_info: 

156 to_hex_byte_string(value=-7, result_width_bits=10) 

157 assert str(exception_info.value) == 'Value "-7" out of 10-bit range.'