diff options
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/compare_stdio.py | 70 | ||||
-rw-r--r-- | yaksh/evaluator_tests/test_bash_evaluation.py | 2 | ||||
-rw-r--r-- | yaksh/evaluator_tests/test_c_cpp_evaluation.py | 8 | ||||
-rw-r--r-- | yaksh/evaluator_tests/test_java_evaluation.py | 4 | ||||
-rw-r--r-- | yaksh/evaluator_tests/test_python_evaluation.py | 2 | ||||
-rw-r--r-- | yaksh/evaluator_tests/test_python_stdio_evaluator.py | 22 | ||||
-rw-r--r-- | yaksh/python_stdio_evaluator.py | 12 | ||||
-rw-r--r-- | yaksh/stdio_evaluator.py | 11 | ||||
-rw-r--r-- | yaksh/templates/exam.html | 4 | ||||
-rw-r--r-- | yaksh/templates/yaksh/grade_user.html | 4 | ||||
-rw-r--r-- | yaksh/templates/yaksh/user_data.html | 4 | ||||
-rw-r--r-- | yaksh/templates/yaksh/view_answerpaper.html | 4 |
12 files changed, 72 insertions, 75 deletions
diff --git a/yaksh/compare_stdio.py b/yaksh/compare_stdio.py index c9c631f..c4076de 100644 --- a/yaksh/compare_stdio.py +++ b/yaksh/compare_stdio.py @@ -3,43 +3,41 @@ try: except ImportError: from itertools import izip_longest as zip_longest -class CompareOutputs(object): - def _incorrect_user_lines(self, exp_lines, user_lines): - err_line_no = [] - for i, (expected_line, user_line) in \ - enumerate(zip_longest(exp_lines, user_lines)): - if not user_line or not expected_line: - err_line_no.append(i) - else: - if user_line.strip() != expected_line.strip(): - err_line_no.append(i) - return err_line_no +def _get_incorrect_user_lines(exp_lines, user_lines): + err_line_numbers = [] + for line_no, (expected_line, user_line) in \ + enumerate(zip_longest(exp_lines, user_lines)): + if not user_line or not expected_line or \ + user_line.strip() != expected_line.strip(): + err_line_numbers.append(line_no) + return err_line_numbers - def compare_outputs(self, expected_output, user_output,given_input=None): - given_lines = user_output.splitlines() - exp_lines = expected_output.splitlines() - msg = {"given_input":given_input, - "expected_output": exp_lines, - "user_output":given_lines - } - ng = len(given_lines) - ne = len(exp_lines) - if ng != ne: - err_line_no = self._incorrect_user_lines(exp_lines, given_lines) - msg["error_no"] = err_line_no - msg["error"] = "Incorrect Answer: \ - We had expected {0} number of lines. \ - We got {1} number of lines. ".format(ne, ng) +def compare_outputs(expected_output, user_output, given_input=None): + given_lines = user_output.splitlines() + exp_lines = expected_output.splitlines() + msg = {"given_input":given_input, + "expected_output": exp_lines, + "user_output":given_lines + } + ng = len(given_lines) + ne = len(exp_lines) + err_line_numbers = _get_incorrect_user_lines(exp_lines, given_lines) + msg["error_line_numbers"] = err_line_numbers + if ng != ne: + msg["error_msg"] = ("Incorrect Answer: " + + "We had expected {} number of lines. ".format(ne) + + "We got {} number of lines.".format(ng) + ) + return False, msg + else: + if err_line_numbers: + msg["error_msg"] = ("Incorrect Answer: " + + "Line number(s) {0} did not match." + .format(", ".join(map( + str,[x+1 for x in err_line_numbers] + )))) return False, msg else: - err_line_no = self._incorrect_user_lines(exp_lines, given_lines) - if err_line_no: - msg["error_no"] = err_line_no - msg["error"] = "Incorrect Answer: "\ - "Line number(s) {0} did not match."\ - .format(", ".join(map(str,[x+1 for x in err_line_no]))) - return False, msg - else: - msg["error"] = "Correct answer" - return True, msg + msg["error_msg"] = "Correct Answer" + return True, msg diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py index d19ef1c..6e7410e 100644 --- a/yaksh/evaluator_tests/test_bash_evaluation.py +++ b/yaksh/evaluator_tests/test_bash_evaluation.py @@ -242,7 +242,7 @@ class BashStdIOEvaluationTestCases(EvaluatorBaseTest): result = grader.evaluate(kwargs) # Then - result_error = result.get('error')[0].get('error') + result_error = result.get('error')[0].get('error_msg') self.assert_correct_output("Incorrect", result_error) self.assertFalse(result.get('success')) diff --git a/yaksh/evaluator_tests/test_c_cpp_evaluation.py b/yaksh/evaluator_tests/test_c_cpp_evaluation.py index 46d5593..5ff4e4c 100644 --- a/yaksh/evaluator_tests/test_c_cpp_evaluation.py +++ b/yaksh/evaluator_tests/test_c_cpp_evaluation.py @@ -350,8 +350,8 @@ class CppStdIOEvaluationTestCases(EvaluatorBaseTest): result = grader.evaluate(kwargs) # Then - lines_of_error = len(result.get('error')[0].get('error_no')) - result_error = result.get('error')[0].get('error') + lines_of_error = len(result.get('error')[0].get('error_line_numbers')) + result_error = result.get('error')[0].get('error_msg') self.assertFalse(result.get('success')) self.assert_correct_output("Incorrect", result_error) self.assertTrue(lines_of_error > 0) @@ -559,8 +559,8 @@ class CppStdIOEvaluationTestCases(EvaluatorBaseTest): result = grader.evaluate(kwargs) # Then - lines_of_error = len(result.get('error')[0].get('error_no')) - result_error = result.get('error')[0].get('error') + lines_of_error = len(result.get('error')[0].get('error_line_numbers')) + result_error = result.get('error')[0].get('error_msg') self.assertFalse(result.get('success')) self.assert_correct_output("Incorrect", result_error) self.assertTrue(lines_of_error > 0) diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py index 7046aa1..c733586 100644 --- a/yaksh/evaluator_tests/test_java_evaluation.py +++ b/yaksh/evaluator_tests/test_java_evaluation.py @@ -349,8 +349,8 @@ class JavaStdIOEvaluationTestCases(EvaluatorBaseTest): result = grader.evaluate(kwargs) # Then - lines_of_error = len(result.get('error')[0].get('error_no')) - result_error = result.get('error')[0].get('error') + lines_of_error = len(result.get('error')[0].get('error_line_numbers')) + result_error = result.get('error')[0].get('error_msg') self.assertFalse(result.get('success')) self.assert_correct_output("Incorrect", result_error) self.assertTrue(lines_of_error > 0) diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index 8b18c9a..a2faf77 100644 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -614,7 +614,7 @@ class PythonStdIOEvaluationTestCases(EvaluatorBaseTest): self.assertFalse(result.get('success')) self.assert_correct_output( "Incorrect Answer: Line number(s) 1 did not match.", - result.get('error')[0].get('error') + result.get('error')[0].get('error_msg') ) def test_file_based_answer(self): diff --git a/yaksh/evaluator_tests/test_python_stdio_evaluator.py b/yaksh/evaluator_tests/test_python_stdio_evaluator.py index 9eab763..8877544 100644 --- a/yaksh/evaluator_tests/test_python_stdio_evaluator.py +++ b/yaksh/evaluator_tests/test_python_stdio_evaluator.py @@ -1,39 +1,39 @@ -from yaksh.compare_stdio import CompareOutputs +from yaksh.compare_stdio import compare_outputs def test_compare_outputs(): exp = "5\n5\n" given = "5\n5\n" - success, msg = CompareOutputs().compare_outputs(given, exp) + success, msg = compare_outputs(given, exp) assert success exp = "5\n5\n" given = "5\n5" - success, msg = CompareOutputs().compare_outputs(given, exp) + success, msg = compare_outputs(given, exp) assert success exp = "5\r5" given = "5\n5" - success, msg = CompareOutputs().compare_outputs(given, exp) + success, msg = compare_outputs(given, exp) assert success exp = " 5 \r 5 " given = " 5 \n 5 " - success, msg = CompareOutputs().compare_outputs(given, exp) + success, msg = compare_outputs(given, exp) assert success exp = "5\n5\n" given = "5 5" - success, msg = CompareOutputs().compare_outputs(given, exp) - error_msg = msg.get('error') + success, msg = compare_outputs(given, exp) + error_msg = msg.get('error_msg') assert not success - m = "Incorrect Answer: We had expected 1 number of lines."\ - + " We got 2 number of lines." + m = ("Incorrect Answer: We had expected 1 number of lines. " + + "We got 2 number of lines.") assert m == error_msg exp = "5\n5\n" given = "5\n6" - success, msg = CompareOutputs().compare_outputs(given, exp) - error_msg = msg.get('error') + success, msg = compare_outputs(given, exp) + error_msg = msg.get('error_msg') m = "Incorrect Answer: Line number(s) 2 did not match." assert not success assert m == error_msg diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index d6201f9..2b443a7 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -9,7 +9,7 @@ except ImportError: # Local imports from .file_utils import copy_files, delete_files from .base_evaluator import BaseEvaluator -from .compare_stdio import CompareOutputs +from .compare_stdio import compare_outputs @contextmanager @@ -46,6 +46,7 @@ class PythonStdIOEvaluator(BaseEvaluator): self.files = copy_files(self.file_paths) submitted = compile(self.user_answer, '<string>', mode='exec') if self.expected_input: + self.expected_input = self.expected_input.replace('\r', '') input_buffer = StringIO() input_buffer.write(self.expected_input) input_buffer.seek(0) @@ -58,9 +59,8 @@ class PythonStdIOEvaluator(BaseEvaluator): def check_code(self): mark_fraction = self.weight - compare = CompareOutputs() - success, err = compare.compare_outputs(self.expected_output, - self.output_value, - self.expected_input - ) + success, err = compare_outputs(self.expected_output, + self.output_value, + self.expected_input + ) return success, err, mark_fraction diff --git a/yaksh/stdio_evaluator.py b/yaksh/stdio_evaluator.py index a5785ea..932ae7e 100644 --- a/yaksh/stdio_evaluator.py +++ b/yaksh/stdio_evaluator.py @@ -5,7 +5,7 @@ import signal # Local imports from .base_evaluator import BaseEvaluator from .grader import TimeoutException -from .compare_stdio import CompareOutputs +from .compare_stdio import compare_outputs class StdIOEvaluator(BaseEvaluator): @@ -21,9 +21,8 @@ class StdIOEvaluator(BaseEvaluator): os.killpg(os.getpgid(proc.pid), signal.SIGTERM) raise expected_output = expected_output.replace("\r", "") - compare = CompareOutputs() - success, err = compare.compare_outputs(expected_output, - user_output, - expected_input - ) + success, err = compare_outputs(expected_output, + user_output, + expected_input + ) return success, err diff --git a/yaksh/templates/exam.html b/yaksh/templates/exam.html index f349b2f..45b85f0 100644 --- a/yaksh/templates/exam.html +++ b/yaksh/templates/exam.html @@ -109,7 +109,7 @@ <td> {{forloop.counter}} </td> <td>{{expected|default:""}} </td> <td>{{user|default:""}}</td> - {% if forloop.counter0 in error.error_no or not expected or not user %} + {% if forloop.counter0 in error.error_line_numbers or not expected or not user %} <td><span class ="glyphicon glyphicon-remove text-warning"/></td> {% else %} <td><span class ="glyphicon glyphicon-ok text-success"/></td> @@ -121,7 +121,7 @@ <col width="10"> <tr class = "danger"> <td><b>Error:</b></td> - <td>{{error.error}}</td> + <td>{{error.error_msg}}</td> </tr> </table> diff --git a/yaksh/templates/yaksh/grade_user.html b/yaksh/templates/yaksh/grade_user.html index a711fc2..9cdfb1a 100644 --- a/yaksh/templates/yaksh/grade_user.html +++ b/yaksh/templates/yaksh/grade_user.html @@ -230,7 +230,7 @@ Status : <b style="color: green;"> Passed </b><br/> <td> {{forloop.counter}} </td> <td>{{expected|default:""}} </td> <td>{{user|default:""}}</td> - {% if forloop.counter0 in error.error_no or not expected or not user %} + {% if forloop.counter0 in error.error_line_numbers or not expected or not user %} <td><span class ="glyphicon glyphicon-remove text-warning"/></td> {% else %} <td><span class ="glyphicon glyphicon-ok text-success"/></td> @@ -242,7 +242,7 @@ Status : <b style="color: green;"> Passed </b><br/> <col width="10"> <tr class = "danger"> <td><b>Error:</b></td> - <td>{{error.error}}</td> + <td>{{error.error_msg}}</td> </tr> </table> </div> diff --git a/yaksh/templates/yaksh/user_data.html b/yaksh/templates/yaksh/user_data.html index 1af27e6..a8adc22 100644 --- a/yaksh/templates/yaksh/user_data.html +++ b/yaksh/templates/yaksh/user_data.html @@ -149,7 +149,7 @@ User IP address: {{ paper.user_ip }} <td> {{forloop.counter}} </td> <td>{{expected|default:""}} </td> <td>{{user|default:""}}</td> - {% if forloop.counter0 in error.error_no or not expected or not user %} + {% if forloop.counter0 in error.error_line_numbers or not expected or not user %} <td><span class ="glyphicon glyphicon-remove text-warning"/></td> {% else %} <td><span class ="glyphicon glyphicon-ok text-success"/></td> @@ -161,7 +161,7 @@ User IP address: {{ paper.user_ip }} <col width="10"> <tr class = "danger"> <td><b>Error:</b></td> - <td>{{error.error}}</td> + <td>{{error.error_msg}}</td> </tr> </table> </div> diff --git a/yaksh/templates/yaksh/view_answerpaper.html b/yaksh/templates/yaksh/view_answerpaper.html index 71536eb..9edff5a 100644 --- a/yaksh/templates/yaksh/view_answerpaper.html +++ b/yaksh/templates/yaksh/view_answerpaper.html @@ -134,7 +134,7 @@ <td> {{forloop.counter}} </td> <td>{{expected|default:""}} </td> <td>{{user|default:""}}</td> - {% if forloop.counter0 in error.error_no or not expected or not user %} + {% if forloop.counter0 in error.error_line_numbers or not expected or not user %} <td><span class ="glyphicon glyphicon-remove text-warning"/></td> {% else %} <td><span class ="glyphicon glyphicon-ok text-success"/></td> @@ -146,7 +146,7 @@ <col width="10"> <tr class = "danger"> <td><b>Error:</b></td> - <td>{{error.error}}</td> + <td>{{error.error_msg}}</td> </tr> </table> </div> |