def testOutboxWithoutPointerRaiseException(self): inbox = iter([]) ops = [ ["OUTBOX"] ] state = cpu.create_state(inbox, ops) with self.assertRaises(ExecutionExceptin): cpu.tick(state)
def testBumpdnWithoutValue(self): inbox = iter([]) ops = [ ["BUMPDN", '0'] ] state = cpu.create_state(inbox, ops) with self.assertRaises(ExecutionExceptin): cpu.tick(state)
def testJumpWithoutLabel(self): inbox = iter([]) ops = [ ["JUMP", 'a'], ] state = cpu.create_state(inbox, ops) with self.assertRaises(ExecutionExceptin): cpu.tick(state)
def testAddWithoutPointerRaiseException(self): inbox = iter([]) ops = [ ["ADD", '0'] ] state = cpu.create_state(inbox, ops) state.regs[0] = 2 with self.assertRaises(ExecutionExceptin): cpu.tick(state)
def testBumpdnWithCharacter(self): inbox = iter([]) ops = [ ["BUMPDN", '0'] ] state = cpu.create_state(inbox, ops) state.regs = ['A'] with self.assertRaises(ExecutionExceptin): cpu.tick(state)
def testSubWithNumberAndCharRaiseException(self): inbox = iter([]) ops = [ ["SUB", '0'] ] state = cpu.create_state(inbox, ops) state.pointer = 1 state.regs[0] = 'A' with self.assertRaises(ExecutionExceptin): cpu.tick(state)
def testOutbox(self): inbox = iter([1]) ops = [ ["OUTBOX"], ["OUTBOX"] ] state = cpu.create_state(inbox, ops) state.pointer = 1 state = cpu.tick(state) state.pointer = "A" state = cpu.tick(state) self.assertEqual(list(state.outbox), [1, "A"]) self.assertEqual(state.pointer, None) self.assertEqual(state.pc, 2)
def testInbox(self): inbox = iter([1]) ops = [ ["INBOX"] ] state = cpu.create_state(inbox, ops) state = cpu.tick(state) self.assertEqual(list(state.inbox), []) self.assertEqual(state.pointer, 1) self.assertEqual(state.pc, 1)
def main(filepath): ops = parse_file(filepath) inbox = (n for n in "0123") state = cpu.create_state(inbox, ops) while cpu.tick(state) != -1: pass print("Result:") print(state.outbox)
def test_state_contains_prev_state(self): inbox = iter([]) ops = [ ["BUMPUP", '3'] ] state = cpu.create_state(inbox, ops) state.regs[3] = 4 new_state = cpu.tick(state) self.assertEquals(state.prev_state, None) self.assertEquals(state, new_state.prev_state)
def test_tick_return_new_state(self): inbox = iter([]) ops = [ ["BUMPUP", '3'] ] state = cpu.create_state(inbox, ops) state.regs[3] = 4 new_state = cpu.tick(state) self.assertNotEquals(state.pointer, new_state.pointer) self.assertNotEquals(state.regs[3], new_state.regs[3]) self.assertNotEquals(str(state), str(new_state))
def testAddWithNegativeValue(self): inbox = iter([]) ops = [ ["ADD", '0'] ] state = cpu.create_state(inbox, ops) state.pointer = 1 state.regs[0] = -2 state = cpu.tick(state) self.assertEqual(list(state.outbox), []) self.assertEqual(state.pointer, -1) self.assertEqual(state.pc, 1)
def testSub(self): inbox = iter([]) ops = [ ["SUB", '0'] ] state = cpu.create_state(inbox, ops) state.pointer = 1 state.regs[0] = 2 state = cpu.tick(state) self.assertEqual(list(state.outbox), []) self.assertEqual(state.pointer, -1) self.assertEqual(state.pc, 1)
def testSubWithSmallerChars(self): inbox = iter([]) ops = [ ["SUB", '0'] ] state = cpu.create_state(inbox, ops) state.pointer = 'X' state.regs[0] = 'B' state = cpu.tick(state) self.assertEqual(list(state.outbox), []) self.assertEqual(state.pointer, 22) self.assertEqual(state.pc, 1)
def testBumpdn(self): inbox = iter([]) ops = [ ["BUMPDN", '0'] ] state = cpu.create_state(inbox, ops) state.regs[0] = 2 state = cpu.tick(state) self.assertEqual(list(state.outbox), []) self.assertEqual(state.pointer, 1) self.assertEqual(state.regs[0], 1) self.assertEqual(state.pc, 1)
def testJump(self): inbox = iter([]) ops = [ ["JUMP", 'a'], ["BUMPUP", '0'], ["a:"] ] state = cpu.create_state(inbox, ops) state.regs[0] = 0 while state.pc != -1: state = cpu.tick(state) self.assertEqual(list(state.outbox), []) self.assertEqual(state.pointer, None) self.assertEqual(state.regs[0], 0)
def testCopyFromToWithNormalIndex(self): inbox = iter([]) ops = [ ["COPYFROM", '0'], ["COPYTO", '1'] ] state = cpu.create_state(inbox, ops) state.regs[0] = 2 while state.pc != -1: state = cpu.tick(state) self.assertEqual(list(state.outbox), []) self.assertEqual(state.regs[0], 2) self.assertEqual(state.regs[1], 2)
def execute_tick(state): try: update(cpu.tick(state)) except Exception as e: _show_error(e)