def Can_render_function_taking_function_as_arg__test(): assert_rendered_program_equals( """ int fn1( double a, double b ); void myfn( int unused, int (*myarg)( double, double ) ); int fn1( double a, double b ) { return 1; } void myfn( int unused, int (*myarg)( double, double ) ) { } int main( int argc, char* argv[] ) { myfn( argc, fn1 ); return 0; } """, """ import sys def int fn1( float a, float b ): return 1 def void myfn( int unused, function( int, ( float, float ) ) myarg ): pass myfn( len( sys.argv ), fn1 ) """ )
def Can_render_function_passed_as_arg__test(): assert_rendered_program_equals( """ int fn1( int unused, double n ); void myfn( int unused, int (*myarg)( int, double ) ); int fn1( int unused, double n ) { return 1; } void myfn( int unused, int (*myarg)( int, double ) ) { myarg( unused, 2.0 ); } int main( int argc, char* argv[] ) { myfn( argc, fn1 ); return 0; } """, """ import sys def int fn1( int unused, float n ): return 1 def void myfn( int unused, function( int, ( int, float ) ) myarg ): myarg( unused, 2.0 ) myfn( len( sys.argv ), fn1 ) """ )
def Can_render_function_passed_as_arg_taking_class__test(): assert_rendered_program_equals( """ struct Cls { }; void Cls_pep_c_pep___init__( Cls& self, int unused ); int fn1( int unused, Cls& c ); void myfn( int unused, int (*myarg)( int, Cls& ) ); void Cls_pep_c_pep___init__( Cls& self, int unused ) { } int fn1( int unused, Cls& c ) { return 1; } void myfn( int unused, int (*myarg)( int, Cls& ) ) { myarg( unused, 2.0 ); } int main( int argc, char* argv[] ) { Cls x; Cls_pep_c_pep___init__( x, argc ); fn1( argc, x ); myfn( argc, fn1 ); return 0; } """, """ import sys class Cls: def_init( Cls self, int unused ): pass def int fn1( int unused, Cls c ): return 1 def void myfn( int unused, function( int, ( int, Cls ) ) myarg ): myarg( unused, 2.0 ) Cls x = Cls.init( len( sys.argv ) ) fn1( len( sys.argv ), x ) myfn( len( sys.argv ), fn1 ) """ )
def Now_evaluates_now_but_otherwise_not__test(): """ If we render some stuff that could be evaluated at compile time, it is not evaluated, unless we wrap it in a call to the now() builtin. """ assert_rendered_program_equals( r"""#include <stdio.h> int main( int argc, char* argv[] ) { printf( "%d\n", (3 * 2) ); printf( "%d\n", 6 ); return 0; } """, """ print( 3 * 2 ) print( now( 3 *2 ) ) """ )
def Can_render_function_returning_function__test(): assert_rendered_program_equals( """ void (*get_fn( int unused ))( int, int ); void myfn( int unused, int i ); void (*get_fn( int unused ))( int, int ) { return myfn; } void myfn( int unused, int i ) { } int main( int argc, char* argv[] ) { void (*got_fn)( int, int ) = get_fn( argc ); got_fn( argc, 1 ); return 0; } """, """ import sys def void myfn( int unused, int i ): pass def function( void, ( int, int ) ) get_fn( int unused ): return myfn function(void, ( int, int ) ) got_fn = get_fn( len( sys.argv ) ) got_fn( len( sys.argv ), 1 ) """ )