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

58 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_array import RegisterArray 

15 

16 

17def test_registers_are_appended_properly_and_can_be_edited_in_place(): 

18 register_array = RegisterArray(name="apa", base_index=0, length=4, description="") 

19 

20 register_hest = register_array.append_register(name="hest", mode="r", description="") 

21 assert register_hest.index == 0 

22 

23 register_zebra = register_array.append_register(name="zebra", mode="r", description="") 

24 assert register_zebra.index == 1 

25 

26 register_hest.description = "new desc" 

27 assert register_array.registers[0].description == "new desc" 

28 

29 

30def test_get_register(): 

31 register_array = RegisterArray(name="apa", base_index=0, length=3, description="") 

32 hest = register_array.append_register(name="hest", mode="w", description="") 

33 zebra = register_array.append_register(name="zebra", mode="r", description="") 

34 

35 assert register_array.get_register("hest") is hest 

36 assert register_array.get_register("zebra") is zebra 

37 

38 with pytest.raises(ValueError) as exception_info: 

39 assert register_array.get_register("non existing") is None 

40 assert ( 

41 str(exception_info.value) 

42 == 'Could not find register "non existing" within register array "apa"' 

43 ) 

44 

45 

46def test_repr_basic(): 

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

48 assert "apa" in repr(RegisterArray(name="apa", base_index=0, length=4, description="")) 

49 

50 # Different name 

51 assert repr(RegisterArray(name="apa", base_index=0, length=4, description="")) != repr( 

52 RegisterArray(name="hest", base_index=0, length=4, description="") 

53 ) 

54 

55 # Different base_index 

56 assert repr(RegisterArray(name="apa", base_index=0, length=4, description="")) != repr( 

57 RegisterArray(name="apa", base_index=8, length=4, description="") 

58 ) 

59 

60 # Different length 

61 assert repr(RegisterArray(name="apa", base_index=0, length=4, description="")) != repr( 

62 RegisterArray(name="apa", base_index=0, length=8, description="") 

63 ) 

64 

65 # Different description 

66 assert repr(RegisterArray(name="apa", base_index=0, length=4, description="hest")) != repr( 

67 RegisterArray(name="apa", base_index=0, length=4, description="zebra") 

68 ) 

69 

70 

71def test_repr_with_registers_appended(): 

72 register_array_a = RegisterArray(name="apa", base_index=0, length=4, description="") 

73 register_array_a.append_register(name="hest", mode="r", description="") 

74 

75 register_array_b = RegisterArray(name="apa", base_index=0, length=4, description="") 

76 register_array_b.append_register(name="hest", mode="r", description="") 

77 

78 assert repr(register_array_a) == repr(register_array_b) 

79 

80 register_array_a.append_register(name="zebra", mode="w", description="") 

81 register_array_b.append_register(name="zebra", mode="r_w", description="") 

82 

83 assert repr(register_array_a) != repr(register_array_b) 

84 

85 

86def test_index(): 

87 register_array = RegisterArray(name="apa", base_index=0, length=4, description="") 

88 register_array.append_register(name="hest", mode="r", description="") 

89 assert register_array.index == 3 

90 

91 register_array.length = 5 

92 assert register_array.index == 4 

93 

94 register_array.append_register(name="zebra", mode="r", description="") 

95 assert register_array.index == 9 

96 

97 

98def test_start_index(): 

99 register_array = RegisterArray(name="apa", base_index=10, length=4, description="") 

100 register_array.append_register(name="hest", mode="r", description="") 

101 assert register_array.get_start_index(0) == 10 

102 assert register_array.get_start_index(1) == 11 

103 assert register_array.get_start_index(2) == 12 

104 

105 register_array.append_register(name="zebra", mode="r", description="") 

106 assert register_array.get_start_index(0) == 10 

107 assert register_array.get_start_index(1) == 12 

108 assert register_array.get_start_index(2) == 14 

109 

110 

111def test_start_index_with_argument_outside_of_length_should_raise_exception(): 

112 register_array = RegisterArray(name="apa", base_index=0, length=4, description="") 

113 register_array.append_register(name="hest", mode="r", description="") 

114 

115 with pytest.raises(ValueError) as exception_info: 

116 register_array.get_start_index(4) 

117 assert str(exception_info.value) == 'Index 4 out of range for register array "apa" of length 4.'