예제 #1
0
    def test_op_code13(self):
        test_um = UM(self.file_path)

        # op_code: 13 - Orthography
        test_um.registers[0] = 0
        test_um.orthography(0, 1234)
        self.assertEqual(1234, test_um.registers[0])
예제 #2
0
    def test_op_code11(self):
        stdin = StringIO(chr(97))
        test_um = UM(self.file_path, stdin=stdin)

        # op_code: 11 - Input
        op_code = 11
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual(97, test_um.registers[2])
예제 #3
0
 def test_get_c(self):
     #                               A     C
     #                               |     |
     #                               vvv   vvv
     #       .--------------------------------.
     #       |VUTSRQPONMLKJIHGFEDCBA9876543210|
     #       `--------------------------------'
     #        ^^^^                      ^^^
     #        |                         |
     #        operator number           B
     self.assertEqual(7, UM.get_c(int('0x00000007', base=16)))
     self.assertEqual(6, UM.get_c(int('0x00000006', base=16)))
     self.assertEqual(5, UM.get_c(int('0x00000005', base=16)))
예제 #4
0
 def test_get_ortho_a(self):
     #            A
     #            |
     #            vvv
     #       .--------------------------------.
     #       |VUTSRQPONMLKJIHGFEDCBA9876543210|
     #       `--------------------------------'
     #        ^^^^   ^^^^^^^^^^^^^^^^^^^^^^^^^
     #        |      |
     #        |      value
     #        |
     #        operator number
     self.assertEqual(1, UM.get_ortho_a(int('0x02000000', base=16)))
     self.assertEqual(2, UM.get_ortho_a(int('0x04000000', base=16)))
     self.assertEqual(3, UM.get_ortho_a(int('0x06000000', base=16)))
예제 #5
0
    def test_op_code0(self):
        test_um = UM(self.file_path)

        # op_code: 0 - Conditional move
        op_code = 0
        test_um.registers[0] = 1
        test_um.registers[1] = 2
        test_um.registers[2] = 3
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual(test_um.registers[0], 2)
        test_um.registers[0] = 1
        test_um.registers[1] = 2
        test_um.registers[2] = 0
        # No change because registers[c] = 0
        self.assertEqual(1, test_um.registers[0])
        self.assertEqual(2, test_um.registers[1])
        self.assertEqual(0, test_um.registers[2])
예제 #6
0
    def test_op_code2(self):
        test_um = UM(self.file_path)
        test_um.arrays.append([1, 2, 3])

        # op_code: 2 - Array Amendment
        op_code = 2
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 123
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual([[1, 123, 3]], test_um.arrays)
예제 #7
0
    def test_op_code6(self):
        test_um = UM(self.file_path)

        # op_code: 6 - Not-And
        op_code = 6
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 7
        test_um.operate(op_code, 0, 1, 2)
        expected_val = ctypes.c_uint32(~(1 & 7)).value
        self.assertEqual(expected_val, test_um.registers[0])
예제 #8
0
    def test_op_code8(self):
        test_um = UM(self.file_path)
        test_um.arrays.append([1, 2, 3])

        # op_code: 8 - Allocation
        op_code = 8
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 2
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual([[1, 2, 3], [0, 0]], test_um.arrays)
        self.assertEqual(1, test_um.registers[1])
예제 #9
0
    def test_op_code10(self):
        stdout = StringIO()
        test_um = UM(self.file_path, stdout=stdout)

        # op_code: 10 - Output
        op_code = 10
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 97
        test_um.operate(op_code, 0, 1, 2)

        output = stdout.getvalue()
        self.assertEqual(chr(97), output)
예제 #10
0
    def test_op_code9(self):
        test_um = UM(self.file_path)
        test_um.arrays.append([1, 2, 3])
        test_um.arrays.append([4, 5, 6])

        # op_code: 9 - Abandonment
        op_code = 9
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 1  # Clears arrays[1]
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual([[1, 2, 3], []], test_um.arrays)

        test_um.registers.pop(2)
        self.assertRaises(Exception, test_um.operate, op_code, 0, 1, 2)
예제 #11
0
    def test_op_code1(self):
        test_um = UM(self.file_path)
        test_um.arrays.append([1, 2, 3])
        test_um.arrays.append([4, 5, 6])
        self.assertEqual([[1, 2, 3], [4, 5, 6]], test_um.arrays)

        # op_code: 1 - Array index
        op_code = 1
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 2
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual(6, test_um.registers[0])
        self.assertEqual(1, test_um.registers[1])
        self.assertEqual(2, test_um.registers[2])
