def test_equality(self) -> None: t1 = Trigger() t2 = Trigger() self.assertEqual(t1, t1) self.assertNotEqual(t1, t2) self.assertNotEqual(t2, t1) self.assertNotEqual(hash(t1), hash(t2))
def test_str(self) -> None: block = DummyInstructionBlock() trigger = Trigger() instr = CJMPInstruction(trigger, InstructionPointer(block, 3)) self.assertEqual( "cjmp to {} on {}".format(InstructionPointer(block, 3), trigger), str(instr))
def test_build_sequence_loop(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() block.add_instruction(DummyInstruction()) delegator = DummySequencingElement() body = DummySequencingElement() trigger = Trigger() condition = HardwareCondition(trigger) condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, {}, block) self.assertEqual(1, len(block.embedded_blocks)) body_block = block.embedded_blocks[0] self.assertEqual( [ DummyInstruction(), CJMPInstruction(trigger, InstructionPointer(body_block)) ], block.instructions, "The expected conditional jump was not generated by HardwareConditon." ) self.assertEqual( InstructionPointer(block, 1), body_block.return_ip, "The return address of the loop body block was set wrongly by HardwareCondition." ) self.assertEqual( {body_block: [(body, {}, {}, {}, {})]}, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the body element to the stack" ) self.assertFalse(condition.requires_stop())
def test_nested_cjmp(self) -> None: parent_block = InstructionBlock() block = InstructionBlock() block.return_ip = InstructionPointer(parent_block, 1) parent_block.add_instruction_cjmp(Trigger(), block) context = dict() immutable_block = ImmutableInstructionBlock(parent_block, context) self.__verify_block(parent_block, immutable_block, context)
def test_initialization(self) -> None: block = InstructionBlock() trigger = Trigger() for offset in [0, 1, 23]: instr = CJMPInstruction(trigger, InstructionPointer(block, offset)) self.assertEqual(trigger, instr.trigger) self.assertEqual(block, instr.target.block) self.assertEqual(offset, instr.target.offset)
def test_add_instruction_cjmp(self) -> None: block = InstructionBlock() expected_instructions = [] targets = [InstructionBlock(), InstructionBlock(), InstructionBlock()] 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]], InstructionPointer(targets[i[0]], 0))) expected_compiled_instructions = expected_instructions.copy() expected_compiled_instructions.append(STOPInstruction()) self.__verify_block(block, expected_instructions, expected_compiled_instructions, None)
def test_equality(self) -> None: blocks = [InstructionBlock(), InstructionBlock()] for offset in [0, 1, 23]: instrA = CJMPInstruction(0, InstructionPointer(blocks[0], offset)) instrB = CJMPInstruction(0, InstructionPointer(blocks[0], offset)) self.assertEqual(instrA, instrB) self.assertEqual(instrB, instrA) instrs = [] for trigger in [Trigger(), Trigger()]: for block in blocks: for offset in [0, 17]: instruction = CJMPInstruction( trigger, InstructionPointer(block, offset)) self.assertEqual(instruction, instruction) for other in instrs: self.assertNotEqual(instruction, other) self.assertNotEqual(other, instruction) self.assertNotEqual(hash(instruction), hash(other)) instrs.append(instruction)
def test_multiple_nested_block_construction(self) -> None: main_block = InstructionBlock() blocks = [] waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] main_block.add_instruction_exec(waveforms[0]) block = InstructionBlock() trigger = Trigger() ip = InstructionPointer(block) main_block.add_instruction_cjmp(trigger, block) block.return_ip = InstructionPointer(main_block, len(main_block)) blocks.append(block) block = InstructionBlock() trigger = Trigger() ip = InstructionPointer(block) main_block.add_instruction_cjmp(trigger, block) 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] block.add_instruction_exec(waveform) block = InstructionBlock() ip = InstructionPointer(block) blocks[0].add_instruction_cjmp(trigger, block) block.return_ip = InstructionPointer(blocks[0], len(blocks[0])) blocks.append(block) for id in [1, 2, 0, 2]: waveform = waveforms[id] block.add_instruction_exec(waveform) context = dict() immutable_block = ImmutableInstructionBlock(main_block, context) self.__verify_block(main_block, immutable_block, context.copy())
def test_str(self) -> None: IB = InstructionBlock() T = Trigger() W = DummyWaveform() a = [ W, T, InstructionPointer(IB, 1), CJMPInstruction(T, IB), GOTOInstruction(IB), EXECInstruction(W), IB ] b = [x.__str__() for x in a] for s in b: self.assertIsInstance(s, str)
def test_build_sequence_branch(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() delegator = DummySequencingElement() if_branch = DummySequencingElement() else_branch = DummySequencingElement() trigger = Trigger() condition = HardwareCondition(trigger) condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, {}, instruction_block=block) self.assertEqual(2, len(block.embedded_blocks)) if_block = block.embedded_blocks[0] else_block = block.embedded_blocks[1] self.assertEqual( [ CJMPInstruction(trigger, InstructionPointer(if_block)), GOTOInstruction(InstructionPointer(else_block)) ], block.instructions, "The expected jump instruction were not generated by HardwareConditon." ) self.assertEqual( InstructionPointer(block, 2), if_block.return_ip, "The return address of the if branch block was set wrongly by HardwareConditon." ) self.assertEqual( InstructionPointer(block, 2), else_block.return_ip, "The return address of the else branch block was set wrongly by HardwareConditon." ) self.assertEqual( { if_block: [(if_branch, {}, {}, {}, {})], else_block: [(else_branch, {}, {}, {}, {})] }, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the branch elements to the stack" )
def test_nested_block_construction(self) -> None: main_block = InstructionBlock() expected_instructions = [[], [], [], []] expected_compiled_instructions = [[], [], [], []] expected_return_ips = [None] blocks = [] waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] main_block.add_instruction_exec(waveforms[0]) expected_instructions[0].append(EXECInstruction(waveforms[0])) block = InstructionBlock() trigger = Trigger() ip = InstructionPointer(block) main_block.add_instruction_cjmp(trigger, block) expected_instructions[0].append(CJMPInstruction(trigger, ip)) block.return_ip = InstructionPointer(main_block, len(main_block)) expected_return_ips.append( InstructionPointer(main_block, len(main_block))) blocks.append(block) block = InstructionBlock() trigger = Trigger() ip = InstructionPointer(block) main_block.add_instruction_cjmp(trigger, block) expected_instructions[0].append(CJMPInstruction(trigger, ip)) block.return_ip = InstructionPointer(main_block, len(main_block)) expected_return_ips.append( 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 = InstructionBlock() ip = InstructionPointer(block) blocks[0].add_instruction_cjmp(trigger, block) expected_instructions[1].append(CJMPInstruction(trigger, ip)) block.return_ip = InstructionPointer(blocks[0], len(blocks[0])) expected_return_ips.append( 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)) positions = [0, None, None, None] positions[3] = len(expected_compiled_instructions[1]) self.__verify_block(blocks[2], expected_instructions[3], expected_compiled_instructions[3], expected_return_ips[3]) self.__verify_block(blocks[1], expected_instructions[2], expected_compiled_instructions[2], expected_return_ips[2]) self.__verify_block(blocks[0], expected_instructions[1], expected_compiled_instructions[1], expected_return_ips[1]) self.__verify_block(main_block, expected_instructions[0], expected_compiled_instructions[0], expected_return_ips[0])