1
|
|
|
""" |
2
|
|
|
:mod:`tests.unit.test_numbertheory` -- Unit Tests |
3
|
|
|
================================================= |
4
|
|
|
|
5
|
|
|
.. module:: tests.unit.test_numbertheory |
6
|
|
|
:synopsis: Unit tests for the lib.numbertheory package. |
7
|
|
|
|
8
|
|
|
.. moduleauthor:: Bill Maroney <[email protected]> |
9
|
|
|
""" |
10
|
|
|
|
11
|
|
|
from collections import Counter |
12
|
|
|
from itertools import takewhile |
13
|
|
|
import pytest |
14
|
|
|
from random import choice, randint |
|
|
|
|
15
|
|
|
|
16
|
|
|
from lib.util import load_dataset |
17
|
|
|
from lib.numbertheory import is_even, is_odd, is_square |
18
|
|
|
from lib.numbertheory import divisor_count, divisor_sum, divisor_sum_aliquot |
19
|
|
|
from lib.numbertheory import is_probably_prime, sieve |
20
|
|
|
from lib.numbertheory.primality import miller_rabin, eratosthenes, segmented_eratosthenes |
21
|
|
|
from lib.numbertheory import factor |
22
|
|
|
from lib.numbertheory.factorisation import perfect_power_exponent |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
@pytest.mark.parametrize("value,expected_answer", [(0, True), (1, False), (200, True), (-123, False), (-24, True)]) |
|
|
|
|
26
|
|
|
def test_is_even_correctness(value: int, expected_answer: bool): |
27
|
|
|
""" Test the correctness of :func:`lib.numbertheory.is_even` using known answer tests |
28
|
|
|
|
29
|
|
|
:param value: the input value |
30
|
|
|
:param expected_answer: the expected answer |
31
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.is_even` returns the wrong type |
32
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.is_even` returns the wrong value |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
computed_answer = is_even(value) |
36
|
|
|
assert isinstance(computed_answer, is_even.__annotations__["return"]), "wrong type" |
37
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def test_is_even_bad_n_type_str(): |
41
|
|
|
""" Test that :func:`lib.numbertheory.is_even` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) """ |
|
|
|
|
42
|
|
|
with pytest.raises(TypeError): |
43
|
|
|
is_even("123") |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def test_is_even_bad_n_type_float(): |
47
|
|
|
""" Test that :func:`lib.numbertheory.is_even` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) |
|
|
|
|
48
|
|
|
""" |
49
|
|
|
with pytest.raises(TypeError): |
50
|
|
|
is_even(123.0) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
@pytest.mark.parametrize("value,expected_answer", [(0, False), (1, True), (200, False), (-123, True), (-24, False)]) |
|
|
|
|
54
|
|
|
def test_is_odd_correctness(value: int, expected_answer: bool): |
55
|
|
|
""" Test the correctness of :func:`lib.numbertheory.is_odd` using known answer tests |
56
|
|
|
|
57
|
|
|
:param value: the input value |
58
|
|
|
:param expected_answer: the expected answer |
59
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.is_odd` returns the wrong type |
60
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.is_odd` returns the wrong value |
61
|
|
|
""" |
62
|
|
|
|
63
|
|
|
computed_answer = is_odd(value) |
64
|
|
|
assert isinstance(computed_answer, is_odd.__annotations__["return"]), "wrong type" |
65
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def test_is_odd_bad_n_type_str(): |
69
|
|
|
""" Test that :func:`lib.numbertheory.is_odd` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) """ |
|
|
|
|
70
|
|
|
with pytest.raises(TypeError): |
71
|
|
|
is_odd("123") |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def test_is_odd_bad_n_type_float(): |
75
|
|
|
""" Test that :func:`lib.numbertheory.is_odd` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) """ |
|
|
|
|
76
|
|
|
with pytest.raises(TypeError): |
77
|
|
|
is_odd(123.0) |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
@pytest.mark.parametrize("value,expected_answer", [(0, True), (1, True), (122, False), (16, True), (10000, True)]) |
|
|
|
|
81
|
|
|
def test_is_square_correctness(value: int, expected_answer: bool): |
82
|
|
|
""" Test the correctness of :func:`lib.numbertheory.is_square` using known answer tests |
83
|
|
|
|
84
|
|
|
:param value: the input value |
85
|
|
|
:param expected_answer: the expected answer |
86
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.is_square` returns the wrong type |
87
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.is_square` returns the wrong value |
88
|
|
|
""" |
89
|
|
|
|
90
|
|
|
computed_answer = is_square(value) |
91
|
|
|
assert isinstance(computed_answer, is_square.__annotations__["return"]), "wrong type" |
92
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def test_is_square_bad_n_type_str(): |
96
|
|
|
""" Test that :func:`lib.numbertheory.is_square` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) |
|
|
|
|
97
|
|
|
""" |
98
|
|
|
with pytest.raises(TypeError): |
99
|
|
|
is_square("123") |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
def test_is_square_bad_n_type_float(): |
103
|
|
|
""" Test that :func:`lib.numbertheory.is_square` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) |
|
|
|
|
104
|
|
|
""" |
105
|
|
|
with pytest.raises(TypeError): |
106
|
|
|
is_square(123.0) |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
@pytest.mark.parametrize("value,expected_answer", [(1, 1), (17, 2), (10, 4), (30, 8), (275, 6), (10000, 25)]) |
|
|
|
|
110
|
|
|
def test_divisor_count_correctness(value: int, expected_answer: bool): |
111
|
|
|
""" Test the correctness of :func:`lib.numbertheory.divisor_count` using known answer tests |
112
|
|
|
|
113
|
|
|
:param value: the input value |
114
|
|
|
:param expected_answer: the expected answer |
115
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.divisor_count` returns the wrong type |
116
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.divisor_count` returns the wrong value |
117
|
|
|
""" |
118
|
|
|
|
119
|
|
|
computed_answer = divisor_count(value) |
120
|
|
|
assert isinstance(computed_answer, divisor_count.__annotations__["return"]), "wrong type" |
121
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
def test_divisor_count_bad_n_type_str(): |
|
|
|
|
125
|
|
|
""" Test that :func:`lib.numbertheory.divisor_count` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
126
|
|
|
(``str``) |
127
|
|
|
""" |
128
|
|
|
with pytest.raises(TypeError): |
129
|
|
|
divisor_count("123") |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
def test_divisor_count_bad_n_type_float(): |
|
|
|
|
133
|
|
|
""" Test that :func:`lib.numbertheory.divisor_count` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
134
|
|
|
(``float``) |
135
|
|
|
""" |
136
|
|
|
with pytest.raises(TypeError): |
137
|
|
|
divisor_count(123.0) |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
def test_divisor_count_bad_n_zero(): |
141
|
|
|
""" Test that :func:`lib.numbertheory.divisor_count` raises a ``ValueError`` for a non-positive value for `n` """ |
|
|
|
|
142
|
|
|
with pytest.raises(ValueError): |
143
|
|
|
divisor_count(0) |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
@pytest.mark.parametrize("value,expected_answer", [(1, 1), (17, 18), (10, 18), (30, 72), (275, 372), (10000, 24211)]) |
|
|
|
|
147
|
|
|
def test_divisor_sum_correctness(value: int, expected_answer: bool): |
148
|
|
|
""" Test the correctness of :func:`lib.numbertheory.divisor_sum` using known answer tests |
149
|
|
|
|
150
|
|
|
:param value: the input value |
151
|
|
|
:param expected_answer: the expected answer |
152
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.divisor_sum` returns the wrong type |
153
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.divisor_sum` returns the wrong value |
154
|
|
|
""" |
155
|
|
|
|
156
|
|
|
computed_answer = divisor_sum(value) |
157
|
|
|
assert isinstance(computed_answer, divisor_sum.__annotations__["return"]), "wrong type" |
158
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
def test_divisor_sum_bad_n_type_str(): |
162
|
|
|
""" Test that :func:`lib.numbertheory.divisor_sum` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) |
|
|
|
|
163
|
|
|
""" |
164
|
|
|
with pytest.raises(TypeError): |
165
|
|
|
divisor_sum("123") |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
def test_divisor_sum_bad_n_type_float(): |
|
|
|
|
169
|
|
|
""" Test that :func:`lib.numbertheory.divisor_sum` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
170
|
|
|
(``float``) |
171
|
|
|
""" |
172
|
|
|
with pytest.raises(TypeError): |
173
|
|
|
divisor_sum(123.0) |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
def test_divisor_sum_bad_n_zero(): |
177
|
|
|
""" Test that :func:`lib.numbertheory.divisor_sum` raises a ``ValueError`` for a non-positive value for `n` """ |
|
|
|
|
178
|
|
|
with pytest.raises(ValueError): |
179
|
|
|
divisor_sum(0) |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
@pytest.mark.parametrize("value,expected_answer", [(1, 0), (17, 1), (10, 8), (30, 42), (275, 97), (10000, 14211)]) |
|
|
|
|
183
|
|
|
def test_divisor_sum_aliquot_correctness(value: int, expected_answer: bool): |
|
|
|
|
184
|
|
|
""" Test the correctness of :func:`lib.numbertheory.divisor_sum_aliquot` using known answer tests |
|
|
|
|
185
|
|
|
|
186
|
|
|
:param value: the input value |
187
|
|
|
:param expected_answer: the expected answer |
188
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.divisor_sum_aliquot` returns the wrong type |
189
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.divisor_sum_aliquot` returns the wrong value |
190
|
|
|
""" |
191
|
|
|
|
192
|
|
|
computed_answer = divisor_sum_aliquot(value) |
193
|
|
|
assert isinstance(computed_answer, divisor_sum_aliquot.__annotations__["return"]), "wrong type" |
194
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
def test_divisor_sum_aliquot_bad_n_type_str(): |
|
|
|
|
198
|
|
|
""" Test that :func:`lib.numbertheory.divisor_sum_aliquot` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
199
|
|
|
(``str``) |
200
|
|
|
""" |
201
|
|
|
with pytest.raises(TypeError): |
202
|
|
|
divisor_sum_aliquot("123") |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
def test_divisor_sum_aliquot_bad_n_type_float(): |
|
|
|
|
206
|
|
|
""" Test that :func:`lib.numbertheory.divisor_sum_aliquot` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
207
|
|
|
(``float``) |
208
|
|
|
""" |
209
|
|
|
with pytest.raises(TypeError): |
210
|
|
|
divisor_sum_aliquot(123.0) |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
def test_divisor_sum_aliquot_bad_n_zero(): |
|
|
|
|
214
|
|
|
""" Test that :func:`lib.numbertheory.divisor_sum_aliquot` raises a ``ValueError`` for a non-positive value for `n` |
|
|
|
|
215
|
|
|
""" |
216
|
|
|
with pytest.raises(ValueError): |
217
|
|
|
divisor_sum_aliquot(0) |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
def test_is_probably_prime_correctness(): |
|
|
|
|
221
|
|
|
""" Test the correctness of :func:`lib.numbertheory.is_probably_prime` |
222
|
|
|
|
223
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.is_probably_prime` returns the wrong type |
224
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.is_probably_prime` returns the wrong value |
225
|
|
|
""" |
226
|
|
|
|
227
|
|
|
primes = load_dataset("general", "primes", data_type=int) |
228
|
|
|
primes = set(primes) |
229
|
|
|
upper_limit = 1000 # artificial cap to limit the run-time of this test |
230
|
|
|
max_prime = min(upper_limit, max(primes)) |
231
|
|
|
for n in range(1, max_prime + 1): |
|
|
|
|
232
|
|
|
computed_answer = is_probably_prime(n, k=5) |
233
|
|
|
assert isinstance(computed_answer, is_probably_prime.__annotations__["return"]), "wrong type" |
|
|
|
|
234
|
|
|
assert (n in primes) == computed_answer, "wrong answer" |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
def test_is_probably_prime_bad_n_type_str(): |
|
|
|
|
238
|
|
|
""" Test that :func:`lib.numbertheory.is_probably_prime` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
239
|
|
|
(``str``) |
240
|
|
|
""" |
241
|
|
|
with pytest.raises(TypeError): |
242
|
|
|
is_probably_prime("2") |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
def test_is_probably_prime_bad_n_type_float(): |
|
|
|
|
246
|
|
|
""" Test that :func:`lib.numbertheory.is_probably_prime` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
247
|
|
|
(``float``) |
248
|
|
|
""" |
249
|
|
|
with pytest.raises(TypeError): |
250
|
|
|
is_probably_prime(2.4) |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
def test_miller_rabin_bad_n_type_str(): |
|
|
|
|
254
|
|
|
""" Test that :func:`lib.numbertheory.miller_rabin` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) |
|
|
|
|
255
|
|
|
""" |
256
|
|
|
with pytest.raises(TypeError): |
257
|
|
|
miller_rabin("2") |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
def test_miller_rabin_bad_n_type_float(): |
|
|
|
|
261
|
|
|
""" Test that :func:`lib.numbertheory.miller_rabin` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
262
|
|
|
(``float``) |
263
|
|
|
""" |
264
|
|
|
with pytest.raises(TypeError): |
265
|
|
|
miller_rabin(2.4) |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
def test_miller_rabin_bad_n_too_small(): |
|
|
|
|
269
|
|
|
""" Test that :func:`lib.numbertheory.miller_rabin` raises a ``ValueError`` for too small a value for `n` """ |
|
|
|
|
270
|
|
|
with pytest.raises(ValueError): |
271
|
|
|
miller_rabin(2) |
272
|
|
|
|
273
|
|
|
|
274
|
|
|
def test_is_probably_prime_bad_k_type_str(): |
|
|
|
|
275
|
|
|
""" Test that :func:`lib.numbertheory.is_probably_prime` raises a ``TypeError`` for a non-``int`` value for `k` |
|
|
|
|
276
|
|
|
(``str``) |
277
|
|
|
""" |
278
|
|
|
with pytest.raises(TypeError): |
279
|
|
|
is_probably_prime(2, "14") |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
def test_is_probably_prime_bad_k_type_float(): |
|
|
|
|
283
|
|
|
""" Test that :func:`lib.numbertheory.is_probably_prime` raises a ``TypeError`` for a non-``int`` value for `k` |
|
|
|
|
284
|
|
|
(``float``) |
285
|
|
|
""" |
286
|
|
|
with pytest.raises(TypeError): |
287
|
|
|
is_probably_prime(2, 12.3) |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
def test_miller_rabin_bad_k_type_str(): |
|
|
|
|
291
|
|
|
""" Test that :func:`lib.numbertheory.miller_rabin` raises a ``TypeError`` for a non-``int`` value for `k` (``str``) |
|
|
|
|
292
|
|
|
""" |
293
|
|
|
with pytest.raises(TypeError): |
294
|
|
|
miller_rabin(2, "14") |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
def test_miller_rabin_bad_k_type_float(): |
|
|
|
|
298
|
|
|
""" Test that :func:`lib.numbertheory.miller_rabin` raises a ``TypeError`` for a non-``int`` value for `k` |
|
|
|
|
299
|
|
|
(``float``) |
300
|
|
|
""" |
301
|
|
|
with pytest.raises(TypeError): |
302
|
|
|
miller_rabin(2, 12.3) |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
def test_miller_rabin_bad_k_zero(): |
306
|
|
|
""" Test that :func:`lib.numbertheory.miller_rabin` raises a ``ValueError`` for :math:`k=0` """ |
307
|
|
|
with pytest.raises(ValueError): |
308
|
|
|
miller_rabin(9, 0) |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
@pytest.mark.parametrize("siever", [sieve, eratosthenes, segmented_eratosthenes]) |
312
|
|
|
def test_sieve_correctness(siever): |
313
|
|
|
""" Test the correctness of all sieves in :func:`lib.numbertheory` including: |
314
|
|
|
* :func:`lib.numbertheory.primality.sieve` |
315
|
|
|
* :func:`lib.numbertheory.primality.eratosthenes` |
316
|
|
|
* :func:`lib.numbertheory.primality.segmented_eratosthenes` |
317
|
|
|
|
318
|
|
|
The integers yielded by these sieves are tested for primality, up to some moderate bound. |
319
|
|
|
|
320
|
|
|
:raises AssertionError: if ``siever(upper_bound)`` yields the wrong type |
321
|
|
|
:raises AssertionError: if ``siever(upper_bound)`` yields the wrong value |
322
|
|
|
""" |
323
|
|
|
|
324
|
|
|
for p in siever(upper_bound=100): |
|
|
|
|
325
|
|
|
computed_answer = is_probably_prime(p) |
326
|
|
|
# assert isinstance(computed_answer, sieve.__annotations__["return"]), "wrong type" |
327
|
|
|
assert computed_answer is True, "wrong answer" |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
@pytest.mark.parametrize("upper_bound", [100, 10 ** 5, 10 ** 8, 10 ** 10]) |
331
|
|
|
def test_sieve_correctness_bound(upper_bound): |
332
|
|
|
""" Test the correctness of all sieves in :mod:`lib.numbertheory` including: |
333
|
|
|
* :func:`lib.numbertheory.primality.sieve` |
334
|
|
|
* :func:`lib.numbertheory.primality.eratosthenes` |
335
|
|
|
* :func:`lib.numbertheory.primality.segmented_eratosthenes` |
336
|
|
|
|
337
|
|
|
The integers yielded by these sieves are tested for primality, up to some moderate bound. The parameter |
|
|
|
|
338
|
|
|
`upper_bound` is set to various values to ensure all sieves are utilised. |
339
|
|
|
|
340
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.sieve` yields the wrong type |
341
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.sieve` yields the wrong value |
342
|
|
|
""" |
343
|
|
|
|
344
|
|
|
for p in takewhile(lambda _p: _p <= 100, sieve(upper_bound)): |
|
|
|
|
345
|
|
|
computed_answer = is_probably_prime(p) |
346
|
|
|
# assert isinstance(computed_answer, sieve.__annotations__["return"]), "wrong type" |
347
|
|
|
assert computed_answer is True, "wrong answer" |
348
|
|
|
|
349
|
|
|
|
350
|
|
|
def test_sieve_bad_upper_bound_type_str(): |
|
|
|
|
351
|
|
|
""" Test that :func:`lib.numbertheory.sieve` raises a ``TypeError`` for a non-``int`` value for `upper_bound` |
|
|
|
|
352
|
|
|
(``str``) |
353
|
|
|
""" |
354
|
|
|
with pytest.raises(TypeError): |
355
|
|
|
sieve("123") |
356
|
|
|
|
357
|
|
|
|
358
|
|
|
def test_sieve_bad_upper_bound_type_float(): |
|
|
|
|
359
|
|
|
""" Test that :func:`lib.numbertheory.sieve` raises a ``TypeError`` for a non-``int`` value for `upper_bound` |
|
|
|
|
360
|
|
|
(``float``) |
361
|
|
|
""" |
362
|
|
|
with pytest.raises(TypeError): |
363
|
|
|
sieve(12.3) |
364
|
|
|
|
365
|
|
|
|
366
|
|
|
def test_sieve_bad_upper_bound_negative(): |
|
|
|
|
367
|
|
|
""" Test that :func:`lib.numbertheory.sieve` raises a ``ValueError`` for :math:`\\mbox{upper_bound} \lt 0` """ |
|
|
|
|
368
|
|
|
with pytest.raises(ValueError): |
369
|
|
|
sieve(-14) |
370
|
|
|
|
371
|
|
|
|
372
|
|
|
def test_sieve_bad_upper_bound_too_big(): |
|
|
|
|
373
|
|
|
""" Test that :func:`lib.numbertheory.sieve` raises a ``ValueError`` for :math:`\\mbox{upper_bound} \ge 10^{16}` """ |
|
|
|
|
374
|
|
|
too_big = 10 ** 20 |
375
|
|
|
with pytest.raises(ValueError): |
376
|
|
|
sieve(upper_bound=too_big) |
377
|
|
|
|
378
|
|
|
|
379
|
|
|
@pytest.mark.parametrize("value,expected_answer", [(0, {0: 1}), (1, {1: 1}), (16, {2: 4})]) |
380
|
|
|
def test_factor_correctness(value, expected_answer): |
381
|
|
|
""" Test the correctness of :func:`lib.numbertheory.factor` with known-answer-tests |
382
|
|
|
|
383
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.factor` returns the wrong type |
384
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.factor` returns the wrong value |
385
|
|
|
""" |
386
|
|
|
|
387
|
|
|
computed_value = factor(value) |
388
|
|
|
# assert isinstance(computed_value, factor.__annotations__["return"]), "wrong type" |
389
|
|
|
assert computed_value == expected_answer, "wrong value" |
390
|
|
|
|
391
|
|
|
|
392
|
|
|
@pytest.mark.parametrize("value,expected_answer", [(16, {2: 4}), (35, {5: 1, 7: 1}), (1001, {7: 1, 11: 1, 13: 1})]) |
|
|
|
|
393
|
|
|
def test_factor_correctness_negative(value, expected_answer): |
|
|
|
|
394
|
|
|
""" Test the correctness of :func:`lib.numbertheory.factor` with known-answer-tests |
395
|
|
|
|
396
|
|
|
Each `value` is factored. This factorisation is combined with a factor of :math:`-1` to give the expected |
|
|
|
|
397
|
|
|
factorisation of :math:`-value`. This negative value is then factored, and the two results are checked for equality. |
|
|
|
|
398
|
|
|
|
399
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.factor` returns the wrong type |
400
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.factor` returns the wrong value |
401
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.factor` doesn't include a factor of :math:`-1` |
|
|
|
|
402
|
|
|
""" |
403
|
|
|
|
404
|
|
|
computed_value = factor(value) |
405
|
|
|
# assert isinstance(computed_value, factor.__annotations__["return"]), "wrong type" |
406
|
|
|
assert computed_value == expected_answer, "wrong value" |
407
|
|
|
computed_value_negative = factor(-1 * value) |
408
|
|
|
# assert isinstance(computed_value_negative, factor.__annotations__["return"]), "wrong type" |
409
|
|
|
assert -1 not in computed_value.keys(), "missing factor of -1" |
410
|
|
|
computed_value[-1] = 1 |
411
|
|
|
assert computed_value == computed_value_negative, "wrong value" |
412
|
|
|
|
413
|
|
|
|
414
|
|
|
def test_factor_correctness_random(): |
415
|
|
|
""" Test the correctness of :func:`lib.numbertheory.factor` with known-answer-tests |
416
|
|
|
|
417
|
|
|
A random integer is built as a product of several small primes (selected with replacement); this random integer has |
|
|
|
|
418
|
|
|
a known factorisation. :func:`lib.numbertheory.factor` is used to factor the constructed number and it is compared |
|
|
|
|
419
|
|
|
to the known and expected factorisation. |
420
|
|
|
|
421
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.factor` returns the wrong type |
422
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.factor` returns the wrong value |
423
|
|
|
""" |
424
|
|
|
|
425
|
|
|
# First, build a random integer as a product of several small primes (selected with replacement) |
426
|
|
|
primes = load_dataset("general", "primes", data_type=int) |
427
|
|
|
value, factorisation = 1, Counter() |
428
|
|
|
while value < 10 ** 6: |
429
|
|
|
p = choice(primes) |
|
|
|
|
430
|
|
|
value *= p |
431
|
|
|
factorisation[p] += 1 |
432
|
|
|
factorisation = dict(factorisation) |
433
|
|
|
|
434
|
|
|
# Factor the constructed integer and check that it matches the construction |
435
|
|
|
computed_value = factor(value) |
436
|
|
|
# assert isinstance(computed_value_negative, factor.__annotations__["return"]), "wrong type" |
437
|
|
|
assert computed_value == factorisation, "wrong value" |
438
|
|
|
|
439
|
|
|
|
440
|
|
|
def test_factor_bad_n_type_str(): |
441
|
|
|
""" Test that :func:`lib.numbertheory.factor` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) """ |
|
|
|
|
442
|
|
|
with pytest.raises(TypeError): |
443
|
|
|
factor("123") |
444
|
|
|
|
445
|
|
|
|
446
|
|
|
def test_factor_bad_n_type_float(): |
447
|
|
|
""" Test that :func:`lib.numbertheory.factor` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) """ |
|
|
|
|
448
|
|
|
with pytest.raises(TypeError): |
449
|
|
|
factor(145.678) |
450
|
|
|
|
451
|
|
|
|
452
|
|
|
@pytest.mark.parametrize("square_free", [6, 35, 77]) |
453
|
|
|
def test_perfect_power_exponent_correctness_square_free(square_free): |
|
|
|
|
454
|
|
|
""" Test the correctness of :func:`lib.numbertheory.perfect_power_exponent` for square-free inputs |
|
|
|
|
455
|
|
|
|
456
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.perfect_power_exponent` returns the wrong type |
|
|
|
|
457
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.perfect_power_exponent` returns the wrong value |
|
|
|
|
458
|
|
|
""" |
459
|
|
|
|
460
|
|
|
computed_value = perfect_power_exponent(square_free) |
461
|
|
|
assert isinstance(computed_value, perfect_power_exponent.__annotations__["return"]), "wrong type" |
|
|
|
|
462
|
|
|
assert computed_value == 1, "wrong value" |
463
|
|
|
|
464
|
|
|
|
465
|
|
|
@pytest.mark.parametrize("square_free", [6, 35, 77]) |
466
|
|
|
def test_perfect_power_exponent_correctness_not_square_free(square_free): |
|
|
|
|
467
|
|
|
""" Test the correctness of :func:`lib.numbertheory.perfect_power_exponent` for **non** square-free inputs |
|
|
|
|
468
|
|
|
|
469
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.perfect_power_exponent` returns the wrong type |
|
|
|
|
470
|
|
|
:raises AssertionError: if :func:`lib.numbertheory.perfect_power_exponent` returns the wrong value |
|
|
|
|
471
|
|
|
""" |
472
|
|
|
|
473
|
|
|
exponent = randint(2, 10) # force each square-free number to be a perfect power |
474
|
|
|
computed_value = perfect_power_exponent(square_free ** exponent) |
475
|
|
|
assert isinstance(computed_value, perfect_power_exponent.__annotations__["return"]), "wrong type" |
|
|
|
|
476
|
|
|
assert computed_value == exponent, "wrong value" |
477
|
|
|
|
478
|
|
|
|
479
|
|
|
def test_perfect_power_exponent_bad_n_type_str(): |
|
|
|
|
480
|
|
|
""" Test that :func:`lib.numbertheory.perfect_power_exponent` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
481
|
|
|
(``str``) |
482
|
|
|
""" |
483
|
|
|
with pytest.raises(TypeError): |
484
|
|
|
perfect_power_exponent("123") |
485
|
|
|
|
486
|
|
|
|
487
|
|
|
def test_perfect_power_exponent_bad_n_type_float(): |
|
|
|
|
488
|
|
|
""" Test that :func:`lib.numbertheory.perfect_power_exponent` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
489
|
|
|
(``float``) |
490
|
|
|
""" |
491
|
|
|
with pytest.raises(TypeError): |
492
|
|
|
perfect_power_exponent(145.678) |
493
|
|
|
|
494
|
|
|
|
495
|
|
|
def test_perfect_power_exponent_bad_n_zero(): |
|
|
|
|
496
|
|
|
""" Test that :func:`lib.numbertheory.perfect_power_exponent` raises a ``ValueError`` for :math:`n=0` """ |
|
|
|
|
497
|
|
|
with pytest.raises(ValueError): |
498
|
|
|
perfect_power_exponent(0) |
499
|
|
|
|