Ejemplo n.º 1
0
 def test_evaluate_with_argument_to_resolve_named_as_unresolvable_function(
     self,
 ):
   self.function_likes.update({
       'f': pre_ast.DefineFunctionLike(
           name='f',
           arguments=['x'],
           replacement=c_ast.CFunctionCall(
               function_name='g',
               arguments=[c_ast.CVariable('h')],
           ),
           string_replacement='g(h)',
       ),
       'g': pre_ast.DefineFunctionLike(
           name='g',
           arguments=['f'],
           replacement=c_ast.CFunctionCall(
               function_name='f',
               arguments=[c_ast.CNumber(42)],
           ),
           string_replacement='f(42)',
       ),
   })
   expression = c_ast.CFunctionCall(
       function_name='f',
       arguments=[c_ast.CNumber(33)],
   )
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CFunctionCall(
       function_name='h',
       arguments=[c_ast.CNumber(42)],
   )
   self.assertEqual(actual, expected)
Ejemplo n.º 2
0
 def test_evaluate_against_mistnesting(
     self,
 ):
   self.function_likes.update({
       'twice': pre_ast.DefineFunctionLike(
           name='twice',
           arguments=['x'],
           replacement=c_ast.CFunctionCall(
               function_name='*',
               arguments=[
                   c_ast.CNumber(2),
                   c_ast.CVariable('x'),
               ],
           ),
           string_replacement='(2*(x))',
       ),
       'call_with_1': pre_ast.DefineFunctionLike(
           name='call_with_1',
           arguments=['x'],
           replacement=c_ast.CFunctionCall(
               function_name='x',
               arguments=[c_ast.CNumber(1)],
           ),
           string_replacement='(2*(x))',
       ),
   })
   expression = c_ast.CFunctionCall(
       function_name='call_with_1',
       arguments=[c_ast.CVariable('twice')],
   )
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CNumber(2)
   self.assertEqual(actual, expected)
Ejemplo n.º 3
0
 def test_evaluate_with_argument_passing_to_function_call(
     self,
 ):
   self.function_likes.update({
       'f': pre_ast.DefineFunctionLike(
           name='f',
           arguments=['x'],
           replacement=c_ast.CFunctionCall(
               function_name='g',
               arguments=[c_ast.CVariable('x')],
           ),
           string_replacement='g(x)',
       ),
       'g': pre_ast.DefineFunctionLike(
           name='g',
           arguments=['y'],
           replacement=c_ast.CVariable('y'),
           string_replacement='y',
       ),
   })
   expression = c_ast.CFunctionCall(
       function_name='f',
       arguments=[c_ast.CNumber(42)],
   )
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CNumber(42)
   self.assertEqual(actual, expected)
Ejemplo n.º 4
0
 def test_evaluate_nested_call_access_top_level_variable(
     self,
 ):
   self.object_likes.update({
       'x': pre_ast.DefineObjectLike(
           name='x',
           replacement=c_ast.CNumber(33),
           string_replacement='33',
       ),
   })
   self.function_likes.update({
       'f': pre_ast.DefineFunctionLike(
           name='f',
           arguments=['x'],
           replacement=c_ast.CFunctionCall(
               function_name='g',
               arguments=[],
           ),
           string_replacement='g()',
       ),
       'g': pre_ast.DefineFunctionLike(
           name='g',
           arguments=[],
           replacement=c_ast.CVariable('x'),
           string_replacement='x',
       ),
   })
   expression = c_ast.CVariable('x')
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CNumber(33)
   self.assertEqual(actual, expected)
Ejemplo n.º 5
0
 def test_evaluate_function_variable_cycle(
     self,
 ):
   self.object_likes.update({
       'y': pre_ast.DefineObjectLike(
           name='y',
           replacement=c_ast.CFunctionCall(
               function_name='f',
               arguments=[c_ast.CVariable('x')],
           ),
           string_replacement='f(x)',
       ),
   })
   self.function_likes.update({
       'f': pre_ast.DefineFunctionLike(
           name='f',
           arguments=['x'],
           replacement=c_ast.CVariable('y'),
           string_replacement='y',
       ),
   })
   expression = c_ast.CFunctionCall(
       function_name='f',
       arguments=[c_ast.CNumber(42)],
   )
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CFunctionCall(
       function_name='f',
       arguments=[c_ast.CVariable('x')],
   )
   self.assertEqual(actual, expected)
