summaryrefslogtreecommitdiff
path: root/yaksh
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh')
-rw-r--r--yaksh/evaluator_tests/test_bash_evaluation.py81
-rw-r--r--yaksh/evaluator_tests/test_c_cpp_evaluation.py2
-rw-r--r--yaksh/evaluator_tests/test_java_evaluation.py2
-rw-r--r--yaksh/evaluator_tests/test_python_evaluation.py2
-rw-r--r--yaksh/settings.py6
-rw-r--r--yaksh/stdio_evaluator.py21
6 files changed, 98 insertions, 16 deletions
diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py
index 4ff3e0a..f954ef0 100644
--- a/yaksh/evaluator_tests/test_bash_evaluation.py
+++ b/yaksh/evaluator_tests/test_bash_evaluation.py
@@ -1,9 +1,12 @@
import unittest
import os
from yaksh.bash_code_evaluator import BashCodeEvaluator
+from yaksh.bash_stdio_evaluator import BashStdioEvaluator
from yaksh.settings import SERVER_TIMEOUT
+from textwrap import dedent
-class BashEvaluationTestCases(unittest.TestCase):
+
+class BashAssertionEvaluationTestCases(unittest.TestCase):
def setUp(self):
self.test_case_data = [
{"test_case": "bash_files/sample.sh,bash_files/sample.args"}
@@ -48,5 +51,81 @@ class BashEvaluationTestCases(unittest.TestCase):
self.assertEquals(result.get("error"), self.timeout_msg)
+class BashStdioEvaluationTestCases(unittest.TestCase):
+ def setUp(self):
+ 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 = dedent(""" #!/bin/bash
+ read A
+ read B
+ echo -n `expr $A + $B`
+ """
+ )
+ test_case_data = [{'expected_output': '11', 'expected_input': '5\n6'}]
+ get_class = BashStdioEvaluator()
+ kwargs = {"user_answer": user_answer,
+ "test_case_data": test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_array_input(self):
+ user_answer = dedent(""" readarray arr;
+ COUNTER=0
+ while [ $COUNTER -lt 3 ]; do
+ echo -n "${arr[$COUNTER]}"
+ let COUNTER=COUNTER+1
+ done
+ """
+ )
+ test_case_data = [{'expected_output': '1 2 3\n4 5 6\n7 8 9\n',
+ 'expected_input': '1,2,3\n4,5,6\n7,8,9'
+ }]
+ get_class = BashStdioEvaluator()
+ kwargs = {"user_answer": user_answer,
+ "test_case_data": test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
+ def test_incorrect_answer(self):
+ user_answer = dedent(""" #!/bin/bash
+ read A
+ read B
+ echo -n `expr $A + $B`
+ """
+ )
+ test_case_data = [{'expected_output': '12', 'expected_input': '5\n6'}]
+ get_class = BashStdioEvaluator()
+ kwargs = {"user_answer": user_answer,
+ "test_case_data": test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertIn("Incorrect", result.get('error'))
+ self.assertFalse(result.get('success'))
+
+ def test_stdout_only(self):
+ user_answer = dedent(""" #!/bin/bash
+ A=6
+ B=4
+ echo -n `expr $A + $B`
+ """
+ )
+ test_case_data = [{'expected_output': '10',
+ 'expected_input': ''
+ }]
+ get_class = BashStdioEvaluator()
+ kwargs = {"user_answer": user_answer,
+ "test_case_data": test_case_data
+ }
+ result = get_class.evaluate(**kwargs)
+ self.assertEquals(result.get('error'), "Correct Answer")
+ self.assertTrue(result.get('success'))
+
if __name__ == '__main__':
unittest.main()
diff --git a/yaksh/evaluator_tests/test_c_cpp_evaluation.py b/yaksh/evaluator_tests/test_c_cpp_evaluation.py
index 7a87d87..3d2e9fe 100644
--- a/yaksh/evaluator_tests/test_c_cpp_evaluation.py
+++ b/yaksh/evaluator_tests/test_c_cpp_evaluation.py
@@ -6,7 +6,7 @@ from yaksh.settings import SERVER_TIMEOUT
from textwrap import dedent
-class CEvaluationTestCases(unittest.TestCase):
+class CAssertionEvaluationTestCases(unittest.TestCase):
def setUp(self):
self.test_case_data = [{"test_case": "c_cpp_files/main.cpp"}]
self.in_dir = "/tmp"
diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py
index fa8d68c..211b324 100644
--- a/yaksh/evaluator_tests/test_java_evaluation.py
+++ b/yaksh/evaluator_tests/test_java_evaluation.py
@@ -7,7 +7,7 @@ from yaksh.settings import SERVER_TIMEOUT
from textwrap import dedent
-class JavaEvaluationTestCases(unittest.TestCase):
+class JavaAssertionEvaluationTestCases(unittest.TestCase):
def setUp(self):
self.test_case_data = [
{"test_case": "java_files/main_square.java"}
diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py
index dfdc6ec..a935f1d 100644
--- a/yaksh/evaluator_tests/test_python_evaluation.py
+++ b/yaksh/evaluator_tests/test_python_evaluation.py
@@ -181,7 +181,7 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase):
self.assertIn(msg, result.get("error"))
-class PythonStdioEvaluationTestCases(unittest.TestCase):
+class PythonStdoutEvaluationTestCases(unittest.TestCase):
def setUp(self):
self.test_case_data = [{"expected_input": None,
"expected_output": "0 1 1 2 3"
diff --git a/yaksh/settings.py b/yaksh/settings.py
index f4122b6..b1336f4 100644
--- a/yaksh/settings.py
+++ b/yaksh/settings.py
@@ -31,6 +31,10 @@ code_evaluators = {
},
"java": {"standardtestcase": "java_code_evaluator.JavaCodeEvaluator",
"stdiobasedtestcase": "java_stdio_evaluator.JavaStdioEvaluator"},
- "bash": {"standardtestcase": "bash_code_evaluator.BashCodeEvaluator"},
+
+ "bash": {"standardtestcase": "bash_code_evaluator.BashCodeEvaluator",
+ "stdiobasedtestcase": "bash_stdio_evaluator.BashStdioEvaluator"
+ },
+
"scilab": {"standardtestcase": "scilab_code_evaluator.ScilabCodeEvaluator"},
}
diff --git a/yaksh/stdio_evaluator.py b/yaksh/stdio_evaluator.py
index 037ad3d..236c0ab 100644
--- a/yaksh/stdio_evaluator.py
+++ b/yaksh/stdio_evaluator.py
@@ -1,23 +1,22 @@
class Evaluator(object):
-
def evaluate(self, user_answer, proc, expected_input, expected_output):
+
success = False
- if expected_input:
- ip = expected_input.replace(",", " ")
- proc.stdin.write('{0}\n'.format(ip))
- error_msg = " Given Input is {0} \n Expected Output is {1} ".\
- format(expected_input, expected_output)
- else:
- error_msg = "Expected output is {0}".format(expected_output)
- output_err = proc.stderr.read()
- user_output = proc.stdout.read()
+ ip = expected_input.replace(",", " ")
+ user_output, output_err = proc.communicate(input='{0}\n'.format(ip))
expected_output = expected_output.replace("\r", "")
+ if not expected_input:
+ error_msg = "Expected Output is {0} ".\
+ format(repr(expected_output))
+ else:
+ error_msg = " Given Input is {0} \n Expected Output is {1} ".\
+ format(expected_input, repr(expected_output))
if output_err == '':
if user_output == expected_output:
success, err = True, "Correct Answer"
else:
err = " Incorrect Answer\n" + error_msg +\
- "\n Your output is {0}".format(user_output)
+ "\n Your output is {0}".format(repr(user_output))
else:
err = "Error:"+"\n"+output_err
return success, err