def test_block_start(self): self.inter.stack.append(2) self.inter.stack.append(1) self.assertEqual(len(self.inter.stack_stack), 1) cmds.execute_command(self.inter, '{') self.assertEqual(len(self.inter.stack_stack), 2) self.assertEqual(len(self.inter.stack), 1)
def test_clear_stack(self): self.inter.stack.append(ord('a')) self.inter.stack.append(0) self.inter.stack.append(1) self.assertEqual(len(self.inter.stack), 3) cmds.execute_command(self.inter, 'n') self.assertEqual(len(self.inter.stack), 0)
def test_under_stack(self): self.inter.stack.append(2) self.inter.stack.append(1) cmds.execute_command(self.inter, '{') self.inter.stack.append(1) cmds.execute_command(self.inter, 'u') self.assertEqual(self.inter.stack, [2, 0])
def execute_one_step(self): if not self.program: raise InitInterpreter symb = self.program[self.ip[1]][self.ip[0]] if self.iter_count == 0: self.iter_count = 1 elif symb == '"': self._change_text_mode() elif self.text_mode: self._push_char(symb) elif symb == ";": self._change_action_mode() elif self.nothing_mode: pass elif symb in self.directions.keys(): self.delta = self.directions[symb] elif symb.isdigit() or (ord('a') <= ord(symb) <= ord('f')): self._push_digit(symb) else: for _ in range(self.iter_count): cmds.execute_command(self, symb) if symb != "k": self.iter_count = 1 if self.input_mode: return self._go_1_step()
def test_load_unload_fp(self): u.write_string_to_stack(self.inter, 'STRN') self.inter.stack.append(4) cmds.execute_command(self.inter, '(') self.assertEqual('STRN', self.inter.imported_fps[0]['semantics']) self.assertIn('A', cmds.commands.commands) self.assertIn('C', cmds.commands.commands) self.assertIn('D', cmds.commands.commands) self.assertIn('P', cmds.commands.commands) cmds.execute_command(self.inter, ')') self.assertEqual(len(self.inter.imported_fps), 0) self.assertNotIn('A', cmds.commands.commands) self.assertNotIn('C', cmds.commands.commands) self.assertNotIn('D', cmds.commands.commands) self.assertNotIn('P', cmds.commands.commands)
def test_put_out_of_range(self): self.inter.stack.append(ord('a')) self.inter.stack.append(0) self.inter.stack.append(1) cmds.execute_command(self.inter, 'p') self.assertEqual(self.inter.program[1][0], 'a')
def test_store(self): self.inter.stack.append(ord('a')) cmds.execute_command(self.inter, 's') self.assertEqual(self.inter.program[0][1], 'a')
def test_compare_equal(self): self.inter._push_digit("2") self.inter._push_digit("2") cmds.execute_command(self.inter, "w") self.assertEqual(self.inter.directions[">"], self.inter.delta)
def test_double_stack_value(self): self.inter._push_digit("2") cmds.execute_command(self.inter, ":") self.assertEqual(2, len(self.inter.stack)) self.assertEqual(2, self.inter.stack[-1]) self.assertEqual(self.inter.stack[-1], self.inter.stack[-2])
def test_iterate(self): self.inter.stack.append(0) self.inter.stack.append(1) cmds.execute_command(self.inter, 'x') self.inter.run() self.assertEqual('hhh', self.out.getvalue())
def test_execute(self): u.write_string_to_stack(self.inter, 'echo "hi"') cmds.execute_command(self.inter, "=") self.assertEqual(0, self.inter.stack.pop())
def test_negotiation(self): cmds.execute_command(self.inter, "!") self.assertEqual(1, self.inter.stack[-1]) cmds.execute_command(self.inter, "!") self.assertEqual(0, self.inter.stack[-1])
def test_reflect_vertical(self): self.inter.delta = (0, 1) cmds.execute_command(self.inter, 'r') self.assertEqual(self.inter.delta, (0, -1))
def test_arithmetic_multiply(self): self.inter._push_digit("2") self.inter._push_digit("3") cmds.execute_command(self.inter, "*") self.assertEqual(6, self.inter.stack.pop())
def test_arithmetic_subtract(self): self.inter._push_digit("2") self.inter._push_digit("3") cmds.execute_command(self.inter, "-") self.assertEqual(-1, self.inter.stack.pop())
def test_arithmetic_div_by_zero(self): self.inter._push_digit("2") self.inter._push_digit("0") cmds.execute_command(self.inter, "/") self.assertEqual(0, self.inter.stack.pop())
def test_arithmetic_div(self): self.inter._push_digit("2") self.inter._push_digit("3") cmds.execute_command(self.inter, "/") self.assertEqual(2 / 3, self.inter.stack.pop())
def test_swap_stack(self): self.inter._push_digit("2") self.inter._push_digit("3") cmds.execute_command(self.inter, "\\") self.assertEqual(2, self.inter.stack.pop()) self.assertEqual(3, self.inter.stack.pop())
def test_vertical_dir_change(self): cmds.execute_command(self.inter, "|") self.assertEqual(self.inter.directions["v"], self.inter.delta) self.inter._push_digit("1") cmds.execute_command(self.inter, "|") self.assertEqual(self.inter.directions["^"], self.inter.delta)
def test_read_digit(self): self.inp.write("lkasmd11212llll") self.inp.seek(0) cmds.execute_command(self.inter, "&") cmds.execute_command(self.inter, "&") self.assertEqual(11212, self.inter.stack.pop())
def test_reflect_horizontal(self): self.assertEqual(self.inter.delta, (1, 0)) cmds.execute_command(self.inter, 'r') self.assertEqual(self.inter.delta, (-1, 0))
def test_read_symb(self): self.inp.write("k") self.inp.seek(0) cmds.execute_command(self.inter, "~") cmds.execute_command(self.inter, "~") self.assertEqual(ord('k'), u.pop_with_zero(self.inter))
def test_jump(self): self.inter.stack.append(3) cmds.execute_command(self.inter, 'j') self.assertEqual(self.inter.ip, (3, 0))
def test_print_digit(self): self.inter._push_digit("2") cmds.execute_command(self.inter, ".") self.assertEqual("2", self.out.getvalue())
def test_execute_failure(self): u.write_string_to_stack(self.inter, 'eco') cmds.execute_command(self.inter, "=") self.assertNotEqual(0, u.pop_with_zero(self.inter))
def test_get(self): self.inter.stack.append(1) self.inter.stack.append(0) cmds.execute_command(self.inter, 'g') self.assertEqual(u.pop_with_zero(self.inter), ord('h'))
def test_turn(self): cmds.execute_command(self.inter, "]") self.assertEqual(self.inter.directions["v"], self.inter.delta) cmds.execute_command(self.inter, "[") self.assertEqual(self.inter.directions[">"], self.inter.delta)
def test_put(self): self.inter.stack.append(ord('a')) self.inter.stack.append(1) self.inter.stack.append(0) cmds.execute_command(self.inter, 'p') self.assertEqual(self.inter.program[0][1], 'a')
def test_one_char_push(self): cmds.execute_command(self.inter, "'") self.assertEqual(ord("h"), self.inter.stack.pop())
def test_horizontal_dir_change(self): cmds.execute_command(self.inter, "_") self.assertEqual(self.inter.directions[">"], self.inter.delta) self.inter._push_digit("1") cmds.execute_command(self.inter, "_") self.assertEqual(self.inter.directions["<"], self.inter.delta)