def test_equality(self): instr1 = STOPInstruction() instr2 = STOPInstruction() self.assertEqual(instr1, instr1) self.assertEqual(instr1, instr2) self.assertEqual(instr2, instr1) self.assertEqual(hash(instr1), hash(instr2))
def test_item_access_empty_no_return(self) -> None: block = AbstractInstructionBlockStub([], None) self.assertEqual(STOPInstruction(), block[0]) with self.assertRaises(IndexError): block[1] self.assertEqual(STOPInstruction(), block[-1]) with self.assertRaises(IndexError): block[-2]
def test_add_instruction_stop(self) -> None: block = InstructionBlock() expected_instructions = [STOPInstruction(), STOPInstruction()] block.add_instruction_stop() block.add_instruction_stop() expected_compiled_instructions = expected_instructions.copy() expected_compiled_instructions.append(STOPInstruction()) self.__verify_block(block, expected_instructions, expected_compiled_instructions, None)
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_build_path_o2_m2_i1_t_i1_t_m2_i0_i0_one_element_custom_block_one_element_main_block(self) -> None: sequencer = Sequencer() ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} elem_main = DummySequencingElement(False) sequencer.push(elem_main, ps, cs) target_block = InstructionBlock() elem_cstm = DummySequencingElement(False) sequencer.push(elem_cstm, ps, cs, target_block=target_block) sequence = sequencer.build() self.assertTrue(sequencer.has_finished()) self.assertIs(elem_main, sequence[0].elem) self.assertEqual(ps, elem_main.parameters) self.assertEqual(cs, elem_main.conditions) self.assertEqual(1, elem_main.requires_stop_call_counter) self.assertEqual(1, elem_main.build_call_counter) self.assertIs(target_block, elem_cstm.target_block) self.assertEqual(ps, elem_cstm.parameters) self.assertEqual(cs, elem_cstm.conditions) self.assertEqual(1, elem_cstm.requires_stop_call_counter) self.assertEqual(1, elem_cstm.build_call_counter) self.assertEqual(STOPInstruction(), sequence[1]) self.assertEqual(2, len(sequence))
def test_build_path_o2_m1_i2_tf_m2_i1_f_i1_f_two_elements_main_block_last_requires_stop_add_one_element_requires_stop_new_block(self) -> None: sequencer = Sequencer() ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} new_block = InstructionBlock() new_elem = DummySequencingElement(True) elem1 = DummySequencingElement(False, (new_block, [new_elem])) elem2 = DummySequencingElement(True) sequencer.push(elem2, ps, cs) sequencer.push(elem1, ps, cs) instr, stop = sequencer.build() self.assertFalse(sequencer.has_finished()) self.assertIs(elem1, instr.elem) self.assertEqual(ps, elem1.parameters) self.assertEqual(cs, elem1.conditions) self.assertEqual(1, elem1.requires_stop_call_counter) self.assertEqual(1, elem1.build_call_counter) self.assertEqual(ps, elem2.parameters) self.assertEqual(cs, elem2.conditions) self.assertEqual(2, elem2.requires_stop_call_counter) self.assertEqual(0, elem2.build_call_counter) self.assertEqual(ps, new_elem.parameters) self.assertEqual(cs, new_elem.conditions) self.assertEqual(1, new_elem.requires_stop_call_counter) self.assertEqual(0, new_elem.build_call_counter) self.assertEqual(STOPInstruction(), stop)
def test_build_path_o2_m1_i2_tt_m1_i0_two_elements_main_block(self) -> None: sequencer = Sequencer() ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} elem1 = DummySequencingElement(False) elem2 = DummySequencingElement(False) sequencer.push(elem2, ps, cs) sequencer.push(elem1, ps, cs) sequence = sequencer.build() self.assertTrue(sequencer.has_finished()) self.assertIs(elem1, sequence[0].elem) self.assertEqual(ps, elem1.parameters) self.assertEqual(cs, elem1.conditions) self.assertEqual(1, elem1.requires_stop_call_counter) self.assertEqual(1, elem1.build_call_counter) self.assertIs(elem2, sequence[1].elem) self.assertEqual(ps, elem2.parameters) self.assertEqual(cs, elem2.conditions) self.assertEqual(1, elem2.requires_stop_call_counter) self.assertEqual(1, elem2.build_call_counter) self.assertEqual(STOPInstruction(), sequence[2]) self.assertEqual(3, len(sequence))
def test_build_path_o2_m1_i1_t_m2_i0_i1_f_one_element_main_block_adds_one_element_requires_stop_new_block(self) -> None: sequencer = Sequencer() ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} new_block = InstructionBlock() new_elem = DummySequencingElement(True) elem = DummySequencingElement(False, (new_block, [new_elem])) sequencer.push(elem, ps, cs) sequence = sequencer.build() self.assertFalse(sequencer.has_finished()) self.assertIs(elem, sequence[0].elem) self.assertEqual(ps, elem.parameters) self.assertEqual(cs, elem.conditions) self.assertEqual(1, elem.requires_stop_call_counter) self.assertEqual(1, elem.build_call_counter) self.assertEqual(ps, new_elem.parameters) self.assertEqual(cs, new_elem.conditions) self.assertEqual(1, new_elem.requires_stop_call_counter) self.assertEqual(0, new_elem.build_call_counter) self.assertEqual(STOPInstruction(), sequence[1]) self.assertEqual(2, len(sequence))
def test_create_embedded_block(self) -> None: parent_block = InstructionBlock() block = InstructionBlock() block.return_ip = InstructionPointer(parent_block, 18) self.__verify_block( block, [], [GOTOInstruction(InstructionPointer(parent_block, 18))], InstructionPointer(parent_block, 18)) self.__verify_block(parent_block, [], [STOPInstruction()], None)
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_add_instruction_goto(self) -> None: block = InstructionBlock() expected_instructions = [] targets = [InstructionBlock(), InstructionBlock(), InstructionBlock()] LOOKUP = [0, 1, 1, 0, 2, 1, 0, 0, 0, 1, 2, 2] for id in LOOKUP: target = targets[id] instruction = GOTOInstruction(InstructionPointer(target)) expected_instructions.append(instruction) block.add_instruction_goto(target) expected_compiled_instructions = expected_instructions.copy() expected_compiled_instructions.append(STOPInstruction()) self.__verify_block(block, expected_instructions, expected_compiled_instructions, None)
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_add_instruction_repj(self) -> None: block = InstructionBlock() expected_instructions = [] targets = [InstructionBlock(), InstructionBlock(), InstructionBlock()] counts = [3, 8, 857] LOOKUP = [(0, 0), (0, 1), (1, 1), (0, 2), (2, 0), (1, 0), (2, 2), (2, 1), (1, 0), (1, 2)] for i in LOOKUP: block.add_instruction_repj(counts[i[0]], targets[i[1]]) expected_instructions.append( REPJInstruction(counts[i[0]], InstructionPointer(targets[i[1]], 0))) expected_compiled_instructions = expected_instructions.copy() expected_compiled_instructions.append(STOPInstruction()) self.__verify_block(block, expected_instructions, expected_compiled_instructions, None)
def test_build_path_o2_m2_i2_tt_t_i2_tt_m2_i0_i0_two_elements_custom_block_two_element_main_block(self) -> None: sequencer = Sequencer() ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} wm = {'foo': 'bar'} target_block = InstructionBlock() elem2 = DummySequencingElement(False) sequencer.push(elem2, ps, cs, window_mapping=wm, target_block=target_block) elem1 = DummySequencingElement(False) sequencer.push(elem1, ps, cs, window_mapping=wm, target_block=target_block) elem_main2 = DummySequencingElement(False) sequencer.push(elem_main2, ps, cs) elem_main1 = DummySequencingElement(False) sequencer.push(elem_main1, ps, cs) sequence = sequencer.build() self.assertTrue(sequencer.has_finished()) self.assertIs(target_block, elem1.target_block) self.assertEqual(ps, elem1.parameters) self.assertEqual(cs, elem1.conditions) self.assertEqual(1, elem1.requires_stop_call_counter) self.assertEqual(1, elem1.build_call_counter) self.assertIs(target_block, elem2.target_block) self.assertEqual(ps, elem2.parameters) self.assertEqual(cs, elem2.conditions) self.assertEqual(1, elem2.requires_stop_call_counter) self.assertEqual(1, elem2.build_call_counter) self.assertIs(elem_main1, sequence[0].elem) self.assertEqual(ps, elem_main1.parameters) self.assertEqual(cs, elem_main1.conditions) self.assertEqual(1, elem_main1.requires_stop_call_counter) self.assertEqual(1, elem_main1.build_call_counter) self.assertIs(elem_main2, sequence[1].elem) self.assertEqual(ps, elem_main2.parameters) self.assertEqual(cs, elem_main2.conditions) self.assertEqual(1, elem_main2.requires_stop_call_counter) self.assertEqual(1, elem_main2.build_call_counter) self.assertEqual(STOPInstruction(), sequence[2]) self.assertEqual(3, len(sequence))
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_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_empty_unreturning_block(self) -> None: block = InstructionBlock() self.__verify_block(block, [], [STOPInstruction()], None)
def test_str(self): instr = STOPInstruction() self.assertEqual('stop', str(instr))