unit_tests/run_tests.pl
changeset 736 a392aabaa21c
child 740 13c0707a2e3f
equal deleted inserted replaced
735:78c882b8c813 736:a392aabaa21c
       
     1 #!/usr/bin/perl -w
       
     2 
       
     3 use warnings;
       
     4 use strict;
       
     5 use Digest::SHA1;
       
     6 
       
     7 use FindBin qw($Bin);
       
     8 my $testdir = $Bin;
       
     9 undef $Bin;
       
    10 #print("testdir is $testdir\n");
       
    11 
       
    12 my @modules = qw(
       
    13     preprocessor
       
    14     assembler
       
    15     compiler
       
    16     parser
       
    17 );
       
    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 = Digest::SHA1->new;
       
    32     my $sha2 = Digest::SHA1->new;
       
    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 = "./mojoshader-compiler -P '$fname' -o '$output'";
       
    64     } else {
       
    65         return (0, "Don't know how to do this module type");
       
    66     }
       
    67 
       
    68     if (system($cmd) != 0) {
       
    69         unlink($output) if (-f $output);
       
    70         return (0, "External program reported error");
       
    71     }
       
    72 
       
    73     if (not -f $output) { return (0, "Didn't get any output file"); }
       
    74 
       
    75     my @retval = compare_files($desired, $output, $endlines);
       
    76     unlink($output);
       
    77     return @retval;
       
    78 };
       
    79 
       
    80 
       
    81 my $totaltests = 0;
       
    82 my $pass = 0;
       
    83 my $fail = 0;
       
    84 my $skip = 0;
       
    85 
       
    86 my $result = '';
       
    87 foreach (@modules) {
       
    88     my $module = $_;
       
    89     foreach (keys %tests) {
       
    90         my $testtype = $_;
       
    91         my $fn = $tests{$_};
       
    92         my $d = "$testdir/$module/$testtype";
       
    93         next if (not -d $d);  # no tests at the moment.
       
    94         opendir(TESTDIR, $d) || die("Failed to open dir '$d': $!\n");
       
    95         print(" ... $module / $testtype ...\n");
       
    96         my $fname = readdir(TESTDIR);
       
    97         while (defined $fname) {
       
    98             my $origfname = $fname;
       
    99             $fname = readdir(TESTDIR);  # set for next iteration.
       
   100             next if (-d $origfname);
       
   101             next if ($origfname =~ /\.correct\Z/);
       
   102             my $fullfname = "$d/$origfname";
       
   103             my ($rc, $reason) = &$fn($module, $fullfname);
       
   104             if ($rc == 1) {
       
   105                 $result = 'PASS';
       
   106                 $pass++;
       
   107             } elsif ($rc == 0) {
       
   108                 $result = 'FAIL';
       
   109                 $fail++;
       
   110             } elsif ($rc == -1) {
       
   111                 $result = 'SKIP';
       
   112                 $skip++;
       
   113             }
       
   114 
       
   115             if (defined $reason) {
       
   116                 $reason = " ($reason)";
       
   117             } else {
       
   118                 $reason = '';
       
   119             }
       
   120             print("$result ${origfname}${reason}\n");
       
   121 
       
   122             $totaltests++;
       
   123         }
       
   124         closedir(TESTDIR);
       
   125     }
       
   126 }
       
   127 
       
   128 print("\n$totaltests tests, $pass passed, $fail failed, $skip skipped.\n\n");
       
   129 
       
   130 exit(($fail > 0) ? 1 : 0);
       
   131 
       
   132 # end if run_tests.pl ...
       
   133