|
1
|
|
|
""" |
|
2
|
|
|
:mod:`tests.unit.test_digital` -- Unit Tests |
|
3
|
|
|
============================================ |
|
4
|
|
|
|
|
5
|
|
|
.. module:: tests.unit.test_digital |
|
6
|
|
|
:synopsis: Unit tests for the lib.digital module. |
|
7
|
|
|
|
|
8
|
|
|
.. moduleauthor:: Bill Maroney <[email protected]> |
|
9
|
|
|
""" |
|
10
|
|
|
|
|
11
|
|
|
from typing import List, Union |
|
12
|
|
|
import pytest |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class TestDigitsOf(object): |
|
|
|
|
|
|
16
|
|
|
""" A collection of positive and negative unit tests for the :func:`lib.digital.digits_of` function """ |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
@classmethod |
|
19
|
|
|
def get_import(cls): |
|
20
|
|
|
""" Get and return the :func:`lib.digital.digits_of` function |
|
21
|
|
|
|
|
22
|
|
|
.. note:: this isolates the import so any error/exception is raised by individual unit tests. |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
:return: the :func:`lib.digital.digits_of` function |
|
25
|
|
|
""" |
|
26
|
|
|
from lib.digital import digits_of |
|
27
|
|
|
return digits_of |
|
28
|
|
|
|
|
29
|
|
|
@pytest.mark.parametrize("n,base,expected_answer", [(10, 10, [1, 0]), (1579, 10, [1, 5, 7, 9]), (7, 2, [1, 1, 1]), |
|
|
|
|
|
|
30
|
|
|
(10, 2, [1, 0, 1, 0]), (0, 9, [0])]) |
|
31
|
|
|
def test_digits_of_correctness(self, n: int, base: int, expected_answer: int): |
|
|
|
|
|
|
32
|
|
|
""" Test the correctness of :func:`lib.digital.digits_of` using known answer tests |
|
33
|
|
|
|
|
34
|
|
|
:param n: the input integer |
|
35
|
|
|
:param base: the base to build digits in |
|
36
|
|
|
:param expected_answer: the expected answer |
|
37
|
|
|
:raises AssertionError: if :func:`lib.digital.digits_of` returns the wrong type |
|
38
|
|
|
:raises AssertionError: if :func:`lib.digital.digits_of` returns the wrong value |
|
39
|
|
|
""" |
|
40
|
|
|
|
|
41
|
|
|
digits_of = self.get_import() |
|
42
|
|
|
computed_answer = digits_of(n, base=base) |
|
43
|
|
|
# assert isinstance(computed_answer, digits_of.__annotations__["return"]), "wrong type" |
|
44
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
|
45
|
|
|
|
|
46
|
|
|
def test_digits_of_bad_n_type_str(self): |
|
47
|
|
|
""" Test that :func:`lib.digital.digits_of` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) """ |
|
|
|
|
|
|
48
|
|
|
digits_of = self.get_import() |
|
49
|
|
|
with pytest.raises(TypeError): |
|
50
|
|
|
digits_of("123") |
|
51
|
|
|
|
|
52
|
|
|
def test_digits_of_bad_n_type_float(self): |
|
53
|
|
|
""" Test that :func:`lib.digital.digits_of` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) |
|
|
|
|
|
|
54
|
|
|
""" |
|
55
|
|
|
digits_of = self.get_import() |
|
56
|
|
|
with pytest.raises(TypeError): |
|
57
|
|
|
digits_of(123.0) |
|
58
|
|
|
|
|
59
|
|
|
def test_digits_of_bad_n_negative(self): |
|
60
|
|
|
""" Test that :func:`lib.digital.digits_of` raises a ``ValueError`` for a negative value for `n` """ |
|
|
|
|
|
|
61
|
|
|
digits_of = self.get_import() |
|
62
|
|
|
with pytest.raises(ValueError): |
|
63
|
|
|
digits_of(-123) |
|
64
|
|
|
|
|
65
|
|
|
def test_digits_of_bad_base_type_str(self): |
|
|
|
|
|
|
66
|
|
|
""" Test that :func:`lib.digital.digits_of` raises a ``TypeError`` for a non-``int`` value for `base` (``str``) |
|
|
|
|
|
|
67
|
|
|
""" |
|
68
|
|
|
digits_of = self.get_import() |
|
69
|
|
|
with pytest.raises(TypeError): |
|
70
|
|
|
digits_of(123, base="2") |
|
71
|
|
|
|
|
72
|
|
|
def test_digits_of_bad_base_type_float(self): |
|
|
|
|
|
|
73
|
|
|
""" Test that :func:`lib.digital.digits_of` raises a ``TypeError`` for a non-``int`` value for `base` |
|
|
|
|
|
|
74
|
|
|
(``float``) |
|
75
|
|
|
""" |
|
76
|
|
|
digits_of = self.get_import() |
|
77
|
|
|
with pytest.raises(TypeError): |
|
78
|
|
|
digits_of(123, base=2.0) |
|
79
|
|
|
|
|
80
|
|
|
def test_digits_of_bad_base_negative(self): |
|
|
|
|
|
|
81
|
|
|
""" Test that :func:`lib.digital.digits_of` raises a ``ValueError`` for a negative value for `base` """ |
|
|
|
|
|
|
82
|
|
|
digits_of = self.get_import() |
|
83
|
|
|
with pytest.raises(ValueError): |
|
84
|
|
|
digits_of(123, base=-2) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
class TestDigitsToNum(object): |
|
|
|
|
|
|
88
|
|
|
""" A collection of positive and negative unit tests for the :func:`lib.digital.digits_to_num` function """ |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
@classmethod |
|
91
|
|
|
def get_import(cls): |
|
92
|
|
|
""" Get and return the :func:`lib.digital.digits_to_sum` function |
|
93
|
|
|
|
|
94
|
|
|
.. note:: this isolates the import so any error/exception is raised by individual unit tests. |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
:return: the :func:`lib.digital.digits_to_num` function |
|
97
|
|
|
""" |
|
98
|
|
|
from lib.digital import digits_to_num |
|
99
|
|
|
return digits_to_num |
|
100
|
|
|
|
|
101
|
|
|
@pytest.mark.parametrize("digits,base,expected_answer", [([1, 2, 3], 10, 123), ([3, 2, 1], 10, 321), ([7], 10, 7), |
|
|
|
|
|
|
102
|
|
|
([1, 0, 1, 0], 2, 10), ([1, 1, 1], 2, 7)]) |
|
|
|
|
|
|
103
|
|
|
def test_digits_to_num_correctness(self, digits: List[int], base: int, expected_answer: int): |
|
104
|
|
|
""" Test the correctness of :func:`lib.digital.digits_to_num` using known answer tests |
|
105
|
|
|
|
|
106
|
|
|
:param digits: the input digits |
|
107
|
|
|
:param base: the base to build digits in |
|
108
|
|
|
:param expected_answer: the expected answer |
|
109
|
|
|
:raises AssertionError: if :func:`lib.digital.digits_to_num` returns the wrong type |
|
110
|
|
|
:raises AssertionError: if :func:`lib.digital.digits_to_num` returns the wrong value |
|
111
|
|
|
""" |
|
112
|
|
|
|
|
113
|
|
|
digits_to_num = self.get_import() |
|
114
|
|
|
computed_answer = digits_to_num(digits, base=base) |
|
115
|
|
|
assert isinstance(computed_answer, digits_to_num.__annotations__["return"]), "wrong type" |
|
116
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
|
117
|
|
|
|
|
118
|
|
|
def test_digits_to_num_bad_digits_type_str(self): |
|
|
|
|
|
|
119
|
|
|
""" Test that :func:`lib.digital.digits_to_num` raises a ``TypeError`` for a non-``list`` value for `digits` |
|
|
|
|
|
|
120
|
|
|
(``str``) |
|
121
|
|
|
""" |
|
122
|
|
|
digits_to_num = self.get_import() |
|
123
|
|
|
with pytest.raises(TypeError): |
|
124
|
|
|
digits_to_num("123") |
|
125
|
|
|
|
|
126
|
|
|
def test_digits_to_num_bad_digits_type_float(self): |
|
|
|
|
|
|
127
|
|
|
""" Test that :func:`lib.digital.digits_to_num` raises a ``TypeError`` for a non-``list`` value for `digits` |
|
|
|
|
|
|
128
|
|
|
(``float``) |
|
129
|
|
|
""" |
|
130
|
|
|
digits_to_num = self.get_import() |
|
131
|
|
|
with pytest.raises(TypeError): |
|
132
|
|
|
digits_to_num(123.0) |
|
133
|
|
|
|
|
134
|
|
|
def test_digits_to_num_bad_digits_type_list_of_str(self): |
|
|
|
|
|
|
135
|
|
|
""" Test that :func:`lib.digital.digits_to_num` raises a ``TypeError`` for a list of non-``int`` values for |
|
|
|
|
|
|
136
|
|
|
`digits` (list of ``str``) |
|
137
|
|
|
""" |
|
138
|
|
|
digits_to_num = self.get_import() |
|
139
|
|
|
with pytest.raises(TypeError): |
|
140
|
|
|
digits_to_num(["123"]) |
|
141
|
|
|
|
|
142
|
|
|
def test_digits_to_num_bad_digits_type_list_of_float(self): |
|
|
|
|
|
|
143
|
|
|
""" Test that :func:`lib.digital.digits_to_num` raises a ``TypeError`` for a list of non-``int`` values for |
|
|
|
|
|
|
144
|
|
|
`digits` (list of ``float``) |
|
145
|
|
|
""" |
|
146
|
|
|
digits_to_num = self.get_import() |
|
147
|
|
|
with pytest.raises(TypeError): |
|
148
|
|
|
digits_to_num([123.0]) |
|
149
|
|
|
|
|
150
|
|
|
def test_digits_to_num_bad_digits_negative_element(self): |
|
|
|
|
|
|
151
|
|
|
""" Test that :func:`lib.digital.digits_to_num` raises a ``ValueError`` for a negative value in `digits` |
|
|
|
|
|
|
152
|
|
|
""" |
|
153
|
|
|
digits_to_num = self.get_import() |
|
154
|
|
|
with pytest.raises(ValueError): |
|
155
|
|
|
digits_to_num([-1, 2, 3]) |
|
156
|
|
|
|
|
157
|
|
|
def test_digits_to_num_bad_base_type_str(self): |
|
|
|
|
|
|
158
|
|
|
""" Test that :func:`lib.digital.digits_to_num` raises a ``TypeError`` for a non-``int`` value for `base` |
|
|
|
|
|
|
159
|
|
|
(``str``) |
|
160
|
|
|
""" |
|
161
|
|
|
digits_to_num = self.get_import() |
|
162
|
|
|
with pytest.raises(TypeError): |
|
163
|
|
|
digits_to_num([1, 2, 3], base="2") |
|
164
|
|
|
|
|
165
|
|
|
def test_digits_to_num_bad_base_type_float(self): |
|
|
|
|
|
|
166
|
|
|
""" Test that :func:`lib.digital.digits_to_num` raises a ``TypeError`` for a non-``int`` value for `base` |
|
|
|
|
|
|
167
|
|
|
(``float``) |
|
168
|
|
|
""" |
|
169
|
|
|
digits_to_num = self.get_import() |
|
170
|
|
|
with pytest.raises(TypeError): |
|
171
|
|
|
digits_to_num([1, 2, 3], base=2.0) |
|
172
|
|
|
|
|
173
|
|
|
def test_digits_to_num_bad_base_negative(self): |
|
|
|
|
|
|
174
|
|
|
""" Test that :func:`lib.digital.digits_to_num` raises a ``ValueError`` for a negative value for `base` """ |
|
|
|
|
|
|
175
|
|
|
digits_to_num = self.get_import() |
|
176
|
|
|
with pytest.raises(ValueError): |
|
177
|
|
|
digits_to_num(123, base=-2) |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
class TestDigitSum(object): |
|
|
|
|
|
|
181
|
|
|
""" A collection of positive and negative unit tests for the :func:`lib.digital.digit_sum` function """ |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
@classmethod |
|
184
|
|
|
def get_import(cls): |
|
185
|
|
|
""" Get and return the :func:`lib.digital.digit_sum` function |
|
186
|
|
|
|
|
187
|
|
|
.. note:: this isolates the import so any error/exception is raised by individual unit tests. |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
:return: the :func:`lib.digital.digit_sum` function |
|
190
|
|
|
""" |
|
191
|
|
|
from lib.digital import digit_sum |
|
192
|
|
|
return digit_sum |
|
193
|
|
|
|
|
194
|
|
|
@pytest.mark.parametrize("value,expected_answer", [(0, 0), (9, 9), (11, 2), (123, 6), (654321, 21)]) |
|
|
|
|
|
|
195
|
|
|
def test_digit_sum_correctness(self, value: int, expected_answer: int): |
|
196
|
|
|
""" Test the correctness of :func:`lib.digital.digit_sum` using known answer tests |
|
197
|
|
|
|
|
198
|
|
|
:param value: the input value |
|
199
|
|
|
:param expected_answer: the expected answer |
|
200
|
|
|
:raises AssertionError: if :func:`lib.digital.digit_sum` returns the wrong type |
|
201
|
|
|
:raises AssertionError: if :func:`lib.digital.digit_sum` returns the wrong value |
|
202
|
|
|
""" |
|
203
|
|
|
|
|
204
|
|
|
digit_sum = self.get_import() |
|
205
|
|
|
computed_answer = digit_sum(value) |
|
206
|
|
|
assert isinstance(computed_answer, digit_sum.__annotations__["return"]), "wrong type" |
|
207
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
|
208
|
|
|
|
|
209
|
|
|
def test_digit_sum_bad_n_type_str(self): |
|
210
|
|
|
""" Test that :func:`lib.digital.digit_sum` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) """ |
|
|
|
|
|
|
211
|
|
|
digit_sum = self.get_import() |
|
212
|
|
|
with pytest.raises(TypeError): |
|
213
|
|
|
digit_sum("123") |
|
214
|
|
|
|
|
215
|
|
|
def test_digit_sum_bad_n_type_float(self): |
|
216
|
|
|
""" Test that :func:`lib.digital.digit_sum` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) |
|
|
|
|
|
|
217
|
|
|
""" |
|
218
|
|
|
digit_sum = self.get_import() |
|
219
|
|
|
with pytest.raises(TypeError): |
|
220
|
|
|
digit_sum(123.0) |
|
221
|
|
|
|
|
222
|
|
|
def test_digit_sum_bad_n_negative(self): |
|
223
|
|
|
""" Test that :func:`lib.digital.digit_sum` raises a ``ValueError`` for a negative value for `n` """ |
|
|
|
|
|
|
224
|
|
|
digit_sum = self.get_import() |
|
225
|
|
|
with pytest.raises(ValueError): |
|
226
|
|
|
digit_sum(-123) |
|
227
|
|
|
|
|
228
|
|
|
def test_digit_sum_bad_base_type_str(self): |
|
|
|
|
|
|
229
|
|
|
""" Test that :func:`lib.digital.digit_sum` raises a ``TypeError`` for a non-``int`` value for `base` (``str``) |
|
|
|
|
|
|
230
|
|
|
""" |
|
231
|
|
|
digit_sum = self.get_import() |
|
232
|
|
|
with pytest.raises(TypeError): |
|
233
|
|
|
digit_sum(123, base="2") |
|
234
|
|
|
|
|
235
|
|
|
def test_digit_sum_bad_base_type_float(self): |
|
|
|
|
|
|
236
|
|
|
""" Test that :func:`lib.digital.digit_sum` raises a ``TypeError`` for a non-``int`` value for `base` |
|
|
|
|
|
|
237
|
|
|
(``float``) |
|
238
|
|
|
""" |
|
239
|
|
|
digit_sum = self.get_import() |
|
240
|
|
|
with pytest.raises(TypeError): |
|
241
|
|
|
digit_sum(123, base=2.0) |
|
242
|
|
|
|
|
243
|
|
|
def test_digit_sum_bad_base_negative(self): |
|
|
|
|
|
|
244
|
|
|
""" Test that :func:`lib.digital.digit_sum` raises a ``ValueError`` for a negative value for `base` """ |
|
|
|
|
|
|
245
|
|
|
digit_sum = self.get_import() |
|
246
|
|
|
with pytest.raises(ValueError): |
|
247
|
|
|
digit_sum(123, base=-2) |
|
248
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
class TestNumDigits(object): |
|
|
|
|
|
|
251
|
|
|
""" A collection of positive and negative unit tests for the :func:`lib.digital.num_digits` function """ |
|
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
@classmethod |
|
254
|
|
|
def get_import(cls): |
|
255
|
|
|
""" Get and return the :func:`lib.digital.num_digits` function |
|
256
|
|
|
|
|
257
|
|
|
.. note:: this isolates the import so any error/exception is raised by individual unit tests. |
|
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
:return: the :func:`lib.digital.num_digits` function |
|
260
|
|
|
""" |
|
261
|
|
|
from lib.digital import num_digits |
|
262
|
|
|
return num_digits |
|
263
|
|
|
|
|
264
|
|
|
@pytest.mark.parametrize("value,expected_answer", |
|
265
|
|
|
[(0, 1), (9, 1), (10, 2), (55, 2), (99, 2), (100, 3), (999, 3), (1000, 4)]) |
|
|
|
|
|
|
266
|
|
|
def test_num_digits_correctness_kat(self, value: int, expected_answer: int): |
|
267
|
|
|
""" Test the correctness of :func:`lib.digital.num_digits` using known answer tests |
|
268
|
|
|
|
|
269
|
|
|
:param value: the input value |
|
270
|
|
|
:param expected_answer: the expected answer |
|
271
|
|
|
:raises AssertionError: if :func:`lib.digital.num_digits` returns the wrong type |
|
272
|
|
|
:raises AssertionError: if :func:`lib.digital.num_digits` returns the wrong value |
|
273
|
|
|
""" |
|
274
|
|
|
|
|
275
|
|
|
num_digits = self.get_import() |
|
276
|
|
|
computed_answer = num_digits(value) |
|
277
|
|
|
assert isinstance(computed_answer, num_digits.__annotations__["return"]), "wrong type" |
|
278
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
|
279
|
|
|
|
|
280
|
|
|
def test_num_digits_correctness_random(self): |
|
|
|
|
|
|
281
|
|
|
""" Test the correctness of :func:`lib.digital.num_digits` using random inputs with predictable answers |
|
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
That is, generate a random length (`n`) and a random integer of that length (`x`). Then, check that |
|
|
|
|
|
|
284
|
|
|
:func:`lib.digital.num_digits` evaluated at `x` equals `n`. |
|
285
|
|
|
|
|
286
|
|
|
:raises AssertionError: if :func:`lib.digital.num_digits` returns the wrong type |
|
287
|
|
|
:raises AssertionError: if :func:`lib.digital.num_digits` returns the wrong value |
|
288
|
|
|
""" |
|
289
|
|
|
|
|
290
|
|
|
from random import randint |
|
291
|
|
|
num_digits = self.get_import() |
|
292
|
|
|
for i in range(1000): # a moderate but arbitrary number of random tests |
|
|
|
|
|
|
293
|
|
|
digit_len = randint(1, 50) # a moderate but arbitrary length (n) |
|
294
|
|
|
random_integer = randint(10 ** (digit_len - 1), 10 ** digit_len - 1) # the random number (x) |
|
|
|
|
|
|
295
|
|
|
computed_answer = num_digits(random_integer) |
|
296
|
|
|
assert isinstance(computed_answer, num_digits.__annotations__["return"]), "wrong type" |
|
297
|
|
|
assert computed_answer == digit_len, "wrong answer" |
|
298
|
|
|
|
|
299
|
|
|
def test_num_digits_bad_n_type_str(self): |
|
300
|
|
|
""" Test that :func:`lib.digital.num_digits` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) |
|
|
|
|
|
|
301
|
|
|
""" |
|
302
|
|
|
num_digits = self.get_import() |
|
303
|
|
|
with pytest.raises(TypeError): |
|
304
|
|
|
num_digits("123") |
|
305
|
|
|
|
|
306
|
|
|
def test_num_digits_bad_n_type_float(self): |
|
|
|
|
|
|
307
|
|
|
""" Test that :func:`lib.digital.num_digits` raises a ``TypeError`` for a non-``int`` value for `n` (``float``) |
|
|
|
|
|
|
308
|
|
|
""" |
|
309
|
|
|
num_digits = self.get_import() |
|
310
|
|
|
with pytest.raises(TypeError): |
|
311
|
|
|
num_digits(123.0) |
|
312
|
|
|
|
|
313
|
|
|
def test_num_digits_bad_n_negative(self): |
|
314
|
|
|
""" Test that :func:`lib.digital.num_digits` raises a ``ValueError`` for a negative value for `n` """ |
|
|
|
|
|
|
315
|
|
|
num_digits = self.get_import() |
|
316
|
|
|
with pytest.raises(ValueError): |
|
317
|
|
|
num_digits(-123) |
|
318
|
|
|
|
|
319
|
|
|
def test_num_digits_bad_base_type_str(self): |
|
|
|
|
|
|
320
|
|
|
""" Test that :func:`lib.digital.num_digits` raises a ``TypeError`` for a non-``int`` value for `base` (``str``) |
|
|
|
|
|
|
321
|
|
|
""" |
|
322
|
|
|
num_digits = self.get_import() |
|
323
|
|
|
with pytest.raises(TypeError): |
|
324
|
|
|
num_digits(123, base="2") |
|
325
|
|
|
|
|
326
|
|
|
def test_num_digits_bad_base_type_float(self): |
|
|
|
|
|
|
327
|
|
|
""" Test that :func:`lib.digital.num_digits` raises a ``TypeError`` for a non-``int`` value for `base` |
|
|
|
|
|
|
328
|
|
|
(``float``) |
|
329
|
|
|
""" |
|
330
|
|
|
num_digits = self.get_import() |
|
331
|
|
|
with pytest.raises(TypeError): |
|
332
|
|
|
num_digits(123, base=2.0) |
|
333
|
|
|
|
|
334
|
|
|
def test_num_digits_bad_base_negative(self): |
|
|
|
|
|
|
335
|
|
|
""" Test that :func:`lib.digital.num_digits` raises a ``ValueError`` for a negative value for `base` """ |
|
|
|
|
|
|
336
|
|
|
num_digits = self.get_import() |
|
337
|
|
|
with pytest.raises(ValueError): |
|
338
|
|
|
num_digits(123, base=-2) |
|
339
|
|
|
|
|
340
|
|
|
|
|
341
|
|
|
class TestIsPandigital(object): |
|
|
|
|
|
|
342
|
|
|
""" A collection of positive and negative unit tests for the :func:`lib.digital.is_pandigital` function """ |
|
|
|
|
|
|
343
|
|
|
|
|
344
|
|
|
@classmethod |
|
345
|
|
|
def get_import(cls): |
|
346
|
|
|
""" Get and return the :func:`lib.digital.is_pandigital` function |
|
347
|
|
|
|
|
348
|
|
|
.. note:: this isolates the import so any error/exception is raised by individual unit tests. |
|
|
|
|
|
|
349
|
|
|
|
|
350
|
|
|
:return: the :func:`lib.digital.is_pandigital` function |
|
351
|
|
|
""" |
|
352
|
|
|
from lib.digital import is_pandigital |
|
353
|
|
|
return is_pandigital |
|
354
|
|
|
|
|
355
|
|
|
@pytest.mark.parametrize("value,d,expected_answer", |
|
356
|
|
|
[(123, 3, True), (200, 3, False), (123, 4, False), ([1, 25, 43], 5, True), |
|
|
|
|
|
|
357
|
|
|
([1, 2, 2], 3, False), ([1, 2, 3, 4], 3, False)]) |
|
358
|
|
|
def test_is_pandigital_correctness(self, value: Union[int, List[int]], d: int, expected_answer: int): |
|
|
|
|
|
|
359
|
|
|
""" Test the correctness of :func:`lib.digital.is_pandigital` using known answer tests |
|
360
|
|
|
|
|
361
|
|
|
:param value: the input value |
|
362
|
|
|
:param d: parameter in `d`-pandigital |
|
363
|
|
|
:param expected_answer: the expected answer |
|
364
|
|
|
:raises AssertionError: if :func:`lib.digital.is_pandigital` returns the wrong type |
|
365
|
|
|
:raises AssertionError: if :func:`lib.digital.is_pandigital` returns the wrong value |
|
366
|
|
|
""" |
|
367
|
|
|
|
|
368
|
|
|
is_pandigital = self.get_import() |
|
369
|
|
|
computed_answer = is_pandigital(value, d) |
|
370
|
|
|
assert isinstance(computed_answer, is_pandigital.__annotations__["return"]), "wrong type" |
|
371
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
|
372
|
|
|
|
|
373
|
|
|
def test_is_pandigital_bad_int_n_type_str(self): |
|
|
|
|
|
|
374
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) |
|
|
|
|
|
|
375
|
|
|
""" |
|
376
|
|
|
is_pandigital = self.get_import() |
|
377
|
|
|
with pytest.raises(TypeError): |
|
378
|
|
|
is_pandigital("123", 3) |
|
379
|
|
|
|
|
380
|
|
|
def test_is_pandigital_bad_int_n_type_float(self): |
|
|
|
|
|
|
381
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
|
|
382
|
|
|
(``float``) |
|
383
|
|
|
""" |
|
384
|
|
|
is_pandigital = self.get_import() |
|
385
|
|
|
with pytest.raises(TypeError): |
|
386
|
|
|
is_pandigital(123.0, 3) |
|
387
|
|
|
|
|
388
|
|
|
def test_is_pandigital_bad_list_n_type_str(self): |
|
|
|
|
|
|
389
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) |
|
|
|
|
|
|
390
|
|
|
""" |
|
391
|
|
|
is_pandigital = self.get_import() |
|
392
|
|
|
with pytest.raises(TypeError): |
|
393
|
|
|
is_pandigital([1, 2, "3"], 3) |
|
394
|
|
|
|
|
395
|
|
|
def test_is_pandigital_bad_list_n_type_float(self): |
|
|
|
|
|
|
396
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
|
|
397
|
|
|
(``float``) |
|
398
|
|
|
""" |
|
399
|
|
|
is_pandigital = self.get_import() |
|
400
|
|
|
with pytest.raises(TypeError): |
|
401
|
|
|
is_pandigital([1, 2.3], 3) |
|
402
|
|
|
|
|
403
|
|
|
def test_is_pandigital_bad_int_n_negative(self): |
|
|
|
|
|
|
404
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``ValueError`` for a negative value for `n` """ |
|
|
|
|
|
|
405
|
|
|
is_pandigital = self.get_import() |
|
406
|
|
|
with pytest.raises(ValueError): |
|
407
|
|
|
is_pandigital(-123, 3) |
|
408
|
|
|
|
|
409
|
|
|
def test_is_pandigital_bad_list_n_negative(self): |
|
|
|
|
|
|
410
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``ValueError`` for a negative value for `n` """ |
|
|
|
|
|
|
411
|
|
|
is_pandigital = self.get_import() |
|
412
|
|
|
with pytest.raises(ValueError): |
|
413
|
|
|
is_pandigital([-1, 2, 3], 3) |
|
414
|
|
|
|
|
415
|
|
|
def test_is_pandigital_bad_d_type_str(self): |
|
|
|
|
|
|
416
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `d` (``str``) |
|
|
|
|
|
|
417
|
|
|
""" |
|
418
|
|
|
is_pandigital = self.get_import() |
|
419
|
|
|
with pytest.raises(TypeError): |
|
420
|
|
|
is_pandigital(123, "3") |
|
421
|
|
|
|
|
422
|
|
|
def test_is_pandigital_bad_d_type_float(self): |
|
|
|
|
|
|
423
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `d` |
|
|
|
|
|
|
424
|
|
|
(``float``) |
|
425
|
|
|
""" |
|
426
|
|
|
is_pandigital = self.get_import() |
|
427
|
|
|
with pytest.raises(TypeError): |
|
428
|
|
|
is_pandigital(123, 3.0) |
|
429
|
|
|
|
|
430
|
|
|
def test_is_pandigital_bad_d_negative(self): |
|
|
|
|
|
|
431
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``ValueError`` for a negative value for `d` """ |
|
|
|
|
|
|
432
|
|
|
is_pandigital = self.get_import() |
|
433
|
|
|
with pytest.raises(ValueError): |
|
434
|
|
|
is_pandigital(123, -3) |
|
435
|
|
|
|
|
436
|
|
|
def test_is_pandigital_bad_lower_type_str(self): |
|
|
|
|
|
|
437
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `lower` |
|
|
|
|
|
|
438
|
|
|
(``str``) |
|
439
|
|
|
""" |
|
440
|
|
|
is_pandigital = self.get_import() |
|
441
|
|
|
with pytest.raises(TypeError): |
|
442
|
|
|
is_pandigital([1, 2, 3], 3, lower="10") |
|
443
|
|
|
|
|
444
|
|
|
def test_is_pandigital_bad_lower_type_float(self): |
|
|
|
|
|
|
445
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `lower` |
|
|
|
|
|
|
446
|
|
|
(``float``) |
|
447
|
|
|
""" |
|
448
|
|
|
is_pandigital = self.get_import() |
|
449
|
|
|
with pytest.raises(TypeError): |
|
450
|
|
|
is_pandigital([1, 2, 3], 3, lower=10.0) |
|
451
|
|
|
|
|
452
|
|
|
def test_is_pandigital_bad_lower_negative(self): |
|
|
|
|
|
|
453
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``ValueError`` for a negative value for `lower` """ |
|
|
|
|
|
|
454
|
|
|
is_pandigital = self.get_import() |
|
455
|
|
|
with pytest.raises(ValueError): |
|
456
|
|
|
is_pandigital(123, 3, lower=-10) |
|
457
|
|
|
|
|
458
|
|
|
def test_is_pandigital_bad_base_type_str(self): |
|
|
|
|
|
|
459
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `base` |
|
|
|
|
|
|
460
|
|
|
(``str``) |
|
461
|
|
|
""" |
|
462
|
|
|
is_pandigital = self.get_import() |
|
463
|
|
|
with pytest.raises(TypeError): |
|
464
|
|
|
is_pandigital(123, 3, base="10") |
|
465
|
|
|
|
|
466
|
|
|
def test_is_pandigital_bad_base_type_float(self): |
|
|
|
|
|
|
467
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``TypeError`` for a non-``int`` value for `base` |
|
|
|
|
|
|
468
|
|
|
(``float``) |
|
469
|
|
|
""" |
|
470
|
|
|
is_pandigital = self.get_import() |
|
471
|
|
|
with pytest.raises(TypeError): |
|
472
|
|
|
is_pandigital(123, 3, base=2.0) |
|
473
|
|
|
|
|
474
|
|
|
def test_is_pandigital_bad_base_negative(self): |
|
|
|
|
|
|
475
|
|
|
""" Test that :func:`lib.digital.is_pandigital` raises a ``ValueError`` for a negative value for `base` """ |
|
|
|
|
|
|
476
|
|
|
is_pandigital = self.get_import() |
|
477
|
|
|
with pytest.raises(ValueError): |
|
478
|
|
|
is_pandigital(123, 3, base=-10) |
|
479
|
|
|
|
|
480
|
|
|
|
|
481
|
|
|
class TestIsPalindrome(object): |
|
|
|
|
|
|
482
|
|
|
""" A collection of positive and negative unit tests for the :func:`lib.digital.is_palindrome` function """ |
|
|
|
|
|
|
483
|
|
|
|
|
484
|
|
|
@classmethod |
|
485
|
|
|
def get_import(cls): |
|
486
|
|
|
""" Get and return the :func:`lib.digital.is_palindrome` function |
|
487
|
|
|
|
|
488
|
|
|
.. note:: this isolates the import so any error/exception is raised by individual unit tests. |
|
|
|
|
|
|
489
|
|
|
|
|
490
|
|
|
:return: the :func:`lib.digital.is_palindrome` function |
|
491
|
|
|
""" |
|
492
|
|
|
from lib.digital import is_palindrome |
|
493
|
|
|
return is_palindrome |
|
494
|
|
|
|
|
495
|
|
|
@pytest.mark.parametrize("value,base,expected_answer", [(121, 10, True), (123, 10, False), (5, 2, True)]) |
|
|
|
|
|
|
496
|
|
|
def test_is_palindrome_correctness(self, value: int, base: int, expected_answer: int): |
|
497
|
|
|
""" Test the correctness of :func:`lib.digital.is_palindrome` using known answer tests |
|
498
|
|
|
|
|
499
|
|
|
:param value: the input value |
|
500
|
|
|
:param base: consider `value` in the given `base` |
|
501
|
|
|
:param expected_answer: the expected answer |
|
502
|
|
|
:raises AssertionError: if :func:`lib.digital.is_palindrome` returns the wrong type |
|
503
|
|
|
:raises AssertionError: if :func:`lib.digital.is_palindrome` returns the wrong value |
|
504
|
|
|
""" |
|
505
|
|
|
|
|
506
|
|
|
is_palindrome = self.get_import() |
|
507
|
|
|
computed_answer = is_palindrome(value, base=base) |
|
508
|
|
|
assert isinstance(computed_answer, is_palindrome.__annotations__["return"]), "wrong type" |
|
509
|
|
|
assert computed_answer == expected_answer, "wrong answer" |
|
510
|
|
|
|
|
511
|
|
|
def test_is_palindrome_bad_n_type_str(self): |
|
|
|
|
|
|
512
|
|
|
""" Test that :func:`lib.digital.is_palindrome` raises a ``TypeError`` for a non-``int`` value for `n` (``str``) |
|
|
|
|
|
|
513
|
|
|
""" |
|
514
|
|
|
is_palindrome = self.get_import() |
|
515
|
|
|
with pytest.raises(TypeError): |
|
516
|
|
|
is_palindrome("123") |
|
517
|
|
|
|
|
518
|
|
|
def test_is_palindrome_bad_n_type_float(self): |
|
|
|
|
|
|
519
|
|
|
""" Test that :func:`lib.digital.is_palindrome` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
|
|
520
|
|
|
(``float``) |
|
521
|
|
|
""" |
|
522
|
|
|
is_palindrome = self.get_import() |
|
523
|
|
|
with pytest.raises(TypeError): |
|
524
|
|
|
is_palindrome(123.0) |
|
525
|
|
|
|
|
526
|
|
|
def test_is_palindrome_bad_n_negative(self): |
|
|
|
|
|
|
527
|
|
|
""" Test that :func:`lib.digital.is_palindrome` raises a ``ValueError`` for a negative value for `n` """ |
|
|
|
|
|
|
528
|
|
|
is_palindrome = self.get_import() |
|
529
|
|
|
with pytest.raises(ValueError): |
|
530
|
|
|
is_palindrome(-123) |
|
531
|
|
|
|
|
532
|
|
|
def test_is_palindrome_bad_base_type_str(self): |
|
|
|
|
|
|
533
|
|
|
""" Test that :func:`lib.digital.is_palindrome` raises a ``TypeError`` for a non-``int`` value for `base` |
|
|
|
|
|
|
534
|
|
|
(``str``) |
|
535
|
|
|
""" |
|
536
|
|
|
is_palindrome = self.get_import() |
|
537
|
|
|
with pytest.raises(TypeError): |
|
538
|
|
|
is_palindrome(123, base="2") |
|
539
|
|
|
|
|
540
|
|
|
def test_is_palindrome_bad_base_type_float(self): |
|
|
|
|
|
|
541
|
|
|
""" Test that :func:`lib.digital.is_palindrome` raises a ``TypeError`` for a non-``int`` value for `base` |
|
|
|
|
|
|
542
|
|
|
(``float``) |
|
543
|
|
|
""" |
|
544
|
|
|
is_palindrome = self.get_import() |
|
545
|
|
|
with pytest.raises(TypeError): |
|
546
|
|
|
is_palindrome(123, base=2.0) |
|
547
|
|
|
|
|
548
|
|
|
def test_is_palindrome_bad_base_negative(self): |
|
|
|
|
|
|
549
|
|
|
""" Test that :func:`lib.digital.is_palindrome` raises a ``ValueError`` for a negative value for `base` """ |
|
|
|
|
|
|
550
|
|
|
is_palindrome = self.get_import() |
|
551
|
|
|
with pytest.raises(ValueError): |
|
552
|
|
|
is_palindrome(123, base=-2) |
|
553
|
|
|
|
|
554
|
|
|
@pytest.mark.parametrize("base", [1, 3, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15, 200]) |
|
555
|
|
|
def test_is_palindrome_unsupported_base(self, base: int): |
|
|
|
|
|
|
556
|
|
|
""" Test that :func:`lib.digital.is_palindrome` raises a ``ValueError`` for unsupported values of `base` |
|
|
|
|
|
|
557
|
|
|
|
|
558
|
|
|
:param base: consider `value` in the given `base` |
|
559
|
|
|
""" |
|
560
|
|
|
|
|
561
|
|
|
is_palindrome = self.get_import() |
|
562
|
|
|
with pytest.raises(ValueError): |
|
563
|
|
|
is_palindrome(111, base=base) |
|
564
|
|
|
|