Cleanup test scripts.

This commit is contained in:
b1v1r
2009-09-18 08:34:12 +00:00
parent c8b28800d2
commit dc548f01cf
2 changed files with 642 additions and 640 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -4,8 +4,8 @@
# #
# Syntax: # Syntax:
# All: run-tests.pl # All: run-tests.pl
# All in file: run-tests.pl file # All in file: run-tests.pl file
# Nth in file: run-tests.pl file N # Nth in file: run-tests.pl file N
# #
use strict; use strict;
use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG); use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
@@ -22,140 +22,140 @@ my $TOTAL = 0;
my $DEBUG = $ENV{MSC_TEST_DEBUG} || 0; my $DEBUG = $ENV{MSC_TEST_DEBUG} || 0;
if (defined $ARGV[0]) { if (defined $ARGV[0]) {
runfile(dirname($ARGV[0]), basename($ARGV[0]), $ARGV[1]); runfile(dirname($ARGV[0]), basename($ARGV[0]), $ARGV[1]);
done(); done();
} }
for my $type (sort @TYPES) { for my $type (sort @TYPES) {
my $dir = "$SCRIPTDIR/$type"; my $dir = "$SCRIPTDIR/$type";
my @cfg = (); my @cfg = ();
# Get test names # Get test names
opendir(DIR, "$dir") or quit(1, "Failed to open \"$dir\": $!"); opendir(DIR, "$dir") or quit(1, "Failed to open \"$dir\": $!");
@cfg = grep { /\.t$/ && -f "$dir/$_" } readdir(DIR); @cfg = grep { /\.t$/ && -f "$dir/$_" } readdir(DIR);
closedir(DIR); closedir(DIR);
for my $cfg (sort @cfg) { for my $cfg (sort @cfg) {
runfile($dir, $cfg); runfile($dir, $cfg);
} }
} }
done(); done();
sub runfile { sub runfile {
my($dir, $cfg, $testnum) = @_; my($dir, $cfg, $testnum) = @_;
my $fn = "$dir/$cfg"; my $fn = "$dir/$cfg";
my @data = (); my @data = ();
my $edata; my $edata;
my @C = (); my @C = ();
my @test = (); my @test = ();
my $teststr; my $teststr;
my $n = 0; my $n = 0;
my $pass = 0; my $pass = 0;
open(CFG, "<$fn") or quit(1, "Failed to open \"$fn\": $!"); open(CFG, "<$fn") or quit(1, "Failed to open \"$fn\": $!");
@data = <CFG>; @data = <CFG>;
$edata = q/@C = (/ . join("", @data) . q/)/; $edata = q/@C = (/ . join("", @data) . q/)/;
eval $edata; eval $edata;
quit(1, "Failed to read test data \"$cfg\": $@") if ($@); quit(1, "Failed to read test data \"$cfg\": $@") if ($@);
unless (@C) { unless (@C) {
msg("\nNo tests defined for $fn"); msg("\nNo tests defined for $fn");
return; return;
} }
msg("\nLoaded ".@C." tests from $fn"); msg("\nLoaded ".@C." tests from $fn");
for my $t (@C) { for my $t (@C) {
$n++; $n++;
next if (defined $testnum and $n != $testnum); next if (defined $testnum and $n != $testnum);
my %t = %{$t || {}}; my %t = %{$t || {}};
my $id = sprintf("%6d", $n); my $id = sprintf("%6d", $n);
my $in = (exists($t{input}) and defined($t{input})) ? $t{input} : ""; my $in = (exists($t{input}) and defined($t{input})) ? $t{input} : "";
my $out; my $out;
my $test_in = new FileHandle(); my $test_in = new FileHandle();
my $test_out = new FileHandle(); my $test_out = new FileHandle();
my $test_pid; my $test_pid;
my $rc = 0; my $rc = 0;
my $param; my $param;
if ($t{type} eq "tfn") { if ($t{type} eq "tfn") {
$param = escape($t{output}); $param = escape($t{output});
} }
elsif ($t{type} eq "op") { elsif ($t{type} eq "op") {
$param = escape($t{param}); $param = escape($t{param});
} }
elsif ($t{type} eq "action") { elsif ($t{type} eq "action") {
$param = escape($t{param}); $param = escape($t{param});
} }
else { else {
quit(1, "Unknown type \"$t{type}\" - should be one of: " . join(",",@TYPES)); quit(1, "Unknown type \"$t{type}\" - should be one of: " . join(",",@TYPES));
} }
@test = ("-t", $t{type}, "-n", $t{name}, "-p", $param, "-D", "$DEBUG", (exists($t{ret}) ? ("-r", $t{ret}) : ()), (exists($t{iterations}) ? ("-I", $t{iterations}) : ()), (exists($t{prerun}) ? ("-P", $t{prerun}) : ())); @test = ("-t", $t{type}, "-n", $t{name}, "-p", $param, "-D", "$DEBUG", (exists($t{ret}) ? ("-r", $t{ret}) : ()), (exists($t{iterations}) ? ("-I", $t{iterations}) : ()), (exists($t{prerun}) ? ("-P", $t{prerun}) : ()));
$teststr = "$TEST " . join(" ", map { "\"$_\"" } @test); $teststr = "$TEST " . join(" ", map { "\"$_\"" } @test);
$test_pid = open2($test_out, $test_in, $TEST, @test) or quit(1, "Failed to execute test: $teststr\": $!"); $test_pid = open2($test_out, $test_in, $TEST, @test) or quit(1, "Failed to execute test: $teststr\": $!");
print $test_in "$in"; print $test_in "$in";
close $test_in; close $test_in;
$out = join("\\n", split(/\n/, <$test_out>)); $out = join("\\n", split(/\n/, <$test_out>));
close $test_out; close $test_out;
waitpid($test_pid, 0); waitpid($test_pid, 0);
$rc = $?; $rc = $?;
if ( WIFEXITED($rc) ) { if ( WIFEXITED($rc) ) {
$rc = WEXITSTATUS($rc); $rc = WEXITSTATUS($rc);
} }
elsif( WIFSIGNALED($rc) ) { elsif( WIFSIGNALED($rc) ) {
msg("Test exited with signal " . WTERMSIG($rc) . "."); msg("Test exited with signal " . WTERMSIG($rc) . ".");
msg("Executed: $teststr"); msg("Executed: $teststr");
$rc = -1; $rc = -1;
} }
else { else {
msg("Test exited with unknown error."); msg("Test exited with unknown error.");
$rc = -1; $rc = -1;
} }
if ($rc == 0) { if ($rc == 0) {
$pass++; $pass++;
} }
msg(sprintf("%s) %s \"%s\"%s: %s%s", $id, $t{type}, $t{name}, (exists($t{comment}) ? " $t{comment}" : ""), ($rc ? "failed" : "passed"), ((defined($out) && $out ne "")? " ($out)" : ""))); msg(sprintf("%s) %s \"%s\"%s: %s%s", $id, $t{type}, $t{name}, (exists($t{comment}) ? " $t{comment}" : ""), ($rc ? "failed" : "passed"), ((defined($out) && $out ne "")? " ($out)" : "")));
} }
$TOTAL += $testnum ? 1 : $n; $TOTAL += $testnum ? 1 : $n;
$PASSED += $pass; $PASSED += $pass;
msg(sprintf("Passed: %2d; Failed: %2d", $pass, $testnum ? (1 - $pass) : ($n - $pass))); msg(sprintf("Passed: %2d; Failed: %2d", $pass, $testnum ? (1 - $pass) : ($n - $pass)));
} }
sub escape { sub escape {
my @new = (); my @new = ();
for my $c (split(//, $_[0])) { for my $c (split(//, $_[0])) {
push @new, ((ord($c) >= 0x20 and ord($c) <= 0x7e) ? $c : sprintf("\\x%02x", ord($c))); push @new, ((ord($c) >= 0x20 and ord($c) <= 0x7e) ? $c : sprintf("\\x%02x", ord($c)));
} }
join('', @new); join('', @new);
} }
sub msg { sub msg {
print STDOUT "@_\n" if (@_); print STDOUT "@_\n" if (@_);
} }
sub quit { sub quit {
my($ec,$msg) = @_; my($ec,$msg) = @_;
$ec = 0 unless (defined $_[0]); $ec = 0 unless (defined $_[0]);
msg("$msg") if (defined $msg); msg("$msg") if (defined $msg);
exit $ec; exit $ec;
} }
sub done { sub done {
if ($PASSED != $TOTAL) { if ($PASSED != $TOTAL) {
quit(1, "\n$PASSED/$TOTAL tests passed."); quit(1, "\n$PASSED/$TOTAL tests passed.");
} }
quit(0, "\nAll tests passed ($TOTAL)."); quit(0, "\nAll tests passed ($TOTAL).");
} }