Skip to content

Latest commit

 

History

History
executable file
·
183 lines (149 loc) · 4.74 KB

run_tests.pl

File metadata and controls

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