-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW5Tests.py
166 lines (151 loc) · 6.25 KB
/
HW5Tests.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
#!/usr/bin/env python3
import unittest
from HW5 import *
class HW5SampleTests(unittest.TestCase):
def setUp(self):
clearBoth() #clear both stacks
dictstack.append((0,{}))
def test_input1Static(self):
testinput1 = """
/x 4 def
/g { x stack } def
/f { /x 7 def g } def
f
"""
opstackOutput = [4]
dictstackOutput = [(0, {'/x': 4, '/g': ['x', 'stack'], '/f': ['/x', 7, 'def', 'g']})]
interpreter(testinput1,"static")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
def test_input1Dynamic(self):
testinput1 = """
/x 4 def
/g { x stack } def
/f { /x 7 def g } def
f
"""
opstackOutput = [7]
dictstackOutput = [(0, {'/x': 4, '/g': ['x', 'stack'], '/f': ['/x', 7, 'def', 'g']})]
interpreter(testinput1,"dynamic")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
def test_input2Static(self):
testinput2 = """
/m 50 def
/n 100 def
/egg1 {/m 25 def n} def
/chic
{/n 1 def
/egg2 { n stack} def
m n
egg1
egg2
} def
n
chic
"""
opstackOutput = [100, 50, 1, 100, 1]
dictstackOutput = [(0, {'/m': 50, '/n': 100, '/egg1': ['/m', 25, 'def', 'n'], '/chic': ['/n', 1, 'def', '/egg2', ['n', 'stack'], 'def', 'm', 'n', 'egg1', 'egg2']})]
interpreter(testinput2,"static")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
def test_input2Dynamic(self):
testinput2 = """
/m 50 def
/n 100 def
/egg1 {/m 25 def n} def
/chic
{/n 1 def
/egg2 { n stack} def
m n
egg1
egg2
} def
n
chic
"""
opstackOutput = [100, 50, 1, 1, 1]
dictstackOutput = [(0, {'/m': 50, '/n': 100, '/egg1': ['/m', 25, 'def', 'n'], '/chic': ['/n', 1, 'def', '/egg2', ['n', 'stack'], 'def', 'm', 'n', 'egg1', 'egg2']})]
interpreter(testinput2,"dynamic")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
def test_input3Static(self):
testinput3 = """
/x 10 def
/A { x } def
/C { /x 40 def A stack } def
/B { /x 30 def /A { x } def C } def
B
"""
opstackOutput = [10]
dictstackOutput = [(0, {'/x': 10, '/A': ['x'], '/C': ['/x', 40, 'def', 'A', 'stack'], '/B': ['/x', 30, 'def', '/A', ['x'], 'def', 'C']})]
interpreter(testinput3,"static")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
def test_input3Dynamic(self):
testinput3 = """
/x 10 def
/A { x } def
/C { /x 40 def A stack } def
/B { /x 30 def /A { x } def C } def
B
"""
opstackOutput = [40]
dictstackOutput = [(0, {'/x': 10, '/A': ['x'], '/C': ['/x', 40, 'def', 'A', 'stack'], '/B': ['/x', 30, 'def', '/A', ['x'], 'def', 'C']})]
interpreter(testinput3,"dynamic")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
def test_input4Static(self):
testinput4 = """
/out true def
/xand { true eq {pop false} {true eq { false } { true } ifelse} ifelse dup /x exch def stack} def
/myput { out dup /x exch def xand } def
/f { /out false def myput } def
false f
"""
opstackOutput = [False]
dictstackOutput = [(0, {'/out': True, '/xand': [True, 'eq', ['pop', False], [True, 'eq', [False], [True], 'ifelse'], 'ifelse', 'dup', '/x', 'exch', 'def', 'stack'], '/myput': ['out', 'dup', '/x', 'exch', 'def', 'xand'], '/f': ['/out', False, 'def', 'myput']})]
interpreter(testinput4,"static")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
def test_input4Dynamic(self):
testinput4 = """
/out true def
/xand { true eq {pop false} {true eq { false } { true } ifelse} ifelse dup /x exch def stack} def
/myput { out dup /x exch def xand } def
/f { /out false def myput } def
false f
"""
opstackOutput = [True]
dictstackOutput = [(0, {'/out': True, '/xand': [True, 'eq', ['pop', False], [True, 'eq', [False], [True], 'ifelse'], 'ifelse', 'dup', '/x', 'exch', 'def', 'stack'], '/myput': ['out', 'dup', '/x', 'exch', 'def', 'xand'], '/f': ['/out', False, 'def', 'myput']})]
interpreter(testinput4,"dynamic")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
def test_input5Static(self):
testinput5 = """
/x [1 2 3 4] def
/A { x length } def
/C { /x [10 20 30 40 50 60] def A stack } def
/B { /x [6 7 8 9] def /A { x 0 get} def C } def
B
"""
opstackOutput = [4]
dictstackOutput = [(0, {'/x': (4, [1, 2, 3, 4]), '/A': ['x', 'length'], '/C': ['/x', (6, [10, 20, 30, 40, 50, 60]), 'def', 'A', 'stack'], '/B': ['/x', (4, [6, 7, 8, 9]), 'def', '/A', ['x', 0, 'get'], 'def', 'C']})]
interpreter(testinput5,"static")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
def test_input5Dynamic(self):
testinput5 = """
/x [1 2 3 4] def
/A { x length } def
/C { /x [10 20 30 40 50 60] def A stack } def
/B { /x [6 7 8 9] def /A { x 0 get} def C } def
B
"""
opstackOutput = [10]
dictstackOutput = [(0, {'/x': (4, [1, 2, 3, 4]), '/A': ['x', 'length'], '/C': ['/x', (6, [10, 20, 30, 40, 50, 60]), 'def', 'A', 'stack'], '/B': ['/x', (4, [6, 7, 8, 9]), 'def', '/A', ['x', 0, 'get'], 'def', 'C']})]
interpreter(testinput5,"dynamic")
self.assertEqual(opstack,opstackOutput)
self.assertEqual(dictstack,dictstackOutput)
if __name__ == '__main__':
unittest.main()