Ejemplo n.º 1
0
 def testOutboxWithoutPointerRaiseException(self):
     inbox = iter([])
     ops = [
         ["OUTBOX"]
     ]
     state = cpu.create_state(inbox, ops)
     with self.assertRaises(ExecutionExceptin):
         cpu.tick(state)
Ejemplo n.º 2
0
    def testBumpdnWithoutValue(self):
        inbox = iter([])
        ops = [
            ["BUMPDN", '0']
        ]
        state = cpu.create_state(inbox, ops)

        with self.assertRaises(ExecutionExceptin):
            cpu.tick(state)
Ejemplo n.º 3
0
    def testJumpWithoutLabel(self):
        inbox = iter([])
        ops = [
            ["JUMP", 'a'],
        ]
        state = cpu.create_state(inbox, ops)

        with self.assertRaises(ExecutionExceptin):
            cpu.tick(state)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    def testBumpdnWithCharacter(self):
        inbox = iter([])
        ops = [
            ["BUMPDN", '0']
        ]
        state = cpu.create_state(inbox, ops)
        state.regs = ['A']

        with self.assertRaises(ExecutionExceptin):
            cpu.tick(state)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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))
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
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)
Ejemplo n.º 18
0
 def execute_tick(state):
     try:
         update(cpu.tick(state))
     except Exception as e:
         _show_error(e)
Ejemplo n.º 19
0
 def execute_tick(state):
     try:
         update(cpu.tick(state))
     except Exception as e:
         _show_error(e)