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

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

13 

14 

15def test_repr_basic(): 

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

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

18 

19 # Different name 

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

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

22 ) 

23 

24 # Different index 

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

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

27 ) 

28 

29 # Different mode 

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

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

32 ) 

33 

34 # Different description 

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

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

37 ) 

38 

39 

40def test_repr_with_bits_appended(): 

41 """ 

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

43 """ 

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

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

46 

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

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

49 

50 assert repr(register_a) == repr(register_b) 

51 

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

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

54 

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

56 

57 

58def test_bits_are_appended_properly_and_can_be_edited_in_place(): 

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

60 

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

62 assert bit_hest.base_index == 0 

63 

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

65 assert bit_zebra.base_index == 1 

66 

67 bit_hest.description = "new desc" 

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

69 

70 

71def test_bit_vectors_are_appended_properly_and_can_be_edited_in_place(): 

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

73 

74 bit_vector_hest = register.append_bit_vector( 

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

76 ) 

77 assert bit_vector_hest.base_index == 0 

78 

79 bit_vector_zebra = register.append_bit_vector( 

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

81 ) 

82 assert bit_vector_zebra.base_index == 3 

83 

84 bit_vector_hest.description = "new desc" 

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

86 

87 

88def test_appending_bit_to_full_register(): 

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

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

91 

92 with pytest.raises(ValueError) as exception_info: 

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

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

95 

96 

97def test_appending_bit_vector_to_full_register(): 

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

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

100 

101 with pytest.raises(ValueError) as exception_info: 

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

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

104 

105 

106def test_default_value(): 

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

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

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

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

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

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

113 

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

115 

116 

117def test_default_value_can_be_updated(): 

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

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

120 

121 assert register.default_value == 1 

122 

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

124 assert register.default_value == 0 

125 

126 

127def test_get_field(): 

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

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

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

131 

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

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

134 

135 with pytest.raises(ValueError) as exception_info: 

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

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