-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrainFuckInterpreter.py
71 lines (53 loc) · 1.83 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
#!/usr/bin/env python
import unittest
class BrainfuckInterpreter( object ):
def __init__( self ):
self.output = ""
self.inputPointer = 0
self.instructionPointer = 0
def run( self, program, input = "" ):
data = "\0"
for self.instructionPointer in range(len(program)):
if program[self.instructionPointer] == ".":
self.output += data
if program[self.instructionPointer] == ",":
data = str(input[self.inputPointer])
self.inputPointer +=1
return self.output
class BrainfuckInterpreterTest(unittest.TestCase):
def testTruth(self):
self.assertEquals(True,True)
def testEmptyProgram(self):
interpreter = BrainfuckInterpreter()
result = interpreter.run( "" )
self.assertEquals( "", result )
def testLittleOutputProgram(self):
interpreter = BrainfuckInterpreter()
result = interpreter.run( "." )
self.assertEquals( "\0", result )
def testOutput2Characters(self):
interpreter = BrainfuckInterpreter()
result = interpreter.run( ".." )
self.assertEquals( "\0\0", result )
def testInputandOutput(self):
interpreter = BrainfuckInterpreter()
result = interpreter.run( ",.", "a" )
self.assertEquals( "a", result )
def testInputAndOutputAndInputAndOutput(self):
interpreter = BrainfuckInterpreter()
result = interpreter.run( ",..,..", "ab" )
self.assertEquals( "aabb", result )
def testInputAndOutputAndInputAndOutput2(self):
interpreter = BrainfuckInterpreter()
result = interpreter.run( ",..,..", "xy" )
self.assertEquals( "xxyy", result )
def testInputWithoutOutput(self):
interpreter = BrainfuckInterpreter()
result = interpreter.run( ",", "a" )
self.assertEquals( "", result )
def testInputAndInputWithoutOutput(self):
interpreter = BrainfuckInterpreter()
result = interpreter.run( ",,", "ab" )
self.assertEquals( "", result )
if __name__ == "__main__":
unittest.main()