Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9import pytest 

10 

11from tsfpga.registers.register_array import RegisterArray 

12 

13 

14def test_registers_are_appended_properly_and_can_be_edited_in_place(): 

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

16 

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

18 assert register_hest.index == 0 

19 

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

21 assert register_zebra.index == 1 

22 

23 register_hest.description = "new desc" 

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

25 

26 

27def test_get_register(): 

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

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

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

31 

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

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

34 

35 with pytest.raises(ValueError) as exception_info: 

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

37 assert ( 

38 str(exception_info.value) 

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

40 ) 

41 

42 

43def test_repr_basic(): 

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

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

46 

47 # Different name 

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

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

50 ) 

51 

52 # Different base_index 

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

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

55 ) 

56 

57 # Different length 

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

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

60 ) 

61 

62 # Different description 

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

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

65 ) 

66 

67 

68def test_repr_with_registers_appended(): 

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

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

71 

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

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

74 

75 assert repr(register_array_a) == repr(register_array_b) 

76 

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

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

79 

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

81 

82 

83def test_index(): 

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

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

86 assert register_array.index == 3 

87 

88 register_array.length = 5 

89 assert register_array.index == 4 

90 

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

92 assert register_array.index == 9 

93 

94 

95def test_start_index(): 

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

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

98 assert register_array.get_start_index(0) == 10 

99 assert register_array.get_start_index(1) == 11 

100 assert register_array.get_start_index(2) == 12 

101 

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

103 assert register_array.get_start_index(0) == 10 

104 assert register_array.get_start_index(1) == 12 

105 assert register_array.get_start_index(2) == 14 

106 

107 

108def test_start_index_with_argument_outside_of_length_should_raise_exception(): 

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

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

111 

112 with pytest.raises(ValueError) as exception_info: 

113 register_array.get_start_index(4) 

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