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