-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_unit.py
438 lines (379 loc) · 15.6 KB
/
test_unit.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import decimal
import math
import pickle
import pytest
import cloudpickle
from plusminus import ArithmeticParser, ArithmeticParseException
import sys
sys.setrecursionlimit(4000)
# Normally this would be kept in a "fixtures.py" file or similar, for access by all test scripts.
@pytest.fixture
def basic_arithmetic_parser():
arith_parser = ArithmeticParser()
# Add temp conversions - you could do this in the test, or create a separate/inheriting fixture with these.
arith_parser.parse("temp_c @= (ftemp - 32) * 5 / 9")
arith_parser.parse("temp_f @= 32 + ctemp * 9 / 5")
arith_parser.parse("ctemp = 38")
arith_parser.parse("feverish @= temp_f > 98.6")
return arith_parser
# You don't actually need to place test functions in a class,
# but you can setup/scope fixtures for one test/class/module/test_suite run etc
class TestBasicArithmetic:
def _test_evaluate(self, basic_arithmetic_parser, input_string, expected_value):
if isinstance(expected_value, (int, float)):
assert math.isclose(
basic_arithmetic_parser.evaluate(input_string),
expected_value,
abs_tol=1e-12,
)
elif isinstance(expected_value, complex):
observed_value = basic_arithmetic_parser.evaluate(input_string)
assert math.isclose(
observed_value.real,
expected_value.real,
abs_tol=1e-12,
) and math.isclose(
observed_value.imag,
expected_value.imag,
abs_tol=1e-12,
)
else:
assert basic_arithmetic_parser.evaluate(input_string) == expected_value
@pytest.mark.parametrize(
"input_string, expected_value",
[
("0**0", 1),
("+5", 5),
("+(5-3)", 2),
("-(5-3)", -2),
("-(5-+3)", -2),
("-(5--3)", -8),
("3**2**3", 3 ** 2 ** 3),
('"You" + " win"*3', "You win win win"),
("(0)", 0),
("((0))", 0),
("(((0)))", 0),
("((((0))))", 0),
("(((((0)))))", 0),
("((((((0))))))", 0),
(
"{{{{{{100}}}}}}",
frozenset(
[
frozenset(
[frozenset([frozenset([frozenset([frozenset([100])])])])]
)
]
),
),
# evaluate formula expressions
("ctemp", 38),
("temp_f", 100.4),
(
'"You " + (feverish ? "have" : "dont have") + " a fever"',
"You have a fever",
),
],
)
def test_evaluate(self, basic_arithmetic_parser, input_string, expected_value):
self._test_evaluate(basic_arithmetic_parser, input_string, expected_value)
@pytest.mark.parametrize(
"input_string, expected_value",
[
("False", False),
("True", True),
("False or False", False),
("False or True", True),
("True or False", True),
("True or True", True),
("False or False or False", False),
("False or True or False", True),
("True or False or False", True),
("True or True or False", True),
("False or False or True", True),
("False or True or True", True),
("True or False or True", True),
("True or True or True", True),
("False and False", False),
("False and True", False),
("True and False", False),
("True and True", True),
("False and False and False", False),
("False and True and False", False),
("True and False and False", False),
("True and True and False", False),
("False and False and True", False),
("False and True and True", False),
("True and False and True", False),
("True and True and True", True),
("False or False and False", False),
("False or True and False", False),
("True or False and False", True),
("True or True and False", True),
("False or False and True", False),
("False or True and True", True),
("True or False and True", True),
("True or True and True", True),
("False and False or False", False),
("False and True or False", False),
("True and False or False", False),
("True and True or False", True),
("False and False or True", True),
("False and True or True", True),
("True and False or True", True),
("True and True or True", True),
("(False or False) and False", False),
("(False or True) and False", False),
("(True or False) and False", False),
("(True or True) and False", False),
("(False or False) and True", False),
("(False or True) and True", True),
("(True or False) and True", True),
("(True or True) and True", True),
],
)
def test_evaluate_boolean_expressions(self, basic_arithmetic_parser, input_string, expected_value):
self._test_evaluate(basic_arithmetic_parser, input_string, expected_value)
@pytest.mark.parametrize(
"input_string, expected_value",
[
("sin(rad(30))", 0.5),
("sin(π/2)", 1.0),
("sin(30°)", 0.5),
("sin²(30°)", 0.25),
("sin³(30°)", 0.125),
("cos(30°)", 3**0.5 / 2),
("cos²(30°)", 0.75),
("cos³(30°)", 3**1.5 / 8),
("tan(30°)", 1 / 3**0.5),
("tan²(30°)", 1 / 3),
("tan³(30°)", 1 / 3**1.5),
("deg(sin⁻¹(0.5))", 30),
("deg(cos⁻¹(0.5))", 60),
("deg(tan⁻¹(1.0))", 45),
("deg(tan⁻¹(-1.0))", -45),
("log(10)", math.log(10)),
("log(10, 2)", math.log(10, 2)),
("log(10, 10)", math.log(10, 10)),
("log(e)", math.log(math.e)),
("hypot(3)", 3),
("hypot(3, 4)", 5),
("hypot(3, 4, 5, 6) == hypot(3, hypot(4, hypot(5, 6)))", True),
("hypot()", 0),
("max(10, 11, 12)", 12),
("max({10, 11, 12})", 12),
],
)
def test_evaluate_functions(
self, basic_arithmetic_parser, input_string, expected_value
):
self._test_evaluate(basic_arithmetic_parser, input_string, expected_value)
@pytest.mark.parametrize(
"input_string, expected_value",
[
("²√2", math.sqrt(2)),
("2√2", 2 * math.sqrt(2)),
("³√2", math.pow(2, 1 / 3)),
("3³√2", 3 * math.pow(2, 1 / 3)),
("(3³)√2", 3 ** 3 * math.sqrt(2)),
("⁹√2", math.pow(2, 1 / 9)),
("2⁹√2", 2 * math.pow(2, 1 / 9)),
("√-1", (-1) ** (1 / 2)),
],
)
def test_evaluate_radical_expressions(
self, basic_arithmetic_parser, input_string, expected_value
):
self._test_evaluate(basic_arithmetic_parser, input_string, expected_value)
@pytest.mark.parametrize(
"input_string, expected_error_type",
[
("sin()", TypeError),
("sin(1, 2)", TypeError),
("1/0", ZeroDivisionError),
("1000000**1000000", OverflowError),
("((0)", ArithmeticParseException),
("(((((((((((0)))))))))))", OverflowError),
("((((((0)))))))", ArithmeticParseException),
("sin({1, 2, 4})", TypeError),
("{{{{{{{100}}}}}}}", OverflowError),
],
)
def test_evaluate_throws_errors(
self, basic_arithmetic_parser, input_string, expected_error_type
):
with pytest.raises(expected_error_type):
basic_arithmetic_parser.evaluate(input_string)
pytest.fail(
"Exception {} not raised evaluating {!r}".format(
expected_error_type.__name__, input_string
)
)
@pytest.mark.parametrize(
"input_string, expected_value",
[
# fmt: off
("1 in { a, 11, 22, 53}", True),
("1 not in {b, 0}", True),
("1 in myset", True),
("{ 0, 2, 22}", {0, 2, 22}),
("{ a, 11, 22, 53} ∩ { 0, 2, 22}", {22},),
("{ a, 11, 22, 53} ∪ { 0, 2, 22}", {0, 1, 2, 11, 22, 53}),
("{ a, 11, 22, 53} ∩ {}", set()),
("{ a, 11, 22, 53} ∪ {}", {1, 11, 22, 53}),
("{ a, 11, 22, 53} & { 0, 2, 22}", {22},),
("{ a, 11, 22, 53} | { 0, 2, 22}", {0, 1, 2, 11, 22, 53}),
("{ a, 11, 22, 53} & {}", set()),
("{ a, 11, 22, 53} | {}", {1, 11, 22, 53}),
("myset ∩ { 0, 2, 22}", {22},),
("myset ∪ { 0, 2, 22}", {0, 1, 2, 11, 22, 53}),
("1 in (myset ∩ { 0, 2, 22})", False),
("1 in (myset ∪ { 0, 2, 22})", True),
("1 ∈ (myset ∩ { 0, 2, 22})", False),
("1 ∉ (myset ∪ { 0, 2, 22})", False),
("1 in (myset ∩ {})", False),
("1 in (myset ∪ {})", True),
("max(aset)", 3),
("max({1, 2, 4})", 4),
("max({1, 2} ∪ aset)", 3),
("min({1, 2, 4})", 1),
("{ a, 11, 22, 53} - { 0, 2, 22}", {1, 11, 53}),
("{ 0, 31, 1.2} - { 0, 1.2, 31}", set()),
("{} - { 0, 2, 22}", set()),
("{} ^ {}", set()),
("{1, 2, 3} ^ {2, 3, 4}", {1, 4}),
("{1, 2, 3} ∆ {2, 3, 4}", {1, 4}),
# fmt: on
],
)
def test_set_expressions(
self, basic_arithmetic_parser, input_string, expected_value
):
basic_arithmetic_parser.parse("a, b = 1, 10")
basic_arithmetic_parser.parse("myset = { a, 11, 22, 53}")
basic_arithmetic_parser.parse("aset = {1, 2, 3}")
self._test_evaluate(basic_arithmetic_parser, input_string, expected_value)
@pytest.mark.parametrize(
"input_string, nesting_depth, expected_value, expected_error_type",
[
("((((0))))", 4, 0, None),
("(((((0)))))", 4, None, OverflowError),
],
)
def test_customize_max_expression_depth(
self,
basic_arithmetic_parser,
input_string,
nesting_depth,
expected_value,
expected_error_type,
):
basic_arithmetic_parser.maximum_expression_depth = nesting_depth
if expected_error_type is not None:
with pytest.raises(expected_error_type):
basic_arithmetic_parser.evaluate(input_string)
pytest.fail(
"Exception {} not raised evaluating {!r}".format(
expected_error_type.__name__, input_string
)
)
else:
assert basic_arithmetic_parser.evaluate(input_string) == expected_value
def test_set_parser_vars(self, basic_arithmetic_parser):
x2_res = []
y_res = []
observed_x = []
expected_x2 = []
observed_y = []
for x in range(10):
basic_arithmetic_parser["x"] = x
x2_res.append(basic_arithmetic_parser.evaluate("x²"))
y_res.append(basic_arithmetic_parser.evaluate("y = x²"))
expected_x2.append(x * x)
observed_x.append(basic_arithmetic_parser["x"])
observed_y.append(basic_arithmetic_parser["y"])
print(x2_res)
print(y_res)
print(observed_x)
print(expected_x2)
print(observed_y)
print(basic_arithmetic_parser.vars())
assert list(range(10)) == observed_x
assert x2_res == expected_x2
assert observed_y == expected_x2
with pytest.raises(NameError):
z_value = basic_arithmetic_parser["z"]
pytest.fail("returned unexpected 'z' value {!r}".format(z_value))
def test_clearing_parser_vars(self, basic_arithmetic_parser):
with pytest.raises(NameError):
basic_arithmetic_parser.evaluate("a")
print("a, b", basic_arithmetic_parser.parse("a, b = 1, 2"))
print("c", basic_arithmetic_parser.parse("c = a + b"))
print("clear a", basic_arithmetic_parser.parse("a ="))
with pytest.raises(NameError):
basic_arithmetic_parser["a"]
with pytest.raises(NameError):
basic_arithmetic_parser.evaluate("c = a + b")
def test_maximum_formula_depth(self, basic_arithmetic_parser):
basic_arithmetic_parser.maximum_formula_depth = 5
basic_arithmetic_parser.parse("a @= b + b")
basic_arithmetic_parser.parse("b @= c + c")
basic_arithmetic_parser.parse("c @= d + d")
basic_arithmetic_parser.parse("d @= e + e")
basic_arithmetic_parser.parse("e @= f + f")
with pytest.raises(OverflowError):
basic_arithmetic_parser.parse("f @= g + g")
basic_arithmetic_parser.parse("f = 1")
a_value = basic_arithmetic_parser.evaluate("a")
print(a_value)
assert a_value == 32
basic_arithmetic_parser.parse("a, b, c, d, e=")
basic_arithmetic_parser.parse("a @= b")
basic_arithmetic_parser.parse("b @= c")
basic_arithmetic_parser.parse("c @= d")
basic_arithmetic_parser.parse("d @= e")
with pytest.raises(OverflowError):
basic_arithmetic_parser.parse("e @= f")
def test_max_number_of_vars(self, basic_arithmetic_parser):
var_limit = 20
basic_arithmetic_parser.max_number_of_vars = var_limit
# compute number of vars that are safe to define by subtracting
# the number of predefined vars from the allowed limit
vars_to_define = var_limit - len(basic_arithmetic_parser.vars())
for i in range(vars_to_define):
basic_arithmetic_parser.evaluate("a{} = 0".format(i))
# now define one more, which should put us over the limit and raise
# the exception
with pytest.raises(Exception):
basic_arithmetic_parser.evaluate("a{} = 0".format(var_limit))
def test_parser_pickling(self, basic_arithmetic_parser):
pickled_parser = cloudpickle.dumps((basic_arithmetic_parser,))
parser, = pickle.loads(pickled_parser)
assert parser["ctemp"] == 38
parser.parse("ftemp = 212")
assert parser.evaluate("temp_c") == 100
assert parser.evaluate("feverish") is True
def test_parser_pickling2(self):
parser = ArithmeticParser()
parser.parse('α = π²')
parsed_res = parser.parse('β = 90')
# send parser and results on round trip through cloudpickle
parsed_pair = cloudpickle.dumps((parser, parsed_res))
parser, parsed_res = cloudpickle.loads(parsed_pair)
assert parser["α"] == math.pi ** 2
assert parser["β"] == 90
assert parser.evaluate("α * β") == math.pi ** 2 * 90
assert parser.evaluate("sin(β°)") == 1.0
assert parsed_res.evaluate() == 90
def test_decimal_evaluate(self):
parser = ArithmeticParser(use_decimal=True)
test_str = "3.000000000000000000001"
result = parser.evaluate(test_str)
assert isinstance(result, decimal.Decimal)
assert str(result) == test_str
# make sure a pickled parser retains decimal-ness
parsed_pair = cloudpickle.dumps((parser,))
parser, = cloudpickle.loads(parsed_pair)
result = parser.evaluate("100")
assert isinstance(result, decimal.Decimal)
assert str(result) == "100"