commit 704d0bdf8c311082d0cc7328d859395bcea8d59c
parent 4716d881afc644f4a78596f4c735b19c6c6418f8
Author: Kyle Milz <kyle@0x30.net>
Date: Sat, 31 Dec 2016 12:35:32 -0700
t: convert inst_return.sh to perl
Diffstat:
D | t/inst_return.sh | | | 58 | ---------------------------------------------------------- |
A | t/inst_return.t | | | 76 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 76 insertions(+), 58 deletions(-)
diff --git a/t/inst_return.sh b/t/inst_return.sh
@@ -1,58 +0,0 @@
-#!/bin/sh -u
-#
-# Check that return statement values (if any) are instrumented correctly.
-#
-. t/utils.subr
-plan 5
-
-
-cat <<EOF > return.c
-int foo() {
- return 0;
-}
-
-int main(void) {
- return 10;
-
- return 10 + 10;
-
- return foo();
-}
-EOF
-
-cat <<EOF > return.c.inst_good
-int foo() {++_citrun.data[0];
- return (++_citrun.data[1], 0);
-}
-
-int main(void) {++_citrun.data[4];
- return (++_citrun.data[5], 10);
-
- return (++_citrun.data[7], (++_citrun.data[7], 10 + 10));
-
- return (++_citrun.data[9], (++_citrun.data[9], foo()));
-}
-EOF
-
-cat <<EOF > check.good
-Summary:
- 1 Source files used as input
- 1 Rewrite successes
-
-Totals:
- 12 Lines of source code
- 2 Function definitions
- 4 Return statement values
- 1 Call expressions
- 14 Total statements
- 1 Binary operators
-EOF
-
-ok "running citrun_inst" citrun_inst -c return.c
-ok "running citrun_check" citrun_check -o check.out
-
-strip_preamble return.c
-strip_millis check.out
-
-ok "instrumented src diff" diff -u return.c.inst_good return.c.citrun_nohdr
-ok "citrun_check diff" diff -u check.good check.out
diff --git a/t/inst_return.t b/t/inst_return.t
@@ -0,0 +1,76 @@
+#
+# Check that return statement values (if any) are instrumented correctly.
+#
+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( 'return.c', <<EOF );
+int foo() {
+ return 0;
+}
+
+int main(void) {
+ return 10;
+
+ return 10 + 10;
+
+ return foo();
+}
+EOF
+
+# Known good output.
+my $inst_good = <<EOF ;
+int foo() {++_citrun.data[0];
+ return (++_citrun.data[1], 0);
+}
+
+int main(void) {++_citrun.data[4];
+ return (++_citrun.data[5], 10);
+
+ return (++_citrun.data[7], (++_citrun.data[7], 10 + 10));
+
+ return (++_citrun.data[9], (++_citrun.data[9], foo()));
+}
+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
+ 2 Function definitions
+ 4 Return statement values
+ 1 Call expressions
+ 14 Total statements
+ 1 Binary operators
+Modified source written successfully.
+Rewriting successful.
+EOF
+
+# Run the command.
+$inst->run( args => '-c return.c', chdir => $inst->curdir );
+
+# This file should have been rewritten in place.
+my $inst_out;
+$inst->read(\$inst_out, 'return.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' );