]> mj.ucw.cz Git - eval.git/blob - judge/tester
fd6cce3fcd620b886260d7178828793b921c1bd8
[eval.git] / judge / tester
1 #!/usr/bin/perl
2 # A simple unit testing script
3 # (c) 2004--2007 Martin Mares <mj@ucw.cz>
4 # (c) 2007 Pavel Charvat <pchar@ucw.cz>
5
6 # Tests in the test file have a syntax similar to mail headers,
7 # individual test case are separated by blank lines and they can contain
8 # the following fields:
9 #
10 #       Name:   name of the case (default: sequence number since start of file)
11 #       Run:    command to run (default: command from the previous test case)
12 #               This can be an arbitrary shell pipeline, sequences $0 to $9 are
13 #               replaced by file names of In<N> or Out<N> files (see below).
14 #       In:     lines to pass to the program as standard input
15 #       Out:    lines to expect at the program's standard output
16 #       In<N>:  lines to pass to the program as input file <N>
17 #       Out<N>: lines to expect from the program in output file <N>
18 #               Both In<N> and Out<N> can be specified simultaneously if we
19 #               are testing a program which modifies some of its input files.
20 #       Exit:   expected exit code of the program (default: 0)
21
22 use Getopt::Long;
23
24 my $verbose = 0;
25 my $rundir = ".";
26 GetOptions("verbose!" => \$verbose,
27            "rundir=s" => \$rundir)
28         or die "Usage: tester [--verbose] [--rundir=<dir>] <tests>\n";
29
30 my @tests = ();
31 my $tt;
32 my $append_to;
33
34 while (<>) {
35         /^#/ && next;
36         if (/^\s*$/) {
37                 $tt = undef;
38                 $append_to = undef;
39         } elsif (defined($append_to) && /^\s+(.*)$/) {
40                 $$append_to .= "\n$1";
41         } elsif (my ($n,$v) = /^(\w+):\s+(.*)$/) {
42                 if (!$tt) {
43                         $tt = {};
44                         push @tests, $tt;
45                 }
46                 ($tt->{$n}) && die "$n already defined";
47                 $tt->{$n} = $v;
48                 $append_to = \($tt->{$n});
49         } else {
50                 die "Test script syntax error";
51         }
52 }
53
54 my $i = 0;
55 my $errors = 0;
56 my $prev_run = undef;
57 TEST: foreach $tt (@tests) {
58         $i++;
59         my $name = $tt->{'Name'} || $i;
60         print "Test $name: ";
61         $run = ($tt->{'Run'} || $prev_run) or die "Don't know what to run";
62         $prev_run = $run;
63
64         my @out_files = ();
65         my @out_checks = ();
66         my @temps = ();
67         my $redirs = "";
68
69         if (defined $tt->{'In'}) {
70                 my $ifi = "tmp/test$i.in";
71                 open X, ">$rundir/$ifi" or die "Unable to create $ifi";
72                 print X $tt->{'In'}, "\n";
73                 close X;
74                 $redirs .= " <$ifi";
75                 push @temps, $ifi;
76         } else {
77                 $redirs .= " </dev/null";
78         }
79         if (defined $tt->{'Out'}) {
80                 my $ofi = "tmp/test$i.out";
81                 unlink "$rundir/$ofi";
82                 $redirs .= " >$ofi";
83                 push @temps, $ofi;
84                 push @out_files, $ofi;
85                 push @out_checks, $tt->{'Out'};
86         } else {
87                 $redirs .= " >/dev/null";
88         }
89         foreach my $arg (0..9) {
90                 my $f = "tmp/test$i.$arg";
91                 if (defined $tt->{"Out$arg"}) {
92                         unlink "$rundir/$f";
93                         push @temps, $f;
94                         push @out_files, $f;
95                         push @out_checks, $tt->{"Out$arg"};
96                 }
97                 if (defined $tt->{"In$arg"}) {
98                         open X, ">$rundir/$f" or die "Unable to create $f";
99                         print X $tt->{"In$arg"}, "\n";
100                         close X;
101                         push @temps, $f;
102                 }
103         }
104         $run =~ s/\$(\d)/tmp\/test$i.$1/g;
105         print "(running $run) " if $verbose;
106         system "cd $rundir && ( $run ) $redirs";
107         if ($? % 256) {
108                 print "FAILED with status code $?\n";
109                 $errors++;
110                 next;
111         }
112         my $ec = $? / 256;
113         my $expect_ec = $tt->{'Exit'} || 0;
114         if ($ec != $expect_ec) {
115                 print "FAILED: unexpected exit code $ec\n";
116                 $errors++;
117                 next;
118         }
119
120         for (my $i=0; $i<=$#out_files; $i++) {
121                 my $ofi = $out_files[$i];
122                 open X, "<$rundir/$ofi" or die "Unable to read $ofi";
123                 my $out;
124                 {
125                         local $/ = undef;
126                         $out = <X>;
127                 }
128                 close X;
129                 if ($out ne $out_checks[$i] . "\n") {
130                         print "FAILED (see $ofi)\n";
131                         $errors++;
132                         next TEST;
133                 }
134         }
135
136         foreach my $f (@temps) {
137                 unlink "$rundir/$f";
138         }
139         print "OK\n";
140 }
141
142 exit !!$errors;