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