-\f
-sub verify_common {
- my (@output) = @_;
-
- my (@assertion) = grep (/PANIC/, @output);
- if (@assertion != 0) {
- my ($details) = "Kernel panic:\n $assertion[0]\n";
-
- my (@stack_line) = grep (/Call stack:/, @output);
- if (@stack_line != 0) {
- $details .= " $stack_line[0]\n\n";
- $details .= "Translation of backtrace:\n";
- my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
-
- my ($A2L);
- if (`uname -m`
- =~ /i.86|pentium.*|[pk][56]|nexgen|viac3|6x86|athlon.*/) {
- $A2L = "addr2line";
- } else {
- $A2L = "i386-elf-addr2line";
- }
- open (A2L, "$A2L -fe output/$test/kernel.o @addrs|");
- for (;;) {
- my ($function, $line);
- last unless defined ($function = <A2L>);
- $line = <A2L>;
- chomp $function;
- chomp $line;
- $details .= " $function ($line)\n";
- }
- }
- $extra{$test} = $details;
- die "Kernel panic. Details at end of file.\n"
- }
-
- die "No output at all\n" if @output == 0;
- die "Didn't start up properly: no \"Pintos booting\" startup message\n"
- if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
- die "Didn't start up properly: no \"Boot complete\" startup message\n"
- if !grep (/Boot complete/, @output);
- die "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
- if !grep (/Timer: \d+ ticks/, @output);
- die "Didn't shut down properly: no \"Powering off\" shutdown message\n"
- if !grep (/Powering off/, @output);
-}
-
-sub compare_output {
- my ($exp_file, @actual) = @_;
- my (@expected) = snarf ($exp_file);
-
- @actual = map ("$_\n", @actual);
- @expected = map ("$_\n", @expected);
-
- # Trim header and trailer from @actual.
- while (scalar (@actual) && $actual[0] ne $expected[0]) {
- shift (@actual);
- }
- die "First line of expected output was not present.\n" if !@actual;
- while (scalar (@actual) && $actual[$#actual] ne $expected[$#expected]) {
- pop (@actual);
- }
- die "Final line of expected output was not present.\n" if !@actual;
-
- # Check whether they're the same.
- if ($#actual == $#expected) {
- my ($eq) = 1;
- for (my ($i) = 0; $i <= $#expected; $i++) {
- $eq = 0 if $actual[$i] ne $expected[$i];
- }
- return if $eq;
- }
-
- # They differ. Output a diff.
- my (@diff) = "";
- my ($d) = Algorithm::Diff->new (\@expected, \@actual);
- while ($d->Next ()) {
- my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
- if ($d->Same ()) {
- push (@diff, map (" $_", $d->Items (1)));
- } else {
- push (@diff, map ("- $_", $d->Items (1))) if $d->Items (1);
- push (@diff, map ("+ $_", $d->Items (2))) if $d->Items (2);
- }
- }
-
- my ($details) = "";
- $details .= "$test actual output:\n";
- $details .= join ('', map (" $_", @actual));
- $details .= "\n$test expected output:\n";
- $details .= join ('', map (" $_", @expected));
- $details .= "\nOutput differences in `diff -u' format:\n";
- $details .= join ('', @diff) . "\n";
- $details{$test} = $details;
- die "Output differs from expected. Details at end of file.\n";
-}
-\f
-sub write_grades {
- my (@summary) = snarf ("$GRADES_DIR/tests.txt");
-
- my ($ploss) = 0;
- my ($tloss) = 0;
- my ($total) = 0;
- for (my ($i) = 0; $i <= $#summary; $i++) {
- local ($_) = $summary[$i];
- if (my ($loss, $test) = /^ -(\d+) ([-a-zA-Z0-9]+):/) {
- my ($result) = $result{$test} || "Not tested.";
-
- if ($result eq 'ok') {
- splice (@summary, $i, 1);
- $i--;
- } else {
- $ploss += $loss;
- $tloss += $loss;
- splice (@summary, $i + 1, 0,
- map (" $_", split ("\n", $result)));
- }
- } elsif (my ($ptotal) = /^Score: \/(\d+)$/) {
- $total += $ptotal;
- $summary[$i] = "Score: " . ($ptotal - $ploss) . "/$ptotal";
- splice (@summary, $i, 0, " All tests passed.") if $ploss == 0;
- $ploss = 0;
- $i++;
- }
- }
- my ($ts) = "(" . ($total - $tloss) . "/" . $total . ")";
- $summary[0] =~ s/\[\[total\]\]/$ts/;
-
- open (SUMMARY, ">tests.out");
- print SUMMARY map ("$_\n", @summary);
- close (SUMMARY);
-}
-
-sub write_details {
- open (DETAILS, ">details.out");
- my ($n) = 0;
- for my $test (@TESTS) {
- next if $result{$test} eq 'ok';
-
- my ($details) = $details{$test};
- next if !defined ($details) && ! -e "output/$test/run.out";
-
- print DETAILS "\n" if $n++;
- print DETAILS "--- $test details ", '-' x (50 - length ($test));
- print DETAILS "\n\n";
-
- if (!defined $details) {
- $details = "Output:\n\n" . snarf ("output/$test/run.out");
- }
- print DETAILS $details;
-
- print DETAILS "\n", "-" x 10, "\n\n$extra{$test}"
- if defined $extra{$test};
- }
- close (DETAILS);
-
-}
-\f
-sub xsystem {
- my ($command, %options) = @_;
- print "$command\n" if $VERBOSE || $options{VERBOSE};
-
- my ($log) = $options{LOG};
- if (defined ($log)) {
- $command = "($command) >output/$log.out 2>output/$log.err";
- }
-
- my ($pid, $status);
- eval {
- local $SIG{ALRM} = sub { die "alarm\n" };
- alarm $options{TIMEOUT} if defined $options{TIMEOUT};
- $pid = fork;
- die "fork: $!\n" if !defined $pid;
- exec ($command), die "$command: exec: $!\n" if !$pid;
- waitpid ($pid, 0);
- $status = $?;
- alarm 0;
- };
- if ($@) {
- die unless $@ eq "alarm\n"; # propagate unexpected errors
- print "Timed out.\n";
- kill SIGTERM, $pid;
- $status = 0;
- }
-
- if (WIFSIGNALED ($status)) {
- my ($signal) = WTERMSIG ($status);
- die "Interrupted\n" if $signal == SIGINT;
- print "Child terminated with signal $signal\n";
- }
-
- unlink ("output/$log.err") if defined ($log) && $status == 0;
-
- die $options{DIE} if $status != 0 && defined $options{DIE};
-
- return $status == 0;
-}
-
-sub snarf {
- my ($file) = @_;
- open (OUTPUT, $file) or die "$file: open: $!\n";
- my (@lines) = <OUTPUT>;
- chomp (@lines);
- close (OUTPUT);
- return wantarray ? @lines : join ('', map ("$_\n", @lines));
-}
-
-sub files_equal {
- my ($a, $b) = @_;
- my ($equal);
- open (A, "<$a") or die "$a: open: $!\n";
- open (B, "<$b") or die "$b: open: $!\n";
- if (-s A != -s B) {
- $equal = 0;
- } else {
- my ($sa, $sb);
- for (;;) {
- sysread (A, $sa, 1024);
- sysread (B, $sb, 1024);
- $equal = 0, last if $sa ne $sb;
- $equal = 1, last if $sa eq '';
- }
- }
- close (A);
- close (B);
- return $equal;
-}
-
-sub file_contains {
- my ($file, $expected) = @_;
- open (FILE, "<$file") or die "$file: open: $!\n";
- my ($actual);
- sysread (FILE, $actual, -s FILE);
- my ($equal) = $actual eq $expected;
- close (FILE);
- return $equal;
-}
-
-sub number_lines {
- my ($ln, $lines) = @_;
- my ($out);
- for my $line (@$lines) {
- chomp $line;
- $out .= sprintf "%4d %s\n", $ln++, $line;
- }
- return $out;
-}