-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
143 lines (135 loc) · 6.37 KB
/
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
from gparser import GeckoParser
from glexer import GeckoLexer
from colorama import init
from termcolor import colored
import math
from cmath import rect
import sys
import os
if __name__ == "__main__":
print("\n---------- RUNNING TESTS -----------\n\n")
failed = 0
init() # Colorama stuff
lexer = GeckoLexer()
parser = GeckoParser()
tests = [
# Last expression is called twice, keep in mind if you're testing 'ans' stuff
("-4+5^2*10/2-2* 2+2", -4+5**2*10/2-2* 2+2),
("5+15a*b^2 with a=sin(4)^2, b=2pi", 5 + 15*(math.sin(4)**2)*(4*math.pi*math.pi)),
("x=10; -2x^3", -2000),
("x=10; (-2x)^3", (-20)**3),
("4+5", 9),
("a=b=c=d=e=f=g=1; a+b+c then x*(d+BEN) with BEN=(e+f)*(g+1)", 3*(1+4)),
(""" a=b=c=d=e=f=g=h=i=j=k=l=m=n=o=p=q=r=s=t=u=v=w=x=y=z=1
a+b+c+d+e+f+g+h+i then x*(j+k+l+m+n+o+p+q+BEN) with BEN=(r+s)*(t+u)*(v+w)*(x+y)*z""", 9*(8+2**3*(9+1))),
(""" a=b=c=d=e=f=g=h=i=j=k=l=m=n=o=p=q=r=s=t=u=v=w=x=y=z=1
a+b+c+d+e+f+g+h+i then x*(j+k+l+m+n+o+p+q+BEN) with BEN=(r+s)*(t+u)*(v+w)*(1+y)*z""", 9*(8+16)),
("_=__=___=____=_____=a; __/___+____/_____+(_+1)/(_+1); d = 14ans; d", 14),
("a=10; ----------a", 10),
("a=10; -(-a) then --x^2 then --rt(x)", 10),
("a=10; --a^2 then (--x^2) then (--x)^2 then --(-x^2)", -10**16),
("x=10; f(x,y)=x+y; f(2,3)", 5),
("x=10; f(x,y)=y then 's x+s'; f(2,3)", 5),
("x=10; f(x,y)=y+c with c=0 then 's s+x'; f(2,3)", 5),
("x=10; f(x,y)=y+c with c=x; f(2,3)", 5),
("a=b=2; d=-2a^2+5b; ans", 0),
("a=b=2; d=-2a^2+5b; d; 10ans; ans", 10*(-2*4+10)),
("x=10; d=a+x with a=6; d", 16),
("3+a with a=12 then 1/x", 1/15),
("f(x,y)=x+y; f(2,3)", 5),
(""" sum(x,y)=x+y
sub(x,y) = x - y
diff(x,y)=sum(x,y) * sub ( x ,y)
diff(14,27)""", 14**2 - 27**2),
("d=1+a; a=14; calc d; d", 15),
(""" n1 = 5+4j
n2 = rt(-16)
final = n1 * n2 then x^2
final""", ((5+4j)*(4j))**2),
("a=2; result = 10a+b with b=3 then rt(x); result", 23**0.5),
("a", 0),
("f(x,y) = x + 2^x; f(2, -135)", 2+2**2),
(""" f(x,y)=x+y
f(1+s with s=22 then rt(x), 5)""", 23**0.5+5),
("sum(x,y)=x+y; sum=15; sum(sum,sum(sum,sum))", 15+(15+15)), # what??
("x=10; d = a+b+x with a=2, b=3a then x^2 then 's s+x'; d", (2+6+10)**2+10),
("f(x,y)=x+y; f(15)", 0),
("f(x,y^2)=x+y", None),
("polar 3+2j", 3+2j),
("f(x) = -3j+rt(x); n1 = rt(-16) + 1j(rt(-9)); n2 = f(-4); n1*n2 then x^0.5", ((-3+4j)*(-1j))**0.5),
("r_p(a,b,c) = -b+rt(disc) then x/2a with disc=b^2-4a*c; r_p(2,3,-5)",1),
("r_p(a,b,c) = -b+rt(disc) then x/2a with disc=b^2-4a*c; r_p(1,0,1)",1j),
("f(x)=2x^2+1; g(x)=f(x) then 1/x^2+a with a=6; int g(x) from 0 to 1", 6.504422096094687),
("f(x) = sin(x)+1; int f(x) from -2pi to 0.5pi", 1+(5*math.pi)/2),
("f(x)=2x; int f(x) from f(1) to f(f(f(2)))", 16*16 - 2*2),
(""" x=10; f(x)=x
(int f(x) from x-9 to (a with a=x))$ + (int f(x) from 0 to x/x)""", 50),
(""" x=10; f(x)=x
int f(x) from x-9 to (a with a=x)$ + int f(x) from 0 to x/x""", 50),
(""" x=10; f(x)=x
int f(x) from x-9 to a with a=x+ int f(x) from 0 to x/x""", 54.625),
(""" x=10; f(x)=x
int f(x) from x-9 to (a with a=x+ int f(x) from 0 to x/x)""", 54.625),
(""" x=10; f(x)=x
int f(x) from x-9 to a with a=x $ + int f(x) from 0 to x/x""", 50),
("f(x) = |x|; int f(x) from -10 to 10", 100),
("f(x) = x; int f(x) from -10 to 10", 0),
("wtf(x,y) = 2y + int e^x from x to 3; wtf(1,2)", 21.367255094728623),
("x=10; int 1/x then 1/x from x with x=1 to x then x-8", 3/2),
("x=10; int 0 then 's s+x' from 5 to 10", 37.50000000000001), # my face
("a=10; int 0 then 's s+a' from 5 to 10", 50),
("1+2*3", 7),
("1+2 $ *3", 9),
("1+1+1+1+1+1 $ * 5", 30),
("int x from 0 to 1 + 14", 112.50000000000001),
("int x from 0 to 1 $+ 14", 14.5),
("5j(2)", 10j),
("j2=6; 5j2", 30),
("j(x)=2x; 5j(2)", 20),
("j(x)=x^2+1; 5 j(2)", 25),
("first=5j(2); j(x)=10x; second=5j(2); first+second", 10j+100),
("e0 = 15; 8e0", 15*8),
("8e0", 0),
("1@1", rect(1,1)),
("1@<1", rect(1,math.radians(1))),
("1@1 + 2@2", rect(1,1) + rect(2,2)),
("1@<1 + 2@2", rect(1,math.radians(1)) + rect(2,2)),
("14@0.5pi - 14 @ <90", 0),
("(3)(2)^3", 3*2**3),
("f(x)=(x-1)^2(x+1)^2; f(3)", 2**(2*4**2)),
("f(s) = n/d with n=s+1, d=(s-1)(s-2); f(j)", (1j+1)/((1j-1)*(1j-2))),
("1j(3j)", -3),
("j(x)=15x; 1j(3j)", 45j),
("j(x)=15x; new; 1j(3j)", -3),
("num1 = j; j = 400; num2 = num1 + j; num2", 400+1j),
("4%2", 0),
("1+15%2", 2),
("1+15%2*3", 4),
(r"10 % int 1 from 0 to 3", 1),
(r"1%2%3", 1),
("f(x) = (x%4)^2; f(10)", 4),
("f(x) = x%2; int f(x) from 0 to 6", 5.999999999999999)
]
for (expr, val) in tests:
# Parsing wrapped with a stdout redirect to prevent prints unless --echo flag
if (len(sys.argv)>1 and sys.argv[1]=='--echo'):
tree = parser.parse(lexer.tokenize("new;"+expr))
comp = parser.eval_tree(tree) if tree else None
# print(tree)
else:
old_stdout = sys.stdout
sys.stdout = open(os.devnull, "w")
tree = parser.parse(lexer.tokenize("new;"+expr))
comp = parser.eval_tree(tree) if tree else None
sys.stdout = old_stdout
if comp == val:
print( f"{colored('PASSED','green')} [ {expr} ] == {colored(f'({val})','cyan')}\n")
else:
print( f"{colored('FAILED','red')} [ {expr} ], {colored(f'EXPECTED ({val})','yellow')}"+
f", {colored(f'GOT ({comp})','red')}\n")
failed += 1
print("------------------------------------")
if failed>0:
print(f"{colored(f'FAILED: {failed}','red')} (out of {len(tests)})\n")
else:
print(colored(f"All {len(tests)} tests were successful.\n",'green'))