Ejemplo n.º 6
0
 def test_evaluate_function_call_with_identifier_defined_to_other_identifier(
     self,
 ):
   self.object_likes.update({
       'foo': pre_ast.DefineObjectLike(
           name='foo',
           replacement=c_ast.CVariable('bar'),
           string_replacement='bar',
       ),
   })
   self.function_likes.update({
       'bar': pre_ast.DefineFunctionLike(
           name='bar',
           arguments=['x'],
           replacement=c_ast.CNumber(42),
           string_replacement='42',
       ),
   })
   expression = c_ast.CFunctionCall(
       function_name='foo',
       arguments=[c_ast.CVariable('x')],
   )
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CNumber(42)
   self.assertEqual(actual, expected)
Ejemplo n.º 7
0
 def test_evaluate_function_argument_cycle_with_composition(
     self,
 ):
   self.function_likes.update({
       'f': pre_ast.DefineFunctionLike(
           name='f',
           arguments=['x'],
           replacement=c_ast.CFunctionCall(
               function_name='f',
               arguments=[
                   c_ast.CFunctionCall(
                       function_name='f',
                       arguments=[c_ast.CVariable('x')],
                   ),
               ],
           ),
           string_replacement='f(f(x))',
       ),
   })
   expression = c_ast.CFunctionCall(
       function_name='f',
       arguments=[c_ast.CNumber(42)],
   )
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CFunctionCall(
       function_name='f',
       arguments=[
           c_ast.CFunctionCall(
               function_name='f',
               arguments=[c_ast.CNumber(42)],
           )
       ]
   )
   self.assertEqual(actual, expected)
Ejemplo n.º 8
0
 def create_define_function_like(identifier, arguments, string_replacement):
   replacement = _try_to_parse(replacement_list, string_replacement)
   return pre_ast.DefineFunctionLike(
       name=identifier,
       arguments=arguments,
       replacement=replacement,
       string_replacement=string_replacement,
   )
 def test_preproces_define_function_like_with_already_defined_name(self):
     define_function_like_1 = pre_ast.DefineFunctionLike(
         name='some_name',
         arguments='some_arguments_1',
         replacement='some_replacement_1',
         string_replacement='some_string_replacement_2',
     )
     self.function_likes['some_name'] = define_function_like_1
     define_function_like_2 = pre_ast.DefineFunctionLike(
         name='some_name',
         arguments='some_arguments_2',
         replacement='some_replacement_2',
         string_replacement='some_string_replacement_2',
     )
     actual = self.preprocessing_visitor.preprocess(define_function_like_2)
     self.assertIsNone(actual)
     self.assertEquals(self.function_likes['some_name'],
                       define_function_like_2)
Ejemplo n.º 10
0
 def test_collect_includes_with_define_function_like(self):
     node = pre_ast.DefineFunctionLike(
         name='some_name',
         arguments='some_arguments',
         replacement='some_replacement',
         string_replacement='some_string_replacement',
     )
     actual = self.include_collector.collect_includes(node)
     expected = []
     self.assertEqual(actual, expected)
