diff options
author | ankitjavalkar | 2016-05-09 13:00:04 +0530 |
---|---|---|
committer | ankitjavalkar | 2016-05-10 11:54:34 +0530 |
commit | c384c60c6d7fb5d30f3f929c518e0b41e084c4c4 (patch) | |
tree | d5b937e90bc7d3051b9c9128c4e1560b09db1c2c /yaksh/cpp_code_evaluator.py | |
parent | d953f6f9e62671eeb5d6ea6498475167301dfe91 (diff) | |
download | online_test-c384c60c6d7fb5d30f3f929c518e0b41e084c4c4.tar.gz online_test-c384c60c6d7fb5d30f3f929c518e0b41e084c4c4.tar.bz2 online_test-c384c60c6d7fb5d30f3f929c518e0b41e084c4c4.zip |
- Adhere to 80 columns
- add docstrings
- Fix further tests
Diffstat (limited to 'yaksh/cpp_code_evaluator.py')
-rw-r--r-- | yaksh/cpp_code_evaluator.py | 148 |
1 files changed, 80 insertions, 68 deletions
diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index becf371..b869442 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -43,91 +43,103 @@ class CppCodeEvaluator(CodeEvaluator): return None else: ref_code_path = test_case - clean_ref_code_path, clean_test_case_path = self._set_test_code_file_path(ref_code_path) + clean_ref_code_path, clean_test_case_path = \ + self._set_test_code_file_path(ref_code_path) if not isfile(clean_ref_code_path): - return False, "No file at %s or Incorrect path" % clean_ref_code_path + msg = "No file at %s or Incorrect path" % clean_ref_code_path + return False, msg if not isfile(self.submit_code_path): - return False, 'No file at %s or Incorrect path' % self.submit_code_path + msg = "No file at %s or Incorrect path" % self.submit_code_path + return False, msg self.write_to_submit_code_file(self.submit_code_path, user_answer) self.user_output_path, self.ref_output_path = self.set_file_paths() - self.compile_command, self.compile_main = self.get_commands(clean_ref_code_path, self.user_output_path, self.ref_output_path) - self.compiled_user_answer = self._run_command(self.compile_command, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - - self.compiled_test_code = self._run_command(self.compile_main, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - + self.compile_command, self.compile_main = self.get_commands( + clean_ref_code_path, + self.user_output_path, + self.ref_output_path + ) + self.compiled_user_answer = self._run_command( + self.compile_command, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + + self.compiled_test_code = self._run_command( + self.compile_main, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) return self.compiled_user_answer, self.compiled_test_code def check_code(self, user_answer, test_case): - """ Function validates student code using instructor code as - reference.The first argument ref_code_path, is the path to - instructor code, it is assumed to have executable permission. - The second argument submit_code_path, is the path to the student - code, it is assumed to have executable permission. - - Returns - -------- - - returns (True, "Correct answer") : If the student function returns - expected output when called by reference code. - - returns (False, error_msg): If the student function fails to return - expected output when called by reference code. - - Returns (False, error_msg): If mandatory arguments are not files or - if the required permissions are not given to the file(s). - - """ - success = False - proc, stdnt_out, stdnt_stderr = self.compiled_user_answer - stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) - - # Only if compilation is successful, the program is executed - # And tested with testcases - if stdnt_stderr == '': - proc, main_out, main_err = self.compiled_test_code - main_err = self._remove_null_substitute_char(main_err) - - if main_err == '': - ret = self._run_command([self.ref_output_path], stdin=None, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - proc, stdout, stderr = ret - if proc.returncode == 0: - success, err = True, "Correct answer" - else: - err = stdout + "\n" + stderr - os.remove(self.ref_output_path) + """ Function validates student code using instructor code as + reference.The first argument ref_code_path, is the path to + instructor code, it is assumed to have executable permission. + The second argument submit_code_path, is the path to the student + code, it is assumed to have executable permission. + + Returns + -------- + + returns (True, "Correct answer") : If the student function returns + expected output when called by reference code. + + returns (False, error_msg): If the student function fails to return + expected output when called by reference code. + + Returns (False, error_msg): If mandatory arguments are not files or + if the required permissions are not given to the file(s). + """ + + success = False + proc, stdnt_out, stdnt_stderr = self.compiled_user_answer + stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) + + # Only if compilation is successful, the program is executed + # And tested with testcases + if stdnt_stderr == '': + proc, main_out, main_err = self.compiled_test_code + main_err = self._remove_null_substitute_char(main_err) + + if main_err == '': + ret = self._run_command([self.ref_output_path], + stdin=None, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + proc, stdout, stderr = ret + if proc.returncode == 0: + success, err = True, "Correct answer" else: - err = "Error:" - try: - error_lines = main_err.splitlines() - for e in error_lines: - if ':' in e: - err = err + "\n" + e.split(":", 1)[1] - else: - err = err + "\n" + e - except: - err = err + "\n" + main_err - os.remove(self.user_output_path) + err = stdout + "\n" + stderr + os.remove(self.ref_output_path) else: - err = "Compilation Error:" + err = "Error:" try: - error_lines = stdnt_stderr.splitlines() + error_lines = main_err.splitlines() for e in error_lines: if ':' in e: err = err + "\n" + e.split(":", 1)[1] else: err = err + "\n" + e except: - err = err + "\n" + stdnt_stderr + err = err + "\n" + main_err + os.remove(self.user_output_path) + else: + err = "Compilation Error:" + try: + error_lines = stdnt_stderr.splitlines() + for e in error_lines: + if ':' in e: + err = err + "\n" + e.split(":", 1)[1] + else: + err = err + "\n" + e + except: + err = err + "\n" + stdnt_stderr - return success, err + return success, err |