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

55 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-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/tsfpga/hdl_registers 

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

9 

10import pytest 

11 

12from hdl_registers.bit import Bit 

13from hdl_registers.register_field_type import Unsigned 

14 

15 

16def test_get_value(): 

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

18 

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

20 assert bit.get_value(register_value) == 0 

21 

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

23 assert bit.get_value(register_value) == 1 

24 

25 

26def test_max_binary_value(): 

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

28 assert bit.max_binary_value == 0b1 

29 

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

31 assert bit.max_binary_value == 0b1 

32 

33 

34def test_set_value(): 

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

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

37 

38 with pytest.raises(ValueError): 

39 bit.set_value(0b11) 

40 

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

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

43 

44 with pytest.raises(ValueError): 

45 bit.set_value(0b11) 

46 

47 

48def test_repr(): 

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

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

51 

52 # Different name 

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

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

55 ) 

56 

57 # Different index 

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

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

60 ) 

61 

62 # Different description 

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

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

65 ) 

66 

67 # Different default_value 

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

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

70 ) 

71 

72 

73def test_default_value_uint(): 

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

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

76 

77 

78def test_invalid_default_value_should_raise_exception(): 

79 with pytest.raises(ValueError) as exception_info: 

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

81 assert str(exception_info.value) == ( 

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

83 ) 

84 

85 with pytest.raises(ValueError) as exception_info: 

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

87 assert str(exception_info.value) == ( 

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

89 ) 

90 

91 with pytest.raises(ValueError) as exception_info: 

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

93 assert str(exception_info.value) == ( 

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

95 ) 

96 

97 

98def test_can_update_default_value(): 

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

100 assert bit.default_value == "1" 

101 

102 bit.default_value = "0" 

103 assert bit.default_value == "0" 

104 

105 

106def test_updating_to_invalid_default_value_should_raise_exception(): 

107 # Create with a valid default_value 

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

109 

110 # Update to an invalid value 

111 with pytest.raises(ValueError) as exception_info: 

112 bit.default_value = "2" 

113 assert str(exception_info.value) == ( 

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

115 ) 

116 

117 

118def test_field_type(): 

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

120 assert isinstance(bit.field_type, Unsigned)