def test_iterable_no_return(self) -> None: wf = DummyWaveform() block = AbstractInstructionBlockStub([EXECInstruction(wf)], None) count = 0 for expected_instruction, instruction in zip( [EXECInstruction(wf), STOPInstruction()], block): self.assertEqual(expected_instruction, instruction) count += 1 self.assertEqual(2, count)
def test_item_access_no_return(self) -> None: wf = DummyWaveform() block = AbstractInstructionBlockStub([EXECInstruction(wf)], None) self.assertEqual(EXECInstruction(wf), block[0]) self.assertEqual(STOPInstruction(), block[1]) with self.assertRaises(IndexError): block[2] self.assertEqual(STOPInstruction(), block[-1]) self.assertEqual(EXECInstruction(wf), block[-2]) with self.assertRaises(IndexError): block[-3]
def test_equality(self): wf1 = DummyWaveform() wf2 = DummyWaveform() instr11 = EXECInstruction(wf1) instr12 = EXECInstruction(wf1) instr20 = EXECInstruction(wf2) self.assertEqual(instr11, instr11) self.assertEqual(instr11, instr12) self.assertEqual(instr12, instr11) self.assertNotEqual(instr11, instr20) self.assertNotEqual(instr20, instr11) self.assertEqual(hash(instr11), hash(instr12)) self.assertNotEqual(hash(instr11), hash(instr20))
def test_iterable_return(self) -> None: parent_block = InstructionBlock() wf = DummyWaveform() block = AbstractInstructionBlockStub([EXECInstruction(wf)], InstructionPointer( parent_block, 11)) count = 0 for expected_instruction, instruction in zip([ EXECInstruction(wf), GOTOInstruction(InstructionPointer(parent_block, 11)) ], block): self.assertEqual(expected_instruction, instruction) count += 1 self.assertEqual(2, count)
def test_item_access_return(self) -> None: wf = DummyWaveform() parent_block = InstructionBlock() block = AbstractInstructionBlockStub([EXECInstruction(wf)], InstructionPointer( parent_block, 29)) self.assertEqual(EXECInstruction(wf), block[0]) self.assertEqual(GOTOInstruction(InstructionPointer(parent_block, 29)), block[1]) with self.assertRaises(IndexError): block[2] self.assertEqual(GOTOInstruction(InstructionPointer(parent_block, 29)), block[-1]) self.assertEqual(EXECInstruction(wf), block[-2]) with self.assertRaises(IndexError): block[-3]
def test_build_sequence(self) -> None: wf = DummyWaveform() sequencer = DummySequencer() block = DummyInstructionBlock() template = AtomicPulseTemplateStub(wf) template.build_sequence(sequencer, {}, {}, block) self.assertEqual([EXECInstruction(wf)], block.instructions)
def test_sliced_item_access(self) -> None: wf = DummyWaveform() parent_block = InstructionBlock() block = AbstractInstructionBlockStub( [EXECInstruction(wf), EXECInstruction(wf)], InstructionPointer(parent_block, 29)) for instruction in block[:-1]: self.assertEqual(EXECInstruction(wf), instruction) expections = [ EXECInstruction(wf), EXECInstruction(wf), GOTOInstruction(InstructionPointer(parent_block, 29)) ] for expected, instruction in zip(expections, block[:4]): self.assertEqual(expected, instruction) for instruction, expected in zip(block[::-1], reversed(expections)): self.assertEqual(expected, instruction) with self.assertRaises(StopIteration): next(iter(block[3:]))
def test_add_instruction_exec(self) -> None: block = InstructionBlock() expected_instructions = [] waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] LOOKUP = [0, 1, 1, 0, 2, 1, 0, 0, 0, 1, 2, 2] for id in LOOKUP: waveform = waveforms[id] instruction = EXECInstruction(waveform) expected_instructions.append(instruction) block.add_instruction_exec(waveform) expected_compiled_instructions = expected_instructions.copy() expected_compiled_instructions.append(STOPInstruction()) self.__verify_block(block, expected_instructions, expected_compiled_instructions, None)
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_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])
def test_len(self) -> None: block = AbstractInstructionBlockStub( [EXECInstruction(DummyWaveform())], None) self.assertEqual(2, len(block)) self.assertEqual(1, len(block.instructions))
def test_str(self) -> None: wf = DummyWaveform() instr = EXECInstruction(wf) self.assertEqual("exec {}".format(str(wf)), str(instr))
def test_initialization(self): waveform = DummyWaveform() instr = EXECInstruction(waveform) self.assertIs(waveform, instr.waveform)