def test_evaluate_same_function_call_as_argument(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='f', arguments=[ 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)
def test_parse_pragma_with_argument_with_natural_number_argument(self): source = '#pragma foo(42)' actual = self.parser.parse(source) expected = pre_ast.File(content=pre_ast.CompositeBlock([ pre_ast.Pragma([ pre_ast.PragmaArgument( name='foo', arguments=[ c_ast.CNumber(42), ], ), ]), ]), ) self.assertEqual(actual, expected)
def test_parse_binary_operators_with_parentheses_on_left(self): source = '(x == 4 && y >= 3) || y > 4' expected = c_ast.CFunctionCall( function_name='||', arguments=[ c_ast.CNestedExpression( opener='(', content=c_ast.CFunctionCall( function_name='&&', arguments=[ c_ast.CFunctionCall( function_name='==', arguments=[ c_ast.CVariable('x'), c_ast.CNumber(4), ], ), c_ast.CFunctionCall(function_name='>=', arguments=[ c_ast.CVariable('y'), c_ast.CNumber(3), ]), ], ), closer=')', ), c_ast.CFunctionCall( function_name='>', arguments=[ c_ast.CVariable('y'), c_ast.CNumber(4), ], ), ], ) actual = self.parser.parseString(source, parseAll=True) self.assertEqual(actual.asList(), [expected])
def test_parse_define_object_like_as_numeric_constant_with_multiline_comment( self, ): source = """ #define foo 42 /* bar baz */ """ actual = self.parser.parse(source) expected = pre_ast.File(content=pre_ast.CompositeBlock([ pre_ast.DefineObjectLike( name='foo', replacement=c_ast.CNumber(42), string_replacement=' 42 ', ), ]), ) self.assertEqual(actual, expected)
def test_evaluate_function_like_with_simple_recursion( self, ): self.function_likes.update({ 'f': pre_ast.DefineFunctionLike( name='f', arguments=['x'], replacement=c_ast.CFunctionCall( function_name='f', arguments=[c_ast.CVariable('x')], ), string_replacement='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.CNumber(42)], ) self.assertEqual(actual, expected)
def test_expand_with_function_call(self): source = 'foo(bar, 42)' self.expression_evaluator.evaluate.return_value = c_ast.CVariable( 'baz') actual = self.macro_expander.expand(source) self.expression_evaluator.evaluate.assert_called_with( c_ast.CFunctionCall( function_name='foo', arguments=[ c_ast.CVariable('bar'), c_ast.CNumber(42), ], ), ) expected = 'baz' self.assertEqual(actual, expected)
def test_parse_if_with_comments(self): source = """ #if 0 /* foo bar */ /* 42 */ #endif """ actual = self.parser.parse(source) expected = pre_ast.File(content=pre_ast.CompositeBlock([ pre_ast.If(conditional_blocks=[ pre_ast.ConditionalBlock( conditional_expression=c_ast.CNumber(0), content=pre_ast.CompositeBlock([]), ), ], ), ]), ) self.assertEqual(actual, expected)
def test_division(self): actual = self.preprocessor_and_64bit_functions['/']( c_ast.CNumber(33), c_ast.CNumber(5), ) self.assertEqual(actual, c_ast.CNumber(6)) actual = self.preprocessor_and_64bit_functions['/']( c_ast.CNumber(21), c_ast.CNumber(7), ) self.assertEqual(actual, c_ast.CNumber(3))
def test_evaluate_defined_with_defined_object_like( self, ): self.object_likes.update({ 'x': pre_ast.DefineObjectLike( name='x', replacement=c_ast.CLiteral(''), string_replacement='', ), }) expression = c_ast.CFunctionCall( function_name='defined', arguments=[c_ast.CVariable('x')], ) actual = self.evaluator.evaluate(expression) expected = c_ast.CNumber(1) self.assertEqual(actual, expected)
def test_modulo(self): actual = self.preprocessor_and_64bit_functions['%']( c_ast.CNumber(33), c_ast.CNumber(5), ) self.assertEqual(actual, c_ast.CNumber(3)) actual = self.preprocessor_and_64bit_functions['%']( c_ast.CNumber(21), c_ast.CNumber(7), ) self.assertEqual(actual, c_ast.CNumber(0))
def test_ternary_conditional(self): actual = self.preprocessor_and_64bit_functions['?:']( True, c_ast.CNumber(42), c_ast.CNumber(33), ) self.assertEqual(actual, c_ast.CNumber(42)) actual = self.preprocessor_and_64bit_functions['?:']( False, c_ast.CNumber(42), c_ast.CNumber(33), ) self.assertEqual(actual, c_ast.CNumber(33))
def test_get_descripiton_with_array_of_structs(self): type_definition = c_ast.CArray( length=c_ast.CNumber(33), type_definition=c_ast.CTypeReference('struct some_struct'), evaluated_length=33, ) self.typedef_resolver.resolve.return_value = ( c_ast.CTypeReference('struct some_struct')) actual = self.type_description_visitor.get_description( type_definition, self.types, ) expected = [ 'Array', { 'count': 33, 'target_args': None, 'target': 'some_struct' } ] self.assertEqual(actual, expected)
def test_parse_with_empty_if_block_and_expression(self): source = """ #if CONFIG_SOMETHING == 32 #endif """ actual = self.parser.parse(source) expected = pre_ast.File(content=pre_ast.CompositeBlock([ pre_ast.If(conditional_blocks=[ pre_ast.ConditionalBlock( conditional_expression=c_ast.CFunctionCall( function_name='==', arguments=[ c_ast.CVariable('CONFIG_SOMETHING'), c_ast.CNumber(32), ], ), content=pre_ast.CompositeBlock([]), ), ], ), ]), ) self.assertEqual(actual, expected)
def test_expand_struct_definition(self): source = """ struct s { FOO bar; BAR(33, 51) *baz[42]; }; """ struct_evaluation = c_ast.CVariable('struct') s_evaluation = c_ast.CVariable('s') foo_evaluation = c_ast.CVariable('int') bar_evaluation = c_ast.CVariable('x') bar_of_33_51_baz_42_evaluation = c_ast.CFunctionCall( function_name='*', arguments=[ c_ast.CVariable('float'), c_ast.CFunctionCall( function_name='[]', arguments=[ c_ast.CVariable('y'), c_ast.CNumber(42), ], ), ], ) self.expression_evaluator.evaluate.side_effect = ( struct_evaluation, s_evaluation, foo_evaluation, bar_evaluation, bar_of_33_51_baz_42_evaluation, ) actual = self.macro_expander.expand(source) expected = """ struct s { int x ; float * y[42] ; }; """ self.assertEqual(actual, expected)
def test_get_descripiton_with_array_of_pointers_to_struct(self): type_definition = c_ast.CArray( length=c_ast.CNumber(42), type_definition=c_ast.CPointer(c_ast.CTypeReference('struct s')), evaluated_length=42, ) self.typedef_resolver.resolve.return_value = ( c_ast.CTypeReference('struct s')) actual = self.type_description_visitor.get_description( type_definition, self.types, ) expected = [ 'Array', { 'count': 42, 'target_args': { 'target_args': None, 'target': 's', }, 'target': 'Pointer' } ] self.assertEqual(actual, expected)
def get_x86_64_kernel_compile_object_likes(): return { '__STDC__': pre_ast.DefineObjectLike( name='__STDC__', replacement=c_ast.CNumber(1), string_replacement='1', ), '__STDC_VERSION__': pre_ast.DefineObjectLike( name='__STDC_VERSION__', replacement=c_ast.CNumber(201112L), string_replacement='201112L', ), '_XOPEN_SOURCE': pre_ast.DefineObjectLike( name='_XOPEN_SOURCE', replacement=c_ast.CNumber(500), string_replacement='500', ), '__GNUC__': pre_ast.DefineObjectLike( name='__GNUC__', replacement=c_ast.CNumber(4), string_replacement='4', ), '__GNUC_MINOR__': pre_ast.DefineObjectLike( name='__GNUC_MINOR__', replacement=c_ast.CNumber(8), string_replacement='8', ), '__GNUC_PATCHLEVEL__': pre_ast.DefineObjectLike( name='__GNUC_PATCHLEVEL__', replacement=c_ast.CNumber(4), string_replacement='4', ), '__x86_64__': pre_ast.DefineObjectLike( name='__x86_64__', replacement=c_ast.CNumber(1), string_replacement='1', ), '__KERNEL__': pre_ast.DefineObjectLike( name='__KERNEL__', replacement=c_ast.CLiteral(''), string_replacement='', ), }
def test_shift_right(self): actual = self.preprocessor_and_64bit_functions['>>']( c_ast.CNumber(42), c_ast.CNumber(3), ) self.assertEqual(actual, c_ast.CNumber(5))
def test_visit_number(self): number = c_ast.CNumber(42) actual = self.expression_evaluator.evaluate(number) self.assertEqual(actual, 42)
def test_lazy_or_with_true_first(self): self.evaluate.return_value = c_ast.CNumber(24) actual = self.lazy_functions['||'](self.evaluate, 42, 33) self.evaluate.assert_called_once_with(42) expected = c_ast.CNumber(24) self.assertEqual(actual, expected)
def test_lazy_and_with_false_first(self): self.evaluate.return_value = c_ast.CNumber(0) actual = self.lazy_functions['&&'](self.evaluate, 42, 33) self.evaluate.assert_called_once_with(42) expected = c_ast.CNumber(0) self.assertEqual(actual, expected)
def test_bitwise_negation(self): actual = self.preprocessor_and_64bit_functions['~'](c_ast.CNumber(42)) self.assertEqual(actual, c_ast.CNumber(-43))
def test_logical_or(self): or_ = self.preprocessor_and_64bit_functions['||'] self.assertEqual( or_(c_ast.CNumber(1), c_ast.CNumber(1)), c_ast.CNumber(1), ) self.assertEqual( or_(c_ast.CNumber(1), c_ast.CNumber(0)), c_ast.CNumber(1), ) self.assertEqual( or_(c_ast.CNumber(0), c_ast.CNumber(1)), c_ast.CNumber(1), ) self.assertEqual( or_(c_ast.CNumber(0), c_ast.CNumber(0)), c_ast.CNumber(0), )
def test_logical_and(self): and_ = self.preprocessor_and_64bit_functions['&&'] self.assertEqual( and_(c_ast.CNumber(1), c_ast.CNumber(1)), c_ast.CNumber(1), ) self.assertEqual( and_(c_ast.CNumber(1), c_ast.CNumber(0)), c_ast.CNumber(0), ) self.assertEqual( and_(c_ast.CNumber(0), c_ast.CNumber(1)), c_ast.CNumber(0), ) self.assertEqual( and_(c_ast.CNumber(0), c_ast.CNumber(0)), c_ast.CNumber(0), )
def test_multiplication(self): actual = self.preprocessor_and_64bit_functions['*']( c_ast.CNumber(5), c_ast.CNumber(7), ) self.assertEqual(actual, c_ast.CNumber(35))
def test_parse_with_module_flag(self): config = 'FLAG=m' actual = self.parser.parse(config) expected = {'FLAG_MODULE': c_ast.CNumber(1)} self.assertEqual(actual, expected)
def test_shift_left(self): actual = self.preprocessor_and_64bit_functions['<<']( c_ast.CNumber(5), c_ast.CNumber(2), ) self.assertEqual(actual, c_ast.CNumber(20))
def test_parse_with_yes_flag(self): config = 'FLAG=y' actual = self.parser.parse(config) expected = {'FLAG': c_ast.CNumber(1)} self.assertEqual(actual, expected)
def test_bitwise_xor(self): actual = self.preprocessor_and_64bit_functions['^']( c_ast.CNumber(42), c_ast.CNumber(33), ) self.assertEqual(actual, c_ast.CNumber(11))
def test_parse_with_integer_flag(self): config = 'FLAG=42' actual = self.parser.parse(config) expected = {'FLAG': c_ast.CNumber(42)} self.assertEqual(actual, expected)
def test_bitwise_or(self): actual = self.preprocessor_and_64bit_functions['|']( c_ast.CNumber(42), c_ast.CNumber(24), ) self.assertEqual(actual, c_ast.CNumber(58))