citrun

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

commit fce14231ccf978a3250c8f71eeb87368358671a5
parent dd940128dabcdb567ca930be787cf47a8b7c84f8
Author: Kyle Milz <kyle@0x30.net>
Date:   Sat, 31 Dec 2016 12:03:23 -0700

t: convert inst_if.sh to perl

Diffstat:
Dt/inst_if.sh | 58----------------------------------------------------------
At/inst_if.t | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 76 insertions(+), 58 deletions(-)

diff --git a/t/inst_if.sh b/t/inst_if.sh @@ -1,58 +0,0 @@ -#!/bin/sh -u -# -# Check that if statement conditions are instrumented properly. -# -. t/utils.subr -plan 5 - - -cat <<EOF > if.c -int main(int argc, char *argv[]) { - if (argc == 1) - return 1; - else - return(14); - - if ((argc = 2)) - return 5; - else - return(0); -} -EOF - -cat <<EOF > if.c.inst_good -int main(int argc, char *argv[]) {++_citrun.data[0]; - if ((++_citrun.data[1], (++_citrun.data[1], argc == 1))) - return (++_citrun.data[2], 1); - else - return(++_citrun.data[4], (14)); - - if ((++_citrun.data[6], ((++_citrun.data[6], argc = 2)))) - return (++_citrun.data[7], 5); - else - return(++_citrun.data[9], (0)); -} -EOF - -cat <<EOF > check.good -Summary: - 1 Source files used as input - 1 Rewrite successes - -Totals: - 12 Lines of source code - 1 Function definitions - 2 If statements - 4 Return statement values - 21 Total statements - 2 Binary operators -EOF - -ok "running citrun_inst" citrun_inst -c if.c -ok "running citrun_check" citrun_check -o check.out - -strip_preamble if.c -strip_millis check.out - -ok "known good instrumented diff" diff -u if.c.inst_good if.c.citrun_nohdr -ok "citrun_check diff" diff -u check.good check.out diff --git a/t/inst_if.t b/t/inst_if.t @@ -0,0 +1,76 @@ +# +# Check that if statement conditions are instrumented properly. +# +use strict; +use warnings; +use Test::Cmd; +use Test::Differences; +use Test::More tests => 4; +unified_diff; # for Test::Differences + + +my $inst = Test::Cmd->new( prog => 'src/citrun_inst', workdir => '' ); +$inst->write( 'if.c', <<EOF ); +int main(int argc, char *argv[]) { + if (argc == 1) + return 1; + else + return(14); + + if ((argc = 2)) + return 5; + else + return(0); +} +EOF + +# Known good output. +my $inst_good = <<EOF ; +int main(int argc, char *argv[]) {++_citrun.data[0]; + if ((++_citrun.data[1], (++_citrun.data[1], argc == 1))) + return (++_citrun.data[2], 1); + else + return(++_citrun.data[4], (14)); + + if ((++_citrun.data[6], ((++_citrun.data[6], argc = 2)))) + return (++_citrun.data[7], 5); + else + return(++_citrun.data[9], (0)); +} +EOF + +my $check_good = <<EOF; +>> citrun_inst v0.0 () +CITRUN_SHARE = '' +Switching argv[0] '' +Found source file '' +Modified command line is '' +Added clangtool argument '' +Instrumentation of '' finished: + 12 Lines of source code + 1 Function definitions + 2 If statements + 4 Return statement values + 21 Total statements + 2 Binary operators +Modified source written successfully. +Rewriting successful. +EOF + +# Run the command. +$inst->run( args => '-c if.c', chdir => $inst->curdir ); + +# This file should have been rewritten in place. +my $inst_out; +$inst->read(\$inst_out, 'if.c'); + +# Sanitize paths from stdout. +my $check_out = $inst->stdout; +$check_out =~ s/^.*Milliseconds spent.*\n//gm; +$check_out =~ s/'.*'/''/gm; +$check_out =~ s/\(.*\)/\(\)/gm; + +eq_or_diff( $inst_out, $inst_good, 'is instrumented file identical', { context => 3 } ); +eq_or_diff $check_good, $check_out, 'is citrun_inst output identical'; +is( $inst->stderr, '', 'is citrun_inst stderr silent' ); +is( $? >> 8, 0, 'is citrun_inst exit code 0' );