-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrainfuckInterpreter.py
101 lines (79 loc) · 3.09 KB
/
BrainfuckInterpreter.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
import unittest
class BrainfuckInterpreter:
def __init__ (self):
self._cells = [0] * 30000
self._output = ""
self.dataPointer = 0
def output(self):
return self._output
def cells(self):
return self._cells
def eval(self, commands) :
for nextCommandChar in commands:
nextCommand = self.__commands.get(nextCommandChar, lambda x: x)
nextCommand( self )
def commandPlus(self):
self._cells[self.dataPointer] += 1
def commandMinus(self):
self._cells[self.dataPointer] -= 1
self._cells[self.dataPointer] %= 256
def commandDot(self):
self._output += str(chr(self._cells[self.dataPointer]))
def commandGreat(self):
self.dataPointer += 1
def commandLess(self):
self.dataPointer -= 1
__commands = {
"+": commandPlus,
"-": commandMinus,
".": commandDot,
">": commandGreat,
"<": commandLess,
}
class BrainfuckInterpreterTest(unittest.TestCase):
def test_the_truth(self):
self.assertEquals(True, True)
def test_plus_as_a_program_increase_the_first_cell_by_one(self):
myInterpreter = BrainfuckInterpreter()
before = myInterpreter.cells()[0]
myInterpreter.eval('+')
after = myInterpreter.cells()[0]
expected = before+1
self.assertEquals(expected, after)
def test_minus_as_a_program_decrease_the_first_cell_by_one(self):
myInterpreter = BrainfuckInterpreter()
before = myInterpreter.cells()[0]
myInterpreter.eval("-")
after = myInterpreter.cells()[0]
expected = (before - 1) % 256
self.assertEquals(expected, after)
def test_dot_as_a_program_output_null(self):
myInterpreter = BrainfuckInterpreter()
myInterpreter.eval(".")
self.assertEquals("\0",myInterpreter.output())
def test_plus_dot_as_program_output_backslash_1(self):
myInterpreter = BrainfuckInterpreter()
myInterpreter.eval("+.")
self.assertEquals("\1", myInterpreter.output())
def test_great_plus_as_program_increase_the_second_cell_by_one(self):
myInterpreter = BrainfuckInterpreter()
before = myInterpreter.cells()[1]
myInterpreter.eval('>+')
after = myInterpreter.cells()[1]
expected = before + 1
self.assertEquals(expected, after)
def test_great_plus_dot_as_program_output_backslash_one(self):
myInterpreter = BrainfuckInterpreter()
myInterpreter.eval('>+.')
self.assertEquals("\1", myInterpreter.output())
def test_hello_as_a_program_do_nothing_and_output_nothing(self):
myInterpreter = BrainfuckInterpreter()
myInterpreter.eval('hello')
self.assertEquals("", myInterpreter.output())
self.assertEquals([0] * 30000, myInterpreter.cells())
def test_great_less_plus_program_first_cell_is_1(self):
myInterpreter = BrainfuckInterpreter()
myInterpreter.eval('><+')
self.assertEquals(1, myInterpreter.cells()[0])
if __name__ == "__main__":
unittest.main()