citrun

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

commit 2ddb5b7dde003ed0c5926e975fbf05a61bba7d63
parent 40f5d61404ef752b1ed3e6a83965d25433eabb76
Author: Kyle Milz <kyle@0x30.net>
Date:   Sat, 31 Dec 2016 11:49:07 -0700

t: convert inst_binop.sh to perl

Diffstat:
Dt/inst_binop.sh | 70----------------------------------------------------------------------
At/inst_binop.t | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+), 70 deletions(-)

diff --git a/t/inst_binop.sh b/t/inst_binop.sh @@ -1,70 +0,0 @@ -#!/bin/sh -u -# -# Test that binary operators in strange cases work. Includes enums and globals. -# -. t/utils.subr -plan 5 - - -cat <<EOF > enum.c -enum ASDF { - ONE = (1 << 0), - TWO = (1 << 1), - THR = (1 << 2) -}; - -static int foo = 5 + 5; - -static const struct { - int i; - unsigned char data[0 + 64 * 6]; -} blah; - -int main(void) { - if (4 + 3) - return 0; -} -EOF - -cat <<EOF > enum.c.inst_good -enum ASDF { - ONE = (1 << 0), - TWO = (1 << 1), - THR = (1 << 2) -}; - -static int foo = 5 + 5; - -static const struct { - int i; - unsigned char data[0 + 64 * 6]; -} blah; - -int main(void) {++_citrun.data[13]; - if ((++_citrun.data[14], (++_citrun.data[14], 4 + 3))) - return (++_citrun.data[15], 0); -} -EOF - -cat <<EOF > check.good -Summary: - 1 Source files used as input - 1 Rewrite successes - -Totals: - 18 Lines of source code - 1 Function definitions - 1 If statements - 1 Return statement values - 7 Total statements - 1 Binary operators -EOF - -ok "running citrun_inst" citrun_inst -c enum.c -ok "running citrun_check" citrun_check -o check.out - -strip_preamble enum.c -strip_millis check.out - -ok "instrumented src file diff" diff -u enum.c.inst_good enum.c.citrun_nohdr -ok "citrun_check diff" diff -u check.good check.out diff --git a/t/inst_binop.t b/t/inst_binop.t @@ -0,0 +1,88 @@ +# +# Test that binary operators in strange cases work. Includes enums and globals. +# +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( 'binop.c', <<EOF ); +enum ASDF { + ONE = (1 << 0), + TWO = (1 << 1), + THR = (1 << 2) +}; + +static int foo = 5 + 5; + +static const struct { + int i; + unsigned char data[0 + 64 * 6]; +} blah; + +int main(void) { + if (4 + 3) + return 0; +} +EOF + +# Known good output. +my $inst_good = <<EOF ; +enum ASDF { + ONE = (1 << 0), + TWO = (1 << 1), + THR = (1 << 2) +}; + +static int foo = 5 + 5; + +static const struct { + int i; + unsigned char data[0 + 64 * 6]; +} blah; + +int main(void) {++_citrun.data[13]; + if ((++_citrun.data[14], (++_citrun.data[14], 4 + 3))) + return (++_citrun.data[15], 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: + 18 Lines of source code + 1 Function definitions + 1 If statements + 1 Return statement values + 7 Total statements + 1 Binary operators +Modified source written successfully. +Rewriting successful. +EOF + +# Run the command. +$inst->run( args => '-c binop.c', chdir => $inst->curdir ); + +# This file should have been rewritten in place. +my $inst_out; +$inst->read(\$inst_out, 'binop.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' );