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

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

13 

14 

15def test_registers_are_appended_properly_and_can_be_edited_in_place(): 

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

17 

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

19 assert register_hest.index == 0 

20 

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

22 assert register_zebra.index == 1 

23 

24 register_hest.description = "new desc" 

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

26 

27 

28def test_get_register(): 

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

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

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

32 

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

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

35 

36 with pytest.raises(ValueError) as exception_info: 

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

38 assert ( 

39 str(exception_info.value) 

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

41 ) 

42 

43 

44def test_repr_basic(): 

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

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

47 

48 # Different name 

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

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

51 ) 

52 

53 # Different base_index 

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

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

56 ) 

57 

58 # Different length 

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

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

61 ) 

62 

63 # Different description 

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

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

66 ) 

67 

68 

69def test_repr_with_registers_appended(): 

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

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

72 

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

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

75 

76 assert repr(register_array_a) == repr(register_array_b) 

77 

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

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

80 

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

82 

83 

84def test_index(): 

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

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

87 assert register_array.index == 3 

88 

89 register_array.length = 5 

90 assert register_array.index == 4 

91 

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

93 assert register_array.index == 9 

94 

95 

96def test_start_index(): 

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

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

99 assert register_array.get_start_index(0) == 10 

100 assert register_array.get_start_index(1) == 11 

101 assert register_array.get_start_index(2) == 12 

102 

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

104 assert register_array.get_start_index(0) == 10 

105 assert register_array.get_start_index(1) == 12 

106 assert register_array.get_start_index(2) == 14 

107 

108 

109def test_start_index_with_argument_outside_of_length_should_raise_exception(): 

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

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

112 

113 with pytest.raises(ValueError) as exception_info: 

114 register_array.get_start_index(4) 

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