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