unit_tests/run_tests.pl
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2009 00:28:03 -0400
changeset 742 2108031e4ef5
parent 741 0da871dd944b
child 750 238675cfa788
permissions -rwxr-xr-x
Fixed command lines for unit tests.
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
742
2108031e4ef5 Fixed command lines for unit tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 741
diff changeset
    12
2108031e4ef5 Fixed command lines for unit tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 741
diff changeset
    13
my $GPrintCmds = 0;
2108031e4ef5 Fixed command lines for unit tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 741
diff changeset
    14
740
13c0707a2e3f Minor cleanup; crunch down list to one line.
Ryan C. Gordon <icculus@icculus.org>
parents: 736
diff changeset
    15
my @modules = qw( preprocessor assembler compiler parser );
736
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
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
sub compare_files {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
    my ($a, $b, $endlines) = @_;
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(FILE1, '<', $a)) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
        return (0, "Couldn't open '$a' for checksum");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    if (not open(FILE2, '<', $b)) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
        close(FILE1);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
        return (0, "Couldn't open '$b' for checksum");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
    }
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
    my $sha1 = Digest::SHA1->new;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    my $sha2 = Digest::SHA1->new;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
    if (not $endlines) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
        $sha1->addfile(*FILE1);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
        $sha2->addfile(*FILE2);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    } else {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
        while (<FILE1>) { s/[\r\n]//g; $sha1->add($_); }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
        while (<FILE2>) { s/[\r\n]//g; $sha2->add($_); }
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    }
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
    close(FILE1);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    close(FILE2);
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
    if ($sha1->hexdigest ne $sha2->hexdigest) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
        return (0, "Result doesn't match expectations");
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
    return (1);
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
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
my %tests = ();
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
$tests{'output'} = sub {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    my ($module, $fname) = @_;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    my $output = 'unittest_tempoutput';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    my $desired = $fname . '.correct';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    my $cmd = undef;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    my $endlines = 1;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    # !!! FIXME: this should go elsewhere.
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    if ($module eq 'preprocessor') {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        $cmd = "./mojoshader-compiler -P '$fname' -o '$output'";
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    } else {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
        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
    64
    }
742
2108031e4ef5 Fixed command lines for unit tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 741
diff changeset
    65
    $cmd .= ' 2>/dev/null 1>/dev/null';
2108031e4ef5 Fixed command lines for unit tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 741
diff changeset
    66
2108031e4ef5 Fixed command lines for unit tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 741
diff changeset
    67
    print("$cmd\n") if ($GPrintCmds);
736
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    if (system($cmd) != 0) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
        unlink($output) if (-f $output);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        return (0, "External program reported error");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    }
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
    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
    75
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    my @retval = compare_files($desired, $output, $endlines);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    unlink($output);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    return @retval;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
};
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
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
my $totaltests = 0;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
my $pass = 0;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
my $fail = 0;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
my $skip = 0;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
my $result = '';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
foreach (@modules) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    my $module = $_;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    foreach (keys %tests) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
        my $testtype = $_;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
        my $fn = $tests{$_};
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
        my $d = "$testdir/$module/$testtype";
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
        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
    95
        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
    96
        print(" ... $module / $testtype ...\n");
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
        my $fname = readdir(TESTDIR);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
        while (defined $fname) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
            my $origfname = $fname;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
            $fname = readdir(TESTDIR);  # set for next iteration.
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
            next if (-d $origfname);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
            next if ($origfname =~ /\.correct\Z/);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
            my $fullfname = "$d/$origfname";
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
            my ($rc, $reason) = &$fn($module, $fullfname);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
            if ($rc == 1) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
                $result = 'PASS';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
                $pass++;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
            } elsif ($rc == 0) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
                $result = 'FAIL';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
                $fail++;
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
            } elsif ($rc == -1) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
                $result = 'SKIP';
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
                $skip++;
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
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
            if (defined $reason) {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
                $reason = " ($reason)";
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
            } else {
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
                $reason = '';
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
            print("$result ${origfname}${reason}\n");
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
            $totaltests++;
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
        closedir(TESTDIR);
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
}
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
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
   130
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
exit(($fail > 0) ? 1 : 0);
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
# end if run_tests.pl ...
a392aabaa21c Initial framework for unit testing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134