-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharith_tests.py
394 lines (354 loc) · 7.44 KB
/
arith_tests.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
#
# arith_tests.py
#
# Demo/tests of various plusminus parsers
#
# Copyright 2020, Paul McGuire
#
from pprint import pprint
from plusminus import ArithmeticParser
from plusminus.examples.dice_roll_parser import DiceRollParser
from plusminus.examples.combinatorics_arithmetic_parser import CombinatoricsArithmeticParser
from plusminus.examples.business_arithmetic_parser import BusinessArithmeticParser
from plusminus.examples.date_time_arithmetic_parser import DateTimeArithmeticParser
def post_parse_evaluate(teststr, result):
if "@=" not in teststr and not teststr.strip().endswith("="):
return result[0].evaluate()
parser = ArithmeticParser()
parser.maximum_formula_depth = 5
parser.runTests(
"""\
k @= j
j @= i
i @= h
h @= g
g @= f
# function too deeply nested expected
f @= e
e @= d
d @= c
c @= b
b @= a
a @= 1
# name error expected
k
""",
postParse=post_parse_evaluate,
)
parser = ArithmeticParser()
parser.runTests(
"""\
a, b, c =
# illegal recursion expected
a @= a
a @= b
b @= c
# illegal recursion expected
c @= a
# name error expected
a
""",
postParse=post_parse_evaluate,
)
parser = ArithmeticParser()
parser.runTests(
"""\
# illegal recursion expected
a @= a + 1
b @= a + 1
# illegal recursion expected
a @= b + b
b, c, d =
a @= b + b
b @= c + c
c @= d + d
d @= e + e
e @= f + f
f @= g + g
f = 1
a
""",
postParse=post_parse_evaluate,
)
parser = ArithmeticParser()
parser.initialize_variable("temp_c", "(ftemp - 32) * 5 / 9", as_formula=True)
parser.initialize_variable("temp_f", "32 + ctemp * 9 / 5", as_formula=True)
parser.runTests(
"""\
False
False or True
True or False
True or True
False or False
False or False or True
""",
postParse=post_parse_evaluate,
)
parser.runTests(
"""\
sin(rad(30))
sin(30°)
# test rejection of functions with wrong number of args
sin()
sin(1, 2)
hypot(1)
sin(pi)
sin(π/2)
sin²(pi)
sin²(π/2)
sin²(30°)
sin³(30°)
deg(sin⁻¹(0.5))
cos(pi)
cos(π/2)
cos²(pi)
cos²(π/2)
cos²(30°)
cos³(30°)
deg(cos⁻¹(0.5))
deg(tan⁻¹(1.0))
deg(tan⁻¹(-1.0))
rnd()
# division by zero expected
1/0
0**0
32 + 37 * 9 / 5
# verify right-to-left eval of exponents
# 3**2**3 should eval as 3**(2**3)
3**2**3
3**(2**3)
(3**2)**3
# special exponents
10⁻¹
10⁰
10¹
10²
10³
# division by zero expected
0⁻¹
0⁰
0¹
0²
0³
(-1)⁻¹
(-1)⁰
(-1)¹
(-1)²
(-1)³
# addition and multiplication of strings
"You" + " win"
"You" + " win"*3
# ints as bools
not not 0
not not 1
1 or 0
1 and not 0
# set membership
2 in {1, 2, 3}
32 + 37 * 9 / 5 == 98.6
ctemp = 37
temp_f = 100.2
temp_f > 98.6 ? "fever" : "normal"
"You " + (temp_f > 98.6 ? "have" : "don't have") + " a fever"
ctemp = 38
feverish @= temp_f > 98.6
"You " + (feverish ? "have" : "don't have") + " a fever"
temp_f = 98.2
"You " + (feverish ? "have" : "don't have") + " a fever"
a = 100
b @= a / 10
a / 2
b
a = 5
b
a = b + 3
b
a = b + 3
a + c
'x' < 'y' < 'z'
5 mod 3
circle_area @= pi * circle_radius**2
circle_radius = 100
circle_area
coin_toss @= rnd() > 0.5? "heads" : "tails"
coin_toss
coin_toss
coin_toss
coin_toss
die_roll @= randint(1, 6)
die_roll
die_roll
die_roll
# unary and binary square root
# and imaginary square root
√2
²√2
2√2
³√2
2³√2
(2³)√2
⁹√2
2⁹√2
√-1
# test safe_pow
# expect overflow error
10000**100000
0**10000000000**10000000000
0**(-1)**2
# expect zero division error
0**(-1)**3
1000000000000**1000000000000**0
1000000000000**0**1000000000000**1000000000000
# test all comparison operators
100 < 101
100 <= 101
100 > 101
100 >= 101
100 == 101
100 != 101
100 < 99
100 <= 99
100 > 99
100 >= 99
100 == 99
100 != 99
100 < 100+1E-18
100 <= 100+1E-18
100 > 100+1E-18
100 >= 100+1E-18
100 == 100+1E-18
100 != 100+1E-18
# function call with variable number of args
hypot(3)
hypot(3, 4)
hypot(3, 4, 5, 6) == hypot(3, hypot(4, hypot(5, 6)))
hypot()
# set operations
a, b = 1, 10
1 in { a, 11, 22, 53}
1 not in {b, 0}
myset = { a, 11, 22, 53, 'z', 'x' ,'a', {100, 101, 99}}
myset
1 in myset
{ 0, 2, 22}
{ a, 11, 22, 53} ∩ { 0, 2, 22}
{ a, 11, 22, 53} & { 0, 2, 22}
{ a, 11, 22, 53} ∪ { 0, 2, 22}
{ a, 11, 22, 53} | { 0, 2, 22}
{ a, 11, 22, 53} ∩ {}
{ a, 11, 22, 53} & {}
{ a, 11, 22, 53} ∪ {}
{ a, 11, 22, 53} | {}
{ a, 11, 22, 53} - { 0, 2, 22}
{ 0, 31, 1.2} - { 0, 1.2, 31}
{} - { 0, 2, 22}
{} ^ {}
{1, 2, 3} ^ {2, 3}
myset ∩ { 0, 2, 22}
myset ∪ { 0, 2, 22}
myset & { 0, 2, 22}
myset | { 0, 2, 22}
myset - {11, 22, 34}
myset ^ {11, 22, 34}
1 in (myset ∩ { 0, 2, 22})
1 in (myset ∪ { 0, 2, 22})
1 ∈ (myset ∩ { 0, 2, 22})
1 ∉ (myset ∪ { 0, 2, 22})
1 in (myset & { 0, 2, 22})
1 in (myset | { 0, 2, 22})
1 ∈ (myset & { 0, 2, 22})
1 ∉ (myset | { 0, 2, 22})
1 ∈ (myset & { 0, |-2|, |22|})
1 ∉ (myset | { |0|, 2, |-22|})
1 in (myset ∩ {})
1 in (myset ∪ {})
1 in (myset & {})
1 in (myset | {})
{{1, 2}, 99, 100}
{99, 'z', 'a'} ∪ {'a', 't', 100}
{99, 'z', 'a'} | {'a', 't', 100}
# sets as function arguments
a = {1, 2, 3}
max(a)
max({1, 2, 4})
max({1, 2} ∪ a)
max({1, 2} | a)
max({1, 2} ∩ a)
max({1, 2} & a)
min({1, 2, 4})
# expect type error
sin({1, 2, 4})
# mismatched parentheses
5 + (3*
""",
postParse=post_parse_evaluate,
)
pprint(parser.vars())
print("circle_area =", parser["circle_area"])
print("circle_area =", parser.evaluate("circle_area"))
print("del parser['circle_radius']")
del parser["circle_radius"]
try:
print("circle_area =", end=" ")
print(parser.evaluate("circle_area"))
except NameError as ne:
print(ne)
print(parser.parse("6.02e24 * 100").evaluate())
parser = CombinatoricsArithmeticParser()
parser.runTests(
"""\
# CombinatoricsArithmeticParser
3!
-3!
3!!
6! / (6-2)!
6 P 2
6! / (2!*(6-2)!)
6 C 2
6P6
6C6
""",
postParse=post_parse_evaluate,
)
parser = BusinessArithmeticParser()
parser.runTests(
"""\
# BusinessArithmeticParser
25%
20 * 50%
50% of 20
20 * (1-20%)
(100-20)% of 20
20% off
20% off of 20
5 / 20%
FV(20000, 3%, 30)
PV(FV(20000, 3%, 30), 3%, 30)
FV(20000, 3%/12, 30*12)
""",
postParse=post_parse_evaluate,
)
parser = DateTimeArithmeticParser()
parser.runTests(
"""\
# DateTimeArithmeticParser
now()
str(now())
str(today())
"A day from now: " + str(now() + 1d)
"A day and an hour from now: " + str(now() + 1d + 1h)
str(now() + 3*(1d + 1h))
""",
postParse=post_parse_evaluate,
)
parser = DiceRollParser()
parser.runTests(
"""\
# DiceRollParser
d20
3d6
d20 + 3d4
(3d6)/3
""",
postParse=post_parse_evaluate,
)