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.bit import Bit 

12 

13 

14def test_get_value(): 

15 bit = Bit(name="", index=2, description="", default_value="1") 

16 

17 register_value = int("1111011", base=2) 

18 assert bit.get_value(register_value) == 0 

19 

20 register_value = int("0000100", base=2) 

21 assert bit.get_value(register_value) == 1 

22 

23 

24def test_repr(): 

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

26 assert "apa" in repr(Bit(name="apa", index=0, description="", default_value="0")) 

27 

28 # Different name 

29 assert repr(Bit(name="apa", index=0, description="", default_value="0")) != repr( 

30 Bit(name="hest", index=0, description="", default_value="0") 

31 ) 

32 

33 # Different index 

34 assert repr(Bit(name="apa", index=0, description="", default_value="0")) != repr( 

35 Bit(name="apa", index=1, description="", default_value="0") 

36 ) 

37 

38 # Different description 

39 assert repr(Bit(name="apa", index=0, description="Blah", default_value="0")) != repr( 

40 Bit(name="apa", index=0, description="Gaah", default_value="0") 

41 ) 

42 

43 # Different default_value 

44 assert repr(Bit(name="apa", index=0, description="", default_value="1")) != repr( 

45 Bit(name="apa", index=0, description="", default_value="0") 

46 ) 

47 

48 

49def test_default_value_uint(): 

50 assert Bit(name="apa", index=0, description="", default_value="1").default_value_uint == 1 

51 assert Bit(name="apa", index=0, description="", default_value="0").default_value_uint == 0 

52 

53 

54def test_invalid_default_value_should_raise_exception(): 

55 with pytest.raises(ValueError) as exception_info: 

56 Bit(name="hest", index=0, description="", default_value=1) 

57 assert str(exception_info.value) == ( 

58 'Bit "hest" should have string value for "default_value". Got "1".' 

59 ) 

60 

61 with pytest.raises(ValueError) as exception_info: 

62 Bit(name="hest", index=0, description="", default_value="11") 

63 assert str(exception_info.value) == ( 

64 'Bit "hest" invalid binary value for "default_value". Got: "11".' 

65 ) 

66 

67 with pytest.raises(ValueError) as exception_info: 

68 Bit(name="hest", index=0, description="", default_value="2") 

69 assert str(exception_info.value) == ( 

70 'Bit "hest" invalid binary value for "default_value". Got: "2".' 

71 ) 

72 

73 

74def test_can_update_default_value(): 

75 bit = Bit(name="hest", index=0, description="", default_value="1") 

76 assert bit.default_value == "1" 

77 

78 bit.default_value = "0" 

79 assert bit.default_value == "0" 

80 

81 

82def test_updating_to_invalid_default_value_should_raise_exception(): 

83 # Create with a valid default_value 

84 bit = Bit(name="hest", index=0, description="", default_value="1") 

85 

86 # Update to an invalid value 

87 with pytest.raises(ValueError) as exception_info: 

88 bit.default_value = "2" 

89 assert str(exception_info.value) == ( 

90 'Bit "hest" invalid binary value for "default_value". Got: "2".' 

91 )