/
tests.py
55 lines (47 loc) · 1.88 KB
/
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
import unittest
import brainfuck
import sys
from StringIO import StringIO
def run_program(program, input = None):
old_stdout = sys.stdout
old_stdin = sys.stdin
try:
out = StringIO()
sys.stdout = out
if input is not None:
input = StringIO(input)
sys.stdin = input
brainfuck.brainfuck(program)
finally:
sys.stdout = old_stdout
sys.stdin = old_stdin
return out.getvalue().strip()
class TestInterpreter(unittest.TestCase):
def setUp(self):
brainfuck.set_cell_size()
def test_HelloWorld(self):
result = run_program("""
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.
+++++++..+++.>++.<<+++++++++++++++.>.+++.------.-
-------.>+.>.""")
self.assertEquals(result, "Hello World!")
def test_Squares(self):
result = run_program("""
++++[>+++++<-]>[<+++++>-]+<+[>[>+>+<<-]++>>[<<+>>
-]>>>[-]++>[-]+>>>+[[-]++++++>>>]<<<[[<++++++++<+
+>>-]+<.<[>----<-]<]<<[>>>>>[>>>[-]+++++++++<[>-<
-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""")
expected_result = "\n".join([str(x**2) for x in range(101)])
self.assertEquals(result, expected_result)
def test_ROT13(self):
result = run_program("""
-,+[-[>>++++[>++++++++<-]<+<-[>+>+>-[>>>]<[[>+<-]
>>+>]<<<<<-]]>>>[-]+>--[-[<->+++[-]]]<[++++++++++
++<[>-[>+>>]>[+[<+>-]>+>>]<<<<<-]>>[<+>-]>[-[-<<[
-]>>]<<[<<->>-]>>]<<[<<+>>-]]<[-]<.[-]<-,+]""", "applesauce")
self.assertEquals(result, "nccyrfnhpr")
def test_Clean(self):
self.assertRaises(Exception, brainfuck.clean, "[[]")
self.assertRaises(Exception, brainfuck.clean, "][")
if __name__ == '__main__':
unittest.main()