test_goto_interpreter.py 7.54 KB
Newer Older
1
2
3
4
5
6
from gotointerpreter import interpret
from unittest import mock
from unittest import TestCase


def input_continue(prompt):
7
    return ''
8
9
10


def input_exit(prompt):
11
    return 'EXIT'
12
13
14
15


class GOTOInterpreterTest(TestCase):
    def test_assignment_default_zero(self):
16
17
18
        self.assertEqual(0, interpret('M1:x0:=x0 + 0;M2:HALT;'))
        self.assertEqual(0, interpret('M1:x0:=x1 + 0;M2:HALT;'))
        self.assertEqual(0, interpret('M1:x0:=x2 + 0;M2:HALT;'))
19
20

    def test_assignment_non_negative(self):
21
22
23
        self.assertEqual(0, interpret('M1:x0:=x0-1;M2:HALT;'))
        self.assertEqual(0, interpret('M1:x0:=x1-1;M2:HALT;'))
        self.assertEqual(0, interpret('M1:x0:=x2-6;M2:HALT;'))
24
25

    def test_assignment_number(self):
26
27
28
        self.assertEqual(5, interpret('M1:x0:=5;M2:HALT;'))
        self.assertEqual(2, interpret('M1:x0:=2;M2:HALT;'))
        self.assertEqual(3, interpret('M1:x0:=3;M2:HALT;'))
29
30

    def test_assignment_variable(self):
31
32
33
34
        self.assertEqual(1, interpret('M1:x0:=x0+1;M2:HALT;'), 1)
        self.assertEqual(4, interpret('M1:x0:= 5; M2:x0:=x0-1; M3:HALT;'))
        self.assertEqual(1, interpret('M1:x0:=x1-1; M2:x0:=x0+1; M3:HALT;'))
        self.assertEqual(3, interpret('M1:x0:=4; M2:x0:= x0 − 1; M3:HALT;'))
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

    def test_assignment_wrong_syntax(self):
        with self.assertRaises(SyntaxError):
            interpret('M1:x1:=x2;M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:x1:=0+x2;M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:x5:=-1+x4;M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:x5:=-x3+x1;M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:x5:=x1-x3;M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:x2:=x1+x4;M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:x2:=x1+2;;M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:x1:=c;M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:xi:=2;M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:x0:=xj+1;M2:HALT;')

    def test_goto_skip_lines(self):
59
60
61
        self.assertEqual(2, interpret('M1:x0:=2;M2:GOTO M4;M3:x0:=0;M4:HALT;'))
        self.assertEqual(3, interpret('M1:x0:=3;M2:GOTO M5;M3:GOTO M1;M4:x0:=5;M5:HALT;'))
        self.assertEqual(0, interpret('M1:GOTO M3;M2:x0:=x2+5;M3:HALT;'))
62
63

    def test_if(self):
64
65
66
67
68
        self.assertEqual(10, interpret('M1:x2:=10;' +
                                       'M2:IF x2=0 THEN GOTO M6;' +
                                       'M3:x0:=x0+1;M4:x2:=x2-1;' +
                                       'M5:GOTO M2; M6:HALT;'))
        self.assertEqual(1, interpret('M1:IF x0=5 THEN GOTO M4;M2:IF x0=2 THEN GOTO M4;M3:x0:=x0+1;M4:HALT;'))
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    def test_syntax_missing_operator(self):
        with self.assertRaises(SyntaxError):
            interpret('M1:x0:=x1 2;M2:HALT;')

    def test_markers_start_with_one(self):
        with self.assertRaises(SyntaxError):
            interpret('M0:x0:=2;M1:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M2:x0:=2;M3:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M10:x0:=2;M11:HALT;')

    def test_markers_increment_by_one(self):
        with self.assertRaises(SyntaxError):
            interpret('M1:x0:=3;M3:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:x0:=2;M1:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M3; M3:HALT;')

    def test_linebreaks(self):
91
        self.assertEqual(0, interpret('''
92
93
94
        M1:x1:=2;
        M2: IF x1=2 THEN GOTO M4;
        M3:HALT;
95
        M4:GOTO M3;'''))
96
97
98

    @mock.patch('loopinterpreter.input', side_effect=input_continue)
    def test_break(self, custom_input):
99
        self.assertEqual(2, interpret('M1:x0:=2;BREAK M2:HALT;'))
100
101
102

    @mock.patch('loopinterpreter.input', side_effect=input_exit)
    def test_break_exit(self, custom_input):
103
        self.assertEqual(-1, interpret('M1:GOTO M1; BREAK'))
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

    def test_assignment_missing_tokens(self):
        with self.assertRaises(SyntaxError):
            interpret('M1: x0')
        with self.assertRaises(SyntaxError):
            interpret('M1: x1 x2+2;')
        with self.assertRaises(SyntaxError):
            interpret('M1: x1 := x2+2 M2')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M3; M2:x1 x2+0; M3: HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2:x1:=2 M3: HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2: x1:= M3: HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M3; M2: x1 := x2 M3: HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M3; M2: x1 := x2 - M3: HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M3; M2: x1:= x2 + 1 M3: HALT;')
124

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    def test_goto_missing_tokens(self):
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO :HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M2' +
                      'M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2: GOTO :HALT; M3: HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M3;' +
                      'M2: GOTO M3' +
                      'M3:HALT;')

    def test_goto_jump_not_defined(self):
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M2;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2: GOTO M4; M3:HALT;')
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
    def test_halt_missing_tokens(self):
        with self.assertRaises(SyntaxError):
            interpret('M1:HALT M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2: HALT M3:HALT;')

    def test_if_missing_tokens(self):
        with self.assertRaises(SyntaxError):
            interpret('M1: IF THEN GOTO M2; M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: IF x1 THEN GOTO M2; M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: IF x2 = THEN GOTO M2; M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: IF x0 = 0 GOTO M2; M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: IF x3 = 5 THEN M2; M2:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2: IF THEN GOTO M3; M3:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2: IF x1 THEN GOTO M2; M3:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2: IF x2 = THEN GOTO M2; M3:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2: IF x0 = 0 GOTO M2; M3:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1:GOTO M3; M2: IF x3 = 5 THEN M2; M3:HALT;')
171

172
173
174
175
176
177
178
179
180
181
182
183
184
    def test_line_missing_tokens(self):
        with self.assertRaises(SyntaxError):
            interpret('M1 GOTO M2; M2: HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M3; GOTO M3; M3:HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M3; M2 GOTO M3; M3: HALT;')
        with self.assertRaises(SyntaxError):
            interpret('M1: GOTO M3; M2: M3; M3: HALT;')

    def test_end_without_halt(self):
        with self.assertRaises(SyntaxError):
            interpret('M1: x0:=2;')