def test_add_instruction_cjmp(self):
        block = InstructionBlock()
        expected_instructions = []
        expected_compiled_instructions = []
        
        targets = [(InstructionBlock(), 0), (InstructionBlock(), 1), (InstructionBlock(), 50)]
        triggers = [Trigger(), Trigger()]
        LOOKUP = [(0, 0), (1, 0), (1, 1), (0, 1), (2, 0), (1, 0), (0, 1), (0, 1), (0, 0), (1, 0), (2, 1), (2, 1)]
        for i in LOOKUP:
            block.add_instruction_cjmp(triggers[i[1]], targets[i[0]])
            expected_instructions.append(CJMPInstruction(triggers[i[1]], targets[i[0]], 0))

        expected_compiled_instructions = expected_instructions.copy()
        expected_compiled_instructions.append(STOPInstruction())
        self.__verify_block(block, expected_instructions, expected_compiled_instructions)
 def test_nested_block_construction(self):
     main_block = InstructionBlock()
     expected_instructions = [[], [], [], []]
     expected_compiled_instructions = [[], [], [], []]
     
     blocks = []
         
     waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()]
     
     main_block.add_instruction_exec(waveforms[0])
     expected_instructions[0].append(EXECInstruction(waveforms[0]))
     
     block = main_block.create_embedded_block() 
     trigger = Trigger()
     main_block.add_instruction_cjmp(trigger, block)
     expected_instructions[0].append(CJMPInstruction(trigger, block, 0))
     block.return_ip = InstructionPointer(main_block, len(main_block))
     blocks.append(block)
     
     block = main_block.create_embedded_block()
     trigger = Trigger()
     main_block.add_instruction_cjmp(trigger, block)
     expected_instructions[0].append(CJMPInstruction(trigger, block, 0))
     block.return_ip = InstructionPointer(main_block, len(main_block))
     blocks.append(block)
     
     WAVEFORM_LOOKUP = [[2, 2, 1, 1],[0, 1, 1, 0, 2, 1]]
     for i in [0, 1]:
         block = blocks[i]
         lookup = WAVEFORM_LOOKUP[i]
         for id in lookup:
             waveform = waveforms[id]
             expected_instructions[i + 1].append(EXECInstruction(waveform))
             block.add_instruction_exec(waveform)
         
     block = blocks[0].create_embedded_block()
     blocks[0].add_instruction_cjmp(trigger, block)
     expected_instructions[1].append(CJMPInstruction(trigger, block, 0))
     block.return_ip = InstructionPointer(blocks[0], len(blocks[0]))
     blocks.append(block)
     
     for id in [1, 2, 0, 2]:
         waveform = waveforms[id]
         expected_instructions[3].append(EXECInstruction(waveform))
         block.add_instruction_exec(waveform)
     
     for i in [0, 1, 2, 3]:
         expected_compiled_instructions[i] = expected_instructions[i].copy()
     
     expected_compiled_instructions[0].append(STOPInstruction())
     for i in [0, 1, 2]:
         expected_compiled_instructions[i + 1].append(GOTOInstruction(blocks[i].return_ip.block, blocks[i].return_ip.offset))
     
     positions = [0, None, None, None]                
     positions[3] = len(expected_compiled_instructions[1])
     
     expected_compiled_instructions[1].extend(expected_compiled_instructions[3])
     for i in [1, 2]:
         positions[i] = len(expected_compiled_instructions[0])
         expected_compiled_instructions[0].extend(expected_compiled_instructions[i])
         
     positions[3] += positions[1]
     
     self.__verify_block(blocks[2], expected_instructions[3], expected_compiled_instructions[3])
     self.__verify_block(blocks[1], expected_instructions[2], expected_compiled_instructions[2])
     self.__verify_block(blocks[0], expected_instructions[1], expected_compiled_instructions[1])
     self.__verify_block(main_block, expected_instructions[0], expected_compiled_instructions[0])
     
     self.assertEqual(positions[3], blocks[2].get_start_address())
     self.assertEqual(positions[2], blocks[1].get_start_address())
     self.assertEqual(positions[1], blocks[0].get_start_address())
     self.assertEqual(positions[0], main_block.get_start_address())
     
     for instruction in main_block.instructions:
         if isinstance(instruction, GOTOInstruction) or isinstance(instruction, CJMPInstruction):
             self.assertIsInstance(instruction.target.get_absolute_address(), int)