Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

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

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

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

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

8 

9import pytest 

10 

11from tsfpga.registers.bit_vector import BitVector 

12 

13 

14def test_get_value(): 

15 bit = BitVector(name="", base_index=2, description="", width=4, default_value="0000") 

16 

17 register_value = int("111000011", base=2) 

18 assert bit.get_value(register_value) == 0 

19 

20 register_value = int("000111100", base=2) 

21 assert bit.get_value(register_value) == 15 

22 

23 register_value = int("101010101", base=2) 

24 assert bit.get_value(register_value) == 5 

25 

26 

27def test_repr(): 

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

29 assert "apa" in repr( 

30 BitVector(name="apa", base_index=0, description="", width=1, default_value="0") 

31 ) 

32 

33 # Different name 

34 assert repr( 

35 BitVector(name="apa", base_index=0, description="", width=1, default_value="0") 

36 ) != repr(BitVector(name="hest", base_index=0, description="", width=1, default_value="0")) 

37 

38 # Different base_index 

39 assert repr( 

40 BitVector(name="apa", base_index=0, description="", width=1, default_value="0") 

41 ) != repr(BitVector(name="apa", base_index=1, description="", width=1, default_value="0")) 

42 

43 # Different description 

44 assert repr( 

45 BitVector(name="apa", base_index=0, description="Blah", width=1, default_value="0") 

46 ) != repr(BitVector(name="apa", base_index=0, description="Gaah", width=1, default_value="0")) 

47 

48 # Different width 

49 assert repr( 

50 BitVector(name="apa", base_index=0, description="", width=1, default_value="1") 

51 ) != repr(BitVector(name="apa", base_index=0, description="", width=2, default_value="11")) 

52 

53 # Different default_value 

54 assert repr( 

55 BitVector(name="apa", base_index=0, description="", width=1, default_value="1") 

56 ) != repr(BitVector(name="apa", base_index=0, description="", width=1, default_value="0")) 

57 

58 

59def test_invalid_width(): 

60 with pytest.raises(ValueError) as exception_info: 

61 BitVector(name="foo", base_index=0, width="4", description="", default_value="0000") 

62 assert ( 

63 str(exception_info.value) 

64 == 'Bit vector "foo" should have integer value for "width". Got: "4".' 

65 ) 

66 

67 with pytest.raises(ValueError) as exception_info: 

68 BitVector(name="foo", base_index=0, width=33, description="", default_value="0") 

69 assert str(exception_info.value) == 'Invalid bit vector width for "foo". Got: "33".' 

70 

71 with pytest.raises(ValueError) as exception_info: 

72 BitVector(name="foo", base_index=0, width=0, description="", default_value="0") 

73 assert str(exception_info.value) == 'Invalid bit vector width for "foo". Got: "0".' 

74 

75 

76def test_invalid_default_value_should_raise_exception(): 

77 with pytest.raises(ValueError) as exception_info: 

78 BitVector(name="hest", base_index=0, description="", width=4, default_value=1111) 

79 assert str(exception_info.value) == ( 

80 'Bit vector "hest" should have string value for "default_value". Got: "1111"' 

81 ) 

82 

83 with pytest.raises(ValueError) as exception_info: 

84 BitVector(name="hest", base_index=0, description="", width=4, default_value="11") 

85 assert str(exception_info.value) == ( 

86 'Bit vector "hest" should have "default_value" of length 4. Got: "11".' 

87 ) 

88 

89 with pytest.raises(ValueError) as exception_info: 

90 BitVector(name="hest", base_index=0, description="", width=4, default_value="1121") 

91 assert str(exception_info.value) == ( 

92 'Bit vector "hest" invalid binary value for "default_value". Got: "1121".' 

93 ) 

94 

95 

96def test_can_update_default_value(): 

97 bit_vector = BitVector(name="hest", base_index=0, description="", width=4, default_value="1111") 

98 assert bit_vector.default_value == "1111" 

99 

100 bit_vector.default_value = "0000" 

101 assert bit_vector.default_value == "0000" 

102 

103 

104def test_updating_to_invalid_default_value_should_raise_exception(): 

105 # Create with a valid default_value 

106 bit_vector = BitVector(name="hest", base_index=0, description="", width=4, default_value="1111") 

107 

108 # Update to an invalid value 

109 with pytest.raises(ValueError) as exception_info: 

110 bit_vector.default_value = 1111 

111 assert str(exception_info.value) == ( 

112 'Bit vector "hest" should have string value for "default_value". Got: "1111"' 

113 ) 

114 

115 

116def test_default_value_uint(): 

117 bit_vector = BitVector(name="apa", base_index=0, description="", width=4, default_value="0000") 

118 assert bit_vector.default_value_uint == 0 

119 

120 bit_vector.default_value = "0010" 

121 assert bit_vector.default_value_uint == 2 

122 

123 bit_vector.default_value = "1001" 

124 assert bit_vector.default_value_uint == 9