-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
98 lines (81 loc) · 2.88 KB
/
test.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
import unittest
import machine
import commands
import io_handler
class MachineTest1(unittest.TestCase):
def setUp(self) -> None:
self.machine = machine.Machine()
self.io = io_handler.IOHandler()
commands.init(self.machine, self.io)
def machine_run(self, instr):
self.io.clear()
self.machine.set(instr)
self.machine.run()
def test_exit(self):
self.machine_run('@')
self.machine_run(' @')
pass
def test_move(self):
self.machine_run('>v\n @')
self.machine_run('< @')
pass
def test_const(self):
self.machine_run('123@')
self.assertEqual(self.machine.stack, [1,2,3])
def test_string(self):
self.machine_run('"hello, world!"@')
self.assertEqual(self.machine.stack, [ord(i) for i in 'hello, world!'])
def test_stack(self):
self.machine_run('1:@')
self.assertEqual(self.machine.stack, [1,1])
self.machine_run('123...@')
self.assertEqual(self.io.output, '3 2 1 ')
self.machine_run('"abc",,,@')
self.assertEqual(self.io.output, 'cba')
self.io.input = ['60','61','62']
self.machine_run('&&&,,,@')
self.assertEqual(self.io.output, '>=<')
def test_math(self):
self.machine_run('23+@')
self.assertEqual(self.machine.stack, [5])
self.machine_run('23-@')
self.assertEqual(self.machine.stack, [-1])
self.machine_run('23*@')
self.assertEqual(self.machine.stack, [6])
self.machine_run('53/@')
self.assertEqual(self.machine.stack, [1])
self.machine_run('53%@')
self.assertEqual(self.machine.stack, [2])
def test_logic(self):
self.machine_run('1!0!@')
self.assertEqual(self.machine.stack, [0,1])
self.machine_run('32`@')
self.assertEqual(self.machine.stack, [1])
self.machine_run('0>v \n ^_@')
self.machine_run('0v1 \n >| \n >@')
self.assertEqual(self.machine.stack, [])
def test_spec(self):
self.machine_run('00g,@')
self.assertEqual(self.io.output, '0')
self.machine_run('"@"00p@')
self.assertEqual(''.join(self.machine.instructions[0]), '@@"00p@')
class MachineTest2(unittest.TestCase):
def setUp(self) -> None:
self.machine = machine.Machine()
self.io = io_handler.IOHandler()
commands.init(self.machine, self.io)
def machine_run(self, instr):
self.io.clear()
self.machine.set(instr)
self.machine.run()
files = [
('hello.txt', 'Hello, World!'),
]
def test(self):
for file,answer in self.files:
with open(f'./tests/{file}', 'r') as f:
self.machine_run(f.read())
# print('\n'.join(self.io.outputLines))
self.assertEqual(self.io.output, answer)
if __name__ == "__main__":
unittest.main()