Ejemplo n.º 11
0
 def test_evaluate_cycle_in_function_like_definitions(
     self,
 ):
   self.function_likes.update({
       'foo': pre_ast.DefineFunctionLike(
           name='foo',
           arguments=[],
           replacement=c_ast.CFunctionCall(
               function_name='bar',
               arguments=[],
           ),
           string_replacement='bar()',
       ),
       'bar': pre_ast.DefineFunctionLike(
           name='bar',
           arguments=[],
           replacement=c_ast.CFunctionCall(
               function_name='baz',
               arguments=[],
           ),
           string_replacement='baz()',
       ),
       'baz': pre_ast.DefineFunctionLike(
           name='baz',
           arguments=[],
           replacement=c_ast.CFunctionCall(
               function_name='foo',
               arguments=[],
           ),
           string_replacement='foo()',
       ),
   })
   expression = c_ast.CFunctionCall(
       function_name='foo',
       arguments=[],
   )
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CFunctionCall(
       function_name='foo',
       arguments=[],
   )
   self.assertEqual(actual, expected)
 def test_parse_define_funcion_like_without_arguments(self):
     source = '#define foo() bar'
     actual = self.parser.parse(source)
     expected = pre_ast.File(content=pre_ast.CompositeBlock([
         pre_ast.DefineFunctionLike(
             name='foo',
             arguments=[],
             replacement=c_ast.CVariable('bar'),
             string_replacement=' bar',
         ),
     ]))
     self.assertEqual(actual, expected)
 def test_preproces_undef_with_existing_function_like(self):
     define_function_like = pre_ast.DefineFunctionLike(
         name='some_name',
         arguments='some_arguments',
         replacement='some_replacement',
         string_replacement='some_string_replacement',
     )
     self.function_likes['some_name'] = define_function_like
     undef = pre_ast.Undef('some_name')
     actual = self.preprocessing_visitor.preprocess(undef)
     self.assertIsNone(actual)
     self.assertFalse('some_name' in self.function_likes)
 def test_parse_define_empty_funcion_like(self):
     source = '#define foo()'
     actual = self.parser.parse(source)
     expected = pre_ast.File(content=pre_ast.CompositeBlock([
         pre_ast.DefineFunctionLike(
             name='foo',
             arguments=[],
             replacement=None,
             string_replacement='',
         ),
     ]))
     self.assertEqual(actual, expected)
Ejemplo n.º 15
0
 def test_evaluate_no_implicit_function_like_argument_pass(
     self,
 ):
   self.function_likes.update({
       'f': pre_ast.DefineFunctionLike(
           name='f',
           arguments=['x'],
           replacement=c_ast.CFunctionCall(
               function_name='g',
               arguments=[],
           ),
           string_replacement='g()',
       ),
       'g': pre_ast.DefineFunctionLike(
           name='g',
           arguments=[],
           replacement=c_ast.CVariable('x'),
           string_replacement='x',
       ),
   })
   expression = c_ast.CVariable('x')
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CVariable('x')
   self.assertEqual(actual, expected)
 def test_parse_define_statement_as_functional_like(self):
     source = '#define module_init(x)  __initcall(x);'
     actual = self.parser.parse(source)
     expected = pre_ast.File(content=pre_ast.CompositeBlock([
         pre_ast.DefineFunctionLike(
             name='module_init',
             arguments=['x'],
             replacement=pre_ast.CompositeBlock([
                 c_ast.CFunctionCall(
                     function_name='__initcall',
                     arguments=[c_ast.CVariable('x')],
                 ),
                 c_ast.CLiteral(';'),
             ]),
             string_replacement='  __initcall(x);',
         )
     ]))
     self.assertEqual(actual, expected)
Ejemplo n.º 17
0
 def test_evaluate_defined_with_defined_function_like(
     self,
 ):
   self.function_likes.update({
       'f': pre_ast.DefineFunctionLike(
           name='f',
           arguments=['x'],
           replacement=c_ast.CVariable('x'),
           string_replacement='x',
       ),
   })
   expression = c_ast.CFunctionCall(
       function_name='defined',
       arguments=[c_ast.CVariable('f')],
   )
   actual = self.evaluator.evaluate(expression)
   expected = c_ast.CNumber(1)
   self.assertEqual(actual, expected)
    def test_preproces_undef_with_undefined_name(self):
        define_object_like = pre_ast.DefineObjectLike(
            name='some_name_1',
            replacement='some_replacement_1',
            string_replacement='some_string_replacement_1',
        )
        self.object_likes['some_name_1'] = define_object_like

        define_function_like = pre_ast.DefineFunctionLike(
            name='some_name_2',
            arguments='some_arguments_2',
            replacement='some_replacement_2',
            string_replacement='some_string_replacement_2',
        )
        self.function_likes['some_name_2'] = define_function_like

        undef = pre_ast.Undef('some_name_3')
        actual = self.preprocessing_visitor.preprocess(undef)
        self.assertIsNone(actual)
        self.assertEqual(self.object_likes['some_name_1'], define_object_like)
        self.assertEqual(self.function_likes['some_name_2'],
                         define_function_like)
        self.assertFalse('some_name_3' in self.object_likes)
        self.assertFalse('some_name_3' in self.function_likes)