Пример #1
0
    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)
Пример #2
0
 def test_halt(self):
     """
     Tests for the halt op code
     """
     e = ElfCPU()
     e.load_string('1,0,0,0,99')
     e.step()
     self.assertFalse(e.is_halted)
     e.step()
     self.assertTrue(e.is_halted)
Пример #3
0
 def test_invalid_instr(self):
     """
     Tests for invalid op code
     """
     e = ElfCPU()
     e.load_string('123456789')
     with self.assertRaises(InvalidInstructionError):
         e.execute()
Пример #4
0
 def test_load_string_types(self):
     """
     Checks for TypeError
     """
     e = ElfCPU()
     with self.assertRaises(TypeError):
         # noinspection PyTypeChecker
         e.load_string(0)
     e.load_string('1,2,3,4')
Пример #5
0
 def test_gpf(self):
     """
     Tests for a general protection fault by allowing the program counter to
     go past the end of the memory
     """
     e = ElfCPU()
     e.load_string('1,0,0,0')
     with self.assertRaises(ProtectionFaultError):
         e.execute()
Пример #6
0
    def test_op_add(self):
        """
        Tests ADD op code
        [dst]:=[a]+[b]
        """
        e = ElfCPU()
        # Invalid address 44 for a
        e.load_string('1,44,0,0')
        with self.assertRaises(ProtectionFaultError):
            e.step()

        # Invalid address 44 for b
        e.load_string('1,0,44,0')
        with self.assertRaises(ProtectionFaultError):
            e.step()

        # Invalid address 44 for dst
        e.load_string('1,0,0,44')
        with self.assertRaises(ProtectionFaultError):
            e.step()

        # 1 + 1 = 2 @ address 0
        e.load_string('1,0,0,0,99')
        e.step()
        self.assertEqual(e.peek(0), 2)

        # 2**64 + 1 = 1 @ address 0 (overflow and wrap)
        e.load_string('1,5,6,0,99,' + str(2**64) + ',1')
        e.step()
        self.assertEqual(e.peek(0), 1)
Пример #7
0
    def test_poke(self):
        """
        Tests address range and data for poke
        """
        e = ElfCPU()
        e.load_string('0,1,2,3,4,5,6,7,8,9')

        # Address
        with self.assertRaises(TypeError):
            # noinspection PyTypeChecker
            e.poke('x', 2)
        with self.assertRaises(ValueError):
            e.poke(20, 2)
        with self.assertRaises(ValueError):
            e.poke(-1, 2)

        # Value
        with self.assertRaises(ValueError):
            e.poke(0, 2**64 + 1)

        self.assertEqual(e.poke(0, 99), 99)
        self.assertEqual(e.poke(9, 88), 88)
        self.assertEqual(e.peek(0), 99)
        self.assertEqual(e.peek(9), 88)
Пример #8
0
    def test_peek(self):
        """
        Tests address range for peek
        """
        e = ElfCPU()
        e.load_string('0,1,2,3,4,5,6,7,8,9')
        with self.assertRaises(TypeError):
            # noinspection PyTypeChecker
            e.peek('x')
        with self.assertRaises(ValueError):
            e.peek(20)
        with self.assertRaises(ValueError):
            e.peek(-1)

        self.assertEqual(e.peek(0), 0)
        self.assertEqual(e.peek(9), 9)
Пример #9
0
    def test_op_mul(self):
        """
        Tests MUL op code
        [dst]:=[a]*[b]
        """
        e = ElfCPU()
        # Invalid address 44 for a
        e.load_string('2,44,0,0')
        with self.assertRaises(ProtectionFaultError):
            e.step()

        # Invalid address 44 for b
        e.load_string('2,0,44,0')
        with self.assertRaises(ProtectionFaultError):
            e.step()

        # Invalid address 44 for dst
        e.load_string('2,0,0,44')
        with self.assertRaises(ProtectionFaultError):
            e.step()

        # 2 * 2 = 4 @ address 0
        e.load_string('2,0,0,0,99')
        e.step()
        self.assertEqual(e.peek(0), 4)

        # 2**63 * 3 = 9223372036854775808 @ address 0 (overflow and wrap)
        e.load_string('2,5,6,0,99,' + str(2**63) + ',3')
        e.step()
        self.assertEqual(e.peek(0), 9223372036854775808)