-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
73 lines (65 loc) · 1.95 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
# Test cases for EAGER evaluator
def testEval():
env = {}
Assign(Variable('a'), Number(5.0)).eval(env)
print Variable('a').eval(env)
env['b'] = 2.0
print Variable('b').eval(env)
env['c'] = 4.0
print Variable('c').eval(env)
print Sum(Variable('a'), Variable('b')).eval(env)
print Sum(Diff(Variable('a'), Variable('c')), Variable('b')).eval(env)
Assign(Variable('a'), Sum(Variable('a'), Variable('b'))).eval(env)
print Variable('a').eval(env)
print env
# Simple tokenizer tests
'''Answers are:
['fred']
['777']
['777', 'hi', '33']
['*', '*', '-', ')', '(']
['(', 'hi', '*', 'ho', ')']
['(', 'fred', '+', 'george', ')']
['(', 'hi', '*', 'ho', ')']
['(', 'fred', '+', 'george', ')']
'''
def testTokenize():
print tokenize('fred ')
print tokenize('777 ')
print tokenize('777 hi 33 ')
print tokenize('**-)(')
print tokenize('( hi * ho )')
print tokenize('(fred + george)')
print tokenize('(hi*ho)')
print tokenize('( fred+george )')
# Simple parsing tests
'''Expected Answers :
Var(a)
Num(888.0)
Sum(Var(fred), Var(george))
Quot(Prod(Var(a), Var(b)), Diff(Var(cee), Var(doh)))
Quot(Prod(Var(a), Var(b)), Diff(Var(cee), Var(doh)))
Assign(Var(a), Prod(Num(3.0), Num(5.0)))
'''
def testParse():
print parse(['a'])
print parse(['888'])
print parse(['(', 'fred', '+', 'george', ')'])
print parse(['(', '(', 'a', '*', 'b', ')', '/', '(', 'cee', '-', 'doh', ')' ,')'])
print parse(tokenize('((a * b) / (cee - doh))'))
print parse(tokenize('(a = (3 * 5))')
## Test for calc()
# exprs is a list of strings
# runs calculator on those strings, in sequence, using the same environment
testExprs = ['(2 + 5)',
'(z = 6)',
'z',
'(w = (z + 1))',
'w'
]
def calcTest(exprs):
env = {}
for e in exprs:
print '%', e # e is the experession
print # your expression here
print ' env =', env