| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use strict; |
| •5 |
use Crypt::Digest::SHA256; |
| 6 |
use Cwd; |
| 7 |
|
| 8 |
use FindBin qw($Bin); |
| 9 |
my $testdir = $Bin; |
| 10 |
undef $Bin; |
| 11 |
#print("testdir is $testdir\n"); |
| 12 |
my $binpath = getcwd(); |
| 13 |
#print("binpath is $binpath\n"); |
| 14 |
|
| 15 |
my $GPrintCmds = 0; |
| 16 |
|
| 17 |
my @modules = qw( preprocessor assembler compiler parser ); |
| 18 |
|
| 19 |
|
| 20 |
sub compare_files { |
| 21 |
my ($a, $b, $endlines) = @_; |
| 22 |
|
| 23 |
if (not open(FILE1, '<', $a)) { |
| 24 |
return (0, "Couldn't open '$a' for checksum"); |
| 25 |
} |
| 26 |
if (not open(FILE2, '<', $b)) { |
| 27 |
close(FILE1); |
| 28 |
return (0, "Couldn't open '$b' for checksum"); |
| 29 |
} |
| 30 |
|
| •31 |
my $sha1 = Crypt::Digest::SHA256->new('sha256'); |
| 32 |
my $sha2 = Crypt::Digest::SHA256->new('sha256'); |
| 33 |
|
| 34 |
if (not $endlines) { |
| 35 |
$sha1->addfile(*FILE1); |
| 36 |
$sha2->addfile(*FILE2); |
| 37 |
} else { |
| 38 |
while (<FILE1>) { s/[\r\n]//g; $sha1->add($_); } |
| 39 |
while (<FILE2>) { s/[\r\n]//g; $sha2->add($_); } |
| 40 |
} |
| 41 |
|
| 42 |
close(FILE1); |
| 43 |
close(FILE2); |
| 44 |
|
| 45 |
if ($sha1->hexdigest ne $sha2->hexdigest) { |
| 46 |
return (0, "Result doesn't match expectations"); |
| 47 |
} |
| 48 |
|
| 49 |
return (1); |
| 50 |
} |
| 51 |
|
| 52 |
my %tests = (); |
| 53 |
|
| 54 |
$tests{'output'} = sub { |
| 55 |
my ($module, $fname) = @_; |
| 56 |
my $output = 'unittest_tempoutput'; |
| 57 |
my $desired = $fname . '.correct'; |
| 58 |
my $cmd = undef; |
| 59 |
my $endlines = 1; |
| 60 |
|
| 61 |
# !!! FIXME: this should go elsewhere. |
| 62 |
if ($module eq 'preprocessor') { |
| 63 |
$cmd = "$binpath/mojoshader-compiler -P '$fname' -o '$output'"; |
| 64 |
} else { |
| 65 |
return (0, "Don't know how to do this module type"); |
| 66 |
} |
| 67 |
$cmd .= ' 2>/dev/null 1>/dev/null'; |
| 68 |
|
| 69 |
print("$cmd\n") if ($GPrintCmds); |
| 70 |
|
| 71 |
if (system($cmd) != 0) { |
| 72 |
unlink($output) if (-f $output); |
| 73 |
return (0, "External program reported error"); |
| 74 |
} |
| 75 |
|
| 76 |
if (not -f $output) { return (0, "Didn't get any output file"); } |
| 77 |
|
| 78 |
my @retval = compare_files($desired, $output, $endlines); |
| 79 |
unlink($output); |
| 80 |
return @retval; |
| 81 |
}; |
| 82 |
|
| 83 |
$tests{'errors'} = sub { |
| 84 |
my ($module, $fname) = @_; |
| 85 |
my $error_output = 'unittest_temperroutput'; |
| 86 |
my $output = 'unittest_tempoutput'; |
| 87 |
my $desired = $fname . '.correct'; |
| 88 |
my $cmd = undef; |
| 89 |
my $endlines = 1; |
| 90 |
|
| 91 |
# !!! FIXME: this should go elsewhere. |
| 92 |
if ($module eq 'preprocessor') { |
| 93 |
$cmd = "$binpath/mojoshader-compiler -P '$fname' -o '$output'"; |
| 94 |
} else { |
| 95 |
return (0, "Don't know how to do this module type"); |
| 96 |
} |
| 97 |
$cmd .= " 2>$error_output 1>/dev/null"; |
| 98 |
|
| 99 |
print("$cmd\n") if ($GPrintCmds); |
| 100 |
|
| 101 |
system($cmd); |
| 102 |
unlink($output) if (-f $output); |
| 103 |
|
| 104 |
if (not -f $error_output) { return (0, "Didn't get any error output"); } |
| 105 |
|
| 106 |
my @retval = compare_files($desired, $error_output, $endlines); |
| 107 |
unlink($error_output); |
| 108 |
return @retval; |
| 109 |
}; |
| 110 |
|
| 111 |
my $totaltests = 0; |
| 112 |
my $pass = 0; |
| 113 |
my $fail = 0; |
| 114 |
my $skip = 0; |
| 115 |
my @fails = (); |
| 116 |
|
| 117 |
my $result = ''; |
| 118 |
chdir($testdir) or die("Failed to chdir('$testdir'): $!\n"); |
| 119 |
foreach (@modules) { |
| 120 |
my $module = $_; |
| 121 |
foreach (keys %tests) { |
| 122 |
my $testtype = $_; |
| 123 |
my $fn = $tests{$_}; |
| 124 |
my $d = "$module/$testtype"; |
| 125 |
next if (not -d $d); # no tests at the moment. |
| 126 |
opendir(TESTDIR, $d) || die("Failed to open dir '$d': $!\n"); |
| 127 |
my $subsection = " ... $module / $testtype ...\n"; |
| 128 |
print($subsection); |
| 129 |
my $addedsubsection = 0; |
| 130 |
my $fname = readdir(TESTDIR); |
| 131 |
while (defined $fname) { |
| 132 |
my $isfail = 0; |
| 133 |
my $origfname = $fname; |
| 134 |
$fname = readdir(TESTDIR); # set for next iteration. |
| 135 |
next if (-d $origfname); |
| 136 |
next if ($origfname =~ /\.correct\Z/); |
| 137 |
my $fullfname = "$d/$origfname"; |
| 138 |
my ($rc, $reason) = &$fn($module, $fullfname); |
| 139 |
if ($rc == 1) { |
| 140 |
$result = 'PASS'; |
| 141 |
$pass++; |
| 142 |
} elsif ($rc == 0) { |
| 143 |
$isfail = 1; |
| 144 |
$result = 'FAIL'; |
| 145 |
$fail++; |
| 146 |
} elsif ($rc == -1) { |
| 147 |
$result = 'SKIP'; |
| 148 |
$skip++; |
| 149 |
} |
| 150 |
|
| 151 |
if (defined $reason) { |
| 152 |
$reason = " ($reason)"; |
| 153 |
} else { |
| 154 |
$reason = ''; |
| 155 |
} |
| 156 |
my $output = "$result ${origfname}${reason}\n"; |
| 157 |
print($output); |
| 158 |
|
| 159 |
if ($isfail) { |
| 160 |
if (!$addedsubsection) { |
| 161 |
$addedsubsection = 1; |
| 162 |
push(@fails, $subsection); |
| 163 |
} |
| 164 |
push(@fails, $output); |
| 165 |
} |
| 166 |
|
| 167 |
$totaltests++; |
| 168 |
} |
| 169 |
closedir(TESTDIR); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if (scalar(@fails)) { |
| 174 |
print("\n\n"); |
| 175 |
print("*************************************************************\n"); |
| 176 |
print("*************************************************************\n"); |
| 177 |
print("** SOME TESTS FAILED! PLEASE CORRECT THE FOLLOWING ISSUES. **\n"); |
| 178 |
print("*************************************************************\n"); |
| 179 |
print("*************************************************************\n"); |
| 180 |
print("\n"); |
| 181 |
foreach (@fails) { |
| 182 |
print $_; |
| 183 |
} |
| 184 |
print("\n\n"); |
| 185 |
} |
| 186 |
|
| 187 |
print("\n$totaltests tests, $pass passed, $fail failed, $skip skipped.\n\n"); |
| 188 |
|
| 189 |
exit(($fail > 0) ? 1 : 0); |
| 190 |
|
| 191 |
# end if run_tests.pl ... |
| 192 |
|