Пример #1
0
 def test_item_access_empty_return(self) -> None:
     parent_block = InstructionBlock()
     block = AbstractInstructionBlockStub([],
                                          InstructionPointer(
                                              parent_block, 84))
     self.assertEqual(GOTOInstruction(InstructionPointer(parent_block, 84)),
                      block[0])
     with self.assertRaises(IndexError):
         block[1]
     self.assertEqual(GOTOInstruction(InstructionPointer(parent_block, 84)),
                      block[-1])
     with self.assertRaises(IndexError):
         block[-2]
Пример #2
0
 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]
Пример #3
0
 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)
Пример #4
0
 def test_equality(self) -> None:
     blocks = [InstructionBlock(), InstructionBlock()]
     for offset in [0, 1, 23]:
         instrA = GOTOInstruction(InstructionPointer(blocks[0], offset))
         instrB = GOTOInstruction(InstructionPointer(blocks[0], offset))
         self.assertEqual(instrA, instrB)
         self.assertEqual(instrB, instrA)
     instrs = []
     for block in blocks:
         for offset in [0, 17]:
             instruction = GOTOInstruction(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)
Пример #5
0
 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)
Пример #6
0
    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)
Пример #7
0
    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)
Пример #8
0
    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"
        )
Пример #9
0
    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:]))
Пример #10
0
    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])
Пример #11
0
 def test_empty_returning_block(self) -> None:
     return_block = InstructionBlock()
     block = InstructionBlock()
     ip = InstructionPointer(return_block, 7)
     block.return_ip = ip
     self.__verify_block(block, [], [GOTOInstruction(ip)], ip)
Пример #12
0
 def test_str(self) -> None:
     block = DummyInstructionBlock()
     instr = GOTOInstruction(InstructionPointer(block, 3))
     self.assertEqual(
         "goto to {}".format(str(InstructionPointer(block, 3))), str(instr))
Пример #13
0
 def test_initialization(self) -> None:
     block = InstructionBlock()
     for offset in [0, 1, 23]:
         instr = GOTOInstruction(InstructionPointer(block, offset))
         self.assertIs(block, instr.target.block)
         self.assertEqual(offset, instr.target.offset)