-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
286 lines (235 loc) · 8.1 KB
/
test.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import unittest
from whitespace import whitespace
class StackManipulationTest(unittest.TestCase):
def test_output_positive_numbers(self):
push_pop_print1 = ' \t\n\t\n \t\n\n\n'
push_pop_print2 = ' \t \n\t\n \t\n\n\n'
push_pop_print3 = ' \t\t\n\t\n \t\n\n\n'
push_pop_print0 = ' \n\t\n \t\n\n\n'
self.assertEqual(whitespace(push_pop_print1), '1')
self.assertEqual(whitespace(push_pop_print2), '2')
self.assertEqual(whitespace(push_pop_print3), '3')
self.assertEqual(whitespace(push_pop_print0), '0')
def test_output_negative__numbers(self):
push_pop_print_negative_1 = ' \t\t\n\t\n \t\n\n\n'
push_pop_print_negative_2 = ' \t\t \n\t\n \t\n\n\n'
push_pop_print_negative_3 = ' \t\t\t\n\t\n \t\n\n\n'
self.assertEqual(whitespace(push_pop_print_negative_1), '-1')
self.assertEqual(whitespace(push_pop_print_negative_2), '-2')
self.assertEqual(whitespace(push_pop_print_negative_3), '-3')
def test_output_letters(self):
output_a = ' \t \t\n\t\n \n\n\n'
output_b = ' \t \t \n\t\n \n\n\n'
output_c = ' \t \t\t\n\t\n \n\n\n'
self.assertEqual(whitespace(output_a), 'A')
self.assertEqual(whitespace(output_b), 'B')
self.assertEqual(whitespace(output_c), 'C')
def test_swap(self):
push_A = ' \t \t\n'
push_B = ' \t \t \n'
swap = ' \n\t'
print_output = '\t\n '
exit = '\n\n\n'
self.assertEqual(whitespace('{}{}{}{}{}{}'.format(
push_A, push_B, swap, print_output, print_output, exit)), 'AB')
def test_duplicate(self):
push_A = ' \t \t\n'
dup_A = ' \n '
print_output = '\t\n '
exit = '\n\n\n'
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push_A, dup_A, print_output, print_output, exit)), 'AA')
def test_discard_top(self):
push_A = ' \t \t\n'
push_B = ' \t \t \n'
discard_top = ' \n\n'
print_output = '\t\n '
exit = '\n\n\n'
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push_A, push_B, discard_top, print_output, exit)), 'A')
def test_duplicate_nth(self):
push1 = ' \t\n'
push2 = ' \t \n'
push3 = ' \t\t\n'
duplicate3 = '\t\n \t'
exit = '\n\n\n'
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push1, push2, push3, duplicate3, exit)), '3')
code = (
# push 0
' \t\n'
# push 2
' \t \n'
# push 3
' \t\t\n'
# duplicate the -1th value from the top
' \t \t\t\n'
# pop and print as number
'\t\n \t'
'\n\n\n'
)
with self.assertRaises(IndexError):
whitespace(code)
def test_discard_top_n(self):
code = (
# push -1
' \t\t\n'
# push 2
' \t \n'
# push 1
' \t\n'
# push 3
' \t\t\n'
# push 6
' \t\t \n'
# push 5
' \t \t\n'
# push 7
' \t\t\t\n'
# swap first two
' \n\t'
# discard top 3 below top
' \t\n \t\t\n'
# pop and print as number
'\t\n \t'
'\t\n \t'
'\t\n \t'
'\t\n \t'
'\n\n\n'
)
self.assertEqual(whitespace(code), '512-1')
class ArithmethicTest(unittest.TestCase):
def test_addition(self):
push1 = ' \t\n'
push2 = ' \t \n'
add = '\t '
print_output = '\t\n \t'
exit = '\n\n\n'
# 1 + 2
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push1, push2, add, print_output, exit)), '3')
push_negative_1 = ' \t\t\n'
# 1 + (-1)
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push1, push_negative_1, add, print_output, exit)), '0')
def test_subtraction(self):
push1 = ' \t\n'
push2 = ' \t \n'
sub = '\t \t'
print_output = '\t\n \t'
exit = '\n\n\n'
# 1 - 2
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push1, push2, sub, print_output, exit)), '-1')
# 2 - 1
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push2, push1, sub, print_output, exit)), '1')
def test_multiplication(self):
push2 = ' \t \n'
mult = '\t \n'
print_output = '\t\n \t'
exit = '\n\n\n'
# 2 * 2
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push2, push2, mult, print_output, exit)), '4')
push0 = ' \n'
# 2 * 0
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push0, push2, mult, print_output, exit)), '0')
def test_division(self):
push2 = ' \t \n'
push4 = ' \t \n'
div = '\t \t '
print_output = '\t\n \t'
exit = '\n\n\n'
# 4 / 2
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push4, push2, div, print_output, exit)), '2')
push0 = ' \n'
# 0 / 2
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push0, push2, div, print_output, exit)), '0')
# 2 / 0
with self.assertRaises(ZeroDivisionError):
whitespace('{}{}{}{}{}'.format(
push2, push0, div, print_output, exit))
def test_modulo(self):
push3 = ' \t\t\n'
push4 = ' \t \n'
mod = '\t \t\t'
print_output = '\t\n \t'
exit = '\n\n\n'
# 4 % 3
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push4, push3, mod, print_output, exit)), '1')
push0 = ' \n'
# 0 % 3
self.assertEqual(whitespace('{}{}{}{}{}'.format(
push0, push3, mod, print_output, exit)), '0')
# 3 % 0
with self.assertRaises(ZeroDivisionError):
whitespace('{}{}{}{}{}'.format(
push3, push0, mod, print_output, exit))
class HeapTest(unittest.TestCase):
def test_store_retrieve(self):
push3 = ' \t\t\n'
push4 = ' \t \n'
heap_store = '\t\t '
heap_retrieve = '\t\t\t'
print_output = '\t\n \t'
exit = '\n\n\n'
self.assertEqual(whitespace('{}{}{}{}{}{}{}'.format(
push3, push4, heap_store, push3,
heap_retrieve, print_output, exit)), '4')
class InputTest(unittest.TestCase):
def test_read_char(self):
inp = 'A'
push1 = ' \t\n'
read_char = '\t\n\t '
heap_retrieve = '\t\t\t'
print_output = '\t\n '
exit = '\n\n\n'
self.assertEqual(whitespace('{}{}{}{}{}{}'.format(
push1, read_char, push1, heap_retrieve,
print_output, exit), inp=inp), 'A')
def test_read_number(self):
inp = '12345\n'
push1 = ' \t\n'
read_number = '\t\n\t\t'
heap_retrieve = '\t\t\t'
print_output = '\t\n \t'
exit = '\n\n\n'
self.assertEqual(whitespace('{}{}{}{}{}{}'.format(
push1, read_number, push1, heap_retrieve,
print_output, exit), inp=inp), '12345')
inp = '2A\n'
self.assertEqual(whitespace('{}{}{}{}{}{}'.format(
push1, read_number, push1, heap_retrieve,
print_output, exit), inp=inp), '42')
class FlowControl(unittest.TestCase):
def test_subroutine(self):
code = (
# push 1
' \t\n'
# call subroutine sn
'\n \t \n'
# push 2
' \t \n'
# call subroutine sn
'\n \t \n'
# push 3
' \t\t\n'
# call subroutine sn
'\n \t \n'
# exit
'\n\n\n'
# mark location sn
'\n \n'
# pop and print as number
'\t\n \t'
# return
'\n\t\n'
)
self.assertEqual(whitespace(code), '123')
if __name__ == '__main__':
unittest.main()