Add more tests.
[pintos-anon] / grading / userprog / run-tests
1 #! /usr/bin/perl
2
3 use warnings;
4 use strict;
5 use POSIX;
6 use Algorithm::Diff;
7 use Getopt::Long;
8
9 our ($VERBOSE) = 0;     # Verbosity of output
10 our (@TESTS);           # Tests to run.
11 my ($clean) = 0;
12 my ($grade) = 0;
13
14 GetOptions ("v|verbose+" => \$VERBOSE,
15             "h|help" => sub { usage (0) },
16             "t|test=s" => \@TESTS,
17             "c|clean" => \$clean,
18             "g|grade" => \$grade)
19     or die "Malformed command line; use --help for help.\n";
20 die "Non-option argument not supported; use --help for help.\n"
21     if @ARGV > 0;
22
23 sub usage {
24     my ($exitcode) = @_;
25     print "run-tests, for grading Pintos multiprogramming projects.\n\n";
26     print "Invoke from a directory containing a student tarball named by\n";
27     print "the submit script, e.g. username.Oct.12.04.20.04.09.tar.gz.\n";
28     print "In normal usage, no options are needed.\n\n";
29     print "Output is produced in tests.out and details.out.\n\n";
30     print "Options:\n";
31     print "  -c, --clean     Remove old output files before starting\n";
32     print "  -t, --test=TEST Execute TEST only (allowed multiple times)\n";
33     print "  -g, --grade     Instead of running tests, compose grade.out\n";
34     print "  -v, --verbose   Print commands before executing them\n";
35     print "  -h, --help      Print this help message\n";
36     exit $exitcode;
37 }
38
39 # Default set of tests.
40 @TESTS = qw (args-argc args-argv0 args-argvn args-single args-multiple
41              args-dbl-space
42              sc-bad-sp sc-bad-arg sc-boundary
43              halt exit
44              create-normal create-empty create-null create-bad-ptr 
45              create-long create-exists create-bound
46              open-normal open-missing open-boundary open-empty open-null
47              open-bad-ptr open-twice
48              close-normal close-twice close-stdin close-stdout close-bad-fd
49              read-normal read-bad-ptr read-boundary read-zero read-stdout
50              read-bad-fd
51              write-normal write-bad-ptr write-boundary write-zero write-stdin
52              write-bad-fd
53              ) unless @TESTS > 0;
54
55 our (%args);
56 for my $key ('args-argc', 'args-argv0', 'args-argvn', 'args-multiple') {
57     $args{$key} = "some arguments for you!";
58 }
59 $args{'args-single'} = "onearg";
60 $args{'args-dbl-space'} = "two  args";
61
62 # Handle final grade mode.
63 if ($grade) {
64     open (OUT, ">grade.out") or die "grade.out: create: $!\n";
65
66     open (GRADE, "<grade.txt") or die "grade.txt: open: $!\n";
67     while (<GRADE>) {
68         last if /^\s*$/;
69         print OUT;
70     }
71     close (GRADE);
72     
73     my (@tests) = snarf ("tests.out");
74     my ($p_got, $p_pos) = $tests[0] =~ m%\((\d+)/(\d+)\)% or die;
75
76     my (@review) = snarf ("review.txt");
77     my ($part_lost) = (0, 0);
78     for (my ($i) = $#review; $i >= 0; $i--) {
79         local ($_) = $review[$i];
80         if (my ($loss) = /^\s*([-+]\d+)/) {
81             $part_lost += $loss;
82         } elsif (my ($out_of) = m%\[\[/(\d+)\]\]%) {
83             my ($got) = $out_of + $part_lost;
84             $got = 0 if $got < 0;
85             $review[$i] =~ s%\[\[/\d+\]\]%($got/$out_of)% or die;
86             $part_lost = 0;
87
88             $p_got += $got;
89             $p_pos += $out_of;
90         }
91     }
92     die "Lost points outside a section\n" if $part_lost;
93
94     for (my ($i) = 1; $i <= $#review; $i++) {
95         if ($review[$i] =~ /^-{3,}\s*$/ && $review[$i - 1] !~ /^\s*$/) {
96             $review[$i] = '-' x (length ($review[$i - 1]));
97         }
98     }
99
100     print OUT "\nOVERALL SCORE\n";
101     print OUT "-------------\n";
102     print OUT "$p_got points out of $p_pos total\n\n";
103
104     print OUT map ("$_\n", @tests), "\n";
105     print OUT map ("$_\n", @review), "\n";
106
107     print OUT "DETAILS\n";
108     print OUT "-------\n\n";
109     print OUT map ("$_\n", snarf ("details.out"));
110
111     exit 0;
112 }
113
114 # Find the directory that contains the grading files.
115 our ($GRADES_DIR);
116 ($GRADES_DIR = $0) =~ s#/[^/]+$##;
117 -d $GRADES_DIR or die "$GRADES_DIR: stat: $!\n";
118
119 if ($clean) {
120     # Verify that we're roughly in the correct directory
121     # before we go blasting away files.
122     choose_tarball ();
123
124     xsystem ("rm -rf output pintos", VERBOSE => 1);
125     xsystem ("rm -f details.out tests.out", VERBOSE => 1);
126 }
127
128 # Create output directory, if it doesn't already exist.
129 -d ("output") || mkdir ("output") or die "output: mkdir: $!\n";
130
131 # Extract submission.
132 extract_tarball () if ! -d "pintos";
133
134 # Compile submission.
135 compile ();
136
137 # Verify that the proper directory was submitted.
138 -d "pintos/src/threads" or die "pintos/src/threads: stat: $!\n";
139
140 # Run and grade the tests.
141 our $test;
142 our %result;
143 our %details;
144 our %extra;
145 for $test (@TESTS) {
146     print "$test: ";
147     my ($result) = run_test ($test);
148     if ($result eq 'ok') {
149         $result = grade_test ($test);
150         $result =~ s/\n$//;
151     }
152     print "$result";
153     print " - with warnings" if $result eq 'ok' && defined $details{$test};
154     print "\n";
155     
156     $result{$test} = $result;
157 }
158
159 # Write output.
160 write_grades ();
161 write_details ();
162 \f
163 sub choose_tarball {
164     my (@tarballs)
165         = grep (/^[a-z0-9]+\.[A-Za-z]+\.\d+\.\d+\.\d+\.\d+.\d+\.tar\.gz$/,
166                 glob ("*.tar.gz"));
167     die "no pintos dir and no source tarball\n" if scalar (@tarballs) == 0;
168
169     # Sort tarballs in reverse order by time.
170     @tarballs = sort { ext_mdyHMS ($b) cmp ext_mdyHMS ($a) } @tarballs;
171
172     print "Multiple tarballs: choosing $tarballs[0]\n"
173         if scalar (@tarballs) > 1;
174     return $tarballs[0];
175 }
176
177 sub extract_tarball {
178     my ($tarball) = choose_tarball ();
179
180     mkdir "pintos" or die "pintos: mkdir: $!\n";
181     mkdir "pintos/src" or die "pintos: mkdir: $!\n";
182
183     print "Extracting $tarball...\n";
184     xsystem ("cd pintos/src && tar xzf ../../$tarball",
185              DIE => "extraction failed\n");
186
187     print "Patching...\n";
188     xsystem ("patch -fs pintos/src/lib/debug.c < $GRADES_DIR/panic.diff",
189              LOG => "patch",
190              DIE => "patch failed\n");
191 }
192
193 sub ext_mdyHMS {
194     my ($s) = @_;
195     my ($ms, $d, $y, $H, $M, $S) =
196         $s =~ /.([A-Za-z]+)\.(\d+)\.(\d+)\.(\d+)\.(\d+).(\d+)\.tar\.gz$/
197         or die;
198     my ($m) = index ("janfebmaraprmayjunjulaugsepoctnovdec", lc $ms) / 3
199         or die;
200     return sprintf "%02d-%02d-%02d %02d:%02d:%02d", $y, $m, $d, $H, $M, $S;
201 }
202 \f
203 sub test_source {
204     my ($test) = @_;
205     my ($src) = "$GRADES_DIR/$test.c";
206     -e $src or die "$src: stat: $!\n";
207     return $src;
208 }
209
210 sub test_constants {
211    my ($defines) = "";
212    return $defines;
213  }
214
215 sub run_test {
216     my ($test) = @_;
217
218     # Reuse older results if any.
219     if (open (DONE, "<output/$test/done")) {
220         my ($status);
221         $status = <DONE>;
222         chomp $status;
223         close (DONE);
224         return $status;
225     }
226
227     # Really run the test.
228     my ($status) = really_run_test ($test);
229
230     # Save the results for later.
231     open (DONE, ">output/$test/done") or die "output/$test/done: create: $!\n";
232     print DONE "$status\n";
233     close (DONE);
234
235     return $status;
236 }
237
238 sub compile {
239     print "Compiling...\n";
240     xsystem ("cd pintos/src/userprog && make", LOG => "make")
241         or return "compile error";
242 }
243
244 sub really_run_test {
245     # Need to run it.
246     # If there's residue from an earlier test, move it to .old.
247     # If there's already a .old, delete it.
248     xsystem ("rm -rf output/$test.old", VERBOSE => 1) if -d "output/$test.old";
249     rename "output/$test", "output/$test.old" or die "rename: $!\n"
250         if -d "output/$test";
251
252     # Make output directory.
253     mkdir "output/$test";
254     xsystem ("cp $GRADES_DIR/$test.dsk output/$test/fs.dsk",
255              DIE => "cp failed\n");
256
257     # Run.
258     my ($timeout) = 10;
259     my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
260     xsystem ("pintos "
261              . "--os-disk=pintos/src/userprog/build/os.dsk "
262              . "--fs-disk=output/$test/fs.dsk "
263              . "-v run -q -ex \"$test$testargs\"",
264              LOG => "$test/run",
265              TIMEOUT => $timeout)
266         or return "Bochs error";
267     
268     return "ok";
269 }
270
271 sub grade_test {
272     my ($test) = @_;
273
274     my (@output) = snarf ("output/$test/run.out");
275
276     my ($grade_func) = "grade_$test";
277     $grade_func =~ s/-/_/g;
278     if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
279         eval {
280             verify_common (@output);
281             compare_output ("$GRADES_DIR/$test.exp", @output);
282         }
283     } else {
284         eval "$grade_func (\@output)";
285     }
286     if ($@) {
287         die $@ if $@ =~ /at \S+ line \d+$/;
288         return $@;
289     }
290     return "ok";
291 }
292 \f
293 sub grade_write_normal {
294     my (@output) = @_;
295     verify_common (@output);
296     compare_output ("$GRADES_DIR/write-normal.exp", @output);
297     my ($test_txt) = "output/$test/test.txt";
298     get_file ("test.txt", $test_txt) if ! -e $test_txt;
299     compare_output ("$GRADES_DIR/sample.txt", snarf ($test_txt));
300 }
301
302 sub get_file {
303     my ($guest_fn, $host_fn) = @_;
304     xsystem ("pintos "
305              . "--os-disk=pintos/src/userprog/build/os.dsk "
306              . "--fs-disk=output/$test/fs.dsk "
307              . "-v get $guest_fn $host_fn",
308              LOG => "$test/get-$guest_fn",
309              TIMEOUT => 10)
310         or die "get $guest_fn failed\n";
311 }
312
313 sub grade_alarm_negative {
314     my (@output) = @_;
315     verify_common (@output);
316     die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output);
317 }
318
319 sub grade_join_invalid {
320     my (@output) = @_;
321     verify_common (@output);
322     grep (/Testing invalid join/, @output) or die "Test didn't start\n";
323     grep (/Invalid join test done/, @output) or die "Test didn't complete\n";
324 }
325
326 sub grade_join_no {
327     my (@output) = @_;
328     verify_common (@output);
329     grep (/Testing no join/, @output) or die "Test didn't start\n";
330     grep (/No join test done/, @output) or die "Test didn't complete\n";
331 }
332
333 sub grade_join_multiple {
334     my (@output) = @_;
335
336     verify_common (@output);
337     my (@t);
338     $t[4] = $t[5] = $t[6] = -1;
339     local ($_);
340     foreach (@output) {
341         my ($idx) = /^Thread (\d+)/ or next;
342         my ($iter) = /iteration (\d+)$/;
343         $iter = 5 if /done!$/;
344         die "Malformed output\n" if !defined $iter;
345         if ($idx == 6) {
346             die "Thread 6 started before either other thread finished\n"
347                 if $t[4] < 5 && $t[5] < 5;
348             die "Thread 6 started before thread 4 finished\n"
349                 if $t[4] < 5;
350             die "Thread 6 started before thread 5 finished\n"
351                 if $t[5] < 5;
352         }
353         die "Thread $idx out of order output\n" if $t[$idx] != $iter - 1;
354         $t[$idx] = $iter;
355     }
356
357     my ($err) = "";
358     for my $idx (4, 5, 6) {
359         if ($t[$idx] == -1) {
360             $err .= "Thread $idx did not run at all\n";
361         } elsif ($t[$idx] != 5) {
362             $err .= "Thread $idx only completed $t[$idx] iterations\n";
363         }
364     }
365     die $err if $err ne '';
366 }
367
368 sub grade_priority_fifo {
369     my (@output) = @_;
370
371     verify_common (@output);
372     my ($thread_cnt) = 10;
373     my ($iter_cnt) = 5;
374     my (@order);
375     my (@t) = (-1) x $thread_cnt;
376     local ($_);
377     foreach (@output) {
378         my ($idx) = /^Thread (\d+)/ or next;
379         my ($iter) = /iteration (\d+)$/;
380         $iter = $iter_cnt if /done!$/;
381         die "Malformed output\n" if !defined $iter;
382         if (@order < $thread_cnt) {
383             push (@order, $idx);
384             die "Thread $idx repeated within first $thread_cnt iterations: "
385                 . join (' ', @order) . ".\n"
386                 if grep ($_ == $idx, @order) != 1;
387         } else {
388             die "Thread $idx ran when $order[0] should have.\n"
389                 if $idx != $order[0];
390             push (@order, shift @order);
391         }
392         die "Thread $idx out of order output.\n" if $t[$idx] != $iter - 1;
393         $t[$idx] = $iter;
394     }
395
396     my ($err) = "";
397     for my $idx (0..$#t) {
398         if ($t[$idx] == -1) {
399             $err .= "Thread $idx did not run at all.\n";
400         } elsif ($t[$idx] != $iter_cnt) {
401             $err .= "Thread $idx only completed $t[$idx] iterations.\n";
402         }
403     }
404     die $err if $err ne '';
405 }
406
407 sub grade_mlfqs_on {
408     my (@output) = @_;
409     verify_common (@output);
410     our (@mlfqs_on_stats) = mlfqs_stats (@output);
411 }
412
413 sub grade_mlfqs_off {
414     my (@output) = @_;
415     verify_common (@output);
416     our (@mlfqs_off_stats) = mlfqs_stats (@output);
417 }
418
419 sub grade_mlfqs_speedup {
420     our (@mlfqs_off_stats);
421     our (@mlfqs_on_stats);
422     eval {
423         check_mlfqs ();
424         my ($off_ticks) = $mlfqs_off_stats[1];
425         my ($on_ticks) = $mlfqs_on_stats[1];
426         die "$off_ticks ticks without MLFQS, $on_ticks with MLFQS\n"
427             if $on_ticks >= $off_ticks;
428         die "ok\n";
429     };
430     chomp $@;
431     $result{'mlfqs-speedup'} = $@;
432 }
433
434 sub grade_mlfqs_priority {
435     our (@mlfqs_off_stats);
436     our (@mlfqs_on_stats);
437     eval {
438         check_mlfqs () if !defined (@mlfqs_on_stats);
439         for my $cat qw (CPU IO MIX) {
440             die "Priority changed away from PRI_DEFAULT (29) without MLFQS\n"
441                 if $mlfqs_off_stats[0]{$cat}{MIN} != 29
442                 || $mlfqs_off_stats[0]{$cat}{MAX} != 29;
443             die "Minimum priority never changed from PRI_DEFAULT (29) "
444                 . "with MLFQS\n"
445                 if $mlfqs_on_stats[0]{$cat}{MIN} == 29;
446             die "Maximum priority never changed from PRI_DEFAULT (29) "
447                 . "with MLFQS\n"
448                 if $mlfqs_on_stats[0]{$cat}{MAX} == 29;
449         }
450         die "ok\n";
451     };
452     chomp $@;
453     $result{'mlfqs-priority'} = $@;
454 }
455
456 sub check_mlfqs {
457     our (@mlfqs_off_stats);
458     our (@mlfqs_on_stats);
459     die "p1-4 didn't finish with MLFQS on or off\n"
460         if !defined (@mlfqs_off_stats) && !defined (@mlfqs_on_stats);
461     die "p1-4 didn't finish with MLFQS on\n"
462         if !defined (@mlfqs_on_stats);
463     die "p1-4 didn't finish with MLFQS off\n"
464         if !defined (@mlfqs_off_stats);
465 }
466
467 sub mlfqs_stats {
468     my (@output) = @_;
469     my (%stats) = (CPU => {}, IO => {}, MIX => {});
470     my (%map) = ("CPU intensive" => 'CPU',
471                  "IO intensive" => 'IO',
472                  "Alternating IO/CPU" => 'MIX');
473     my (%rmap) = reverse %map;
474     my ($ticks);
475     local ($_);
476     foreach (@output) {
477         $ticks = $1 if /Timer: (\d+) ticks/;
478         my ($thread, $pri) = /^([A-Za-z\/ ]+): (\d+)$/ or next;
479         my ($t) = $map{$thread} or next;
480         
481         my ($s) = $stats{$t};
482         $$s{N}++;
483         $$s{SUM} += $pri;
484         $$s{SUM2} += $pri * $pri;
485         $$s{MIN} = $pri if !defined ($$s{MIN}) || $pri < $$s{MIN};
486         $$s{MAX} = $pri if !defined ($$s{MAX}) || $pri > $$s{MAX};
487     }
488
489     my (%expect_n) = (CPU => 5000, IO => 1000, MIX => 12000);
490     for my $cat (values (%map)) {
491         my ($s) = $stats{$cat};
492         die "$rmap{$cat} printed $$s{N} times, not $expect_n{$cat}\n"
493             if $$s{N} != $expect_n{$cat};
494         die "$rmap{$cat} priority dropped to $$s{MIN}, below PRI_MIN (0)\n"
495             if $$s{MIN} < 0;
496         die "$rmap{$cat} priority rose to $$s{MAX}, above PRI_MAX (59)\n"
497             if $$s{MAX} > 59;
498         $$s{MEAN} = $$s{SUM} / $$s{N};
499     }
500
501     return (\%stats, $ticks);
502 }
503 \f
504 sub verify_common {
505     my (@output) = @_;
506
507     my (@assertion) = grep (/PANIC/, @output);
508     if (@assertion != 0) {
509         my ($details) = "Kernel panic:\n  $assertion[0]\n";
510
511         my (@stack_line) = grep (/Call stack:/, @output);
512         if (@stack_line != 0) {
513             $details .= "  $stack_line[0]\n\n";
514             $details .= "Translation of backtrace:\n";
515             my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
516
517             my ($A2L);
518             if (`uname -m`
519                 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
520                 $A2L = "addr2line";
521             } else {
522                 $A2L = "i386-elf-addr2line";
523             }
524             open (A2L, "$A2L -fe output/$test/kernel.o @addrs|");
525             for (;;) {
526                 my ($function, $line);
527                 last unless defined ($function = <A2L>);
528                 $line = <A2L>;
529                 chomp $function;
530                 chomp $line;
531                 $details .= "  $function ($line)\n";
532             }
533         }
534         $extra{$test} = $details;
535         die "Kernel panic.  Details at end of file.\n"
536     }
537
538     die "No output at all\n" if @output == 0;
539     die "Didn't start up properly: no \"Pintos booting\" startup message\n"
540         if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
541     die "Didn't start up properly: no \"Boot complete\" startup message\n"
542         if !grep (/Boot complete/, @output);
543     die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
544         if !grep (/Timer: \d+ ticks/, @output);
545     die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
546         if !grep (/Powering off/, @output);
547 }
548
549 # Get @output without header or trailer.
550 sub get_core_output {
551     my (@output) = @_;
552
553     our ($test);
554     my ($first);
555     for ($first = 0; $first <= $#output; $first++) {
556         $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
557     }
558
559     my ($last);
560     for ($last = $#output; $last >= 0; $last--) {
561         $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
562     }
563
564     if ($last < $first) {
565         my ($no_first) = $first > $#output;
566         my ($no_last) = $last < $#output;
567         die "Couldn't locate output.\n";
568     }
569
570     return @output[$first ... $last];
571 }
572
573 sub compare_output {
574     my ($exp, @actual) = @_;
575     @actual = get_core_output (map ("$_\n", @actual));
576
577     # Fix up lines that look like exit codes.
578     for my $i (0...$#actual) {
579         if (my ($process, $code)
580             = $actual[$i] =~ /^([-a-zA-Z0-9 ]+):.*[ \(](-?\d+)\b\)?$/) {
581             $process = substr ($process, 0, 15);
582             $process =~ s/\s.*//;
583             $actual[$i] = "$process: exit($code)\n";
584         }
585     }
586
587     my ($details) = "";
588     $details .= "$test actual output:\n";
589     $details .= join ('', map ("  $_", @actual));
590
591     my (@exp) = map ("$_\n", snarf ($exp));
592
593     my ($fuzzy_match) = 0;
594     while (@exp != 0) {
595         my (@expected);
596         while (@exp != 0) {
597             my ($s) = shift (@exp);
598             last if $s eq "--OR--\n";
599             push (@expected, $s);
600         }
601
602         $details .= "\n$test acceptable output:\n";
603         $details .= join ('', map ("  $_", @expected));
604
605         # Check whether they're the same.
606         if ($#actual == $#expected) {
607             my ($eq) = 1;
608             for (my ($i) = 0; $i <= $#expected; $i++) {
609                 $eq = 0 if $actual[$i] ne $expected[$i];
610             }
611             return if $eq;
612         }
613
614         # They differ.  Output a diff.
615         my (@diff) = "";
616         my ($d) = Algorithm::Diff->new (\@expected, \@actual);
617         my ($not_fuzzy_match) = 0;
618         while ($d->Next ()) {
619             my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
620             if ($d->Same ()) {
621                 push (@diff, map ("  $_", $d->Items (1)));
622             } else {
623                 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
624                 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
625                 if ($d->Items (1)
626                     || grep (/\($test\)|exit\(-?\d+\)/, $d->Items (2))) {
627                     $not_fuzzy_match = 1;
628                 }
629             }
630         }
631         $fuzzy_match = 1 if !$not_fuzzy_match;
632
633         $details .= "Differences in `diff -u' format:\n";
634         $details .= join ('', @diff);
635         $details .= "(This is considered a `fuzzy match'.)\n" if $fuzzy_match;
636     }
637
638     $details{$test} = $details;
639     die "Output differs from expected.  Details at end of file.\n"
640         unless $fuzzy_match;
641 }
642 \f
643 sub write_grades {
644     my (@summary) = snarf ("$GRADES_DIR/tests.txt");
645
646     my ($ploss) = 0;
647     my ($tloss) = 0;
648     my ($total) = 0;
649     for (my ($i) = 0; $i <= $#summary; $i++) {
650         local ($_) = $summary[$i];
651         if (my ($loss, $test) = /^  -(\d+) ([-a-zA-Z0-9]+):/) {
652             my ($result) = $result{$test} || "Not tested.";
653
654             if ($result eq 'ok') {
655                 splice (@summary, $i, 1);
656                 $i--;
657             } else {
658                 $ploss += $loss;
659                 $tloss += $loss;
660                 splice (@summary, $i + 1, 0,
661                         map ("     $_", split ("\n", $result)));
662             }
663         } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
664             $total += $ptotal;
665             $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
666             splice (@summary, $i, 0, "  All tests passed.") if $ploss == 0;
667             $ploss = 0;
668             $i++;
669         }
670     }
671     my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
672     $summary[0] =~ s/\[\[total\]\]/$ts/;
673
674     open (SUMMARY, ">tests.out");
675     print SUMMARY map ("$_\n", @summary);
676     close (SUMMARY);
677 }
678
679 sub write_details {
680     open (DETAILS, ">details.out");
681     my ($n) = 0;
682     for my $test (@TESTS) {
683         next if $result{$test} eq 'ok' && !defined $details{$test};
684         
685         my ($details) = $details{$test};
686         next if !defined ($details) && ! -e "output/$test/run.out";
687
688         print DETAILS "\n" if $n++;
689         print DETAILS "--- $test details ", '-' x (50 - length ($test));
690         print DETAILS "\n\n";
691
692         if (!defined $details) {
693             $details = "Output:\n\n" . snarf ("output/$test/run.out");
694         }
695         print DETAILS $details;
696
697         print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
698             if defined $extra{$test};
699     }
700     close (DETAILS);
701
702 }
703 \f
704 sub xsystem {
705     my ($command, %options) = @_;
706     print "$command\n" if $VERBOSE || $options{VERBOSE};
707
708     my ($log) = $options{LOG};
709     if (defined ($log)) {
710         $command = "($command) >output/$log.out 2>output/$log.err";
711     }
712
713     my ($pid, $status);
714     eval {
715         local $SIG{ALRM} = sub { die "alarm\n" };
716         alarm $options{TIMEOUT} if defined $options{TIMEOUT};
717         $pid = fork;
718         die "fork: $!\n" if !defined $pid;
719         exec ($command), die "$command: exec: $!\n" if !$pid;
720         waitpid ($pid, 0);
721         $status = $?;
722         alarm 0;
723     };
724     if ($@) {
725         die unless $@ eq "alarm\n";   # propagate unexpected errors
726         print "Timed out.\n";
727         kill SIGTERM, $pid;
728         $status = 0;
729     }
730
731     if (WIFSIGNALED ($status)) {
732         my ($signal) = WTERMSIG ($status);
733         die "Interrupted\n" if $signal == SIGINT;
734         print "Child terminated with signal $signal\n";
735     }
736
737     unlink ("output/$log.err") if defined ($log) && $status == 0;
738
739     die $options{DIE} if $status != 0 && defined $options{DIE};
740
741     return $status == 0;
742 }
743
744 sub snarf {
745     my ($file) = @_;
746     open (OUTPUT, $file) or die "$file: open: $!\n";
747     my (@lines) = <OUTPUT>;
748     chomp (@lines);
749     close (OUTPUT);
750     return wantarray ? @lines : join ('', map ("$_\n", @lines));
751 }
752
753 sub files_equal {
754     my ($a, $b) = @_;
755     my ($equal);
756     open (A, "<$a") or die "$a: open: $!\n";
757     open (B, "<$b") or die "$b: open: $!\n";
758     if (-s A != -s B) {
759         $equal = 0;
760     } else {
761         my ($sa, $sb);
762         for (;;) {
763             sysread (A, $sa, 1024);
764             sysread (B, $sb, 1024);
765             $equal = 0, last if $sa ne $sb;
766             $equal = 1, last if $sa eq '';
767         }
768     }
769     close (A);
770     close (B);
771     return $equal;
772 }
773
774 sub file_contains {
775     my ($file, $expected) = @_;
776     open (FILE, "<$file") or die "$file: open: $!\n";
777     my ($actual);
778     sysread (FILE, $actual, -s FILE);
779     my ($equal) = $actual eq $expected;
780     close (FILE);
781     return $equal;
782 }
783
784 sub number_lines {
785     my ($ln, $lines) = @_;
786     my ($out);
787     for my $line (@$lines) {
788         chomp $line;
789         $out .= sprintf "%4d  %s\n", $ln++, $line;
790     }
791     return $out;
792 }