Beispiel #1
0
    def test_equal(self):
        self.assertEqual(Procedure([]), Procedure([]))
        self.assertEqual(Procedure([Boolean(True)]),
                         Procedure([Boolean(True)]))
        self.assertNotEqual(Procedure([Boolean(True)]),
                            Procedure([Boolean(False)]))
        self.assertNotEqual(Procedure([]), Procedure([Boolean(False)]))

        self.assertNotEqual(
            Procedure([]),
            Procedure([], locn=SourceLocation('path', line=1, column=1)))
Beispiel #2
0
    def test_one_named_procedure (self):
        sections = {}
        # Create a procedure and write it to a new output stream.
        proc = Procedure ([Boolean (True)])
        proc.write (sections)

        # Build the executable image that we'll load.
        content = Executable.new ()
        b = sections [SectionType.text].getbuffer ()
        content.symbols.append (Symbol (name='n1', address=0, size=len (b)))
        content.data [SectionType.text] = b

        self.assertDictEqual (load (content), {'n1': proc})
Beispiel #3
0
    def test_empty_procedure(self):
        filename = 'filename'
        first = 47
        locn = SourceLocation(srcfile=filename, line=first, column=7)
        proc = Procedure([], locn=locn)

        result = rebase.rebase_source_info(proc)

        expected_proc = Procedure([],
                                  locn=SourceLocation(srcfile=filename,
                                                      line=0,
                                                      column=7))
        self.assertEqual(result.procedure, expected_proc)
        self.assertEqual(result.line_base, first)
Beispiel #4
0
    def test_procedure_with_contents(self):
        filename = 'filename'
        first = 16
        proc = Procedure(self._make_body(srcfile=filename, first=first),
                         locn=SourceLocation(srcfile=filename,
                                             line=first,
                                             column=1))

        result = rebase.rebase_source_info(proc)

        expected_proc = Procedure(self._make_body(srcfile=filename, first=0),
                                  locn=SourceLocation(srcfile=filename,
                                                      line=0,
                                                      column=1))
        self.assertEqual(result.procedure, expected_proc)
        self.assertEqual(result.line_base, first)
Beispiel #5
0
 def test_procedure_with_one_operator_used_twice(self):
     fixups = procedure_fixups(
         Procedure([
             Operator('foo'),
             Operator('foo'),
         ]))
     self.assertEqual(fixups, ['foo'])
Beispiel #6
0
 def test_procedure_with_two_different_operators(self):
     fixups = procedure_fixups(
         Procedure([
             Operator('foo'),
             Operator('bar'),
         ]))
     self.assertEqual(sorted(fixups), sorted(['foo', 'bar']))
Beispiel #7
0
def _push_for(m: 'machine.Machine', control: float, increment: float,
              limit: float, proc: Procedure) -> None:
    # Create a function which will handle the end of this iteration of the loop by invoking
    # self.__for_next() again.
    next_iteration = functools.partial(_for_next,
                                       control=control,
                                       limit=limit,
                                       increment=increment,
                                       proc=proc)

    m.operand_push(control)
    # FIXME: I set the source location to that of the procedure. It really ought to be the location of the
    # original 'for'.
    m.exec_s.push(BuiltinState(next_iteration, locn=proc.locn()))
    m.execution_push_proc(proc)
Beispiel #8
0
    def test_procedure_round_trip(self) -> None:
        Procedure([
            Number(value=42.0,
                   locn=SourceLocation(srcfile="foo.toy", line=23, column=29)),
            Number(value=68.0,
                   locn=SourceLocation(srcfile="foo.toy", line=24, column=31))
        ],
                  locn=SourceLocation(srcfile="foo.toy", line=22,
                                      column=1)).write(self.__sections)
        self.__reset()

        n2 = Instruction.read(self.__sections)
        self.assertIsInstance(n2, Procedure)

        expected = [Number(value=42.0), Number(value=68.0)]
        instructions = n2.instructions()
        self.assertSequenceEqual(expected, instructions)

        n2.read_debug(binary=self.__sections[SectionType.debug_line],
                      line_base=0)
        self.assertEqual(SourceLocation(srcfile="foo.toy", line=23, column=29),
                         instructions[0].locn())
        self.assertEqual(SourceLocation(srcfile="foo.toy", line=24, column=31),
                         instructions[1].locn())
Beispiel #9
0
 def test_procedure_with_one_operator(self):
     fixups = procedure_fixups(Procedure([Operator("foo")]))
     self.assertEqual(fixups, ['foo'])
Beispiel #10
0
 def test_execute(self):
     m = Mock(spec=Machine)
     Procedure([Boolean(True)]).execute(m)
     m.operand_push.assert_called_once_with(Procedure([Boolean(True)]))
Beispiel #11
0
 def test_empty_procedure_no_location(self):
     body = []
     proc = Procedure(body, locn=None)
     result = rebase.rebase_source_info(proc)
     self.assertEqual(result.procedure, Procedure(body))
     self.assertEqual(result.line_base, None)
Beispiel #12
0
 def test_nested_procedure(self):
     inner_proc = Procedure([Operator('foo')])
     outer_proc = Procedure([Operator('bar'), inner_proc])
     fixups = procedure_fixups(outer_proc)
     self.assertEqual(sorted(fixups), sorted(['foo', 'bar']))
Beispiel #13
0
 def test_procedure_with_builtin_operator(self):
     name = list(Machine().systemdict().keys())[0]
     fixups = procedure_fixups(Procedure([Operator(name), Number(1.0)]))
     self.assertEqual(fixups, [])
Beispiel #14
0
 def test_procedure_with_unnamed_instruction(self):
     fixups = procedure_fixups(Procedure([Operator('foo'), Number(1.0)]))
     self.assertEqual(fixups, ['foo'])
Beispiel #15
0
 def test_instructions(self):
     self.assertSequenceEqual(
         Procedure([Boolean(True), Boolean(False)]).instructions(),
         [Boolean(True), Boolean(False)])
Beispiel #16
0
 def test_empty_procedure(self):
     fixups = procedure_fixups(Procedure([]))
     self.assertEqual(fixups, [])
Beispiel #17
0
 def test_name(self):
     self.assertEqual(Procedure([]).name(), None)
Beispiel #18
0
 def test_call(self):
     m = Mock(spec=Machine)
     body = [Boolean(True)]
     proc = Procedure(body)
     proc(m)
     m.execution_push_proc.assert_called_once_with(body)
Beispiel #19
0
 def test_iter(self):
     proc = Procedure([Boolean(True), Boolean(False)])
     self.assertSequenceEqual(list(p for p in proc),
                              [Boolean(True), Boolean(False)])
Beispiel #20
0
 def _get_digest(self, *args, **kwargs):
     h = hashlib.new('md5')
     Procedure(*args, **kwargs).digest(h)
     return h.digest()