unit_tests/run_tests.pl
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2009 00:23:57 -0400
changeset 740 13c0707a2e3f
parent 736 a392aabaa21c
child 741 0da871dd944b
permissions -rwxr-xr-x
Minor cleanup; crunch down list to one line.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
736
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
#!/usr/bin/perl -w
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
use warnings;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
use strict;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
use Digest::SHA1;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
use FindBin qw($Bin);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
my $testdir = $Bin;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
undef $Bin;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#print("testdir is $testdir\n");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
740
13c0707a2e3f Minor cleanup; crunch down list to one line.
Ryan C. Gordon <icculus@icculus.org>
parents: 736
diff changeset
    12
my @modules = qw( preprocessor assembler compiler parser );
736
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
sub compare_files {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
    my ($a, $b, $endlines) = @_;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    if (not open(FILE1, '<', $a)) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
        return (0, "Couldn't open '$a' for checksum");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
    if (not open(FILE2, '<', $b)) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
        close(FILE1);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
        return (0, "Couldn't open '$b' for checksum");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
    my $sha1 = Digest::SHA1->new;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
    my $sha2 = Digest::SHA1->new;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
    if (not $endlines) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
        $sha1->addfile(*FILE1);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
        $sha2->addfile(*FILE2);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    } else {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
        while (<FILE1>) { s/[\r\n]//g; $sha1->add($_); }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
        while (<FILE2>) { s/[\r\n]//g; $sha2->add($_); }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
    close(FILE1);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    close(FILE2);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    if ($sha1->hexdigest ne $sha2->hexdigest) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
        return (0, "Result doesn't match expectations");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    return (1);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
}
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
my %tests = ();
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
$tests{'output'} = sub {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    my ($module, $fname) = @_;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    my $output = 'unittest_tempoutput';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    my $desired = $fname . '.correct';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    my $cmd = undef;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    my $endlines = 1;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    # !!! FIXME: this should go elsewhere.
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    if ($module eq 'preprocessor') {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
        $cmd = "./mojoshader-compiler -P '$fname' -o '$output'";
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    } else {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        return (0, "Don't know how to do this module type");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    if (system($cmd) != 0) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
        unlink($output) if (-f $output);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
        return (0, "External program reported error");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    if (not -f $output) { return (0, "Didn't get any output file"); }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    my @retval = compare_files($desired, $output, $endlines);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    unlink($output);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    return @retval;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
};
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
my $totaltests = 0;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
my $pass = 0;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
my $fail = 0;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
my $skip = 0;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
my $result = '';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
foreach (@modules) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    my $module = $_;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    foreach (keys %tests) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
        my $testtype = $_;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
        my $fn = $tests{$_};
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
        my $d = "$testdir/$module/$testtype";
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
        next if (not -d $d);  # no tests at the moment.
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
        opendir(TESTDIR, $d) || die("Failed to open dir '$d': $!\n");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
        print(" ... $module / $testtype ...\n");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
        my $fname = readdir(TESTDIR);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
        while (defined $fname) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
            my $origfname = $fname;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
            $fname = readdir(TESTDIR);  # set for next iteration.
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
            next if (-d $origfname);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
            next if ($origfname =~ /\.correct\Z/);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
            my $fullfname = "$d/$origfname";
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
            my ($rc, $reason) = &$fn($module, $fullfname);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
            if ($rc == 1) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
                $result = 'PASS';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
                $pass++;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
            } elsif ($rc == 0) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
                $result = 'FAIL';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
                $fail++;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
            } elsif ($rc == -1) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
                $result = 'SKIP';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
                $skip++;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
            }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
            if (defined $reason) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
                $reason = " ($reason)";
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
            } else {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
                $reason = '';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
            }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
            print("$result ${origfname}${reason}\n");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
            $totaltests++;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
        }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
        closedir(TESTDIR);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
}
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
print("\n$totaltests tests, $pass passed, $fail failed, $skip skipped.\n\n");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
exit(($fail > 0) ? 1 : 0);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
# end if run_tests.pl ...
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128