예제 #12
0
    def test_op_code12(self):
        test_um = UM(self.file_path)
        test_um.arrays.append([1, 2, 3])
        test_um.arrays.append([4, 5, 6])
        test_um.arrays.append([7, 8, 9])

        # op_code: 12 - Load Program
        op_code = 12

        # register[b] is undef
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 2
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual([4, 5, 6], test_um.arrays[0])
        self.assertEqual(2, test_um.exec_finger)
예제 #13
0
 def test_get_ortho_value(self):
     self.assertEqual(1, UM.get_ortho_value(int('0x00000001', base=16)))
     self.assertEqual(2, UM.get_ortho_value(int('0x00000002', base=16)))
     self.assertEqual(3, UM.get_ortho_value(int('0x00000003', base=16)))
예제 #14
0
    def test_halt(self):
        test_um = UM(self.file_path)

        # op_code: 7 - Halt
        with self.assertRaises(SystemExit):
            test_um.halt()
예제 #15
0
 def test_get_a(self):
     self.assertEqual(1, UM.get_a(int('0x00000040', base=16)))
     self.assertEqual(2, UM.get_a(int('0x00000080', base=16)))
     self.assertEqual(3, UM.get_a(int('0x000000C0', base=16)))
예제 #16
0
 def test_get_b(self):
     self.assertEqual(1, UM.get_b(int('0x00000008', base=16)))
     self.assertEqual(2, UM.get_b(int('0x00000010', base=16)))
     self.assertEqual(3, UM.get_b(int('0x00000018', base=16)))
예제 #17
0
    def test_op_code3(self):
        test_um = UM(self.file_path)

        # op_code: 3 - Array Amendment
        op_code = 3
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 2
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual(3, test_um.registers[0])  # (1 + 2) % 2^32 = 3
        test_um.registers[0] = 0
        test_um.registers[1] = 0
        test_um.registers[2] = 2**32
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual(0, test_um.registers[0])  # (0 + 2^32) % 2^32 = 0
예제 #18
0
 def test_init(self):
     test_um = UM(self.file_path)
     self.assertEqual([0] * 8, test_um.registers)
     self.assertEqual(self.file_path, test_um.file_path)
     self.assertEqual(0, test_um.exec_finger)
     self.assertEqual(0, len(test_um.arrays))
예제 #19
0
    def test_op_code_undef(self):
        test_um = UM(self.file_path)

        # op_code: 123 - Undefined
        op_code = 123
        self.assertRaises(Exception, test_um.operate, op_code, 0, 1, 2)
예제 #20
0
 def test_get_op_code(self):
     self.assertEqual(0, UM.get_op_code(int('0x00000000', base=16)))
     self.assertEqual(1, UM.get_op_code(int('0x10000000', base=16)))
     self.assertEqual(2, UM.get_op_code(int('0x20000000', base=16)))
     self.assertEqual(3, UM.get_op_code(int('0x30000000', base=16)))
     self.assertEqual(4, UM.get_op_code(int('0x40000000', base=16)))
     self.assertEqual(5, UM.get_op_code(int('0x50000000', base=16)))
     self.assertEqual(6, UM.get_op_code(int('0x60000000', base=16)))
     self.assertEqual(7, UM.get_op_code(int('0x70000000', base=16)))
     self.assertEqual(8, UM.get_op_code(int('0x80000000', base=16)))
     self.assertEqual(9, UM.get_op_code(int('0x90000000', base=16)))
     self.assertEqual(10, UM.get_op_code(int('0xA0000000', base=16)))
     self.assertEqual(11, UM.get_op_code(int('0xB0000000', base=16)))
     self.assertEqual(12, UM.get_op_code(int('0xC0000000', base=16)))
     self.assertEqual(13, UM.get_op_code(int('0xD0000000', base=16)))
예제 #21
0
    def test_op_code4(self):
        test_um = UM(self.file_path)

        # op_code: 4 - Multiplication
        op_code = 4
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 2
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual(2, test_um.registers[0])  # (1 * 2) % 2^32 = 3
        test_um.registers[0] = 0
        test_um.registers[1] = 1
        test_um.registers[2] = 2 ** 32
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual(0, test_um.registers[0])  # (1 * 2^32) % 2^32 = 0
예제 #22
0
    def test_op_code5(self):
        test_um = UM(self.file_path)

        # op_code: 5 - Division
        op_code = 5
        test_um.registers[0] = 0
        test_um.registers[1] = 3
        test_um.registers[2] = 2
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual(1, test_um.registers[0])  # 3 // 2 = 1
        test_um.registers[0] = 0
        test_um.registers[1] = 6
        test_um.registers[2] = 2
        test_um.operate(op_code, 0, 1, 2)
        self.assertEqual(3, test_um.registers[0])  # 6 // 2 = 3