More filesys tests.
[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
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/vm && 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/vm/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     for my $fn (@extra_files) {
284         return "format/put error" 
285             if !xsystem ("$pintos_base_cmd put $GRADES_DIR/$fn $fn",
286                          LOG => "$test/put-$fn", TIMEOUT => 60, EXPECT => 1);
287     }
288     
289     # Run.
290     my ($timeout) = 60;
291     my ($testargs) = defined ($args{$test}) ? " $args{$test}" : "";
292     my ($result) =
293         xsystem ("$pintos_base_cmd run -q -ex \"$test$testargs\"",
294                  LOG => "$test/run", TIMEOUT => $timeout, EXPECT => 1)
295         ? "ok" : "Bochs error";
296     unlink ("output/$test/fs.dsk", "output/$test/swap.dsk");
297     return $result;
298 }
299
300 sub grade_test {
301     my ($test) = @_;
302
303     my (@output) = snarf ("output/$test/run.out");
304
305     my ($grade_func) = "grade_$test";
306     $grade_func =~ s/-/_/g;
307     if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) {
308         eval {
309             verify_common (@output);
310             compare_output ("$GRADES_DIR/$test.exp", @output);
311         }
312     } else {
313         eval "$grade_func (\@output)";
314     }
315     if ($@) {
316         die $@ if $@ =~ /at \S+ line \d+$/;
317         return $@;
318     }
319     return "ok";
320 }
321 \f
322 sub grade_process_death {
323     my ($proc_name, @output) = @_;
324
325     verify_common (@output);
326     @output = get_core_output (@output);
327     die "First line of output is not `($proc_name) begin' message.\n"
328         if $output[0] ne "($proc_name) begin";
329     die "Output contains `FAIL' message.\n"
330         if grep (/FAIL/, @output);
331     die "Output contains spurious ($proc_name) message.\n"
332         if grep (/\($proc_name\)/, @output) > 1;
333 }
334
335 sub grade_pt_bad_addr {
336     grade_process_death ('pt-bad-addr', @_);
337 }
338
339 sub grade_pt_write_code {
340     grade_process_death ('pt-write-code', @_);
341 }
342
343 sub grade_mmap_unmap {
344     grade_process_death ('mmap-unmap', @_);
345 }
346 \f
347 sub verify_common {
348     my (@output) = @_;
349
350     my (@assertion) = grep (/PANIC/, @output);
351     if (@assertion != 0) {
352         my ($details) = "Kernel panic:\n  $assertion[0]\n";
353
354         my (@stack_line) = grep (/Call stack:/, @output);
355         if (@stack_line != 0) {
356             $details .= "  $stack_line[0]\n\n";
357             $details .= "Translation of backtrace:\n";
358             my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
359
360             my ($A2L);
361             if (`uname -m`
362                 =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
363                 $A2L = "addr2line";
364             } else {
365                 $A2L = "i386-elf-addr2line";
366             }
367             open (A2L, "$A2L -fe pintos/src/vm/build/kernel.o @addrs|");
368             for (;;) {
369                 my ($function, $line);
370                 last unless defined ($function = <A2L>);
371                 $line = <A2L>;
372                 chomp $function;
373                 chomp $line;
374                 $details .= "  $function ($line)\n";
375             }
376         }
377
378         if ($assertion[0] =~ /sec_no < d->capacity/) {
379             $details .= <<EOF;
380 \nThis assertion commonly fails when accessing a file via
381 an inode that has been closed and freed.  Freeing an inode
382 clears all its sector indexes to 0xcccccccc, which is not
383 a valid sector number for disks smaller than about 1.6 TB.
384 EOF
385         }
386
387         $extra{$test} = $details;
388         die "Kernel panic.  Details at end of file.\n"
389     }
390
391     if (grep (/Pintos booting/, @output) > 1) {
392         my ($details);
393
394         $details = "Pintos spontaneously rebooted during this test.\n";
395         $details .= "This is most often due to unhandled page faults.\n";
396         $details .= "Here's the output from the initial boot through the\n";
397         $details .= "first reboot:\n\n";
398
399         my ($i) = 0;
400         local ($_);
401         for (@output) {
402             $details .= "  $_\n";
403             last if /Pintos booting/ && ++$i > 1;
404         }
405         $details{$test} = $details;
406         die "Triple-fault caused spontaneous reboot(s).  "
407             . "Details at end of file.\n";
408     }
409
410     die "No output at all\n" if @output == 0;
411     die "Didn't start up properly: no \"Pintos booting\" startup message\n"
412         if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
413     die "Didn't start up properly: no \"Boot complete\" startup message\n"
414         if !grep (/Boot complete/, @output);
415     die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
416         if !grep (/Timer: \d+ ticks/, @output);
417     die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
418         if !grep (/Powering off/, @output);
419 }
420
421 # Get @output without header or trailer.
422 sub get_core_output {
423     my (@output) = @_;
424
425     our ($test);
426     my ($first);
427     for ($first = 0; $first <= $#output; $first++) {
428         $first++, last if $output[$first] =~ /^Executing '$test.*':$/;
429     }
430
431     my ($last);
432     for ($last = $#output; $last >= 0; $last--) {
433         $last--, last if $output[$last] =~ /^Timer: \d+ ticks$/;
434     }
435
436     if ($last < $first) {
437         my ($no_first) = $first > $#output;
438         my ($no_last) = $last < $#output;
439         die "Couldn't locate output.\n";
440     }
441
442     return @output[$first ... $last];
443 }
444
445 sub fix_exit_codes {
446     my (@output) = @_;
447
448     # Remove lines that look like exit codes.
449     # Exit codes are supposed to be printed in the form "process: exit(code)"
450     # but people get unfortunately creative with it.
451     for (my ($i) = 0; $i <= $#output; $i++) {
452         local ($_) = $output[$i];
453         
454         my ($process, $code);
455         if ((($process, $code) = /^([-a-z0-9 ]+):.*[ \(](-?\d+)\b\)?$/)
456             || (($process, $code) = /^([-a-z0-9 ]+) exit\((-?\d+)\)$/)
457             || (($process, $code)
458                 = /^([-a-z0-9 ]+) \(.*\): exit\((-?\d+)\)$/)
459             || (($process, $code) = /^([-a-z0-9 ]+):\( (-?\d+) \) $/)
460             || (($code, $process) = /^shell: exit\((-?\d+)\) \| ([-a-z0-9]+)/)
461             ) {
462             splice (@output, $i, 1);
463             $i--;
464         }
465     }
466
467     return @output;
468 }
469
470 sub compare_output {
471     my ($exp, @actual) = @_;
472     @actual = fix_exit_codes (get_core_output (map ("$_\n", @actual)));
473     die "Program produced no output.\n" if !@actual;
474
475     my ($details) = "";
476     $details .= "$test actual output:\n";
477     $details .= join ('', map ("  $_", @actual));
478
479     my (@exp) = map ("$_\n", snarf ($exp));
480
481     my ($fuzzy_match) = 0;
482     while (@exp != 0) {
483         my (@expected);
484         while (@exp != 0) {
485             my ($s) = shift (@exp);
486             last if $s eq "--OR--\n";
487             push (@expected, $s);
488         }
489
490         $details .= "\n$test acceptable output:\n";
491         $details .= join ('', map ("  $_", @expected));
492
493         # Check whether they're the same.
494         if ($#actual == $#expected) {
495             my ($eq) = 1;
496             for (my ($i) = 0; $i <= $#expected; $i++) {
497                 $eq = 0 if $actual[$i] ne $expected[$i];
498             }
499             return if $eq;
500         }
501
502         # They differ.  Output a diff.
503         my (@diff) = "";
504         my ($d) = Algorithm::Diff->new (\@expected, \@actual);
505         my ($not_fuzzy_match) = 0;
506         while ($d->Next ()) {
507             my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
508             if ($d->Same ()) {
509                 push (@diff, map ("  $_", $d->Items (1)));
510             } else {
511                 push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
512                 push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
513                 if ($d->Items (1)
514                     || grep (/\($test\)|exit\(-?\d+\)|dying due to|Page fault/,
515                              $d->Items (2))) {
516                     $not_fuzzy_match = 1;
517                 }
518             }
519         }
520         $fuzzy_match = 1 if !$not_fuzzy_match;
521
522         $details .= "Differences in `diff -u' format:\n";
523         $details .= join ('', @diff);
524         $details .= "(This is considered a `fuzzy match'.)\n"
525             if !$not_fuzzy_match;
526     }
527
528     if ($fuzzy_match) {
529         $details =
530             "This test passed, but with extra, unexpected output.\n"
531             . "Please inspect your code to make sure that it does not\n"
532             . "produce output other than as specified in the project\n"
533             . "description.\n\n"
534             . "$details";
535     } else {
536         $details =
537             "This test failed because its output did not match any\n"
538             . "of the acceptable form(s).\n\n"
539             . "$details";
540     }
541
542     $details{$test} = $details;
543     die "Output differs from expected.  Details at end of file.\n"
544         unless $fuzzy_match;
545 }
546 \f
547 sub write_grades {
548     my (@summary) = snarf ("$GRADES_DIR/tests.txt");
549
550     my ($ploss) = 0;
551     my ($tloss) = 0;
552     my ($total) = 0;
553     my ($warnings) = 0;
554     for (my ($i) = 0; $i <= $#summary; $i++) {
555         local ($_) = $summary[$i];
556         if (my ($loss, $test) = /^  -(\d+) ([-a-zA-Z0-9]+):/) {
557             my ($result) = $result{$test} || "Not tested.";
558
559             if ($result eq 'ok') {
560                 if (!defined $details{$test}) {
561                     # Test successful and no warnings.
562                     splice (@summary, $i, 1);
563                     $i--;
564                 } else {
565                     # Test successful with warnings.
566                     s/-(\d+) //;
567                     $summary[$i] = $_;
568                     splice (@summary, $i + 1, 0,
569                             "     Test passed with warnings.  "
570                             . "Details at end of file.");
571                     $warnings++;
572                 } 
573             } else {
574                 $ploss += $loss;
575                 $tloss += $loss;
576                 splice (@summary, $i + 1, 0,
577                         map ("     $_", split ("\n", $result)));
578             }
579         } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
580             $total += $ptotal;
581             $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
582             splice (@summary, $i, 0, "  All tests passed.")
583                 if $ploss == 0 && !$warnings;
584             $ploss = 0;
585             $warnings = 0;
586             $i++;
587         }
588     }
589     my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
590     $summary[0] =~ s/\[\[total\]\]/$ts/;
591
592     open (SUMMARY, ">tests.out");
593     print SUMMARY map ("$_\n", @summary);
594     close (SUMMARY);
595 }
596
597 sub write_details {
598     open (DETAILS, ">details.out");
599     my ($n) = 0;
600     for my $test (@TESTS) {
601         next if $result{$test} eq 'ok' && !defined $details{$test};
602         
603         my ($details) = $details{$test};
604         next if !defined ($details) && ! -e "output/$test/run.out";
605
606         my ($banner);
607         if ($result{$test} ne 'ok') {
608             $banner = "$test failure details"; 
609         } else {
610             $banner = "$test warnings";
611         }
612
613         print DETAILS "\n" if $n++;
614         print DETAILS "--- $banner ", '-' x (50 - length ($banner));
615         print DETAILS "\n\n";
616
617         if (!defined $details) {
618             my (@output) = snarf ("output/$test/run.out");
619
620             # Print only the first in a series of recursing panics.
621             my ($panic) = 0;
622             for my $i (0...$#output) {
623                 local ($_) = $output[$i];
624                 if (/PANIC/ && $panic++ > 0) {
625                     @output = @output[0...$i];
626                     push (@output,
627                           "[...details of recursive panic omitted...]");
628                     last;
629                 }
630             }
631             $details = "Output:\n\n" . join ('', map ("$_\n", @output));
632         }
633         print DETAILS $details;
634
635         print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
636             if defined $extra{$test};
637     }
638     close (DETAILS);
639
640 }
641 \f
642 sub xsystem {
643     my ($command, %options) = @_;
644     print "$command\n" if $verbose || $options{VERBOSE};
645
646     my ($log) = $options{LOG};
647
648     my ($pid, $status);
649     eval {
650         local $SIG{ALRM} = sub { die "alarm\n" };
651         alarm $options{TIMEOUT} if defined $options{TIMEOUT};
652         $pid = fork;
653         die "fork: $!\n" if !defined $pid;
654         if (!$pid) {
655             if (defined $log) {
656                 open (STDOUT, ">output/$log.out");
657                 open (STDERR, ">output/$log.err");
658             }
659             exec ($command);
660             exit (-1);
661         }
662         waitpid ($pid, 0);
663         $status = $?;
664         alarm 0;
665     };
666
667     my ($ok);
668     if ($@) {
669         die unless $@ eq "alarm\n";   # propagate unexpected errors
670         print "Timed out: ";
671         for (my ($i) = 0; $i < 10; $i++) {
672             kill ('SIGTERM', $pid);
673             sleep (1);
674             my ($retval) = waitpid ($pid, WNOHANG);
675             last if $retval == $pid || $retval == -1;
676             print "Waiting for $pid to die" if $i == 0;
677             print ".";
678         }
679         $ok = 1;
680     } else {
681         if (WIFSIGNALED ($status)) {
682             my ($signal) = WTERMSIG ($status);
683             die "Interrupted\n" if $signal == SIGINT;
684             print "Child terminated with signal $signal\n";
685         }
686
687         my ($exp_status) = !defined ($options{EXPECT}) ? 0 : $options{EXPECT};
688         $ok = WIFEXITED ($status) && WEXITSTATUS ($status) == $exp_status;
689     }
690
691
692     if (!$ok && defined $options{DIE}) {
693         my ($msg) = $options{DIE};
694         if (defined ($log)) {
695             chomp ($msg);
696             $msg .= "; see output/$log.err and output/$log.out for details\n";
697         }
698         die $msg;
699     } elsif (defined ($log) && $ok) {
700         unlink ("output/$log.err");
701     }
702
703     return $ok;
704 }
705
706 sub snarf {
707     my ($file) = @_;
708     open (OUTPUT, $file) or die "$file: open: $!\n";
709     my (@lines) = <OUTPUT>;
710     chomp (@lines);
711     close (OUTPUT);
712     return wantarray ? @lines : join ('', map ("$_\n", @lines));
713 }
714
715 sub files_equal {
716     my ($a, $b) = @_;
717     my ($equal);
718     open (A, "<$a") or die "$a: open: $!\n";
719     open (B, "<$b") or die "$b: open: $!\n";
720     if (-s A != -s B) {
721         $equal = 0;
722     } else {
723         my ($sa, $sb);
724         for (;;) {
725             sysread (A, $sa, 1024);
726             sysread (B, $sb, 1024);
727             $equal = 0, last if $sa ne $sb;
728             $equal = 1, last if $sa eq '';
729         }
730     }
731     close (A);
732     close (B);
733     return $equal;
734 }
735
736 sub file_contains {
737     my ($file, $expected) = @_;
738     open (FILE, "<$file") or die "$file: open: $!\n";
739     my ($actual);
740     sysread (FILE, $actual, -s FILE);
741     my ($equal) = $actual eq $expected;
742     close (FILE);
743     return $equal;
744 }
745
746 sub number_lines {
747     my ($ln, $lines) = @_;
748     my ($out);
749     for my $line (@$lines) {
750         chomp $line;
751         $out .= sprintf "%4d  %s\n", $ln++, $line;
752     }
753     return $out;
754 }