Skip to content

Commit e3059c4

Browse files
author
Philip Guo
committed
incremental
1 parent a8f1b15 commit e3059c4

1 file changed

Lines changed: 27 additions & 93 deletions

File tree

‎edu-python-questions.js‎

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,19 @@ var tests = null;
2929
var expects = null;
3030
var curTestIndex = -1;
3131

32-
// the traces returned by executing the respective 'tests' and 'expects'
33-
// Python code. See resetResultTraces for invariants.
34-
var testsTraces = null;
35-
var expectsTraces = null;
32+
// the results returned by executing the respective 'tests' and 'expects'
33+
// Python code. See resetTestResults for invariants.
34+
var testResults = null;
3635

3736
// Pre: 'tests' and 'expects' are non-null
38-
function resetResultTraces() {
39-
testsTraces = [];
40-
expectsTraces = [];
37+
function resetTestResults() {
38+
testResults = [];
4139
$.each(tests, function(i) {
42-
testsTraces.push(null);
43-
expectsTraces.push(null);
40+
testResults.push(null);
4441
});
4542

46-
assert(testsTraces.length > 0);
47-
assert(testsTraces.length == expectsTraces.length);
48-
assert(testsTraces.length == tests.length);
49-
assert(expectsTraces.length == expects.length);
43+
assert(testResults.length > 0);
44+
assert(testResults.length == tests.length);
5045
}
5146

5247

@@ -118,34 +113,21 @@ function enterGradingMode() {
118113

119114
// returns a curried function!
120115
function genTestResultHandler(idx) {
121-
function ret(traceData) {
122-
assert(testsTraces[idx] === null);
123-
testsTraces[idx] = traceData;
116+
function ret(res) {
117+
assert(testResults[idx] === null);
118+
testResults[idx] = res;
124119

125-
// if ALL results have been delivered, then call
126-
// readyToGradeSubmission() ...
127-
for (var i = 0; i < testsTraces.length; i++) {
128-
if (testsTraces[i] === null || expectsTraces[i] === null) {
129-
return;
130-
}
131-
}
132-
readyToGradeSubmission();
133-
}
134-
return ret;
135-
}
136-
137-
function genExpectResultHandler(idx) {
138-
function ret(traceData) {
139-
assert(expectsTraces[idx] === null);
140-
expectsTraces[idx] = traceData;
120+
console.log(idx, res);
141121

142122
// if ALL results have been delivered, then call
143-
// readyToGradeSubmission() ...
144-
for (var i = 0; i < testsTraces.length; i++) {
145-
if (testsTraces[i] === null || expectsTraces[i] === null) {
123+
// readyToGradeSubmission() ... (remember that each result comes in
124+
// asynchronously and probably out-of-order)
125+
for (var i = 0; i < testResults.length; i++) {
126+
if (testResults[i] === null) {
146127
return;
147128
}
148129
}
130+
149131
readyToGradeSubmission();
150132
}
151133
return ret;
@@ -175,7 +157,7 @@ function finishQuestionsInit(questionsDat) {
175157
expects = questionsDat.expects;
176158
curTestIndex = 0;
177159

178-
resetResultTraces();
160+
resetTestResults();
179161

180162

181163
$("#testCodeInput").val(tests[curTestIndex]);
@@ -216,25 +198,17 @@ function finishQuestionsInit(questionsDat) {
216198
$('#submitBtn').html("Please wait ... submitting ...");
217199
$('#submitBtn').attr('disabled', true);
218200

219-
// right now I make (2 * tests.length) HTTP POST calls, which might
220-
// be inefficient, so optimize later if necessary
221-
//
222-
// remember to code this up VERY CAREFULLY because the responses
223-
// will come in asynchronously and probably OUT-OF-ORDER
201+
resetTestResults(); // prepare for a new fresh set of test results
202+
203+
// remember that these results come in asynchronously and probably
204+
// out-of-order, so code very carefully here!!!
224205
for (var i = 0; i < tests.length; i++) {
225206
var submittedCode = concatSolnTestCode($("#actualCodeInput").val(), tests[i]);
226-
$.post("cgi-bin/web_exec.py",
227-
{user_script : submittedCode},
207+
$.post("cgi-bin/web_run_test.py",
208+
{user_script : submittedCode, expect_script : expects[i]},
228209
genTestResultHandler(i),
229210
"json");
230-
231-
var expectCode = expects[i];
232-
$.post("cgi-bin/web_exec.py",
233-
{user_script : expectCode},
234-
genExpectResultHandler(i),
235-
"json");
236211
}
237-
238212
});
239213

240214
}
@@ -247,54 +221,14 @@ function readyToGradeSubmission() {
247221
$("#submittedCodeRO").val($("#actualCodeInput").val());
248222

249223
for (var i = 0; i < tests.length; i++) {
250-
$("#gradeMatrix tbody").append("<tr></tr>");
251-
252-
var testResults = testsTraces[i];
253-
var expectResults = expectsTraces[i];
254-
assert(testResults && expectResults);
255-
256-
257-
// Procedure for grading testResults vs. expectResults:
258-
// - The final line in expectResults should be a 'return' from
259-
// '<module>' that contains only ONE global variable. THAT'S
260-
// the variable that we're gonna compare against testResults.
261-
262-
var testLastEntry = testResults[testResults.length - 1];
263-
var expectLastEntry = expectResults[expectResults.length - 1];
264-
265-
assert(expectLastEntry.event == 'return');
266-
assert(expectLastEntry.func_name == '<module>');
224+
var res = testResults[i];
267225

268-
// find the SOLE global variable in expectLastEntry and use that as
269-
// the point of comparison against testLastEntry
270-
var expectGlobals = [];
271-
for (g in expectLastEntry.globals) {
272-
expectGlobals.push(g);
273-
}
274-
assert(expectGlobals.length == 1);
275-
var varToCompare = expectGlobals[0];
276-
277-
var resultIsCorrect = false;
278-
279-
if (testLastEntry.event == 'return') {
280-
var testVal = testLastEntry.globals[varToCompare];
281-
var expectVal = expectLastEntry.globals[varToCompare];
282-
console.log(testVal);
283-
console.log(expectVal);
284-
if (equalsModuloID(testVal, expectVal)) {
285-
resultIsCorrect = true;
286-
}
287-
}
288-
else {
289-
// something else happened, and there's no match!
290-
assert(testLastEntry.exception_msg);
291-
console.log(testLastEntry.exception_msg);
292-
}
226+
$("#gradeMatrix tbody").append("<tr></tr>");
293227

294228
$("#gradeMatrix tr:last").append("<td><pre>in</pre></td>");
295229
$("#gradeMatrix tr:last").append("<td><pre>out</pre></td>");
296230

297-
if (resultIsCorrect) {
231+
if (res.passed_test) {
298232
$("#gradeMatrix tr:last").append('<td><img style="vertical-align: middle;" src="yellow-happy-face.png"/></td>');
299233
}
300234
else {

0 commit comments

Comments
 (0)