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

68 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.register import Register 

15 

16 

17def test_repr_basic(): 

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

19 assert "apa" in repr(Register(name="apa", index=0, mode="r", description="")) 

20 

21 # Different name 

22 assert repr(Register(name="apa", index=0, mode="r", description="")) != repr( 

23 Register(name="hest", index=0, mode="r", description="") 

24 ) 

25 

26 # Different index 

27 assert repr(Register(name="apa", index=0, mode="r", description="")) != repr( 

28 Register(name="apa", index=1, mode="r", description="") 

29 ) 

30 

31 # Different mode 

32 assert repr(Register(name="apa", index=0, mode="r", description="")) != repr( 

33 Register(name="apa", index=0, mode="w", description="") 

34 ) 

35 

36 # Different description 

37 assert repr(Register(name="apa", index=0, mode="r", description="Blaah")) != repr( 

38 Register(name="apa", index=0, mode="r", description="Gaah") 

39 ) 

40 

41 

42def test_repr_with_bits_appended(): 

43 """ 

44 Shows that the ``fields`` impact the repr. Do not need to test with other fields than bits. 

45 """ 

46 register_a = Register(name="apa", index=0, mode="r", description="") 

47 register_a.append_bit(name="hest", description="", default_value="0") 

48 

49 register_b = Register(name="apa", index=0, mode="r", description="") 

50 register_b.append_bit(name="hest", description="", default_value="0") 

51 

52 assert repr(register_a) == repr(register_b) 

53 

54 register_a.append_bit(name="zebra", description="", default_value="0") 

55 register_b.append_bit(name="foo", description="", default_value="0") 

56 

57 assert repr(register_a) != repr(register_b) 

58 

59 

60def test_bits_are_appended_properly_and_can_be_edited_in_place(): 

61 register = Register(name="apa", index=0, mode="r", description="") 

62 

63 bit_hest = register.append_bit(name="hest", description="abc", default_value="0") 

64 assert bit_hest.base_index == 0 

65 

66 bit_zebra = register.append_bit(name="zebra", description="def", default_value="0") 

67 assert bit_zebra.base_index == 1 

68 

69 bit_hest.description = "new desc" 

70 assert register.fields[0].description == "new desc" 

71 

72 

73def test_bit_vectors_are_appended_properly_and_can_be_edited_in_place(): 

74 register = Register(name="apa", index=0, mode="r", description="") 

75 

76 bit_vector_hest = register.append_bit_vector( 

77 name="hest", description="abc", width=3, default_value="000" 

78 ) 

79 assert bit_vector_hest.base_index == 0 

80 

81 bit_vector_zebra = register.append_bit_vector( 

82 name="zebra", description="def", width=5, default_value="00000" 

83 ) 

84 assert bit_vector_zebra.base_index == 3 

85 

86 bit_vector_hest.description = "new desc" 

87 assert register.fields[0].description == "new desc" 

88 

89 

90def test_appending_bit_to_full_register(): 

91 register = Register(name="apa", index=0, mode="r", description="") 

92 register.append_bit_vector(name="foo", width=32, description="", default_value="0" * 32) 

93 

94 with pytest.raises(ValueError) as exception_info: 

95 register.append_bit(name="bar", description="", default_value="0") 

96 assert str(exception_info.value).startswith('Maximum width exceeded for register "apa"') 

97 

98 

99def test_appending_bit_vector_to_full_register(): 

100 register = Register(name="apa", index=0, mode="r", description="") 

101 register.append_bit_vector(name="foo", width=30, description="", default_value="0" * 30) 

102 

103 with pytest.raises(ValueError) as exception_info: 

104 register.append_bit_vector(name="bar", description="", width=3, default_value="000") 

105 assert str(exception_info.value).startswith('Maximum width exceeded for register "apa"') 

106 

107 

108def test_default_value(): 

109 register = Register(name="apa", index=0, mode="r", description="") 

110 register.append_bit(name="foo", description="", default_value="1") 

111 register.append_bit(name="foo", description="", default_value="0") 

112 register.append_bit(name="foo", description="", default_value="1") 

113 register.append_bit_vector(name="foo", description="", width=4, default_value="0110") 

114 register.append_bit_vector(name="foo", description="", width=4, default_value="0101") 

115 

116 assert register.default_value == 1 * 2**0 + 1 * 2**2 + 6 * 2**3 + 5 * 2**7 

117 

118 

119def test_default_value_can_be_updated(): 

120 register = Register(name="apa", index=0, mode="r", description="") 

121 register.append_bit(name="foo", description="", default_value="1") 

122 

123 assert register.default_value == 1 

124 

125 register.fields[0].default_value = "0" 

126 assert register.default_value == 0 

127 

128 

129def test_get_field(): 

130 register = Register(name="apa", index=0, mode="r", description="") 

131 hest = register.append_bit(name="hest", description="", default_value="1") 

132 zebra = register.append_bit(name="zebra", description="", default_value="1") 

133 

134 assert register.get_field("hest") is hest 

135 assert register.get_field("zebra") is zebra 

136 

137 with pytest.raises(ValueError) as exception_info: 

138 assert register.get_field("non existing") is None 

139 assert str(exception_info.value) == 'Could not find field "non existing" within register "apa"'