citrun

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

inst_switch.t (1491B)


      1 #
      2 # Make sure that switch statement condition instrumentation works.
      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( 'switch.c', <<EOF );
     15 int main(void)
     16 {
     17 	int i;
     18 
     19 	switch (i) {
     20 	case 0:
     21 		break;
     22 	case 1:
     23 		break;
     24 	}
     25 
     26 	return 0;
     27 }
     28 EOF
     29 
     30 # Known good output.
     31 my $inst_good = <<EOF ;
     32 int main(void)
     33 {++_citrun.data[0];++_citrun.data[1];
     34 	int i;
     35 
     36 	switch ((++_citrun.data[4], i)) {
     37 	case 0:
     38 		break;
     39 	case 1:
     40 		break;
     41 	}
     42 
     43 	return (++_citrun.data[11], 0);
     44 }
     45 EOF
     46 
     47 my $check_good = <<EOF;
     48 >> citrun_inst
     49 Compilers path = ''
     50 Found source file ''
     51 Command line is ''
     52 Added clangtool argument ''
     53 Instrumentation of '' finished:
     54     14 Lines of source code
     55     1 Function definitions
     56     1 Switch statements
     57     1 Return statement values
     58     16 Total statements
     59 Modified source written successfully.
     60 EOF
     61 
     62 # Run the command.
     63 $inst->run( args => '-c switch.c', chdir => $inst->curdir );
     64 
     65 # This file should have been rewritten in place.
     66 my $inst_out;
     67 $inst->read(\$inst_out, 'switch.c');
     68 
     69 # Sanitize paths from stdout.
     70 my $check_out = utils::clean_citrun_log(scalar $inst->stdout);
     71 
     72 eq_or_diff( $inst_out,	$inst_good, 'inst file identical', { context => 3 } );
     73 eq_or_diff( $check_out,	$check_good, 'citrun_inst output identical',
     74 	{ context => 3 } );
     75 is( $inst->stderr,	'',	'citrun_inst stderr silent' );
     76 is( $? >> 8,		0,	'citrun_inst exit code 0' );