diff options
author | ankitjavalkar | 2016-03-22 16:11:28 +0530 |
---|---|---|
committer | ankitjavalkar | 2016-05-05 19:00:33 +0530 |
commit | 1f4542cdbea66899899b9c7de3ea38e39d794112 (patch) | |
tree | 62aa4a8b780888f99464e552ba18557a98bf6ca7 /yaksh | |
parent | 597c23866be3ecfdf94c40693b060fe9ebbf6446 (diff) | |
download | online_test-1f4542cdbea66899899b9c7de3ea38e39d794112.tar.gz online_test-1f4542cdbea66899899b9c7de3ea38e39d794112.tar.bz2 online_test-1f4542cdbea66899899b9c7de3ea38e39d794112.zip |
Add anti-cheating for python stdout evaluator and modify test
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/evaluator_tests/test_python_evaluation.py | 18 | ||||
-rw-r--r-- | yaksh/python_stdout_evaluator.py | 9 |
2 files changed, 22 insertions, 5 deletions
diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index e157cde..b23942f 100644 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -148,12 +148,13 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): class PythonStdoutEvaluationTestCases(unittest.TestCase): def setUp(self): - self.output = ['Hello World\\n'] + self.output = ['0 1 1 2 3'] self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in your code.").format(SERVER_TIMEOUT) def test_correct_answer(self): - user_answer = "a = 'Hello'\nb = 'World'\nprint '{0} {1}'.format(a, b)" + # user_answer = "a = 'Hello'\nb = 'World'\nprint '{0} {1}'.format(a, b)" + user_answer = "a,b=0,1\nfor i in range(5):\n\tprint a,\n\ta,b=b,a+b" get_class = PythonStdoutEvaluator() kwargs = {'user_answer': user_answer, 'test_case_data': self.output @@ -163,7 +164,7 @@ class PythonStdoutEvaluationTestCases(unittest.TestCase): self.assertTrue(result.get('success')) def test_incorrect_answer(self): - user_answer = "a = 'Goodbye'\nb = 'Name'\nprint '{0} {1}'.format(a, b)" + user_answer = "a,b=0,1\nfor i in range(5):\n\tprint b,\n\ta,b=b,a+b" get_class = PythonStdoutEvaluator() kwargs = {'user_answer': user_answer, 'test_case_data': self.output @@ -172,6 +173,17 @@ class PythonStdoutEvaluationTestCases(unittest.TestCase): self.assertFalse(result.get('success')) self.assertEqual(result.get('error'), "Incorrect Answer") + def test_direct_printed_answer(self): + user_answer = "print '0 1 1 2 3'" + error_msg = "Incorrect Answer: Please avoid printing the expected output directly" + get_class = PythonStdoutEvaluator() + kwargs = {'user_answer': user_answer, + 'test_case_data': self.output + } + result = get_class.evaluate(**kwargs) + self.assertFalse(result.get('success')) + self.assertEqual(result.get('error'), error_msg) + def test_infinite_loop(self): user_answer = "def add(a, b):\n\twhile True:\n\t\tpass" get_class = PythonStdoutEvaluator() diff --git a/yaksh/python_stdout_evaluator.py b/yaksh/python_stdout_evaluator.py index 8f3eb65..815752f 100644 --- a/yaksh/python_stdout_evaluator.py +++ b/yaksh/python_stdout_evaluator.py @@ -35,8 +35,12 @@ class PythonStdoutEvaluator(CodeEvaluator): g = {} exec submitted in g raw_output_value = output_buffer.getvalue() - output_value = raw_output_value.encode('string_escape').strip() - if output_value == expected_output: + # output_value = raw_output_value.encode('string_escape').strip() + output_value = raw_output_value.strip() + if expected_output in user_answer: + success = False + err = "Incorrect Answer: Please avoid printing the expected output directly" + elif output_value == expected_output: success = True err = "Correct answer" @@ -46,3 +50,4 @@ class PythonStdoutEvaluator(CodeEvaluator): del tb return success, err + |