Commit 931cbac0 authored by Christopher Happe's avatar Christopher Happe
Browse files

Weitere Tests für WHILE-Interpreter

parent e130d72e
from whileinterpreter import interpret
from test_loop_interpreter import LOOPInterpreterTest
import unittest
class WHILEInterpreterTest(LOOPInterpreterTest):
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)
def test_while_forbidden_identifier(self):
with self.assertRaises(SyntaxError):
interpret("LOOP x1 DO WHILE x1 != 0 DO x0:=x0+1 END END ")
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 ')
def test_while(self):
self.assertEqual(interpret('x1:=1; WHILE x1 != 0 DO x0:=2; x1:=x1-1 END'), 2)
self.assertEqual(interpret('WHILE x1 /= 0 DO x0:=1 END'), 0)
self.assertEqual(interpret('WHILE x1 /= 0 DO WHILE x1 /= 0 DO x0:=2 END END'), 0)
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')
def test_while_non_zero(self):
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')
with self.assertRaises(SyntaxError):
interpret("x1:= 1; WHILE x1 /= 1 DO x0:=2 END")
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):
with self.assertRaises(SyntaxError):
interpret("x1:= 2; WHILE x1 /= 1 DO x0:=2 END")
interpret('x1:=2;; x0:=3')
with self.assertRaises(SyntaxError):
interpret("x1:= 1; WHILE x1 /= 0 DO WHILE x1 /= 1 DO x0:=2 END END")
interpret('WHILE x1 != 0 DO x0:=2;;x2:=1 END')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment