Beispiel #1
0
 def test_jge(self):
     """
     Jump iff sign flag is 0.
     """
     for i in range(NUM_TESTS):
       vmachine.re_init()
       label_loc = random.randint(FIRST_INST_ADDRESS,MAX_INSTRUCTIONS)
       vmachine.labels["test_label"] = label_loc
       sign_flag = random.getrandbits(1)
       vmachine.flags["SF"] = sign_flag
       assemble("jge test_label", vmachine)
       if(not sign_flag):
         self.assertEqual(vmachine.get_ip(), label_loc)
       else:
         self.assertEqual(vmachine.get_ip(), 1)
Beispiel #2
0
 def test_jmp(self):
     """
     Jump to a random location from 0 to MAX_INSTRUCTIONS.
     Assert IP is set to that location by jump.
     """
     for i in range(NUM_TESTS):
         vmachine.re_init()
         label_loc = random.randint(FIRST_INST_ADDRESS,MAX_INSTRUCTIONS)
         vmachine.labels["test_label"] = label_loc
         assemble("jmp test_label", vmachine)
         self.assertEqual(vmachine.get_ip(), label_loc)
Beispiel #3
0
    def test_call(self):
        """
        Tests call by both checking it jumped correctly and pushed correctly. 
        """
        for i in range(NUM_TESTS):
            vmachine.re_init()
            call_instr_addr = random.randint(FIRST_INST_ADDRESS, MAX_INSTRUCTIONS)
            label_loc = random.randint(FIRST_INST_ADDRESS, MAX_INSTRUCTIONS)

            # At the time of writing this test, blank lines are skipped by the tokenizer.
            # In order to have emu jump to the location of label_loc, we have to make
            # no-op lines to assign the correct locations to the lines we test.
            instructions = [NO_OP] * (MAX_INSTRUCTIONS+1)

            instructions[call_instr_addr] = "call " + TEST_LABEL + "\n"
            instructions[label_loc] = TEST_LABEL + ": " + instructions[label_loc]

            vmachine.labels[TEST_LABEL] = label_loc
            vmachine.set_ip(call_instr_addr)

            assemble("".join(instructions), vmachine, True)

            self.assertEqual(vmachine.get_ip(), label_loc)
            self.assertEqual(vmachine.stack[str(STACK_TOP)], call_instr_addr+1)