-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceptance_tests.py
341 lines (302 loc) · 11.2 KB
/
acceptance_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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Tento testovací skript předpokládá, že výkonná část interpretru je uložena v adresáři „brainx“ (a tudíž volána pomocí „__main__.py“). Souhrnně je adresářová struktura semestrálky následující:
semestrálka.kostra/
brainx/
__main__.py
...
tests/
...
README.txt
tests.py
Postupně se volají následující testy:
# memory tests
brainx "[-]" -m b'\x03\x02' -p 1 -t
brainx "[[-]<]" -m b'\x03\x03\x00\x02\x02' -p 4 -t
brainx "[<]" -m b'\x03\x03\x00\x02\x02' -p 4 -t
brainx "[>]" -m b'\x03\x03\x00\x02\x02' -t
brainx "[>+<-]" -m b'\x03\x03' -t
brainx "[>+>+<<-]>>[<<+>>-]" -m b'\x03\x03' -t
brainx "[>-<-]" -m b'\x03\x05' -t
# basic brainfuck tests
brainx tests/hello1.b
brainx tests/hello2.b
brainx -t tests/hello2.b
brainx tests/hello2t.b
# brainfuck with input
brainx tests/numwarp_input.b
# basic PNG
brainx tests/sachovnice.jpg
brainx tests/sachovnice_paleta.png
# brainloller
brainx tests/HelloWorld.png
brainx -t tests/HelloWorld.png
"""
import subprocess
#
# memory tests
#
# test 0a
print('\n\nTest 0a: brainx "[-]" -m b\'\\x03\\x02\' -p 1 -t')
print('\tvynulování aktuální, ale pouze aktuální, buňky')
args = ['python3', 'brainx', '"[-]"', '-m', r"b'\x03\x02'", '-p', '1', '-t']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output.replace(b'\r', b'') == b''
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/memory01_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_01.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
# test 0b
print('\n\nTest 0b: brainx "[[-]<]" -m b\'\\x03\\x03\\x00\\x02\\x02\' -p 4 -t')
print('\tvynulování všech nenulových buněk doleva')
args = ['python3', 'brainx', '"[[-]<]"', '-m', r"b'\x03\x03\x00\x02\x02'", '-p', '4', '-t']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output.replace(b'\r', b'') == b''
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/memory02_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_02.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
# test 0c
print('\n\nTest 0c: brainx "[<]" -m b\'\\x03\\x03\\x00\\x02\\x02\' -p 4 -t')
print('\tpřesun na první nenulovou buňku doleva')
args = ['python3', 'brainx', '"[<]"', '-m', r"b'\x03\x03\x00\x02\x02'", '-p', '4', '-t']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output.replace(b'\r', b'') == b''
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/memory03_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_03.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
# test 0d
print('\n\nTest 0d: brainx "[>]" -m b\'\\x03\\x03\\x00\\x02\\x02\' -t')
print('\tpřesun na první nenulovou buňku doprava')
args = ['python3', 'brainx', '"[>]"', '-m', r"b'\x03\x03\x00\x02\x02'", '-t']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output.replace(b'\r', b'') == b''
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/memory04_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_04.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
# test 0e
print('\n\nTest 0e: brainx "[>+<-]" -m b\'\\x03\\x03\' -t')
print('\tdestruktivní přičtení aktuální buňky k buňce následující')
args = ['python3', 'brainx', '"[>+<-]"', '-m', r"b'\x03\x03'", '-t']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output.replace(b'\r', b'') == b''
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/memory05_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_05.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
# test 0f
print('\n\nTest 0f: brainx "[>+>+<<-]>>[<<+>>-]" -m b\'\\x03\\x03\' -t')
print('\tnedestruktivní přičtení aktuální buňky k buňce následující')
args = ['python3', 'brainx', '"[>+>+<<-]>>[<<+>>-]"', '-m', r"b'\x03\x03'", '-t']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output.replace(b'\r', b'') == b''
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/memory06_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_06.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
# test 0g
print('\n\nTest 0g: brainx "[>-<-]" -m b\'\\x03\\x05\' -t')
print('\tdestruktivní odečtení aktuální buňky od buňky následující')
args = ['python3', 'brainx', '"[>-<-]"', '-m', r"b'\x03\x05'", '-t']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output.replace(b'\r', b'') == b''
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/memory07_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_07.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
#
# basic brainfuck tests
#
# test 1
print('\n\nTest 1: brainx tests/hello1.b')
print('\tHelloWorld s \\n')
args = ['python3', 'brainx', 'tests/hello1.b']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output.replace(b'\r', b'') == b'Hello World!\n'
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
# test 2a
print('\n\nTest 2a: brainx tests/hello2.b')
print('\tHelloWorld bez \\n')
args = ['python3', 'brainx', 'tests/hello2.b']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output == b'Hello World!'
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
# test 2b
print('\n\nTest 2b: brainx -t tests/hello2.b')
print('\tHelloWorld bez \\n plus log')
args = ['python3', 'brainx', '-t', 'tests/hello2.b']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output == b'Hello World!'
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/hello2_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_08.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
# test 2c
print('\n\nTest 2c: brainx tests/hello2t.b')
print('\tHelloWorld bez \\n plus dva průběžné logy')
args = ['python3', 'brainx', 'tests/hello2t.b']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output == b'Hello World!'
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/hello2t_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_09.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
with open('tests/hello2t_debug_02.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_10.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
#
# brainfuck with input
#
# test 3
print('\n\nTest 3: brainx tests/numwarp_input.b')
print('\tnumwarp.b pro vstup "123"')
args = ['python3', 'brainx', 'tests/numwarp_input.b']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output.replace(b'\r', b'') == b' /\\\n /\\\n /\\ /\n / \n \\ \\/\n \\\n \n'
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
#
# basic PNG
#
# test 4a
print('\n\nTest 4a: brainx tests/sachovnice.jpg')
print('\tumíme jen PNG')
args = ['python3', 'brainx', 'tests/sachovnice.jpg']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output == b''
print( "return code:", p.returncode )
assert p.returncode == 4
#print( "error:", error )
assert b'PNGWrongHeaderError' in error
# test 4b
print('\n\nTest 4b: brainx tests/sachovnice_paleta.png')
print('\tumíme jen některá PNG')
args = ['python3', 'brainx', 'tests/sachovnice_paleta.png']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output == b''
print( "return code:", p.returncode )
assert p.returncode == 8
#print( "error:", error )
assert b'PNGNotImplementedError' in error
#
# brainloller
#
# test 5a
print('\n\nTest 5a: brainx tests/HelloWorld.png')
print('\tnačtení dat z obrázku HelloWorld.png')
args = ['python3', 'brainx', 'tests/HelloWorld.png']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
assert output == b'Hello World!'
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
# test 5b
print('\n\nTest 5b: brainx -t tests/HelloWorld.png')
print('\tnačtení dat z obrázku HelloWorld.png plus log')
args = ['python3', 'brainx', '-t', 'tests/HelloWorld.png']
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
print( "output:", output )
#assert output == b'Hello World!'
print( "return code:", p.returncode )
assert p.returncode == 0
#print( "error:", error )
assert error == b''
with open('tests/HelloWorld_debug_01.log', mode='r', encoding='ascii') as f:
txt_in = f.read()
with open('debug_11.log', mode='r', encoding='ascii') as f:
txt_out = f.read()
assert txt_in == txt_out
#
# everything went OK
#
print('\n\n', '-'*70, '\n', 'OK: All tests passed.')