-
Notifications
You must be signed in to change notification settings - Fork 6
/
hw3-tests.py
151 lines (133 loc) · 7.86 KB
/
hw3-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
import re
############################################################
# Load the files.
exec(open('parse.py').read())
exec(open('interpret.py').read())
exec(open('machine.py').read())
exec(open('compile.py').read())
interpretCheck = open('interpret.py').read()
compileCheck = open('compile.py').read()
if interpretCheck.find('import parse') != -1 or\
interpretCheck.find('from parse import') != -1 or\
interpretCheck.find("exec(open('parse.py').read())") == -1:
print('''You did not load the parse.py module correctly in interpret.py. You must use "exec(open('parse.py').read())". Exiting.''')
exit()
if compileCheck.find('import parse') != -1 or\
compileCheck.find('from parse import') != -1 or\
compileCheck.find("exec(open('parse.py').read())") == -1:
print('''You did not load the parse.py module correctly in compile.py. You must use "exec(open('parse.py').read())". Exiting.''')
exit()
if compileCheck.find('import machine') != -1 or\
compileCheck.find('from machine import') != -1 or\
compileCheck.find("exec(open('machine.py').read())") == -1:
print('''You did not load the machine.py module correctly in compile.py. You must use "exec(open('machine.py').read())". Exiting.''')
exit()
def check(function, inputs_result_pairs):
def str_(s): return '"'+str(s)+'"' if type(s) == str else str(s)
(prefix, suffix) = (function + '(', ')')
passed = 0
for (inputs, result) in inputs_result_pairs:
callStr = prefix+', '.join([str_(i) for i in inputs])+suffix
output = None
try:
output = eval(function)(*inputs)
except Exception as e:
output = '<Error>'
if output == result:
passed = passed + 1
elif output == '<Error>':
print("\n Error on:\n " + callStr + "\n\n"+" Should be:\n "+str_(result)+"\n\n"+" A run-time error occurred!\n")
else:
print("\n Failed on:\n " + callStr + "\n\n"+" Should be:\n "+str_(result)+"\n\n"+" Returned:\n "+str(output)+"\n")
print("Passed " + str(passed) + " of " + str(len(inputs_result_pairs)) + " tests.")
print("")
############################################################
# The tests.
print("Problem #1, part (a), evalTerm()...")
try: evalTerm
except: print("The evalTerm() function is not defined.")
else: check('evalTerm', [\
(({}, {'Number': [123]}), {'Number': [123]}),\
(({}, {'Plus': [{'Number': [0]}, {'Number': [1]}]}), {'Number': [1]}),\
(({'x':{'Number':[123]}, 'y':{'Number':[123]}}, {'Plus': [{'Variable': ['x']}, {'Variable': ['y']}]}), {'Number': [246]}),\
])
def testExecProgram(env, stmt):
return execProgram(env, stmt)[1]
print("Problem #1, part (c), execProgram()...")
try: execProgram
except: print("The execProgram() function is not defined.")
else: check('testExecProgram', [\
(({'y':'False'}, {'Print': [{'Not':[{'Variable':['y']}]}, 'End']}), ['True']),\
(({}, {'If': ['True', {'Print': [{'Number': [4]}, 'End']}, 'End']}), [{'Number':[4]}]),\
(({}, {'Assign': [{'Variable': ['x']}, 'False', {'Until': [{'Not': [{'Variable': ['x']}]}, {'Print': ['False', 'End']}, {'Print': ['True', 'End']}]}]}), ['True']),\
(({}, {'If': ['True', {'Print': ['True', 'End']}, {'Print': ['False', 'End']}]}), ['True', 'False']),\
(({}, {'Procedure': [{'Variable': ['example']}, {'Print': [{'Number': [4]}, 'End']}, {'Call': [{'Variable': ['example']}, 'End']}]}), [{'Number':[4]}]),\
(({}, {'Assign': [{'Variable': ['x']}, {'Number': [123]}, {'Procedure': [{'Variable': ['example']}, {'Print': [{'Variable': ['x']}, 'End']}, {'Call': [{'Variable': ['example']}, {'Call': [{'Variable': ['example']}, 'End']}]}]}]}), [{'Number':[123]}, {'Number':[123]}]),\
(({}, {'Procedure': [{'Variable': ['g']}, {'Print': [{'Number': [2]}, 'End']}, {'Procedure': [{'Variable': ['f']}, {'Call': [{'Variable': ['g']}, {'Print': [{'Number': [1]}, {'Call': [{'Variable': ['g']}, 'End']}]}]}, {'Call': [{'Variable': ['f']}, 'End']}]}]}), [{'Number':[2]}, {'Number':[1]}, {'Number':[2]}]),\
])
print("Problem #1, interpret()...")
try: interpret
except: print("The interpret() function is not defined.")
else: check('interpret', [\
(("print 123;",), [{'Number':[123]}]),\
(("print false; print true; print 4;",), ['False', 'True', {'Number':[4]}]),\
(("x := 10; print x;",), [{'Number':[10]}]),\
(("x := 10; print x + x;",), [{'Number':[20]}]),\
(("x := 1; y := 2; z := 3; print x + y + z;",), [{'Number':[6]}]),\
(("print true and false;",), ['False']),\
(("x := true xor false; print x;",), ['True']),\
(("x := true xor false; print x and not(x);",), ['False']),\
(("x := true; y := false; z := true; print (not(x) xor y) and z;",), ['False']),\
(("if true {print 4;}",), [{'Number':[4]}]),\
(("if true {print true;} print false;",), ['True','False']),\
(("x := false; until not(x) {print false;} print true;",), ['True']),\
(("if true {print true;} print false;",), ['True','False']),\
(("procedure f { } print 4;",), [{'Number':[4]}]),\
(("procedure example {print 4;} call example;",), [{'Number':[4]}]),\
(("x := 123; procedure example {print x;} call example; call example;",), [{'Number':[123]},{'Number':[123]}]),\
(("procedure g {print 2;} procedure f {call g; print 1; call g;} call f;",), [{'Number':[2]},{'Number':[1]},{'Number':[2]}]),\
(("procedure g {print 2;} if true and true { call g; }",), [{'Number':[2]}]),\
(("procedure g {print 2;} procedure f {if true and true { call g; }} call g; call f;",), [{'Number':[2]},{'Number':[2]}]),\
(("procedure h {print 3;} procedure g {print 2; call h; call h;} procedure f {call g; print 1; call g;} call f;",), [{'Number':[2]},{'Number':[3]},{'Number':[3]},{'Number':[1]},{'Number':[2]},{'Number':[3]},{'Number':[3]}]),\
])
def testIncrement(addr, initial):
return simulate(['set '+str(addr)+' '+str(initial)] + increment(addr) + copy(addr,5))
print("Problem #2, part (a), increment()...")
try: increment
except: print("The increment() function is not defined.")
else: check('testIncrement', [\
((9, 123), [124]),\
((20, 234), [235]),\
((100, 0), [1]),\
((1000, 1), [2]),\
])
def testCompile(s):
return simulate(compile(s))
print("Problem #3, part (d), compile()...")
try: compile
except: print("The compile() function is not defined.")
else: check('testCompile', [\
(("print 123;",), [123]),\
(("print false; print true; print 4;",), [0, 1, 4]),\
(("x := 10; print x;",), [10]),\
(("x := 10; print x + x;",), [20]),\
(("x := 1; y := 2; z := 3; print x + y + z;",), [6]),\
(("print true and false;",), [0]),\
(("x := true xor false; print x;",), [1]),\
(("x := true xor false; print x and not(x);",), [0]),\
(("x := true; y := false; z := true; print (not(x) xor y) and z;",), [0]),\
(("if true {print 4;}",), [4]),\
(("if true {print true;} print false;",), [1,0]),\
(("x := false; until not(x) {print false;} print true;",), [1]),\
(("x := false; until x { x := true; } print false;",), [0]),\
(("x := false; y := false; until x {print false; x := y and true; if not(y) {y := true;} } print true;",), [0, 0, 1]),\
(("if true {print true;} print false;",), [1,0]),\
(("procedure f { } print 4;",), [4]),\
(("procedure example {print 4;} call example;",), [4]),\
(("x := 123; procedure example {print x;} call example; call example;",), [123,123]),\
(("procedure g {print 2;} procedure f {call g; print 1; call g;} call f;",), [2,1,2]),\
(("procedure g {print 2;} if true and true { call g; }",), [2]),\
(("procedure g {print 2;} procedure f {if true and true { call g; }} call g; call f;",), [2,2]),\
(("procedure h {print 3;} procedure g {print 2; call h; call h;} procedure f {call g; print 1; call g;} call f;",), [2,3,3,1,2,3,3]),\
])
#eof