-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_math.py
146 lines (120 loc) · 5.07 KB
/
test_math.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
import pytest
import types
import utools.math as umath
# --- utools.math.is_prime ---
@pytest.mark.parametrize("n, expected_result", [
(-1, False), (0, False), (1, False), (2, True), (3, True),
(4, False), (5, True), (17, True), (102, False),
(2**19 - 1, True), (2**127 - 1, True), (2**128 - 1, False),
(179425063, True), (3215031751, False), (32416189919, True)
])
def test_is_prime(n, expected_result):
assert umath.is_prime(n) is expected_result
@pytest.mark.parametrize("n, expected_exception", [
(1.0, TypeError), (None, TypeError), ([1], TypeError), ("1", TypeError)
])
def test_is_prime_with_bad_parameters(n, expected_exception):
with pytest.raises(expected_exception):
umath.is_prime(n)
# --- utools.math.binomial_coefficient ---
@pytest.mark.parametrize("n, k, expected_result", [
(0, 0, 1), (1, 0, 1), (1, 1, 1), (2, 1, 2), (3, 2, 3),
(536, 12, 1036996651419873917836700)
])
def test_binomial_coefficient(n, k, expected_result):
assert umath.binomial_coefficient(n, k) == expected_result
@pytest.mark.parametrize("n, k, expected_exception", [
(1, -1, ValueError), (-1, 1, ValueError), (-1, -1, ValueError),
(1, 3, ValueError), (1, "1", TypeError), (1.0, 1, TypeError),
(1, [1], TypeError), ("1", None, TypeError)
])
def test_binomial_coefficient_with_bad_parameters(n, k, expected_exception):
with pytest.raises(expected_exception):
umath.binomial_coefficient(n, k)
# --- utools.math.find_divisors ---
@pytest.mark.parametrize("n, expected_result", [
(1, {1}), (2, {1, 2}), (4, {1, 2, 4}), (12, {1, 2, 3, 4, 6, 12}),
(1442, {1, 2, 7, 14, 103, 206, 721, 1442}), (2**19 - 1, {1, 2**19 - 1})
])
def test_find_divisors(n, expected_result):
generator = umath.find_divisors(n)
assert isinstance(generator, types.GeneratorType) # the method returns a generator
enumerated_result = [d for d in generator]
assert len(enumerated_result) == len(set(enumerated_result)) # no duplicates
assert set(enumerated_result) == expected_result
@pytest.mark.parametrize("n, expected_exception", [
(-1, ValueError), (0, ValueError), (None, TypeError),
("1", TypeError), (1.0, TypeError), ([1], TypeError)
])
def test_find_divisors_with_bad_parameters(n, expected_exception):
generator = umath.find_divisors(n)
with pytest.raises(expected_exception):
next(generator)
# --- utools.math.sieve_of_eratosthenes ---
@pytest.mark.parametrize("p_min, p_max, expected_result", [
(1, 12, [2, 3, 5, 7, 11]),
(2, 2, [2]),
(1, 1, []),
(3, 2, []),
(2, 211, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,
167, 173, 179, 181, 191, 193, 197, 199, 211])
])
def test_sieve_of_eratosthenes(p_min, p_max, expected_result):
last_prime = 0
primes = []
for prime in umath.sieve_of_eratosthenes(p_min, p_max):
assert prime > last_prime
assert p_min <= prime <= p_max
assert umath.is_prime(prime)
primes.append(prime)
last_prime = prime
assert primes == expected_result
@pytest.mark.parametrize("p_min, p_max, expected_exception", [
(None, 5, TypeError), ("1", 5, TypeError), (1, 5.0, TypeError)
])
def test_sieve_of_eratosthenes_with_bad_parameters(p_min, p_max, expected_exception):
generator = umath.sieve_of_eratosthenes(p_min, p_max)
with pytest.raises(expected_exception):
next(generator)
# --- utools.math.prime_generator ---
@pytest.mark.parametrize("p_min, p_max, expected_result", [
(1, 12, [2, 3, 5, 7, 11]),
(2, 2, [2]),
(1, 1, []),
(3, 2, []),
(2, 211, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,
167, 173, 179, 181, 191, 193, 197, 199, 211])
])
def test_prime_generator(p_min, p_max, expected_result):
last_prime = 0
primes = []
for prime in umath.prime_generator(p_min, p_max):
assert prime > last_prime
assert p_min <= prime <= p_max
assert umath.is_prime(prime)
primes.append(prime)
last_prime = prime
assert primes == expected_result
@pytest.mark.parametrize("p_min, p_max, expected_exception", [
(None, 5, TypeError), ("1", 5, TypeError), (1, 5.0, TypeError)
])
def test_prime_generator_with_bad_parameters(p_min, p_max, expected_exception):
generator = umath.prime_generator(p_min, p_max)
with pytest.raises(expected_exception):
next(generator)
# we want to test that the two prime generators functions return the same primes
@pytest.mark.parametrize("p_min, p_max,", [
(2, 4), (4, 2), (3, None), (2, None), (-1, 21), (20, 101)
])
def test_prime_generator_and_sieve_of_eratosthenes(p_min, p_max):
count_max = 1000
count = 0
for p1, p2 in zip(umath.prime_generator(p_min, p_max),
umath.sieve_of_eratosthenes(p_min, p_max)):
assert p1 == p2
count += 1
if count == count_max:
break