6fd0fd9f9288bbe4f9fabb7e36d7342969bdb925
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

1) # SPDX-FileCopyrightText: 2024 Marco Ricci <m@the13thletter.info>
2) #
3) # SPDX-License-Identifier: MIT
4) 
5) """Test sequin.Sequin."""
6) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

7) import collections
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

8) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

9) import pytest
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

10) 
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

11) import sequin
12) 
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

13) 
Marco Ricci Add bitseq function in test...

Marco Ricci authored 3 months ago

14) def bitseq(string: str) -> list[int]:
15)     """Convert a 0/1-string into a list of bits."""
16)     return [int(char, 2) for char in string]
17) 
18) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

19) class TestStaticFunctionality:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

20)     @pytest.mark.parametrize(
21)         ['sequence', 'base', 'expected'],
22)         [
23)             ([1, 2, 3, 4, 5, 6], 10, 123456),
24)             ([1, 2, 3, 4, 5, 6], 100, 10203040506),
25)             ([0, 0, 1, 4, 9, 7], 10, 1497),
26)             ([1, 0, 0, 1, 0, 0, 0, 0], 2, 144),
27)             ([1, 7, 5, 5], 8, 0o1755),
28)         ],
29)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

30)     def test_200_big_endian_number(self, sequence, base, expected):
31)         assert (
32)             sequin.Sequin._big_endian_number(sequence, base=base)
33)         ) == expected
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

34) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

35)     @pytest.mark.parametrize(
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

36)         ['exc_type', 'exc_pattern', 'sequence', 'base'],
37)         [
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

38)             (ValueError, 'invalid base 3 digit:', [-1], 3),
39)             (ValueError, 'invalid base:', [0], 1),
40)             (TypeError, 'not an integer:', [0.0, 1.0, 0.0, 1.0], 2),
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

41)         ],
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

42)     )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

43)     def test_300_big_endian_number_exceptions(
44)         self, exc_type, exc_pattern, sequence, base
45)     ):
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

46)         with pytest.raises(exc_type, match=exc_pattern):
47)             sequin.Sequin._big_endian_number(sequence, base=base)
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

48) 
49) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

50) class TestSequin:
51)     @pytest.mark.parametrize(
52)         ['sequence', 'is_bitstring', 'expected'],
53)         [
54)             (
55)                 [1, 0, 0, 1, 0, 1],
56)                 False,
Marco Ricci Add bitseq function in test...

Marco Ricci authored 3 months ago

57)                 bitseq('000000010000000000000000000000010000000000000001'),
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

58)             ),
59)             ([1, 0, 0, 1, 0, 1], True, [1, 0, 0, 1, 0, 1]),
Marco Ricci Add bitseq function in test...

Marco Ricci authored 3 months ago

60)             (b'OK', False, bitseq('0100111101001011')),
61)             ('OK', False, bitseq('0100111101001011')),
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

62)         ],
63)     )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

64)     def test_200_constructor(self, sequence, is_bitstring, expected):
65)         seq = sequin.Sequin(sequence, is_bitstring=is_bitstring)
66)         assert seq.bases == {2: collections.deque(expected)}
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

67) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

68)     def test_201_generating(self):
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

69)         seq = sequin.Sequin(
70)             [1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1], is_bitstring=True
71)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

72)         assert seq.generate(1) == 0
73)         assert seq.generate(5) == 3
74)         assert seq.generate(5) == 3
75)         assert seq.generate(5) == 1
Marco Ricci Rename SequinExhaustedExcep...

Marco Ricci authored 4 months ago

76)         with pytest.raises(sequin.SequinExhaustedError):
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

77)             seq.generate(5)
Marco Ricci Rename SequinExhaustedExcep...

Marco Ricci authored 4 months ago

78)         with pytest.raises(sequin.SequinExhaustedError):
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

79)             seq.generate(1)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

80)         seq = sequin.Sequin(
81)             [1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1], is_bitstring=True
82)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

83)         with pytest.raises(ValueError, match='invalid target range'):
84)             seq.generate(0)
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

85) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

86)     def test_210_internal_generating(self):
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

87)         seq = sequin.Sequin(
88)             [1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1], is_bitstring=True
89)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

90)         assert seq._generate_inner(5) == 3
91)         assert seq._generate_inner(5) == 3
92)         assert seq._generate_inner(5) == 1
93)         assert seq._generate_inner(5) == 5
94)         assert seq._generate_inner(1) == 0
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

95)         seq = sequin.Sequin(
96)             [1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1], is_bitstring=True
97)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

98)         assert seq._generate_inner(1) == 0
99)         with pytest.raises(ValueError, match='invalid target range'):
100)             seq._generate_inner(0)
101)         with pytest.raises(ValueError, match='invalid base:'):
102)             seq._generate_inner(16, base=1)
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

103) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

104)     def test_211_shifting(self):
105)         seq = sequin.Sequin([1, 0, 1, 0, 0, 1, 0, 0, 0, 1], is_bitstring=True)
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

106)         assert seq.bases == {
107)             2: collections.deque([1, 0, 1, 0, 0, 1, 0, 0, 0, 1])
108)         }
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

109) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

110)         assert seq._all_or_nothing_shift(3) == (1, 0, 1)
111)         assert seq._all_or_nothing_shift(3) == (0, 0, 1)
112)         assert seq.bases[2] == collections.deque([0, 0, 0, 1])
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

113) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

114)         assert seq._all_or_nothing_shift(5) == ()
115)         assert seq.bases[2] == collections.deque([0, 0, 0, 1])
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

116) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

117)         assert seq._all_or_nothing_shift(4), (0, 0, 0, 1)
118)         assert 2 not in seq.bases
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

119) 
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

120)     @pytest.mark.parametrize(
121)         ['sequence', 'is_bitstring', 'exc_type', 'exc_pattern'],
122)         [
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

123)             (
124)                 [0, 1, 2, 3, 4, 5, 6, 7],
125)                 True,
126)                 ValueError,
127)                 'sequence item out of range',
128)             ),
129)             ('こんにちは。', False, ValueError, 'sequence item out of range'),
130)         ],
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

131)     )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

132)     def test_300_constructor_exceptions(
133)         self, sequence, is_bitstring, exc_type, exc_pattern
134)     ):