citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

inst_return.t (1599B)


      1 #
      2 # Check that return statement values (if any) are instrumented correctly.
      3 #
      4 use Modern::Perl;
      5 use Test::Cmd;
      6 use Test::Differences;
      7 use Test::More tests => 4;
      8 
      9 use lib 't';
     10 require utils;
     11 
     12 
     13 my $inst = Test::Cmd->new( prog => 'inst/citrun_inst', workdir => '' );
     14 $inst->write( 'return.c', <<EOF );
     15 int foo() {
     16 	return 0;
     17 }
     18 
     19 int main(void) {
     20 	return 10;
     21 
     22 	return 10 + 10;
     23 
     24 	return foo();
     25 }
     26 EOF
     27 
     28 # Known good output.
     29 my $inst_good = <<EOF ;
     30 int foo() {++_citrun.data[0];
     31 	return (++_citrun.data[1], 0);
     32 }
     33 
     34 int main(void) {++_citrun.data[4];
     35 	return (++_citrun.data[5], 10);
     36 
     37 	return (++_citrun.data[7], (++_citrun.data[7], 10 + 10));
     38 
     39 	return (++_citrun.data[9], (++_citrun.data[9], foo()));
     40 }
     41 EOF
     42 
     43 my $check_good = <<EOF;
     44 >> citrun_inst
     45 Compilers path = ''
     46 Found source file ''
     47 Command line is ''
     48 Added clangtool argument ''
     49 Instrumentation of '' finished:
     50     12 Lines of source code
     51     2 Function definitions
     52     4 Return statement values
     53     1 Call expressions
     54     14 Total statements
     55     1 Binary operators
     56 Modified source written successfully.
     57 EOF
     58 
     59 # Run the command.
     60 $inst->run( args => '-c return.c', chdir => $inst->curdir );
     61 
     62 # This file should have been rewritten in place.
     63 my $inst_out;
     64 $inst->read(\$inst_out, 'return.c');
     65 
     66 # Sanitize paths from stdout.
     67 my $check_out = utils::clean_citrun_log(scalar $inst->stdout);
     68 
     69 eq_or_diff( $inst_out,	$inst_good, 'inst file identical', { context => 3 } );
     70 eq_or_diff( $check_out,	$check_good, 'citrun_inst output identical',
     71 	{ context => 3 } );
     72 is( $inst->stderr,	'',	'citrun_inst stderr silent' );
     73 is( $? >> 8,		0,	'citrun_inst exit code 0' );