-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_expressions.py
186 lines (163 loc) · 5.8 KB
/
test_expressions.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
# noinspection PyUnresolvedReferences
from math import *
import os
import time
from random import choice, randint
from typing import Any, Callable
import InfixParser
from exencolor import Color, Decoration, colored
from mathparse.mathparse import parse
from py_expression_eval import Parser
from tabulate import tabulate
from strmath import evaluate
TEST_EXPRESSIONS_AMOUNT = int(os.getenv("TEST_EXPRESSIONS_AMOUNT", 100))
MAX_EXPRESSION_COMPLEXITY = int(os.getenv("MAX_EXPRESSION_COMPLEXITY", 3))
FAILURES_YELLOW = 3
FAILURES_RED = 10
FLOAT_ROUND = 5
MAX_RESULT_LENGTH = int(os.getenv("MAX_RESULT_LENGTH", 20)) # parse -1 to disable
def generate_expression(complexity: int) -> str:
e = str(random_num(1))
for _ in range(complexity):
op = choice(["+", "-", "*", "/", "//", "%", "**"])
# to avoid very huge numbers the power is limited to random number from 2 to 4
e += f" {op} {random_num(complexity-1) if op != '**' else randint(2, 4)}"
return e
def random_num(complexity: int) -> str:
a = randint(0, 1)
if a or complexity <= 1:
return str(randint(1, 99))
else:
return f"({generate_expression(complexity)})"
def generate_expressions(amount: int, max_complexity: int = 3) -> list[str]:
return [generate_expression(randint(1, max_complexity)) for _ in range(amount)]
def pee_eval(e: str):
return Parser().parse(e).evaluate({})
def time_func(func: Callable, *args) -> tuple[float, Any] | tuple[str, str]:
start = time.time()
try:
r = func(*args)
except:
return ("FAILURE",) * 2
return round(time.time() - start, 5), r
def test_expressions():
names = ["Python", "StrMath", "PEE", "Mathparse", "InfixParser"]
data = [
["No.", "Expression"]
+ [i + " Eval" for i in names]
+ [i + " Eval Time" for i in names]
]
functions_to_test = (eval, evaluate, pee_eval, parse, InfixParser.evaluate)
failures: list[int | str] = [0] * len(functions_to_test)
avg_times: list[float] = [0] * len(functions_to_test)
print()
expressions = generate_expressions(
TEST_EXPRESSIONS_AMOUNT, MAX_EXPRESSION_COMPLEXITY
) + [
"factorial(5)",
"sqrt(4) ** 2",
"sin(30) ** 2 + cos(30) ** 2",
"(50 + log(10 * 49)) // 5",
"pow(20, 3) + sqrt(25 ** 2)",
]
amount = len(expressions)
for num, expr in enumerate(expressions, 1):
results = []
times = []
failure = False
for fc in functions_to_test:
eval_time, result = time_func(fc, expr)
if fc == eval and eval_time == "FAILURE":
failure = True
break
results.append(
round(result, FLOAT_ROUND) if result != "FAILURE" else result
)
times.append(eval_time)
if failure:
colored_results, colored_times = (
(colored("SKIPPED", foreground=Color.BRIGHT_YELLOW),)
* len(functions_to_test),
) * 2
amount -= 1
else:
expected_value = results[0]
colored_results = []
for i, res in enumerate(results):
if res == expected_value:
color = Color.BRIGHT_GREEN
else:
color = Color.BRIGHT_RED
failures[i] += 1
res = str(res)
if -1 < MAX_RESULT_LENGTH < len(res):
res = res[:MAX_RESULT_LENGTH] + "..."
colored_results.append(colored(res, foreground=color))
min_time = min([t for t in times if isinstance(t, (float, int))], default=1)
colored_times = []
for i, res in enumerate(times):
if res != "FAILURE":
avg_times[i] = round((avg_times[i] + res) / 2, 10)
if res == "FAILURE":
color = Color.BRIGHT_RED
elif res == min_time:
color = Color.BRIGHT_GREEN
else:
color = Color.BRIGHT_YELLOW
colored_times.append(colored(res, foreground=color))
d = [num, expr]
d.extend(colored_results)
d.extend(colored_times)
data.append(d)
print("EXPRESSIONS".center(195, "-"))
print(
tabulate(
data,
headers="firstrow",
tablefmt="pretty",
numalign="left",
stralign="left",
)
)
failures_percents = [int(i / amount * 100) for i in failures]
colored_failures = []
colored_failures_percents = []
for i, v in enumerate(failures_percents):
if v >= FAILURES_RED:
color = Color.BRIGHT_RED
elif v >= FAILURES_YELLOW:
color = Color.BRIGHT_YELLOW
else:
color = Color.BRIGHT_GREEN
colored_failures.append(colored(failures[i], foreground=color))
colored_failures_percents.append(
colored(str(failures_percents[i]) + "%", foreground=color)
)
print()
print("SUMMARY".center(195, "-"))
print(
tabulate(
[
["Failures"] + colored_failures,
["Failures (%)"] + colored_failures_percents,
["Average Time"] + list(map(str, avg_times)),
],
[""] + names,
tablefmt="pretty",
stralign="left",
numalign="left",
)
)
if amount != len(expressions):
print(
colored(
len(expressions) - amount,
foreground=Color.BRIGHT_YELLOW,
decoration=Decoration.BOLD,
)
+ colored(
" samples were skipped due to bad expression generation.",
foreground=Color.BRIGHT_YELLOW,
)
)
assert failures_percents[1] < FAILURES_YELLOW