Esempio n. 1
0
    def test_jump_if_true(self):
        program = [0, 5, 0, 2]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(
            result,
            (program, 4, io),
        )

        program = [0, 5, 1, 1]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(
            result,
            (program, 5, io),
        )

        program = [0, 105, 1, 1]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(
            result,
            (program, 105, io),
        )
Esempio n. 2
0
    def test_output(self):
        program = [0, 4, 3, 50]
        io = IO(initial_io={'input': [], 'output': []})
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(result, ([0, 4, 3, 50], 3, io))

        self.assertEqual(io.get_next_output(), '50')
Esempio n. 3
0
    def test_store_input(self):
        program = [0, 3, 2]
        io = IO(initial_io={'input': ['10'], 'output': []})
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(
            result,
            ([0, 3, 10], 3, io),
        )
Esempio n. 4
0
    def test_mul(self):
        program = [0, 1002, 5, 3, 5, 33]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(
            result,
            ([0, 1002, 5, 3, 5, 99], 5, io),
        )
Esempio n. 5
0
    def test_equals(self):
        program = [1, 8, 0, 5, 1, 1]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(result, ([1, 1, 0, 5, 1, 1], 5, io), (
            'if the first parameter (position mode) is equal to the second parameter, '
            '(position mode) it stores 1 in the position given by the third parameter'
        ))

        program = [1, 1108, 5, 5, 1, 1]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(result, ([1, 1, 5, 5, 1, 1], 5, io), (
            'if the first parameter (immediate mode) is equal to the second parameter, '
            '(immediate mode) it stores 1 in the position given by the third parameter'
        ))

        program = [1, 8, 0, 5, 1, 2]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(result, ([1, 0, 0, 5, 1, 2], 5, io), (
            'if the first parameter (position mode) is not equal to the second parameter, '
            '(position mode) it stores 0 in the position given by the third parameter'
        ))

        program = [1, 1108, 5, 4, 1, 1]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(result, ([1, 0, 5, 4, 1, 1], 5, io), (
            'if the first parameter (immediate mode) is not equal to the second parameter, '
            '(immediate mode) it stores 0 in the position given by the third parameter'
        ))
Esempio n. 6
0
    def test_less_than(self):
        program = [1, 7, 3, 2, 1]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(result, ([1, 1, 3, 2, 1], 5, io), (
            'if the first parameter (position mode) is less than the second parameter, '
            '(position mode) it stores 1 in the position given by the third parameter'
        ))

        program = [1, 1107, 2, 3, 1]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(result, ([1, 1, 2, 3, 1], 5, io), (
            'if the first parameter (immediate mode) is less than the second parameter, '
            '(immmediate mode) it stores 1 in the position given by the third parameter'
        ))

        program = [1, 7, 1, 2, 1]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(result, ([1, 0, 1, 2, 1], 5, io), (
            'if the first parameter (position mode) is >= the second parameter, '
            '(position mode) it stores 0 in the position given by the third parameter'
        ))

        program = [1, 1107, 2, 1, 1]
        io = IO()
        result = execute_next_instruction(program, pointer=1, io=io)

        self.assertEqual(result, ([1, 0, 2, 1, 1], 5, io), (
            'if the first parameter (immediate mode) is >= the second parameter, '
            '(immediate mode) it stores 0 in the position given by the third parameter'
        ))
Esempio n. 7
0
    def test_halt(self):
        program = [1001, 4, 3, 4, 99]
        io = IO()
        result = execute_next_instruction(program, pointer=4, io=io)

        self.assertEqual(result, ([1001, 4, 3, 4, 99], None, io))