Пример #1
0
    def test_op_input(self):
        """
        Tests input op code

        Use unittest.mock.patch to fake the input value
        """
        e = ElfCPU()

        # Interrupts off
        e.load_string('3,3,99,-1')
        e.interrupts = False
        with patch('builtins.input', return_value='1234'):
            e.execute()
            self.assertEqual(e.peek(3), 1234)

        # Interrupts on
        e.load_string('3,5,3,5,99,-1')
        e.interrupts = True
        with self.assertRaises(InputInterrupt):
            e.step()

        # Should be back at pc = 0
        self.assertEqual(e.pc, 0)

        # Load input
        e.input_buffer = 567

        # Loading again overflows
        with self.assertRaises(InputOverflow):
            e.input_buffer = 123

        # Execute the input instruction
        e.step()
        self.assertEqual(e.peek(5), 567)

        # Exec next input instruction
        with self.assertRaises(InputInterrupt):
            e.step()

        e.input_buffer = 987

        # Execute until end
        e.execute()
        self.assertEqual(e.peek(5), 987)
Пример #2
0
    for position in range(5):
        # Clear state
        amp.reset()
        # Reload code
        amp.load_string(intcode)
        # Interrupts on
        amp.interrupts = True

        # Run until phase is requested
        try:
            amp.execute()
        except InputInterrupt:
            pass

        # Provide phase
        amp.input_buffer = phase[position]

        # Run until input signal is requested
        try:
            amp.execute()
        except InputInterrupt:
            pass

        # Provide input signal
        amp.input_buffer = input_signal

        # Run until output is ready
        try:
            amp.execute()
        except OutputInterrupt:
            pass