-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_tests.py
211 lines (186 loc) · 3.55 KB
/
unit_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
from calculator_extended import parse, e, resolve
from pprint import pprint
from colorama import Fore, Style
def unit_test(expr: str, expected_value, results):
print(f"Expression: {expr}")
try:
ast = resolve(parse(expr))
# ast = parse(expr)
pprint(ast)
result = e(ast)
print(f"Evaluated Result: {result}")
if result != expected_value:
error_msg = f"Test failed for expression: {expr}. Expected {expected_value}, but got {result}."
results.append(("FAILED", expr, error_msg))
print(error_msg)
print()
else:
results.append(("PASSED", expr, None))
print("Test passed!\n")
except Exception as ep:
error_msg = f"Error evaluating expression: {expr}. Exception: {ep}"
results.append(("ERROR", expr, f"{Fore.RED}{error_msg}{Style.RESET_ALL}"))
print()
log = []
unit_test("2", 2, log)
unit_test("2+3", 5, log)
unit_test("2+3*5", 17, log)
unit_test("2 + 3*5", 17, log)
unit_test("2 + 3*5^2", 77, log)
unit_test("2 + 3*5^2 - 3", 74, log)
unit_test("2 + 3*5^2 - 3 / 2", 75.5, log)
unit_test("2.5 + 3.5", 6.0, log)
unit_test("2^3^2", 512, log)
unit_test("2.5^3^2", 3814.697265625, log)
unit_test("3 + 2*3.5^4/2", 153.0625, log)
unit_test("2-3-5-6", -12, log)
unit_test("2/3/5", 0.13333333333333333, log)
unit_test("((2+3)*5)/5", 5.0, log)
unit_test("3-(5-6)", 4, log)
unit_test("3*(5-6^3)", -633, log)
unit_test("2- -2", 4, log)
unit_test("2- -(3 - 1)", 4, log)
unit_test("5*-5", -25, log)
unit_test("-5*5", -25, log)
unit_test("2 +-3", -1, log)
unit_test("-(5-2)", -3, log)
unit_test("-((4*5)-(4/5))", -19.2, log)
unit_test("\u221a(4)", 2, log)
exp_sq = "\u221a(4 + 12) + \u221a(9)"
unit_test(exp_sq, 7.0, log)
exp_cond1 = """
if 2 < 3 then
0 + 5
else
1 * 6
"""
unit_test("if 2 < 3 then 2 else 3", 2, log)
unit_test(exp_cond1, 5, log)
exp_cond = """
if 2 < 3 then
if 4 > 5 then
1
else if 6 <= 7 then
8
else
9
else
10
"""
unit_test(exp_cond, 8, log)
# Euler Problem 1
exp = """
letFunc func(x, s)
{
if x = 1000 then
s
else if x % 3 = 0 || x % 5 = 0 then
func(x + 1, s + x)
else
func(x + 1, s)
}
in
func(0, 0)
end
"""
unit_test(exp, 233168, log)
# Euler Problem 2
exp = """
letFunc fib(a, b, s)
{
if a >= 4000000 then
s
else if a % 2 = 0 then
fib(b, a + b, s + a)
else
fib(b, a + b, s)
}
in
fib(0, 1, 0)
end
"""
unit_test(exp, 4613732, log)
# Factorial
exp = """
letFunc fact(n)
{
if n = 0 then
1
else
let x := fact(n - 1) in
n * x
end
}
in
fact(5)
end
"""
unit_test(exp, 120, log)
# Fixed this -> added CallFun at the highest precedence in parse_atom()
exp = """
letFunc f(a)
{
a + a
}
in
f(2) + f(3)
end
"""
unit_test(exp, 10, log)
exp = """
let x := 5 in
letFunc f(y) {
x
}
in
letFunc g(z) {
let x := 6
in f(z)
end
}
in
g(0)
end
end
end
"""
unit_test(exp, 5, log)
exp = """
letFunc f(x)
{
x ^ 2
}
in
letFunc g(f, y)
{
f(y) + y
}
in
g(f, 3)
end
end
"""
unit_test(exp, 12, log)
exp = """
letFunc f(x)
{
x * 7
}
in
let g := f in
g(3)
end
end
"""
unit_test(exp, 21, log)
print("\nTest Summary:")
passed_count = 0
for status, expr, error_msg in log:
if status == "PASSED":
print(f"{Fore.GREEN}PASSED: {expr}{Style.RESET_ALL}")
passed_count += 1
else:
print(f"{Fore.RED}{status}: {expr}{Style.RESET_ALL}")
if error_msg:
print(f" -> {error_msg}")
print(f"\nTotal Passed: {passed_count} out of {len(log)}")