def test_op_relative_base(self): """ Tests the relative base mode op code """ e = ElfCPU() # Position e.load_string('9,5,204,1,99,6,7,777') e.interrupts = True # Step over relative mode op e.step() with self.assertRaises(OutputInterrupt): e.execute() self.assertEqual(e.output_buffer, 777) # Immediate e.reset() e.load_string('109,5,204,1,99,444,777') e.interrupts = True # Step over relative mode op e.step() with self.assertRaises(OutputInterrupt): e.execute() self.assertEqual(e.output_buffer, 777) # Relative e.reset() e.load_string('209,9,209,6,204,-2,99,5,333,4,6') e.interrupts = True e.debug = True with self.assertRaises(OutputInterrupt): e.execute() self.assertEqual(e.output_buffer, 333)
def test_reset(self): """ Tests for CPU reset """ e = ElfCPU() e.load_string('1,0,0,0,99') e.execute() e.reset() # Halted gets cleared self.assertFalse(e.is_halted) # Program counter goes to 0 self.assertEqual(e.pc, 0) # Memory gets wiped so address 1 becomes invalid with self.assertRaises(ValueError): e.peek(1)
def test_op_output(self): """ Tests output op code Use io.StringIO() to capture the output """ e = ElfCPU() # Interrupts off e.load_string('4,5,104,66,99,55,5') e.interrupts = False result = None with patch('sys.stdout', new=io.StringIO()) as output: e.execute() result = output.getvalue() result = result.splitlines() # First is a reference to memory address 5 self.assertEqual(result[0].strip(), '55') # Second is an immediate value self.assertEqual(result[1].strip(), '66') # Interrupts on e.load_string('4,5,104,66,99,55,5') e.interrupts = True with self.assertRaises(OutputInterrupt): e.execute() self.assertEqual(e.output_buffer, 55) # Don't clear buffer with self.assertRaises(OutputOverflow): e.execute() # Restart test e.reset() e.load_string('4,5,104,66,99,55,5') e.interrupts = True with self.assertRaises(OutputInterrupt): e.execute() self.assertEqual(e.output_buffer, 55) # Clear buffer del e.output_buffer with self.assertRaises(OutputInterrupt): e.execute() self.assertEqual(e.output_buffer, 66) ############################################### # Interrupts on RELATIVE MODE # Restart test e.reset() e.load_string('109,5,204,1,99,6,1234') e.interrupts = True with self.assertRaises(OutputInterrupt): e.execute() self.assertEqual(e.output_buffer, 1234)