fc8c8f924a2a6876f3f954579e2ad170834a71de
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 Fix typing issues in mypy s...

Marco Ricci authored 3 months ago

30)     def test_200_big_endian_number(
31)         self, sequence: list[int], base: int, expected: int
32)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

33)         assert (
34)             sequin.Sequin._big_endian_number(sequence, base=base)
35)         ) == expected
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

40)             (ValueError, 'invalid base 3 digit:', [-1], 3),
41)             (ValueError, 'invalid base:', [0], 1),
42)             (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

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

Marco Ricci authored 4 months ago

44)     )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

45)     def test_300_big_endian_number_exceptions(
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 3 months ago

46)         self,
47)         exc_type: type[Exception],
48)         exc_pattern: str,
49)         sequence: list[int],
50)         base: int,
51)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 5 months ago

54) 
55) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

56) class TestSequin:
57)     @pytest.mark.parametrize(
58)         ['sequence', 'is_bitstring', 'expected'],
59)         [
60)             (
61)                 [1, 0, 0, 1, 0, 1],
62)                 False,
Marco Ricci Add bitseq function in test...

Marco Ricci authored 3 months ago

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

Marco Ricci authored 3 months ago

64)             ),
65)             ([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

66)             (b'OK', False, bitseq('0100111101001011')),
67)             ('OK', False, bitseq('0100111101001011')),
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

68)         ],
69)     )
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 3 months ago

70)     def test_200_constructor(
71)         self,
72)         sequence: str | bytes | bytearray | list[int],
73)         is_bitstring: bool,
74)         expected: list[int],
75)     ) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

76)         seq = sequin.Sequin(sequence, is_bitstring=is_bitstring)
77)         assert seq.bases == {2: collections.deque(expected)}
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

78) 
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 3 months ago

79)     def test_201_generating(self) -> None:
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)         assert seq.generate(1) == 0
84)         assert seq.generate(5) == 3
85)         assert seq.generate(5) == 3
86)         assert seq.generate(5) == 1
Marco Ricci Rename SequinExhaustedExcep...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

91)         seq = sequin.Sequin(
92)             [1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1], is_bitstring=True
93)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

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

Marco Ricci authored 5 months ago

96) 
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 3 months ago

97)     def test_210_internal_generating(self) -> None:
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

98)         seq = sequin.Sequin(
99)             [1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1], is_bitstring=True
100)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

101)         assert seq._generate_inner(5) == 3
102)         assert seq._generate_inner(5) == 3
103)         assert seq._generate_inner(5) == 1
104)         assert seq._generate_inner(5) == 5
105)         assert seq._generate_inner(1) == 0
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

106)         seq = sequin.Sequin(
107)             [1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1], is_bitstring=True
108)         )
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

109)         assert seq._generate_inner(1) == 0
110)         with pytest.raises(ValueError, match='invalid target range'):
111)             seq._generate_inner(0)
112)         with pytest.raises(ValueError, match='invalid base:'):
113)             seq._generate_inner(16, base=1)
Marco Ricci Add unit tests, both new an...

Marco Ricci authored 5 months ago

114) 
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 3 months ago

115)     def test_211_shifting(self) -> None:
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

116)         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

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 3 months ago

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

Marco Ricci authored 4 months ago

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

Marco Ricci authored 5 months ago

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

Marco Ricci authored 4 months ago

131)     @pytest.mark.parametrize(
132)         ['sequence', 'is_bitstring', 'exc_type', 'exc_pattern'],
133)         [
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

134)             (
135)                 [0, 1, 2, 3, 4, 5, 6, 7],
136)                 True,
137)                 ValueError,
138)                 'sequence item out of range',
139)             ),
140)             ('こんにちは。', False, ValueError, 'sequence item out of range'),
141)         ],
Marco Ricci Rename and regroup all test...

Marco Ricci authored 4 months ago

142)     )
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

143)     def test_300_constructor_exceptions(
Marco Ricci Fix typing issues in mypy s...

Marco Ricci authored 3 months ago

144)         self,
145)         sequence: list[int] | str,
146)         is_bitstring: bool,
147)         exc_type: type[Exception],
148)         exc_pattern: str,
149)     ) -> None: