test_while_interpreter.py 5.72 KB
Newer Older
1
2
from whileinterpreter import interpret
from test_loop_interpreter import LOOPInterpreterTest
3
4
5
6
7
from unittest import mock


def str_yes(a):
    return "J"
8
9
10


class WHILEInterpreterTest(LOOPInterpreterTest):
Chris's avatar
Chris committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    def test_while_assignment(self):
        self.assertEqual(interpret('x1:=1;\n WHILE x1!=0 DO\n x0:=1;\n x1:=x1-1 END'), 1)
        self.assertEqual(interpret('x1:=3; WHILE x1/=0 DO x0:=x0+1; x1:=x1-1 END'), 3)

    def test_while_empty_assignment(self):
        self.assertEqual(interpret('WHILE x1 != 0 DO x0:=2; x1:=x1-1 END'), 0)
        self.assertEqual(interpret('WHILE x1 /= 0 DO x0:=1 END'), 0)
        self.assertEqual(interpret('WHILE x1!=0 DO LOOP x2 DO x0:=3 END END'), 0)

    def test_while_nested_assignment(self):
        self.assertEqual(interpret('x1:=2; WHILE x1!=0 DO  x2:=3;' +
                                   'WHILE x2/=0 DO x0:=x0+1; x2:=x2-1 END; x1:=x1-1 END'), 6)
        self.assertEqual(interpret('x1:=10; WHILE x1!=0 DO LOOP x1 DO x0:=x0+1 END; x1:=x1-1 END'), 55)

    def test_while_nested_empty_assignment(self):
        self.assertEqual(interpret('WHILE x1!=0 DO  x2:=3;' +
                                   'WHILE x2/=0 DO x0:=x0+1; x2:=x2-1 END; x1:=x1-1 END'), 0)
        self.assertEqual(interpret('WHILE x1!=0 DO LOOP x1 DO x0:=x0+1 END; x1:=x1-1 END'), 0)

30
31
    def test_while_forbidden_identifier(self):
        with self.assertRaises(SyntaxError):
Chris's avatar
Chris committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
            interpret('LOOP x1 DO WHILE x1 != 0 DO x0:=x0+1 END END ')
        with self.assertRaises(SyntaxError):
            interpret('LOOP x1 DO WHILE x2 != 0 DO x1:=x0+1 END END ')
        with self.assertRaises(SyntaxError):
            interpret('LOOP x1 DO WHILE x3 != 0 DO x0:=x1+1 END END ')
        with self.assertRaises(SyntaxError):
            interpret('x1:=2; LOOP x1 DO WHILE x1 != 0 DO x0:=x0+1 END END ')
        with self.assertRaises(SyntaxError):
            interpret('x1:=3; x2:=3; LOOP x1 DO WHILE x2 != 0 DO x1:=x0+1 END END ')
        with self.assertRaises(SyntaxError):
            interpret('x1:=2; x2:=4; LOOP x1 DO WHILE x2 != 0 DO x0:=x1+1 END END ')
        with self.assertRaises(SyntaxError):
            interpret('x1:=3; LOOP x1 DO WHILE x2 != 0 DO x1:=x0+1 END END ')
        with self.assertRaises(SyntaxError):
            interpret('x1:=2; LOOP x1 DO WHILE x2 != 0 DO x0:=x1+1 END END ')
47

Chris's avatar
Chris committed
48
49
50
51
52
53
54
55
56
57
58
59
60
    def test_while_wrong_syntax(self):
        with self.assertRaises(SyntaxError):
            interpret('x1:= 1; WHILE x1 /= 1 DO x0:=2 END')
        with self.assertRaises(SyntaxError):
            interpret('x1:= 2; WHILE x1 /= 1 DO x0:=2 END')
        with self.assertRaises(SyntaxError):
            interpret('x1:= 1; WHILE x1 /= 0 DO WHILE x1 /= 1 DO x0:=2 END END')
        with self.assertRaises(SyntaxError):
            interpret('WHILE false DO x2:=2 END')
        with self.assertRaises(SyntaxError):
            interpret('WHILE x1>0 DO x1:=x1-1 END')
        with self.assertRaises(SyntaxError):
            interpret('WHILE 3>0 DO x0:=2 END')
61

Chris's avatar
Chris committed
62
63
64
65
66
67
68
    def test_syntax_missing_semicolon_while(self):
        with self.assertRaises(SyntaxError):
            interpret('x0:=2 WHILE x0!=0 DO x1:=x1+1 END')
        with self.assertRaises(SyntaxError):
            interpret('WHILE x0/= DO x1:=x1+1 x1:=x1+1 END')
        with self.assertRaises(SyntaxError):
            interpret('WHILE x0/=0 DO x1:=x1+1 x1:=x1+1 END')
69
        with self.assertRaises(SyntaxError):
Chris's avatar
Chris committed
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
            interpret('x0:=2; WHILE x0/=0 DO x1:=x1+1 x1:=x1+1 END')
        with self.assertRaises(SyntaxError):
            interpret('WHILE x0/=0 DO x1:=x1+1 END x0:=x1+0')
        with self.assertRaises(SyntaxError):
            interpret('x0:=2; WHILE x0!=0 DO x1:=x1+1; x0=x0-1 END x0:=x1+0')
        with self.assertRaises(SyntaxError):
            interpret('LOOP x0 DO WHILE x1!=0 DO x2:=2 x1:=0 END')

    def test_syntax_missing_identifier_while(self):
        with self.assertRaises(SyntaxError):
            interpret('LOOP x1 DO WHILE /= 0 DO x0:=x0+2 END')
        with self.assertRaises(SyntaxError):
            interpret('x1:=2; LOOP x1 DO WHILE /= 0 DO x0:=x0+2 END')

    def test_syntax_missing_unequal_while(self):
        with self.assertRaises(SyntaxError):
            interpret('LOOP x1 DO WHILE x2 0 DO x0:=x0+2 END END')
        with self.assertRaises(SyntaxError):
            interpret('x1:=2; LOOP x1 DO WHILE x2 0 DO x0:=x0+2 END END')

    def test_syntax_missing_zero_while(self):
        with self.assertRaises(SyntaxError):
            interpret('LOOP x1 DO WHILE x2 != DO x0:=x0+2 END END')
        with self.assertRaises(SyntaxError):
            interpret('x1:=2; LOOP x1 DO WHILE x2 /= DO x0:=x0+2 END END')

    def test_syntax_wrong_number_while(self):
        with self.assertRaises(SyntaxError):
            interpret('LOOP x1 DO WHILE x2 /= 1 DO x0:=x0+2 END END')
        with self.assertRaises(SyntaxError):
            interpret('x1:=2; LOOP x1 DO WHILE x2 /= 3 DO x0:=x0+2 END END')

    def test_syntax_missing_do_while(self):
        with self.assertRaises(SyntaxError):
            interpret('LOOP x1 DO WHILE x2 != 0 x0:=x0+2 END END')
        with self.assertRaises(SyntaxError):
            interpret('x1:=2; LOOP x1 DO WHILE x2 /= 0 x0:=x0+2 END END')
        with self.assertRaises(SyntaxError):
            interpret('WHILE x1 != 0 x2:=2 END')
        with self.assertRaises(SyntaxError):
            interpret('x1:= 2; WHILE x1 != 0 x2:=2 END')

    def test_syntax_unnecessary_semicolon_while(self):
113
        with self.assertRaises(SyntaxError):
Chris's avatar
Chris committed
114
            interpret('x1:=2;; x0:=3')
115
        with self.assertRaises(SyntaxError):
Chris's avatar
Chris committed
116
            interpret('WHILE x1 != 0 DO x0:=2;;x2:=1 END')
117
118

    @mock.patch('whileinterpreter.input', side_effect=str_yes)
Chris's avatar
Chris committed
119
    def test_infinite_loop(self, custom_input):
120
        self.assertEqual(interpret('x1:=100000; WHILE x1 != 0 DO x0:=1; x1:=x1-1 END', 1), -1)