-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_runner.py
executable file
·384 lines (304 loc) · 13.1 KB
/
test_runner.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python
#
# Copyright 2014 Brian Quinlan
# See "LICENSE" file for details.
"""Tests for the Brainfuck executable."""
from __future__ import absolute_import
import functools
import os
import os.path
import random
import subprocess
import tempfile
import time
import unittest
EXECUTABLE_PATH = os.path.join(os.curdir, 'bf')
def _check_datapointer_in_range(commands, restore_offset):
"""Verify that a sequence of brainfuck commands has an offset >= 0.
Args:
commands: The sequence of brainfuck commands e.g. "<>>>+-".
restore_offset: A bool indicating whether the memory offset after
the given sequence of commands must be exactly zero.
Raises:
AssertionError: if the memory offset of the given commands is negative
(or non-zero if restore_offset is True).
"""
offset = 0
for command in commands:
if command == '<':
offset -= 1
elif command == '>':
offset += 1
assert offset >= 0
if restore_offset:
assert offset == 0
def _repeat_for_seconds(seconds):
"""Repeat the decorated function for the given number of seconds."""
def repeat(func):
@functools.wraps(func)
def repeater(*args, **kwds):
end_time = time.time() + seconds
while time.time() < end_time:
func(*args, **kwds)
repeater.count += 1
repeater.count = 0
return repeater
return repeat
def run_brainfuck(args, stdin=''):
"""Run the Brainfuck executable.
Args:
args: A sequence containing the command line arguments to use when
invoking the executable e.g. ['--mode=i', 'example.b'].
stdin: The str that should be used for stdin.
Returns:
A 3-tuple of:
o the process return code as an int
o the string written to stdout by the process
o the string written to stderr by the process
"""
run = subprocess.Popen([EXECUTABLE_PATH] + args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdoutdata, stderrdata = run.communicate(stdin)
return run.returncode, stdoutdata, stderrdata
def generate_brainfuck_code_without_loops(
command_set,
num_commands,
restore_offset=False):
"""Generate Brainfuck code (without loops) using the given set of commands.
Args:
command_set: A sequence containing the commands to use e.g. '<>+-,.'.
May not contain '[' or ']'.
num_commands: The length of the returned string.
restore_offset: If True then there should be no net movement of the
data pointer when the code is finished exited.
Returns:
A sequence of Brainfuck commands from the given command_set e.g.
'+>-<,,.>><<'.
"""
assert '[' not in command_set
assert ']' not in command_set
possible_commands = list(command_set)
possible_commands_no_left = list(set(command_set) - set(['<']))
commands = []
offset = 0
while len(commands) < num_commands:
if restore_offset:
offset_fix = offset
if offset_fix and offset_fix >= len(commands) - num_commands + 1:
if offset > 0:
commands.extend('<' * offset_fix)
else:
commands.extend('>' * offset_fix)
offset = 0
continue
if offset:
command = random.choice(possible_commands)
else:
command = random.choice(possible_commands_no_left)
if (restore_offset and
num_commands - len(commands) == 1 and
command in '<>'):
continue
if command == '<':
offset -= 1
elif command == '>':
offset += 1
commands.append(command)
_check_datapointer_in_range(commands, restore_offset)
assert len(commands) == num_commands
assert set(commands) <= set(command_set)
return ''.join(commands)
_LOOP_TEMPLATE = '-[>%s<%s]'
_EMPTY_LOOP_TEMPLATE_LEN = len(_LOOP_TEMPLATE % ('', ''))
def generate_brainfuck_code(command_set, num_commands, max_loop_depth):
"""Generate Brainfuck code using the given set of commands.
Args:
command_set: A sequence containing the commands to use e.g. '<>+-,.[]'.
num_commands: The length of the returned string.
max_loop_depth: The maximim level of nesting for loops e.g. 2.
Returns:
A sequence of Brainfuck commands from the given command_set e.g.
'+--[>,.-[>>>>.<<<<-]<-]><'.
"""
commands_without_loops = ''.join(set(command_set) - set(['[', ']']))
if '[' not in command_set or ']' not in command_set:
max_loop_depth = 0
code_blocks = []
remaining_commands = num_commands
while remaining_commands:
if (max_loop_depth and
# Need one extra space to decrement the loop counter.
remaining_commands >= _EMPTY_LOOP_TEMPLATE_LEN + 1 and
random.choice(['loop', 'code']) == 'loop'):
decrement_space = remaining_commands - _EMPTY_LOOP_TEMPLATE_LEN
loop_decrement = '-' * random.randrange(1,
min(decrement_space+1, 256),
2)
max_loop_body_size = (remaining_commands -
_EMPTY_LOOP_TEMPLATE_LEN
- len(loop_decrement))
code_block = _LOOP_TEMPLATE % (
generate_brainfuck_code(
command_set,
random.randrange(max_loop_body_size + 1),
max_loop_depth - 1),
loop_decrement)
else:
code_block = generate_brainfuck_code_without_loops(
commands_without_loops,
random.randrange(remaining_commands+1),
restore_offset=True)
remaining_commands -= len(code_block)
code_blocks.append(code_block)
commands = ''.join(code_blocks)
assert len(commands) == num_commands, (
'len(commands) [%d] == num_commands [%d]' % (
len(commands), num_commands))
assert set(commands) <= set(command_set)
return commands
class TestExecutable(unittest.TestCase):
"""Tests the portions of the executable not related to BF interpretation."""
def test_no_arguments(self):
returncode, stdout, stderr = run_brainfuck(args=[])
self.assertEqual(returncode, 1)
self.assertIn('Usage', stdout)
self.assertIn('You need to specify exactly one Brainfuck file', stderr)
def test_help(self):
returncode, stdout, stderr = run_brainfuck(args=['-h'])
self.assertEqual(returncode, 0)
self.assertIn('Usage', stdout)
self.assertEqual(stderr, '')
def test_without_mode(self):
test_hello_world = os.path.join(os.curdir, 'examples', 'hello.b')
returncode, stdout, stderr = run_brainfuck(args=[test_hello_world])
self.assertEqual(returncode, 0)
self.assertEqual(stdout, 'Hello World!\n')
self.assertEqual(stderr, '')
def test_with_mode(self):
test_hello_world = os.path.join(os.curdir, 'examples', 'hello.b')
returncode, stdout, stderr = run_brainfuck(
args=['--mode=jit', test_hello_world])
self.assertEqual(returncode, 0)
self.assertEqual(stdout, 'Hello World!\n')
self.assertEqual(stderr, '')
def test_with_bad_mode(self):
test_hello_world = os.path.join(os.curdir, 'examples', 'hello.b')
returncode, stdout, stderr = run_brainfuck(
args=['--mode=badmode', test_hello_world])
self.assertEqual(returncode, 1)
self.assertEqual(stdout, '')
self.assertIn('Unexpected mode: --mode=badmode', stderr)
def test_with_bad_flag(self):
test_hello_world = os.path.join(os.curdir, 'examples', 'hello.b')
returncode, stdout, stderr = run_brainfuck(
args=['--flag=unknown', test_hello_world])
self.assertEqual(returncode, 1)
self.assertEqual(stdout, '')
self.assertIn('Unexpected argument: --flag=unknown', stderr)
def test_with_mode_no_file(self):
returncode, stdout, stderr = run_brainfuck(args=['--mode=jit'])
self.assertEqual(returncode, 1)
self.assertIn('Usage', stdout)
self.assertIn('You need to specify exactly one Brainfuck file', stderr)
class BrainfuckRunnerTestMixin(object):
"""A abstract class for testing a brainfuck execution mode.
Subclasses must define a "MODE" class variable corresponding to their mode
flag e.g. "jit".
"""
MODE = None
@classmethod
def run_brainfuck(cls, brainfuck_example, stdin=None):
test_brainfuck_path = os.path.join(
os.curdir, 'examples', brainfuck_example)
return run_brainfuck(
['--mode=%s' % cls.MODE, test_brainfuck_path], stdin)
def test_hello_world(self):
returncode, stdout, stderr = self.run_brainfuck('hello.b')
self.assertEqual(returncode, 0)
self.assertEqual(stdout, 'Hello World!\n')
self.assertEqual(stderr, '')
def test_cat(self):
returncode, stdout, stderr = self.run_brainfuck(
'cat.b',
stdin='This should be echoed!')
self.assertEqual(returncode, 0)
self.assertEqual(stdout, 'This should be echoed!')
self.assertEqual(stderr, '')
def test_unbalanced_block(self):
returncode, stdout, stderr = self.run_brainfuck('unbalanced_block.b')
self.assertEqual(returncode, 1)
self.assertEqual(stdout, '')
self.assertIn('Unable to find loop end in block starting with: [++',
stderr)
def test_extra_block_end(self):
returncode, stdout, stderr = self.run_brainfuck('extra_block_end.b')
self.assertEqual(returncode, 0)
self.assertEqual(stdout, 'Hello World!\n')
self.assertEqual(stderr, '')
def test_empty(self):
returncode, stdout, stderr = self.run_brainfuck('empty.b')
self.assertEqual(returncode, 0)
self.assertEqual(stdout, '')
self.assertEqual(stderr, '')
def test_regression1(self):
returncode, stdout, stderr = self.run_brainfuck(
'regression1.b', stdin='mM')
self.assertEqual(returncode, 0)
self.assertEqual(stdout, 'Mm')
self.assertEqual(stderr, '')
def test_regression2(self):
returncode, stdout, stderr = self.run_brainfuck('regression2.b')
self.assertEqual(returncode, 0)
self.assertEqual(stdout, 'Hello World!\n')
self.assertEqual(stderr, '')
# pylint: disable=too-few-public-methods
class TestCompileAndGo(unittest.TestCase, BrainfuckRunnerTestMixin):
MODE = 'cag'
# pylint: disable=too-few-public-methods
class TestInterpreter(unittest.TestCase, BrainfuckRunnerTestMixin):
MODE = 'i'
# pylint: disable=too-few-public-methods
class TestJIT(unittest.TestCase, BrainfuckRunnerTestMixin):
MODE = 'jit'
class ConsistentOutputTest(unittest.TestCase):
"""Check that the various BrainfuckRunners produce consistent output."""
@staticmethod
def _generate_input(length):
return ''.join([chr(random.randrange(256)) for _ in range(length)])
def _check_consistency_with_code(self, brainfuck_code, brainfuck_input):
with tempfile.NamedTemporaryFile(
suffix='.b', delete=False) as brainfuck_source_file:
brainfuck_source_file.write(brainfuck_code)
brainfuck_source_file.close()
stdouts = []
for klass in [TestCompileAndGo, TestInterpreter, TestJIT]:
returncode, stdout, stderr = klass.run_brainfuck(
brainfuck_source_file.name, stdin=brainfuck_input)
self.assertEqual(returncode, 0)
self.assertEqual(stderr, '')
stdouts.append((klass, stdout))
reference_klass, reference_stdout = stdouts[0]
self.longMessage = True # pylint: disable=invalid-name
for klass, stdout in stdouts[1:]:
self.assertSequenceEqual(
reference_stdout, stdout,
'output for %s does not match output for %s for file %s' % (
reference_klass.__name__,
klass.__name__,
brainfuck_source_file.name))
os.unlink(brainfuck_source_file.name)
@_repeat_for_seconds(2)
def test_consistency_with_random_no_loop_input(self):
brainfuck_code = generate_brainfuck_code_without_loops(
'+-<>,.', 80)
# Can't require more input than the length of the code (without loops).
brainfuck_input = self._generate_input(len(brainfuck_code))
self._check_consistency_with_code(brainfuck_code, brainfuck_input)
@_repeat_for_seconds(2)
def test_consistency_with_random_loop_input(self):
brainfuck_code = generate_brainfuck_code('<>+-[].', 80, 2)
self._check_consistency_with_code(brainfuck_code, '')
if __name__ == '__main__':
unittest.main()