test_goto_interpreter.py 7.69 KB
Newer Older
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
from gotointerpreter import interpret
from unittest import mock
from unittest import TestCase


def input_continue(prompt):
    return ""


def input_exit(prompt):
    return "EXIT"


def init_without_tokens(self, regex_to_token, program):
    self.regex_to_token = {}
    self.program = program
    self.current_position = 0


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

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

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

    def test_assignment_variable(self):
        self.assertEqual(interpret('M1:x0:=x0+1;M2:HALT;'), 1)
        self.assertEqual(interpret('M1:x0:= 5; M2:x0:=x0-1; M3:HALT;'), 4)
        self.assertEqual(interpret('M1:x0:=x1-1; M2:x0:=x0+1; M3:HALT;'), 1)
40
        self.assertEqual(interpret('M1:x0:=4; M2:x0:= x0 − 1; M3:HALT;'), 3)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

    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):
        self.assertEqual(interpret('M1:x0:=2;M2:GOTO M4;M3:x0:=0;M4:HALT;'), 2)
        self.assertEqual(interpret('M1:x0:=3;M2:GOTO M5;M3:GOTO M1;M4:x0:=5;M5:HALT;'), 3)
        self.assertEqual(interpret('M1:GOTO M3;M2:x0:=x2+5;M3:HALT;'), 0)

    def test_if(self):
        self.assertEqual(interpret('M1:x2:=10;' +
                                   'M2:IF x2=0 THEN GOTO M6;' +
                                   'M3:x0:=x0+1;M4:x2:=x2-1;' +
                                   'M5:GOTO M2; M6:HALT;'), 10)
        self.assertEqual(interpret('M1:IF x0=5 THEN GOTO M4;M2:IF x0=2 THEN GOTO M4;M3:x0:=x0+1;M4:HALT;'), 1)

    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):
        self.assertEqual(interpret('''
        M1:x1:=2;
        M2: IF x1=2 THEN GOTO M4;
        M3:HALT;
        M4:GOTO M3;'''), 0)

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

    @mock.patch('loopinterpreter.input', side_effect=input_exit)
    def test_break_exit(self, custom_input):
        self.assertEqual(interpret('M1:GOTO M1; BREAK'), -1)

    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;')
    
    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;')
        
    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;')
            
    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;')