commit 48cababf44944677620e4e951e1946d5abf06994
parent 3af396888bca70c06c9feab02a27a26d5f183546
Author: Kyle Milz <kyle@0x30.net>
Date: Thu, 29 Dec 2016 17:17:00 -0700
t: convert inst_switch.sh to perl
Diffstat:
D | t/inst_switch.sh | | | 59 | ----------------------------------------------------------- |
A | t/inst_switch.t | | | 78 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 78 insertions(+), 59 deletions(-)
diff --git a/t/inst_switch.sh b/t/inst_switch.sh
@@ -1,59 +0,0 @@
-#!/bin/sh -u
-#
-# Make sure that switch statement condition instrumentation works.
-#
-. t/utils.subr
-plan 5
-
-
-cat <<EOF > switch.c
-int main(void) {
- int i;
-
- switch (i) {
- case 0:
- break;
- case 1:
- break;
- }
-
- return 0;
-}
-EOF
-
-cat <<EOF > switch.c.inst_good
-int main(void) {++_citrun.data[0];
- int i;
-
- switch ((++_citrun.data[3], i)) {
- case 0:
- break;
- case 1:
- break;
- }
-
- return (++_citrun.data[10], 0);
-}
-EOF
-
-cat <<EOF > check.good
-Summary:
- 1 Source files used as input
- 1 Rewrite successes
-
-Totals:
- 13 Lines of source code
- 1 Function definitions
- 1 Switch statements
- 1 Return statement values
- 14 Total statements
-EOF
-
-ok "citrun_inst" citrun_inst -c switch.c
-ok "citrun_check" citrun_check -o check.out
-
-strip_preamble switch.c
-strip_millis check.out
-
-ok "citrun_inst output diff" diff -u switch.c.inst_good switch.c.citrun_nohdr
-ok "citrun_check diff" diff -u check.good check.out
diff --git a/t/inst_switch.t b/t/inst_switch.t
@@ -0,0 +1,78 @@
+#
+# Make sure that switch statement condition instrumentation works.
+#
+use strict;
+use warnings;
+use File::Slurp;
+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( 'switch.c', <<EOF );
+int main(void)
+{
+ int i;
+
+ switch (i) {
+ case 0:
+ break;
+ case 1:
+ break;
+ }
+
+ return 0;
+}
+EOF
+
+# Known good output.
+my $inst_good = <<EOF ;
+int main(void)
+{++_citrun.data[0];++_citrun.data[1];
+ int i;
+
+ switch ((++_citrun.data[4], i)) {
+ case 0:
+ break;
+ case 1:
+ break;
+ }
+
+ return (++_citrun.data[11], 0);
+}
+EOF
+
+my $check_good = <<EOF;
+>> citrun_inst v0.0 (OpenBSD-6.0 amd64)
+CITRUN_SHARE = ''
+Switching argv[0] ''
+Found source file ''
+Modified command line is ''
+Added clangtool argument ''
+Instrumentation of '' finished:
+ 14 Lines of source code
+ 1 Function definitions
+ 1 Switch statements
+ 1 Return statement values
+ 14 Total statements
+Modified source written successfully.
+Rewriting successful.
+EOF
+
+# Run the command.
+$inst->run( args => '-c switch.c', chdir => $inst->curdir );
+
+# This file should have been rewritten in place.
+my $inst_out = read_file($inst->workdir . '/switch.c');
+
+# Sanitize paths from stdout.
+my $check_out = $inst->stdout;
+$check_out =~ s/^.*Milliseconds spent.*\n//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' );