def test_initial_conditions(self): machine = crianza.Machine([]) machine.run() self.assertEqual(machine.data_stack, crianza.Stack([])) self.assertEqual(machine.return_stack, crianza.Stack([])) self.assertEqual(machine.stack, []) self.assertEqual(machine.instruction_pointer, 0) self.assertEqual(machine.code, []) self.assertEqual(machine.input, sys.stdin) self.assertEqual(machine.output, sys.stdout)
def test_program_subroutine_1(self): fout = StringIO.StringIO() m = self._execfile("tests/subroutine-1.source", output=fout) self.assertEqual(fout.getvalue(), "one\ntwo\nthree\n144\nfinished\n") self.assertEqual(m.top, 0) self.assertEqual(m.stack, ["one", "two", "three", 144, 0]) self.assertEqual(m.return_stack, crianza.Stack([]))
def test_program_sum_mul_1(self): fout = StringIO.StringIO() m = self._execfile("tests/sum-mul-1.source", output=fout) self.assertEqual(fout.getvalue(), "(2+3) * 4 = 20\n") self.assertEqual(m.top, None) self.assertEqual(m.stack, []) self.assertEqual(m.return_stack, crianza.Stack([]))
def test_program_fibonacci_2(self): fout = StringIO.StringIO() m = self._execfile("tests/fibonacci-2.source", output=fout, steps=180) self.assertEqual(fout.getvalue(), "0\n1\n1\n2\n3\n5\n8\n13\n21\n34\n55\n89\n144\n233\n377\n") self.assertEqual(m.top, 377) self.assertEqual(m.stack, [233, 377]) self.assertEqual(m.return_stack, crianza.Stack([6]))
def test_program_fibonacci_1(self): fout = six.StringIO() m = self._execfile("tests/fibonacci.source", output=fout, steps=100) self.assertEqual( fout.getvalue(), "0\n1\n1\n2\n3\n5\n8\n13\n21\n34\n55\n89\n144\n233\n377\n") self.assertEqual(m.top, 610) self.assertEqual(m.stack, [377, 610]) self.assertEqual(m.return_stack, crianza.Stack([]))
def test_program_sum_mul_2(self): fin = six.StringIO("12\n34\n") fout = six.StringIO() m = self._execfile("tests/sum-mul-2.source", input=fin, output=fout) self.assertEqual( fout.getvalue(), "Enter a number: " + "Enter another number: " + "Their sum is: 46\n" + "Their product is: 408\n") self.assertEqual(m.top, None) self.assertEqual(m.stack, []) self.assertEqual(m.return_stack, crianza.Stack([]))
def test_program_even_odd(self): fin = six.StringIO("1\n2\n3\n") fout = six.StringIO() m = self._execfile("tests/even-odd.source", input=fin, output=fout) self.assertEqual( fout.getvalue(), "Enter a number: The number 1 is odd.\n" + "Enter a number: The number 2 is even.\n" + "Enter a number: The number 3 is odd.\n" + "Enter a number: ") self.assertEqual(fin.tell(), len(fin.getvalue())) self.assertEqual(m.top, "") self.assertEqual(m.stack, [""]) self.assertEqual(m.return_stack, crianza.Stack([]))