1
|
|
|
""" |
2
|
|
|
:mod:`tests.unit.test_sequence` -- Unit Tests |
3
|
|
|
============================================= |
4
|
|
|
|
5
|
|
|
.. module:: tests.unit.test_sequence |
6
|
|
|
:synopsis: Unit tests for the lib.sequence module. |
7
|
|
|
|
8
|
|
|
.. moduleauthor:: Bill Maroney <[email protected]> |
9
|
|
|
""" |
10
|
|
|
|
11
|
|
|
import pytest |
12
|
|
|
from typing import Set |
|
|
|
|
13
|
|
|
|
14
|
|
|
from lib.sequence import Factorials, Fibonaccis |
15
|
|
|
from lib.sequence import Figurates, Triangulars, Pentagonals, Hexagonals |
16
|
|
|
from lib.sequence import Pandigitals, Divisors, DivisorsRange, Primes |
17
|
|
|
from lib.sequence import ContinuedFraction, SqrtExpansion |
|
|
|
|
18
|
|
|
from lib.util import load_dataset |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
# Some prime number constants used to test lib.sequence.Primes |
22
|
|
|
SMALL_LIMIT = 10 |
23
|
|
|
PRIMES_UP_TO_SMALL_LIMIT = [2, 3, 5, 7] |
24
|
|
|
FIRST_SMALL_LIMIT_PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
@pytest.mark.parametrize("n,n_factorial", [(0, 1), (1, 1), (2, 2), (3, 6), (4, 24), (5, 120), (12, 479001600), |
|
|
|
|
28
|
|
|
(15, 1307674368000), (20, 2432902008176640000)]) |
29
|
|
|
def test_factorials_correctness_getitem(n: int, n_factorial: int): |
|
|
|
|
30
|
|
|
""" Test the correctness of indexing :class:`lib.sequence.Factorials` instances using known answer tests |
|
|
|
|
31
|
|
|
|
32
|
|
|
:param n: the input value |
33
|
|
|
:param n_factorial: the expected answer |
34
|
|
|
:raises AssertionError: if :class:`lib.sequence.Factorials` produces the wrong type |
35
|
|
|
:raises AssertionError: if :class:`lib.sequence.Factorials` produces the wrong value |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
seq = Factorials() |
39
|
|
|
computed_answer = seq[n] |
40
|
|
|
assert isinstance(computed_answer, Factorials.__getitem__.__annotations__["return"]), "wrong type" |
|
|
|
|
41
|
|
|
assert computed_answer == n_factorial, "wrong value" |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def test_factorials_correctness_batch(): |
|
|
|
|
45
|
|
|
""" Test the correctness of :class:`lib.sequence.Factorials` using known data """ |
46
|
|
|
factorial_numbers = load_dataset("general", "factorial", data_type=int) |
47
|
|
|
for n, seq_n in enumerate(Factorials()): |
|
|
|
|
48
|
|
|
if n >= len(factorial_numbers): |
49
|
|
|
break |
50
|
|
|
assert isinstance(seq_n, Factorials.__next__.__annotations__["return"]), "wrong type" |
51
|
|
|
assert seq_n == factorial_numbers[n], "wrong value" |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def test_factorials_len(): |
55
|
|
|
""" Test that ``len`` of :class:`lib.sequence.Factorials` instances raises a ``TypeError`` """ |
56
|
|
|
with pytest.raises(TypeError): |
57
|
|
|
seq = Factorials() |
58
|
|
|
len(seq) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
@pytest.mark.parametrize("x,is_factorial", [(-10, False), (0, False), (1, True), (6, True), (120, True), (121, False)]) |
|
|
|
|
62
|
|
|
def test_factorials_correctness_in(x: int, is_factorial: bool): |
|
|
|
|
63
|
|
|
""" Test the correctness of membership tests for :class:`lib.sequence.Factorials` instances using known answer tests |
|
|
|
|
64
|
|
|
|
65
|
|
|
:param x: the input value |
66
|
|
|
:param is_factorial: whether :math:`x = N!` :math:`\\exists N \\in \\mathbb{N}` (i.e. :math:`x` is factorial) |
|
|
|
|
67
|
|
|
:raises AssertionError: if :class:`lib.sequence.Factorials` produces the wrong type |
68
|
|
|
:raises AssertionError: if :class:`lib.sequence.Factorials` produces the wrong value |
69
|
|
|
""" |
70
|
|
|
|
71
|
|
|
computed_answer = x in Factorials() |
72
|
|
|
assert isinstance(computed_answer, Factorials.__contains__.__annotations__["return"]), "wrong type" |
|
|
|
|
73
|
|
|
assert computed_answer == is_factorial, "wrong value" |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_factorials_getitem_bad_item_type_str(): |
|
|
|
|
77
|
|
|
""" Test that :class:`lib.sequence.Factorials` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
78
|
|
|
(``str``) |
79
|
|
|
""" |
80
|
|
|
with pytest.raises(TypeError): |
81
|
|
|
seq = Factorials() |
82
|
|
|
_ = seq["bam"] |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
def test_factorials_getitem_bad_item_type_float(): |
|
|
|
|
86
|
|
|
""" Test that :class:`lib.sequence.Factorials` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
87
|
|
|
(``float``) |
88
|
|
|
""" |
89
|
|
|
with pytest.raises(TypeError): |
90
|
|
|
seq = Factorials() |
91
|
|
|
_ = seq[12.3] |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def test_factorials_getitem_bad_item_negative(): |
|
|
|
|
95
|
|
|
""" Test that :class:`lib.sequence.Factorials` raises a ``ValueError`` for an index :math:`\\mbox{item} \\lt 0` """ |
|
|
|
|
96
|
|
|
with pytest.raises(ValueError): |
97
|
|
|
seq = Factorials() |
98
|
|
|
_ = seq[-2] |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
def test_factorials_in_bad_item_type_str(): |
|
|
|
|
102
|
|
|
""" Test that :class:`lib.sequence.Factorials` raises a ``TypeError`` for a non-``int`` value for `x` (``str``) """ |
|
|
|
|
103
|
|
|
with pytest.raises(TypeError): |
104
|
|
|
seq = Factorials() |
105
|
|
|
_ = "abc" in seq |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
@pytest.mark.parametrize("n,nth_fibonacci", [(0, 0), (1, 1), (2, 1), (3, 2), (4, 3), (5, 5), (6, 8), (7, 13), (8, 21), |
|
|
|
|
109
|
|
|
(9, 34), (10, 55), (11, 89), (12, 144), (13, 233), (14, 377), (15, 610)]) |
|
|
|
|
110
|
|
|
def test_fibonaccis_correctness_getitem(n: int, nth_fibonacci: int): |
|
|
|
|
111
|
|
|
""" Test the correctness of :class:`lib.sequence.Fibonaccis` using known answer tests |
112
|
|
|
|
113
|
|
|
:param n: the input value |
114
|
|
|
:param nth_fibonacci: the expected answer |
115
|
|
|
:raises AssertionError: if :class:`lib.sequence.Fibonaccis` produces the wrong type |
116
|
|
|
:raises AssertionError: if :class:`lib.sequence.Fibonaccis` produces the wrong value |
117
|
|
|
""" |
118
|
|
|
|
119
|
|
|
seq = Fibonaccis() |
120
|
|
|
computed_answer = seq[n] |
121
|
|
|
assert isinstance(computed_answer, Fibonaccis.__getitem__.__annotations__["return"]), "wrong type" |
|
|
|
|
122
|
|
|
assert computed_answer == nth_fibonacci, "wrong value" |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
def test_fibonaccis_correctness_batch(): |
|
|
|
|
126
|
|
|
""" Test the correctness of :class:`lib.sequence.Fibonaccis` using known data """ |
127
|
|
|
fibonacci_numbers = load_dataset("general", "fibonacci", data_type=int) |
128
|
|
|
for n, seq_n in enumerate(Fibonaccis()): |
|
|
|
|
129
|
|
|
if n == 0: |
130
|
|
|
continue |
131
|
|
|
elif n >= len(fibonacci_numbers) - 1: |
132
|
|
|
break |
133
|
|
|
else: |
134
|
|
|
assert seq_n == fibonacci_numbers[n - 1] |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
def test_fibonaccis_len(): |
138
|
|
|
""" Test that ``len`` of :class:`lib.sequence.Fibonaccis` instances raises a ``TypeError`` """ |
139
|
|
|
with pytest.raises(TypeError): |
140
|
|
|
seq = Fibonaccis() |
141
|
|
|
len(seq) |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
@pytest.mark.parametrize("x,is_fibonacci", [(-10, False), (0, True), (1, True), (6, False), (21, True), (22, False)]) |
|
|
|
|
145
|
|
|
def test_fibonaccis_correctness_in(x: int, is_fibonacci: bool): |
|
|
|
|
146
|
|
|
""" Test the correctness of membership tests for :class:`lib.sequence.Fibonaccis` instances using known answer tests |
|
|
|
|
147
|
|
|
|
148
|
|
|
:param x: the input value |
149
|
|
|
:param is_fibonacci: whether :math:`x` is a Fibonacci number or not |
150
|
|
|
:raises AssertionError: if :class:`lib.sequence.Fibonaccis` produces the wrong type |
151
|
|
|
:raises AssertionError: if :class:`lib.sequence.Fibonaccis` produces the wrong value |
152
|
|
|
""" |
153
|
|
|
|
154
|
|
|
computed_answer = x in Fibonaccis() |
155
|
|
|
assert isinstance(computed_answer, Fibonaccis.__contains__.__annotations__["return"]), "wrong type" |
|
|
|
|
156
|
|
|
assert computed_answer == is_fibonacci, "wrong value" |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
def test_fibonaccis_getitem_bad_item_type_str(): |
|
|
|
|
160
|
|
|
""" Test that :class:`lib.sequence.Fibonaccis` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
161
|
|
|
(``str``) |
162
|
|
|
""" |
163
|
|
|
with pytest.raises(TypeError): |
164
|
|
|
seq = Fibonaccis() |
165
|
|
|
_ = seq["bam"] |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
def test_fibonaccis_getitem_bad_item_type_float(): |
|
|
|
|
169
|
|
|
""" Test that :class:`lib.sequence.Fibonaccis` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
170
|
|
|
(``float``) |
171
|
|
|
""" |
172
|
|
|
with pytest.raises(TypeError): |
173
|
|
|
seq = Fibonaccis() |
174
|
|
|
_ = seq[12.3] |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
def test_fibonaccis_getitem_bad_item_negative(): |
|
|
|
|
178
|
|
|
""" Test that :class:`lib.sequence.Fibonaccis` raises a ``ValueError`` for an index :math:`\\mbox{item} \\lt 0` """ |
|
|
|
|
179
|
|
|
with pytest.raises(ValueError): |
180
|
|
|
seq = Fibonaccis() |
181
|
|
|
_ = seq[-2] |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
def test_fibonaccis_in_bad_item_type_str(): |
|
|
|
|
185
|
|
|
""" Test that :class:`lib.sequence.Fibonaccis` raises a ``TypeError`` for a non-``int`` value for `x` (``str``) """ |
|
|
|
|
186
|
|
|
with pytest.raises(TypeError): |
187
|
|
|
seq = Fibonaccis() |
188
|
|
|
_ = "abc" in seq |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
def test_figurates_next(): |
192
|
|
|
""" Test that iteration over :class:`lib.sequence.Figurates` instances raises a ``NotImplementedError`` """ |
|
|
|
|
193
|
|
|
with pytest.raises(NotImplementedError): |
194
|
|
|
seq = Figurates() |
195
|
|
|
next(iter(seq)) |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
def test_figurates_len(): |
199
|
|
|
""" Test that ``len`` of :class:`lib.sequence.Figurates` instances raises a ``TypeError`` """ |
200
|
|
|
with pytest.raises(TypeError): |
201
|
|
|
seq = Figurates() |
202
|
|
|
len(seq) |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
def test_figurates_in(): |
206
|
|
|
""" Test that membership tests for :class:`lib.sequence.Figurates` instances raises a ``NotImplementedError`` """ |
|
|
|
|
207
|
|
|
with pytest.raises(NotImplementedError): |
208
|
|
|
seq = Figurates() |
209
|
|
|
_ = 0 in seq |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
@pytest.mark.parametrize("n,nth_triangular", [(1, 1), (2, 3), (3, 6), (4, 10), (5, 15), (6, 21), (7, 28), (8, 36), |
|
|
|
|
213
|
|
|
(9, 45), (10, 55), (11, 66), (12, 78), (13, 91), (14, 105), (15, 120)]) |
|
|
|
|
214
|
|
|
def test_triangulars_correctness_getitem(n: int, nth_triangular: int): |
|
|
|
|
215
|
|
|
""" Test the correctness of :class:`lib.sequence.Triangulars` using known answer tests |
216
|
|
|
|
217
|
|
|
:param n: the input value |
218
|
|
|
:param nth_triangular: the expected answer |
219
|
|
|
:raises AssertionError: if :class:`lib.sequence.Triangulars` produces the wrong type |
220
|
|
|
:raises AssertionError: if :class:`lib.sequence.Triangulars` produces the wrong value |
221
|
|
|
""" |
222
|
|
|
|
223
|
|
|
seq = Triangulars() |
224
|
|
|
assert isinstance(seq[n], Triangulars.__getitem__.__annotations__["return"]), "wrong type" |
225
|
|
|
assert seq[n] == nth_triangular, "wrong value" |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
def test_triangulars_correctness_iter(): |
|
|
|
|
229
|
|
|
""" Test the correctness of iteration over :class:`lib.sequence.Triangulars` instances using known answer tests |
|
|
|
|
230
|
|
|
|
231
|
|
|
:raises AssertionError: if :class:`lib.sequence.Triangulars` produces the wrong type |
232
|
|
|
:raises AssertionError: if :class:`lib.sequence.Triangulars` produces the wrong value |
233
|
|
|
""" |
234
|
|
|
|
235
|
|
|
expected_answer = [1, 3, 6, 10, 15, 21, 28, 36, 45, 55] |
236
|
|
|
seq = Triangulars() |
237
|
|
|
for n, nth_triangular in enumerate(seq): |
|
|
|
|
238
|
|
|
if n >= len(expected_answer): |
239
|
|
|
break |
240
|
|
|
assert isinstance(nth_triangular, Triangulars.__next__.__annotations__["return"]), "wrong type" |
|
|
|
|
241
|
|
|
assert nth_triangular == expected_answer[n], "wrong value" |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
def test_triangulars_len(): |
245
|
|
|
""" Test that ``len`` of :class:`lib.sequence.Triangulars` instances raises a ``TypeError`` """ |
246
|
|
|
with pytest.raises(TypeError): |
247
|
|
|
seq = Triangulars() |
248
|
|
|
len(seq) |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
def test_triangulars_getitem_bad_item_type_str(): |
|
|
|
|
252
|
|
|
""" Test that :class:`lib.sequence.Triangulars` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
253
|
|
|
(``str``) |
254
|
|
|
""" |
255
|
|
|
with pytest.raises(TypeError): |
256
|
|
|
seq = Triangulars() |
257
|
|
|
_ = seq["bam"] |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
def test_triangulars_getitem_bad_item_type_float(): |
|
|
|
|
261
|
|
|
""" Test that :class:`lib.sequence.Triangulars` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
262
|
|
|
(``float``) |
263
|
|
|
""" |
264
|
|
|
with pytest.raises(TypeError): |
265
|
|
|
seq = Triangulars() |
266
|
|
|
_ = seq[12.3] |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
@pytest.mark.parametrize("x,expected_answer", [(1, True), (2, False), (3, True), (50, False)]) |
270
|
|
|
def test_triangulars_correctness_in(x: int, expected_answer: bool): |
|
|
|
|
271
|
|
|
""" Test the correctness of membership tests for :class:`lib.sequence.Triangulars` instances using known answer |
|
|
|
|
272
|
|
|
tests |
273
|
|
|
|
274
|
|
|
:param x: the input value |
275
|
|
|
:param expected_answer: whether :math:`x` is a triangular number or not |
276
|
|
|
:raises AssertionError: if :class:`lib.sequence.Triangulars` produces the wrong type |
277
|
|
|
:raises AssertionError: if :class:`lib.sequence.Triangulars` produces the wrong value |
278
|
|
|
""" |
279
|
|
|
|
280
|
|
|
seq = Triangulars() |
281
|
|
|
computed_answer = x in seq |
282
|
|
|
assert isinstance(computed_answer, Triangulars.__contains__.__annotations__["return"]), "wrong type" |
|
|
|
|
283
|
|
|
assert computed_answer == expected_answer, "wrong value" |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
def test_triangulars_in_bad_item_type_str(): |
|
|
|
|
287
|
|
|
""" Test that membership tests for :class:`lib.sequence.Triangulars` instances raise a ``TypeError`` for a |
|
|
|
|
288
|
|
|
non-``int`` value (``str``) |
289
|
|
|
""" |
290
|
|
|
with pytest.raises(TypeError): |
291
|
|
|
seq = Triangulars() |
292
|
|
|
_ = "abc" in seq |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
def test_triangulars_in_bad_item_type_float(): |
|
|
|
|
296
|
|
|
""" Test that membership tests for :class:`lib.sequence.Triangulars` instances raise a ``TypeError`` for a |
|
|
|
|
297
|
|
|
non-``int`` value (``float``) |
298
|
|
|
""" |
299
|
|
|
with pytest.raises(TypeError): |
300
|
|
|
seq = Triangulars() |
301
|
|
|
_ = 1.23 in seq |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
@pytest.mark.parametrize("n,nth_pentagonal", [(1, 1), (2, 5), (3, 12), (4, 22), (5, 35), (6, 51), (7, 70), (8, 92), |
|
|
|
|
305
|
|
|
(9, 117), (10, 145), (11, 176), (12, 210), (13, 247), (14, 287)]) |
|
|
|
|
306
|
|
|
def test_pentagonals_correctness_getitem(n: int, nth_pentagonal: int): |
|
|
|
|
307
|
|
|
""" Test the correctness of :class:`lib.sequence.Pentagonals` using known answer tests |
308
|
|
|
|
309
|
|
|
:param n: the input value |
310
|
|
|
:param nth_pentagonal: the expected answer |
311
|
|
|
:raises AssertionError: if :class:`lib.sequence.Pentagonals` produces the wrong type |
312
|
|
|
:raises AssertionError: if :class:`lib.sequence.Pentagonals` produces the wrong value |
313
|
|
|
""" |
314
|
|
|
|
315
|
|
|
seq = Pentagonals() |
316
|
|
|
assert isinstance(seq[n], Pentagonals.__getitem__.__annotations__["return"]), "wrong type" |
317
|
|
|
assert seq[n] == nth_pentagonal, "wrong value" |
318
|
|
|
|
319
|
|
|
|
320
|
|
|
def test_pentagonals_correctness_iter(): |
|
|
|
|
321
|
|
|
""" Test the correctness of iteration over :class:`lib.sequence.Pentagonals` instances using known answer tests |
|
|
|
|
322
|
|
|
|
323
|
|
|
:raises AssertionError: if :class:`lib.sequence.Pentagonals` produces the wrong type |
324
|
|
|
:raises AssertionError: if :class:`lib.sequence.Pentagonals` produces the wrong value |
325
|
|
|
""" |
326
|
|
|
|
327
|
|
|
expected_answer = [1, 5, 12, 22, 35, 51, 70, 92, 117, 145] |
328
|
|
|
seq = Pentagonals() |
329
|
|
|
for n, nth_pentagonal in enumerate(seq): |
|
|
|
|
330
|
|
|
if n >= len(expected_answer): |
331
|
|
|
break |
332
|
|
|
assert isinstance(nth_pentagonal, Pentagonals.__next__.__annotations__["return"]), "wrong type" |
|
|
|
|
333
|
|
|
assert nth_pentagonal == expected_answer[n], "wrong value" |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
def test_pentagonals_len(): |
337
|
|
|
""" Test that ``len`` of :class:`lib.sequence.Pentagonals` instances raises a ``TypeError`` """ |
338
|
|
|
with pytest.raises(TypeError): |
339
|
|
|
seq = Pentagonals() |
340
|
|
|
len(seq) |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
def test_pentagonals_getitem_bad_item_type_str(): |
|
|
|
|
344
|
|
|
""" Test that :class:`lib.sequence.Pentagonals` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
345
|
|
|
(``str``) |
346
|
|
|
""" |
347
|
|
|
with pytest.raises(TypeError): |
348
|
|
|
seq = Pentagonals() |
349
|
|
|
_ = seq["bam"] |
350
|
|
|
|
351
|
|
|
|
352
|
|
|
def test_pentagonals_getitem_bad_item_type_float(): |
|
|
|
|
353
|
|
|
""" Test that :class:`lib.sequence.Pentagonals` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
354
|
|
|
(``float``) |
355
|
|
|
""" |
356
|
|
|
with pytest.raises(TypeError): |
357
|
|
|
seq = Pentagonals() |
358
|
|
|
_ = seq[12.3] |
359
|
|
|
|
360
|
|
|
|
361
|
|
|
@pytest.mark.parametrize("x,expected_answer", [(1, True), (2, False), (35, True), (50, False)]) |
362
|
|
|
def test_pentagonals_correctness_in(x: int, expected_answer: bool): |
|
|
|
|
363
|
|
|
""" Test the correctness of membership tests for :class:`lib.sequence.Pentagonals` instances using known answer |
|
|
|
|
364
|
|
|
tests |
365
|
|
|
|
366
|
|
|
:param x: the input value |
367
|
|
|
:param expected_answer: whether :math:`x` is a pentagonal number or not |
368
|
|
|
:raises AssertionError: if :class:`lib.sequence.Pentagonals` produces the wrong type |
369
|
|
|
:raises AssertionError: if :class:`lib.sequence.Pentagonals` produces the wrong value |
370
|
|
|
""" |
371
|
|
|
|
372
|
|
|
seq = Pentagonals() |
373
|
|
|
computed_answer = x in seq |
374
|
|
|
assert isinstance(computed_answer, Pentagonals.__contains__.__annotations__["return"]), "wrong type" |
|
|
|
|
375
|
|
|
assert computed_answer == expected_answer, "wrong value" |
376
|
|
|
|
377
|
|
|
|
378
|
|
|
def test_pentagonals_in_bad_item_type_str(): |
|
|
|
|
379
|
|
|
""" Test that membership tests for :class:`lib.sequence.Pentagonals` instances raise a ``TypeError`` for a |
|
|
|
|
380
|
|
|
non-``int`` value (``str``) |
381
|
|
|
""" |
382
|
|
|
with pytest.raises(TypeError): |
383
|
|
|
seq = Pentagonals() |
384
|
|
|
_ = "abc" in seq |
385
|
|
|
|
386
|
|
|
|
387
|
|
|
def test_pentagonals_in_bad_item_type_float(): |
|
|
|
|
388
|
|
|
""" Test that membership tests for :class:`lib.sequence.Pentagonals` instances raise a ``TypeError`` for a |
|
|
|
|
389
|
|
|
non-``int`` value (``float``) |
390
|
|
|
""" |
391
|
|
|
with pytest.raises(TypeError): |
392
|
|
|
seq = Pentagonals() |
393
|
|
|
_ = 1.23 in seq |
394
|
|
|
|
395
|
|
|
|
396
|
|
|
@pytest.mark.parametrize("n,nth_hexagonal", [(1, 1), (2, 6), (3, 15), (4, 28), (5, 45), (6, 66), (7, 91), (8, 120), |
|
|
|
|
397
|
|
|
(9, 153), (10, 190), (11, 231), (12, 276), (13, 325), (14, 378)]) |
|
|
|
|
398
|
|
|
def test_hexagonals_correctness_getitem(n: int, nth_hexagonal: int): |
|
|
|
|
399
|
|
|
""" Test the correctness of :class:`lib.sequence.Hexagonals` using known answer tests |
400
|
|
|
|
401
|
|
|
:param n: the input value |
402
|
|
|
:param nth_hexagonal: the expected answer |
403
|
|
|
:raises AssertionError: if :class:`lib.sequence.Hexagonals` produces the wrong type |
404
|
|
|
:raises AssertionError: if :class:`lib.sequence.Hexagonals` produces the wrong value |
405
|
|
|
""" |
406
|
|
|
|
407
|
|
|
seq = Hexagonals() |
408
|
|
|
assert isinstance(seq[n], Hexagonals.__getitem__.__annotations__["return"]), "wrong type" |
409
|
|
|
assert seq[n] == nth_hexagonal, "wrong value" |
410
|
|
|
|
411
|
|
|
|
412
|
|
|
def test_hexagonals_correctness_iter(): |
|
|
|
|
413
|
|
|
""" Test the correctness of iteration over :class:`lib.sequence.Hexagonals` instances using known answer tests |
|
|
|
|
414
|
|
|
|
415
|
|
|
:raises AssertionError: if :class:`lib.sequence.Hexagonals` produces the wrong type |
416
|
|
|
:raises AssertionError: if :class:`lib.sequence.Hexagonals` produces the wrong value |
417
|
|
|
""" |
418
|
|
|
|
419
|
|
|
expected_answer = [1, 6, 15, 28, 45, 66, 91, 120, 153, 190] |
420
|
|
|
seq = Hexagonals() |
421
|
|
|
for n, nth_hexagonal in enumerate(seq): |
|
|
|
|
422
|
|
|
if n >= len(expected_answer): |
423
|
|
|
break |
424
|
|
|
assert isinstance(nth_hexagonal, Hexagonals.__next__.__annotations__["return"]), "wrong type" |
|
|
|
|
425
|
|
|
assert nth_hexagonal == expected_answer[n], "wrong value" |
426
|
|
|
|
427
|
|
|
|
428
|
|
|
def test_hexagonals_len(): |
429
|
|
|
""" Test that ``len`` of :class:`lib.sequence.Hexagonals` instances raises a ``TypeError`` """ |
430
|
|
|
with pytest.raises(TypeError): |
431
|
|
|
seq = Hexagonals() |
432
|
|
|
len(seq) |
433
|
|
|
|
434
|
|
|
|
435
|
|
|
def test_hexagonals_getitem_bad_item_type_str(): |
|
|
|
|
436
|
|
|
""" Test that :class:`lib.sequence.Hexagonals` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
437
|
|
|
(``str``) |
438
|
|
|
""" |
439
|
|
|
with pytest.raises(TypeError): |
440
|
|
|
seq = Hexagonals() |
441
|
|
|
_ = seq["bam"] |
442
|
|
|
|
443
|
|
|
|
444
|
|
|
def test_hexagonals_getitem_bad_item_type_float(): |
|
|
|
|
445
|
|
|
""" Test that :class:`lib.sequence.Hexagonals` raises a ``TypeError`` for a non-``int`` value for an index `item` |
|
|
|
|
446
|
|
|
(``float``) |
447
|
|
|
""" |
448
|
|
|
with pytest.raises(TypeError): |
449
|
|
|
seq = Hexagonals() |
450
|
|
|
_ = seq[12.3] |
451
|
|
|
|
452
|
|
|
|
453
|
|
|
@pytest.mark.parametrize("x,expected_answer", [(1, True), (2, False), (45, True), (50, False)]) |
454
|
|
|
def test_hexagonals_correctness_in(x: int, expected_answer: bool): |
|
|
|
|
455
|
|
|
""" Test the correctness of membership tests for :class:`lib.sequence.Hexagonals` instances using known answer tests |
|
|
|
|
456
|
|
|
|
457
|
|
|
:param x: the input value |
458
|
|
|
:param expected_answer: whether :math:`x` is a hexagonal number or not |
459
|
|
|
:raises AssertionError: if :class:`lib.sequence.Hexagonals` produces the wrong type |
460
|
|
|
:raises AssertionError: if :class:`lib.sequence.Hexagonals` produces the wrong value |
461
|
|
|
""" |
462
|
|
|
|
463
|
|
|
seq = Hexagonals() |
464
|
|
|
computed_answer = x in seq |
465
|
|
|
assert isinstance(computed_answer, Hexagonals.__contains__.__annotations__["return"]), "wrong type" |
|
|
|
|
466
|
|
|
assert computed_answer == expected_answer, "wrong value" |
467
|
|
|
|
468
|
|
|
|
469
|
|
|
def test_hexagonals_in_bad_item_type_str(): |
|
|
|
|
470
|
|
|
""" Test that membership tests for :class:`lib.sequence.Hexagonals` instances raise a ``TypeError`` for a |
|
|
|
|
471
|
|
|
non-``int`` value (``str``) |
472
|
|
|
""" |
473
|
|
|
with pytest.raises(TypeError): |
474
|
|
|
seq = Hexagonals() |
475
|
|
|
_ = "abc" in seq |
476
|
|
|
|
477
|
|
|
|
478
|
|
|
def test_hexagonals_in_bad_item_type_float(): |
|
|
|
|
479
|
|
|
""" Test that membership tests for :class:`lib.sequence.Hexagonals` instances raise a ``TypeError`` for a |
|
|
|
|
480
|
|
|
non-``int`` value (``float``) |
481
|
|
|
""" |
482
|
|
|
with pytest.raises(TypeError): |
483
|
|
|
seq = Hexagonals() |
484
|
|
|
_ = 1.23 in seq |
485
|
|
|
|
486
|
|
|
|
487
|
|
|
@pytest.mark.parametrize("n,n_pandigitals", [(1, {1}), (2, {12, 21}), (3, {123, 132, 213, 231, 312, 321})]) |
|
|
|
|
488
|
|
|
def test_pandigitals_correctness(n: int, n_pandigitals: Set[int]): |
|
|
|
|
489
|
|
|
""" Test the correctness of :class:`lib.sequence.Pandigitals` using known answer tests |
490
|
|
|
|
491
|
|
|
:param n: the input value |
492
|
|
|
:param n_pandigitals: the expected answer |
493
|
|
|
:raises AssertionError: if :class:`lib.sequence.Pandigitals` produces the wrong type |
494
|
|
|
:raises AssertionError: if :class:`lib.sequence.Pandigitals` produces the wrong value |
495
|
|
|
""" |
496
|
|
|
|
497
|
|
|
seq = set(Pandigitals(n)) |
498
|
|
|
for elt in seq: |
499
|
|
|
assert isinstance(elt, Pandigitals.__next__.__annotations__["return"]), "wrong type" |
500
|
|
|
assert set(seq) == n_pandigitals, "wrong value" |
501
|
|
|
|
502
|
|
|
|
503
|
|
|
@pytest.mark.parametrize("n,n_pandigitals", [(1, {1}), (2, {12, 21}), (3, {123, 132, 213, 231, 312, 321})]) |
|
|
|
|
504
|
|
|
def test_pandigitals_correctness_len(n: int, n_pandigitals: Set[int]): |
|
|
|
|
505
|
|
|
""" Test the correctness of length calculations of :class:`lib.sequence.Pandigitals` instances using known answer |
|
|
|
|
506
|
|
|
tests |
507
|
|
|
|
508
|
|
|
:param n: the input value |
509
|
|
|
:param n_pandigitals: the expected number of :math:`n`-pandigitals |
510
|
|
|
:raises AssertionError: if :class:`lib.sequence.Pandigitals` produces the wrong value |
511
|
|
|
""" |
512
|
|
|
|
513
|
|
|
seq = Pandigitals(n) |
514
|
|
|
assert len(seq) == len(n_pandigitals), "wrong value" |
515
|
|
|
|
516
|
|
|
|
517
|
|
|
def test_pandigitals_bad_n_type_str(): |
518
|
|
|
""" Test that :class:`lib.sequence.Pandigitals` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) """ |
|
|
|
|
519
|
|
|
with pytest.raises(TypeError): |
520
|
|
|
Pandigitals("abc") |
521
|
|
|
|
522
|
|
|
|
523
|
|
|
def test_pandigitals_bad_n_type_float(): |
|
|
|
|
524
|
|
|
""" Test that :class:`lib.sequence.Pandigitals` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) |
|
|
|
|
525
|
|
|
""" |
526
|
|
|
with pytest.raises(TypeError): |
527
|
|
|
Pandigitals(1.23) |
528
|
|
|
|
529
|
|
|
|
530
|
|
|
def test_pandigitals_bad_n_negative(): |
531
|
|
|
""" Test that :class:`lib.sequence.Pandigitals` raises a ``ValueError`` for :math:`\\mbox{n} \\lt 0` """ |
|
|
|
|
532
|
|
|
with pytest.raises(ValueError): |
533
|
|
|
Pandigitals(-14) |
534
|
|
|
|
535
|
|
|
|
536
|
|
|
@pytest.mark.parametrize("n,proper,expected_answer", [(10, True, {1, 2, 5}), (10, False, {1, 2, 5, 10}), (5, True, {1}), |
|
|
|
|
537
|
|
|
(12, False, {1, 2, 3, 4, 6, 12}), (7, False, {1, 7}), |
|
|
|
|
538
|
|
|
(1, False, {1}), (1, True, set())]) |
539
|
|
|
def test_divisors_correctness(n: int, proper: bool, expected_answer: Set[int]): |
|
|
|
|
540
|
|
|
""" Test the correctness of :class:`lib.sequence.Divisors` using known answer tests |
541
|
|
|
|
542
|
|
|
:param n: the input value |
543
|
|
|
:param proper: whether to only consider proper divisors or not |
544
|
|
|
:param expected_answer: the expected answer |
545
|
|
|
:raises AssertionError: if :class:`lib.sequence.Divisors` produces the wrong type |
546
|
|
|
:raises AssertionError: if :class:`lib.sequence.Divisors` produces the wrong value |
547
|
|
|
""" |
548
|
|
|
|
549
|
|
|
seq = set(Divisors(n, proper)) |
550
|
|
|
for elt in seq: |
551
|
|
|
assert isinstance(elt, Divisors.__next__.__annotations__["return"]), "wrong type" |
552
|
|
|
assert len(seq) == len(expected_answer), "wrong number of divisors" |
553
|
|
|
assert set(seq) == expected_answer, "wrong divisors" |
554
|
|
|
|
555
|
|
|
|
556
|
|
|
def test_divisors_bad_n_type_str(): |
557
|
|
|
""" Test that :class:`lib.sequence.Divisors` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) """ |
|
|
|
|
558
|
|
|
with pytest.raises(TypeError): |
559
|
|
|
Divisors("abc", proper=False) |
560
|
|
|
|
561
|
|
|
|
562
|
|
|
def test_divisors_bad_n_type_float(): |
563
|
|
|
""" Test that :class:`lib.sequence.Divisors` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) """ |
|
|
|
|
564
|
|
|
with pytest.raises(TypeError): |
565
|
|
|
Divisors(1.23, proper=False) |
566
|
|
|
|
567
|
|
|
|
568
|
|
|
def test_divisors_bad_n_zero(): |
569
|
|
|
""" Test that :class:`lib.sequence.Divisors` raises a ``ValueError`` for :math:`\\mbox{n} = 0` """ |
|
|
|
|
570
|
|
|
with pytest.raises(ValueError): |
571
|
|
|
Divisors(0, proper=False) |
572
|
|
|
|
573
|
|
|
|
574
|
|
|
def test_divisors_bad_proper_type_str(): |
|
|
|
|
575
|
|
|
""" Test that :class:`lib.sequence.Divisors` raises a ``TypeError`` for a non-``bool`` value for `proper` (``str``) |
|
|
|
|
576
|
|
|
""" |
577
|
|
|
with pytest.raises(TypeError): |
578
|
|
|
Divisors(10, proper="abc") |
579
|
|
|
|
580
|
|
|
|
581
|
|
|
def test_divisors_bad_proper_type_float(): |
|
|
|
|
582
|
|
|
""" Test that :class:`lib.sequence.Divisors` raises a ``TypeError`` for a non-``bool`` value for `proper` |
|
|
|
|
583
|
|
|
(``float``) |
584
|
|
|
""" |
585
|
|
|
with pytest.raises(TypeError): |
586
|
|
|
Divisors(10, proper=12.3) |
587
|
|
|
|
588
|
|
|
|
589
|
|
|
@pytest.mark.parametrize("n,n_divisors", [(10, 4), (5, 2), (12, 6), (7, 2), (1, 1), (22, 4)]) |
590
|
|
|
def test_divisors_correctness_len(n: int, n_divisors: int): |
|
|
|
|
591
|
|
|
""" Test the correctness of length calculations of :class:`lib.sequence.Divisors` instances using known answer tests |
|
|
|
|
592
|
|
|
|
593
|
|
|
:param n: the input value |
594
|
|
|
:param n_divisors: the expected number of divisors (all, not proper) |
595
|
|
|
:raises AssertionError: if :class:`lib.sequence.Divisors` produces the wrong value for `proper` = ``False`` |
|
|
|
|
596
|
|
|
:raises AssertionError: if :class:`lib.sequence.Divisors` produces the wrong value for `proper` = ``True`` |
|
|
|
|
597
|
|
|
""" |
598
|
|
|
|
599
|
|
|
assert len(Divisors(n, proper=False)) == n_divisors, "wrong number of divisors" |
600
|
|
|
assert len(Divisors(n, proper=True)) == (n_divisors - 1), "wrong number of proper divisors" |
601
|
|
|
|
602
|
|
|
|
603
|
|
|
@pytest.mark.parametrize("n,proper,d,expected_answer", [(10, True, 5, True), (10, True, 10, False), |
604
|
|
|
(4, True, 5, False), (50, False, 10, True)]) |
605
|
|
|
def test_divisors_correctness_in(n: int, proper: bool, d: int, expected_answer: bool): |
|
|
|
|
606
|
|
|
""" Test the correctness of membership tests for :class:`lib.sequence.Divisors` instances using known answer tests |
|
|
|
|
607
|
|
|
|
608
|
|
|
:param n: the input value |
609
|
|
|
:param proper: whether to only consider proper divisors or not |
610
|
|
|
:param d: the input divisor |
611
|
|
|
:param expected_answer: whether :math:`d` is a [proper] divisor of :math:`n` |
612
|
|
|
:raises AssertionError: if :class:`lib.sequence.Divisors` produces the wrong value for `proper` = ``False`` |
|
|
|
|
613
|
|
|
:raises AssertionError: if :class:`lib.sequence.Divisors` produces the wrong value for `proper` = ``True`` |
|
|
|
|
614
|
|
|
""" |
615
|
|
|
|
616
|
|
|
seq = Divisors(n, proper) |
617
|
|
|
computed_answer = d in seq |
618
|
|
|
assert isinstance(computed_answer, Divisors.__contains__.__annotations__["return"]), "wrong type" |
|
|
|
|
619
|
|
|
assert computed_answer == expected_answer, "wrong value" |
620
|
|
|
|
621
|
|
|
|
622
|
|
|
def test_divisors_in_bad_item_type_str(): |
|
|
|
|
623
|
|
|
""" Test that :class:`lib.sequence.Divisors` raises a ``TypeError`` for a non-``int`` value for `item` (``str``) """ |
|
|
|
|
624
|
|
|
with pytest.raises(TypeError): |
625
|
|
|
_ = "abc" in Divisors(10, proper=False) |
626
|
|
|
|
627
|
|
|
|
628
|
|
|
@pytest.mark.parametrize("proper", [True, False]) |
629
|
|
|
def test_divisorsrange_correctness(proper: bool): |
630
|
|
|
""" Test the correctness of :class:`lib.sequence.DivisorsRange` using known answer tests |
631
|
|
|
|
632
|
|
|
The divisors yielded by this sieve are tested to ensure that they do divide the given integer :math:`n`, up to some |
|
|
|
|
633
|
|
|
moderate bound on :math:`n`. Additionally, it is checked that :math:`1` is included as a proper divisor of |
|
|
|
|
634
|
|
|
:math:`n` and that :math:`n` is **not**. |
635
|
|
|
|
636
|
|
|
:param proper: whether to only consider proper divisors or not |
637
|
|
|
:raises AssertionError: if :class:`lib.sequence.DivisorsRange` produces the wrong type |
638
|
|
|
:raises AssertionError: if :class:`lib.sequence.DivisorsRange` produces the wrong value |
639
|
|
|
""" |
640
|
|
|
|
641
|
|
|
for n, divisors in enumerate(DivisorsRange(100, proper)): |
|
|
|
|
642
|
|
|
for divisor in divisors: |
643
|
|
|
# assert isinstance(divisor, DivisorsRange.__next__.__annotations__["return"]), "wrong type" |
|
|
|
|
644
|
|
|
assert (n + 1) % divisor == 0, "non-divisor included by DivisorsRange" |
645
|
|
|
assert 1 in divisors, "wrong answer: 1 divides everything" |
646
|
|
|
if proper: |
647
|
|
|
assert (n + 1) not in divisors or (n + 1) <= 1, "wrong answer: n is not a proper divisor of itself" |
|
|
|
|
648
|
|
|
else: |
649
|
|
|
assert (n + 1) in divisors, "wrong answer: n is a divisor of itself" |
|
|
|
|
650
|
|
|
|
651
|
|
|
|
652
|
|
|
def test_divisorsrange_bad_upper_bound_type_str(): |
|
|
|
|
653
|
|
|
""" Test that :class:`lib.sequence.DivisorsRange` raises a ``TypeError`` for a non-``int`` value for `upper_bound` |
|
|
|
|
654
|
|
|
(``str``) |
655
|
|
|
""" |
656
|
|
|
with pytest.raises(TypeError): |
657
|
|
|
DivisorsRange("abc", proper=False) |
658
|
|
|
|
659
|
|
|
|
660
|
|
|
def test_divisorsrange_bad_upper_bound_type_float(): |
|
|
|
|
661
|
|
|
""" Test that :class:`lib.sequence.DivisorsRange` raises a ``TypeError`` for a non-``int`` value for `upper_bound` |
|
|
|
|
662
|
|
|
(``float``) |
663
|
|
|
""" |
664
|
|
|
with pytest.raises(TypeError): |
665
|
|
|
DivisorsRange(1.23, proper=False) |
666
|
|
|
|
667
|
|
|
|
668
|
|
|
def test_divisorsrange_bad_upper_bound_zero(): |
|
|
|
|
669
|
|
|
""" Test that :class:`lib.sequence.DivisorsRange` raises a ``ValueError`` for :math:`upper\\_bound = 0` """ |
|
|
|
|
670
|
|
|
with pytest.raises(ValueError): |
671
|
|
|
DivisorsRange(0, proper=False) |
672
|
|
|
|
673
|
|
|
|
674
|
|
|
def test_divisorsrange_bad_proper_type_str(): |
|
|
|
|
675
|
|
|
""" Test that :class:`lib.sequence.DivisorsRange` raises a ``TypeError`` for a non-``bool`` value for `proper` |
|
|
|
|
676
|
|
|
(``str``) |
677
|
|
|
""" |
678
|
|
|
with pytest.raises(TypeError): |
679
|
|
|
DivisorsRange(10, proper="abc") |
680
|
|
|
|
681
|
|
|
|
682
|
|
|
def test_divisorsrange_bad_proper_type_float(): |
|
|
|
|
683
|
|
|
""" Test that :class:`lib.sequence.DivisorsRange` raises a ``TypeError`` for a non-``bool`` value for `proper` |
|
|
|
|
684
|
|
|
(``float``) |
685
|
|
|
""" |
686
|
|
|
with pytest.raises(TypeError): |
687
|
|
|
DivisorsRange(10, proper=12.3) |
688
|
|
|
|
689
|
|
|
|
690
|
|
|
def test_primes_correctness_upper_bound(): |
|
|
|
|
691
|
|
|
""" Test the correctness of :class:`lib.sequence.Primes` (with `upper_bound`) using known answer tests """ |
|
|
|
|
692
|
|
|
seq = Primes(upper_bound=10) |
693
|
|
|
assert list(seq) == PRIMES_UP_TO_SMALL_LIMIT, "wrong value" |
694
|
|
|
|
695
|
|
|
|
696
|
|
|
def test_primes_correctness_upper_bound_iter(): |
|
|
|
|
697
|
|
|
""" Test the correctness of iteration over :class:`lib.sequence.Primes` (with `upper_bound`) using known answer |
|
|
|
|
698
|
|
|
tests |
699
|
|
|
|
700
|
|
|
:raises AssertionError: if :class:`lib.sequence.Primes` produces the wrong type |
701
|
|
|
:raises AssertionError: if :class:`lib.sequence.Primes` produces the wrong value |
702
|
|
|
""" |
703
|
|
|
|
704
|
|
|
seq = Primes(upper_bound=SMALL_LIMIT) |
705
|
|
|
for prime in seq: |
706
|
|
|
assert isinstance(prime, Primes.__next__.__annotations__["return"]), "wrong type" |
707
|
|
|
assert prime in PRIMES_UP_TO_SMALL_LIMIT, "wrong value" |
708
|
|
|
|
709
|
|
|
|
710
|
|
|
def test_primes_bad_upper_bound_str(): |
711
|
|
|
""" Test that :func:`lib.sequence.Primes` raises a ``TypeError`` for a non-``int`` value for `upper_bound` (``str``) |
|
|
|
|
712
|
|
|
""" |
713
|
|
|
with pytest.raises(TypeError): |
714
|
|
|
Primes(upper_bound="100") |
715
|
|
|
|
716
|
|
|
|
717
|
|
|
def test_primes_bad_upper_bound_float(): |
|
|
|
|
718
|
|
|
""" Test that :func:`lib.sequence.Primes` raises a ``TypeError`` for a non-``int`` value for `upper_bound` |
|
|
|
|
719
|
|
|
(``float``) |
720
|
|
|
""" |
721
|
|
|
with pytest.raises(TypeError): |
722
|
|
|
Primes(upper_bound=1.23) |
723
|
|
|
|
724
|
|
|
|
725
|
|
|
def test_primes_bad_upper_bound_zero(): |
|
|
|
|
726
|
|
|
""" Test that :func:`lib.sequence.Primes` raises a ``ValueError`` for :math:`\\mbox{upper_bound} = 0` """ |
|
|
|
|
727
|
|
|
with pytest.raises(ValueError): |
728
|
|
|
Primes(upper_bound=0) |
729
|
|
|
|
730
|
|
|
|
731
|
|
|
def test_primes_correctness_n_primes(): |
|
|
|
|
732
|
|
|
""" Test the correctness of :class:`lib.sequence.Primes` (with `n_primes`) using known answer tests """ |
|
|
|
|
733
|
|
|
seq = list(Primes(n_primes=SMALL_LIMIT)) |
734
|
|
|
assert seq == FIRST_SMALL_LIMIT_PRIMES, "wrong value" |
735
|
|
|
assert len(seq) == SMALL_LIMIT, "wrong length" |
736
|
|
|
|
737
|
|
|
|
738
|
|
|
def test_primes_correctness_n_primes_edge_case(): |
|
|
|
|
739
|
|
|
""" Test the edge case where the estimate for :math:`p_n` based on `n_primes` doesn't hold (:math:`n \\le 3`) """ |
|
|
|
|
740
|
|
|
seq = list(Primes(n_primes=3)) |
741
|
|
|
assert seq == [2, 3, 5], "wrong value" |
742
|
|
|
assert len(seq) == 3, "wrong length" |
743
|
|
|
|
744
|
|
|
|
745
|
|
|
def test_primes_correctness_n_primes_iter(): |
|
|
|
|
746
|
|
|
""" Test the correctness of iteration over :class:`lib.sequence.Primes` (with `n_primes`) using known answer tests |
|
|
|
|
747
|
|
|
|
748
|
|
|
:raises AssertionError: if :class:`lib.sequence.Primes` produces the wrong type |
749
|
|
|
:raises AssertionError: if :class:`lib.sequence.Primes` produces the wrong value |
750
|
|
|
""" |
751
|
|
|
|
752
|
|
|
seq = list(Primes(n_primes=SMALL_LIMIT)) |
753
|
|
|
for prime in seq: |
754
|
|
|
assert isinstance(prime, Primes.__next__.__annotations__["return"]), "wrong type" |
755
|
|
|
assert prime in FIRST_SMALL_LIMIT_PRIMES, "wrong value" |
756
|
|
|
assert len(seq) == SMALL_LIMIT, "wrong length" |
757
|
|
|
|
758
|
|
|
|
759
|
|
|
def test_primes_bad_n_primes_str(): |
760
|
|
|
""" Test that :func:`lib.sequence.Primes` raises a ``TypeError`` for a non-``int`` value for `n_primes` (``str``) |
|
|
|
|
761
|
|
|
""" |
762
|
|
|
with pytest.raises(TypeError): |
763
|
|
|
Primes(n_primes="100") |
764
|
|
|
|
765
|
|
|
|
766
|
|
|
def test_primes_bad_n_primes_float(): |
767
|
|
|
""" Test that :func:`lib.sequence.Primes` raises a ``TypeError`` for a non-``int`` value for `n_primes` (``float``) |
|
|
|
|
768
|
|
|
""" |
769
|
|
|
with pytest.raises(TypeError): |
770
|
|
|
Primes(n_primes=1.23) |
771
|
|
|
|
772
|
|
|
|
773
|
|
|
def test_primes_bad_n_primes_negative(): |
|
|
|
|
774
|
|
|
""" Test that :func:`lib.sequence.Primes` raises a ``ValueError`` for :math:`\\mbox{n_primes} \\lt 0` """ |
|
|
|
|
775
|
|
|
with pytest.raises(ValueError): |
776
|
|
|
Primes(n_primes=-1) |
777
|
|
|
|
778
|
|
|
|
779
|
|
|
def test_primes_correctness_batch(): |
780
|
|
|
""" Test the correctness of :class:`lib.sequence.Primes` using known data """ |
781
|
|
|
primes = load_dataset("general", "primes", data_type=int) |
782
|
|
|
largest_prime = max(primes) |
783
|
|
|
seq = Primes(upper_bound=largest_prime) |
784
|
|
|
for n, prime_n in enumerate(seq): |
|
|
|
|
785
|
|
|
assert prime_n == primes[n] |
786
|
|
|
|
787
|
|
|
|
788
|
|
|
def test_primes_len(): |
789
|
|
|
""" Test that ``len`` of :class:`lib.sequence.Primes` instances raises a ``TypeError`` """ |
790
|
|
|
with pytest.raises(TypeError): |
791
|
|
|
seq = Primes(upper_bound=SMALL_LIMIT) |
792
|
|
|
len(seq) |
793
|
|
|
|
794
|
|
|
|
795
|
|
|
def test_continued_fraction(): |
796
|
|
|
""" FIXME: unit tests still to be defined """ |
797
|
|
|
pass |
798
|
|
|
|
799
|
|
|
|
800
|
|
|
def test_sqrt_expansion(): |
801
|
|
|
""" FIXME: unit tests still to be defined """ |
802
|
|
|
pass |
803
|
|
|
|