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