aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/make-grade
blob: a3faa0ebd560ac1f5af6b1180a50f489125860ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /usr/bin/perl

use strict;
use warnings;

@ARGV == 3 || die;
my ($src_dir, $results_file, $grading_file) = @ARGV;

# Read pass/file verdicts from $results_file.
open (RESULTS, '<', $results_file) || die "$results_file: open: $!\n";
my (%verdicts, %verdict_counts);
while (<RESULTS>) {
    my ($verdict, $test) = /^(pass|FAIL) (.*)$/ or die;
    $verdicts{$test} = $verdict eq 'pass';
}
close RESULTS;

my (@failures);
my (@overall, @rubrics, @summary);
my ($pct_actual, $pct_possible) = (0, 0);

# Read grading file.
my (@items);
open (GRADING, '<', $grading_file) || die "$grading_file: open: $!\n";
while (<GRADING>) {
    s/#.*//;
    next if /^\s*$/;
    my ($max_pct, $rubric_suffix) = /^\s*(\d+(?:\.\d+)?)%\t(.*)/ or die;
    my ($dir) = $rubric_suffix =~ /^(.*)\//;
    my ($rubric_file) = "$src_dir/$rubric_suffix";
    open (RUBRIC, '<', $rubric_file) or die "$rubric_file: open: $!\n";

    # Rubric file must begin with title line.
    my $title = <RUBRIC>;
    chomp $title;
    $title =~ s/:$// or die;
    $title .= " ($rubric_suffix):";
    push (@rubrics, $title);

    my ($score, $possible) = (0, 0);
    my ($cnt, $passed) = (0, 0);
    my ($was_score) = 0;
    while (<RUBRIC>) {
	chomp;
	push (@rubrics, "\t$_"), next if /^-/;
	push (@rubrics, ""), next if /^\s*$/;
	my ($poss, $name) = /^(\d+)\t(.*)$/ or die;
	my ($test) = "$dir/$name";
	my ($points) = 0;
	if (!defined $verdicts{$test}) {
	    push (@overall, "warning: $test not tested, assuming failure");
	} elsif ($verdicts{$test}) {
	    $points = $poss;
	    $passed++;
	}
	push (@failures, $test) if !$points;
	$verdict_counts{$test}++;
	push (@rubrics, sprintf ("\t%4s%2d/%2d %s",
				 $points ? '' : '**', $points, $poss, $test));
	$score += $points;
	$possible += $poss;
	$cnt++;
    }
    close (RUBRIC);

    push (@rubrics, "");
    push (@rubrics, "\t- Section summary.");
    push (@rubrics, sprintf ("\t%4s%3d/%3d %s",
			     '', $passed, $cnt, 'tests passed'));
    push (@rubrics, sprintf ("\t%4s%3d/%3d %s",
			     '', $score, $possible, 'points subtotal'));
    push (@rubrics, '');

    my ($pct) = ($score / $possible) * $max_pct;
    push (@summary, sprintf ("%-45s %3d/%3d %5.1f%%/%5.1f%%",
			     $rubric_suffix,
			     $score, $possible,
			     $pct, $max_pct));
    $pct_actual += $pct;
    $pct_possible += $max_pct;
}
close GRADING;

my ($sum_line)
  = "--------------------------------------------- --- --- ------ ------";
unshift (@summary,
	 "SUMMARY BY TEST SET",
	 '',
	 sprintf ("%-45s %3s %3s %6s %6s",
		  "Test Set", "Pts", "Max", "% Ttl", "% Max"),
	 $sum_line);
push (@summary,
      $sum_line,
      sprintf ("%-45s %3s %3s %5.1f%%/%5.1f%%",
	       'Total', '', '', $pct_actual, $pct_possible));

unshift (@rubrics,
	 "SUMMARY OF INDIVIDUAL TESTS",
	 '');

foreach my $name (keys (%verdicts)) {
    my ($count) = $verdict_counts{$name};
    if (!defined ($count) || $count != 1) {
	if (!defined ($count) || !$count) {
	    push (@overall, "warning: test $name doesn't count for grading");
	} else {
	    push (@overall,
		  "warning: test $name counted $count times in grading");
	}
    }
}
push (@overall, sprintf ("TOTAL TESTING SCORE: %.1f%%", $pct_actual));
if (sprintf ("%.1f", $pct_actual) eq sprintf ("%.1f", $pct_possible)) {
    push (@overall, "ALL TESTED PASSED -- PERFECT SCORE");
}

my (@divider) = ('', '- ' x 38, '');

print map ("$_\n", @overall, @divider, @summary, @divider, @rubrics);

for my $test (@failures) {
    print map ("$_\n", @divider);
    print "DETAILS OF $test FAILURE:\n\n";

    if (open (RESULT, '<', "$test.result")) {
	my $first_line = <RESULT>;
	my ($cnt) = 0;
	while (<RESULT>) {
	    print;
	    $cnt++;
	}
	close (RESULT);
    }

    if (open (OUTPUT, '<', "$test.output")) {
	print "\nOUTPUT FROM $test:\n\n";
    
	my ($panics, $boots) = (0, 0);
	while (<OUTPUT>) {
	    if (/PANIC/ && ++$panics > 2) {
		print "[...details of additional panic(s) omitted...]\n";
		last;
	    }
	    print;
	    if (/Pintos booting/ && ++$boots > 1) {
		print "[...details of reboot(s) omitted...]\n";
		last;
	    }
	}
	close (OUTPUT);
    }
}