Commit 50eeaba4 authored by Christopher Happe's avatar Christopher Happe
Browse files

Tests an Fehlermeldungen angepasst

parent 8005a507
......@@ -11,33 +11,27 @@ 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)
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;'))
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)
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;'))
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)
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;'))
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)
self.assertEqual(interpret('M1:x0:=4; M2:x0:= x0 − 1; M3:HALT;'), 3)
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;'))
def test_assignment_wrong_syntax(self):
with self.assertRaises(SyntaxError):
......@@ -62,16 +56,16 @@ class GOTOInterpreterTest(TestCase):
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)
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;'))
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)
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;'))
def test_syntax_missing_operator(self):
with self.assertRaises(SyntaxError):
......@@ -94,19 +88,19 @@ class GOTOInterpreterTest(TestCase):
interpret('M1: GOTO M3; M3:HALT;')
def test_linebreaks(self):
self.assertEqual(interpret('''
self.assertEqual(0, interpret('''
M1:x1:=2;
M2: IF x1=2 THEN GOTO M4;
M3:HALT;
M4:GOTO M3;'''), 0)
M4:GOTO M3;'''))
@mock.patch('loopinterpreter.input', side_effect=input_continue)
def test_break(self, custom_input):
self.assertEqual(interpret('M1:x0:=2;BREAK M2:HALT;'), 2)
self.assertEqual(2, interpret('M1:x0:=2;BREAK M2:HALT;'))
@mock.patch('loopinterpreter.input', side_effect=input_exit)
def test_break_exit(self, custom_input):
self.assertEqual(interpret('M1:GOTO M1; BREAK'), -1)
self.assertEqual(-1, interpret('M1:GOTO M1; BREAK'))
def test_assignment_missing_tokens(self):
with self.assertRaises(SyntaxError):
......@@ -127,7 +121,7 @@ class GOTOInterpreterTest(TestCase):
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;')
......@@ -146,7 +140,7 @@ class GOTOInterpreterTest(TestCase):
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;')
......@@ -174,7 +168,7 @@ class GOTOInterpreterTest(TestCase):
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;')
......
......@@ -19,24 +19,24 @@ def init_without_tokens(self, regex_to_token, program):
class LOOPInterpreterTest(unittest.TestCase):
def test_assignment_default_zero(self):
self.assertEqual(interpret('x0:=x0 + 0'), 0)
self.assertEqual(interpret('x0:=x1 + 0'), 0)
self.assertEqual(interpret('x0:=x2 + 0'), 0)
self.assertEqual(0, interpret('x0:=x0 + 0'))
self.assertEqual(0, interpret('x0:=x1 + 0'))
self.assertEqual(0, interpret('x0:=x2 + 0'))
def test_assignment_non_negative(self):
self.assertEqual(interpret('x0:=x0-1'), 0)
self.assertEqual(interpret('x0:=x1-1'), 0)
self.assertEqual(interpret('x0:=x2-6'), 0)
self.assertEqual(0, interpret('x0:=x0-1'))
self.assertEqual(0, interpret('x0:=x1-1'))
self.assertEqual(0, interpret('x0:=x2-6'))
def test_assignment_number(self):
self.assertEqual(interpret("x0:=5"), 5)
self.assertEqual(interpret("x0:=2"), 2)
self.assertEqual(interpret('x0:=3'), 3)
self.assertEqual(5, interpret("x0:=5"))
self.assertEqual(2, interpret("x0:=2"))
self.assertEqual(3, interpret('x0:=3'))
def test_assignment_variable(self):
self.assertEqual(interpret('x0:=x0+1'), 1)
self.assertEqual(interpret('x0:= 5; x0:=x0-1'), 4)
self.assertEqual(interpret('x0:=x1-1; x0:=x0+1'), 1)
self.assertEqual(1, interpret('x0:=x0+1'))
self.assertEqual(4, interpret('x0:= 5; x0:=x0-1'))
self.assertEqual(1, interpret('x0:=x1-1; x0:=x0+1'))
def test_assignment_wrong_syntax(self):
with self.assertRaises(SyntaxError):
......@@ -61,16 +61,16 @@ class LOOPInterpreterTest(unittest.TestCase):
interpret('x0:=xj+1')
def test_loop_assignment(self):
self.assertEqual(interpret('x1:=1; LOOP x1 DO x0:=1 END'), 1)
self.assertEqual(interpret('x1:=2; LOOP x1 DO x0:=x0 + 2 END'), 4)
self.assertEqual(1, interpret('x1:=1; LOOP x1 DO x0:=1 END'))
self.assertEqual(4, interpret('x1:=2; LOOP x1 DO x0:=x0 + 2 END'))
def test_loop_empty_assignment(self):
self.assertEqual(interpret('LOOP x1 DO x0:=1 END'), 0)
self.assertEqual(interpret('x2:=2;LOOP x1 DO x0:=x2+1 END'), 0)
self.assertEqual(0, interpret('LOOP x1 DO x0:=1 END'))
self.assertEqual(0, interpret('x2:=2;LOOP x1 DO x0:=x2+1 END'))
def test_loop_nested_assignment(self):
self.assertEqual(interpret('x1:=3; LOOP x1 DO x2:=x2+1; LOOP x2 DO x0:=x0+1 END END'), 6)
self.assertEqual(interpret('x1:=3; x2:=3; LOOP x1 DO x2:=x2-1; LOOP x2 DO x0:=x0+1 END END'), 3)
self.assertEqual(6, interpret('x1:=3; LOOP x1 DO x2:=x2+1; LOOP x2 DO x0:=x0+1 END END'))
self.assertEqual(3, interpret('x1:=3; x2:=3; LOOP x1 DO x2:=x2-1; LOOP x2 DO x0:=x0+1 END END'))
def test_loop_forbidden_identifier(self):
with self.assertRaises(SyntaxError):
......@@ -119,8 +119,8 @@ class LOOPInterpreterTest(unittest.TestCase):
interpret('x1:=1; LOOP x1 DO x2:=5 END;')
def test_assignment_with_loop(self):
self.assertEqual(interpret('x0:=2; LOOP x0 DO x1:=x1+1 END; x0:=x1+0'), 2)
self.assertEqual(interpret('x1:=x1+1; LOOP x0 DO x1:=x1+1 END; x0:=x1+0'), 1)
self.assertEqual(2, interpret('x0:=2; LOOP x0 DO x1:=x1+1 END; x0:=x1+0'))
self.assertEqual(1, interpret('x1:=x1+1; LOOP x0 DO x1:=x1+1 END; x0:=x1+0'))
def test_syntax_unnecessary_semicolon(self):
with self.assertRaises(SyntaxError):
......@@ -229,18 +229,18 @@ class LOOPInterpreterTest(unittest.TestCase):
interpret('LOOP x0 DO LOOP DO x1:=x2+0 END END')
def test_newlines(self):
self.assertEqual(interpret('''x2:=3;
x0:=x2+2'''), 5)
self.assertEqual(interpret('x1:=x1-2;\n x0:=x1+2'), 2)
self.assertEqual(5, interpret('''x2:=3;
x0:=x2+2'''))
self.assertEqual(2, interpret('x1:=x1-2;\n x0:=x1+2'))
@mock.patch('loopinterpreter.input', side_effect=input_exit)
def test_break_exit(self, custom_input):
self.assertEqual(interpret('x1:=2; BREAK x0:=2'), -1)
self.assertEqual(interpret('LOOP x1 DO BREAK x2:= 2 END'), -1)
self.assertEqual(-1, interpret('x1:=2; BREAK x0:=2'))
self.assertEqual(-1, interpret('LOOP x1 DO BREAK x2:= 2 END'))
@mock.patch('loopinterpreter.input', side_effect=input_continue)
def test_break_continue(self, custom_input):
self.assertEqual(interpret('x1:=2; LOOP x1 DO x0:=x0+2 BREAK END'), 4)
self.assertEqual(4, interpret('x1:=2; LOOP x1 DO x0:=x0+2 BREAK END'))
@mock.patch('lexer.Lexer.__init__', init_without_tokens)
def test_unknown_tokens(self):
......
......@@ -3,29 +3,29 @@ from test_loop_interpreter import LOOPInterpreterTest
from unittest import mock
def str_yes(a):
def str_yes(prompt):
return "J"
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)
self.assertEqual(1, interpret('x1:=1;\n WHILE x1!=0 DO\n x0:=1;\n x1:=x1-1 END'))
self.assertEqual(3, interpret('x1:=3; WHILE x1/=0 DO x0:=x0+1; x1:=x1-1 END'))
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)
self.assertEqual(0, interpret('WHILE x1 != 0 DO x0:=2; x1:=x1-1 END'))
self.assertEqual(0, interpret('WHILE x1 /= 0 DO x0:=1 END'))
self.assertEqual(0, interpret('WHILE x1!=0 DO LOOP x2 DO x0:=3 END END'))
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)
self.assertEqual(6, interpret('x1:=2; WHILE x1!=0 DO x2:=3;' +
'WHILE x2/=0 DO x0:=x0+1; x2:=x2-1 END; x1:=x1-1 END'))
self.assertEqual(55, interpret('x1:=10; WHILE x1!=0 DO LOOP x1 DO x0:=x0+1 END; x1:=x1-1 END'))
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)
self.assertEqual(0, interpret('WHILE x1!=0 DO x2:=3;' +
'WHILE x2/=0 DO x0:=x0+1; x2:=x2-1 END; x1:=x1-1 END'))
self.assertEqual(0, interpret('WHILE x1!=0 DO LOOP x1 DO x0:=x0+1 END; x1:=x1-1 END'))
def test_while_forbidden_identifier(self):
with self.assertRaises(SyntaxError):
......@@ -117,4 +117,4 @@ class WHILEInterpreterTest(LOOPInterpreterTest):
@mock.patch('whileinterpreter.input', side_effect=str_yes)
def test_infinite_loop(self, custom_input):
self.assertEqual(interpret('x1:=100000; WHILE x1 != 0 DO x0:=1; x1:=x1-1 END', 1), -1)
self.assertEqual(-1, interpret('x1:=100000; WHILE x1 != 0 DO x0:=1; x1:=x1-1 END', 1))
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