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