From 6667efae7acb86f60b0ebd4dabb959bed759bc16 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 6 May 2013 11:56:16 +0530 Subject: Function to check C-C++ code Added a function which compiles C and C++ code submitted by the student. 1) If compilation is successful, then the submitted code is tested using test-cases. 2) To test the function written by the student, a C++ file calls the function and passes the argument to the function. Then the function checks for the expected return value. 3) If the return value is as expected, then a different set of arguments are passed, and the output is checked. 4) If for all set of arguments the output is as expected then the student code is graded correct else the error is displayed to the student. Changed the way the code is graded. Previously, the algorithm checked the student code for all test-cases. If all the test-cases were satisfied, the last-line of the program was reached and printed "All Correct". So at any point if a test-case fails, the last line is not reached as the program was terminate. When the string "All Correct" was found in the output, the code was graded as RIGHT else WRONG. This is not a proper way for code checking, as the student code *may* contain a print statement with the string('All Correct'), and thus can get program RIGHT even though it is WRONG. So now the student code is tested as follows: 1) The code checks for all test-cases. 2) If all test-cases are satisfied then it returns 0. 3) If any one of the test-case fails, the program is terminated and will return 1. 4) Now depending on the return status(0 or 1), it will grade the code. a) if 0 then RIGHT b) if 1 then WRONG This ensures, no manipulation from student side. --- testapp/test_server.py | 179 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 151 insertions(+), 28 deletions(-) (limited to 'testapp/test_server.py') diff --git a/testapp/test_server.py b/testapp/test_server.py index 0790816..fc6ec56 100644 --- a/testapp/test_server.py +++ b/testapp/test_server.py @@ -6,34 +6,154 @@ up the code server as:: """ from exam.xmlrpc_clients import code_server + def check_result(result, check='correct answer'): - if check != 'correct answer': - assert result[0] == False - else: - assert result[0] == True - if "unable to connect" in result[1].lower(): - assert result[0], result[1] - assert check in result[1].lower(), result[1] +# if check != 'correct answer': +# assert result[0] == False +# else: +# assert result[0] == True +# if "unable to connect" in result[1].lower(): +# assert result[0], result[1] +# assert check in result[1].lower(), result[1] + print result + def test_python(): """Test if server runs Python code as expected.""" src = 'while True: pass' - result = code_server.run_code(src, '', '/tmp', language="python") - check_result(result, 'more than ') - + # result = code_server.run_code(src, '', '/tmp', language="python") + # check_result(result, 'more than ') + """ src = 'x = 1' - result = code_server.run_code(src, 'assert x == 1', '/tmp', + result = code_server.run_code(src, 'assert x == 1', '/tmp', language="python") check_result(result, 'correct answer') - result = code_server.run_code(src, 'assert x == 0', '/tmp', + result = code_server.run_code(src, 'assert x == 0', '/tmp', language="python") check_result(result, 'assertionerror') src = 'abracadabra' - result = code_server.run_code(src, 'assert x == 0', '/tmp', + result = code_server.run_code(src, 'assert x == 0', '/tmp', language="python") check_result(result, 'nameerror') + """ + + +def test_c(): + """Test if server runs c code as expected.""" + src = """ + #include + int ad(int a, int b) + {return a+b;} + """ + result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + check_result(result) + + src = """ + int add(int a, int b) + {return a+b} + """ + result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + check_result(result) + + src = """ + int add(int a, int b) + {while(1>0){} + return a+b;} + """ + result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + check_result(result) + + src = """ + int add(int a, int b) + {return a+b;} + """ + result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + check_result(result) + + src = """ + #include + int add(int a, int b) + {printf("All Correct");} + """ + result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + check_result(result) + + +def test_cpp(): + """Test if server runs c code as expected.""" + src = """ + int add(int a, int b) + { + return a+b + } + """ + result = code_server.run_code(src, 'docs/main.cpp', '/tmp', language="C++") + check_result(result) + + src = """ + int add(int a, int b) + { + return a+b; + } + """ + result = code_server.run_code(src, 'docs/main.cpp', '/tmp', language="C++") + check_result(result) + + src = """ + int dd(int a, int b) + { + return a+b; + } + """ + result = code_server.run_code(src, 'docs/main.cpp', '/tmp', language="C++") + check_result(result) + + src = """ + int add(int a, int b) + { + while(0==0) + {} + return a+b; + } + """ + result = code_server.run_code(src, 'docs/main.cpp', '/tmp', language="C++") + check_result(result) + + +def test_java(): + """Test if server runs c code as expected.""" + src = """class Test +{ + public static void main(String arg[]) + { + int sum =Integer.parseInt(arg[0])+Integer.parseInt(arg[1]); + System.out.print(sum); + } +} + """ + +# result = code_server.run_code(src, 'docs/sample.java',\ +# '/tmp', language="Java") +# check_result(result) + + +def test_scilab(): + """Test if server runs c code as expected.""" + src = """ + +A=sciargs( +a=strtod(A(6)) +b=strtod(A(7)) +c=a+b +disp(c) +exit() + """ +# result = code_server.run_code(src, '/tmp/sample.sce',\ +# '/tmp', language="Scilab") +# check_result(result) + def test_bash(): """Test if server runs Bash code as expected.""" @@ -41,43 +161,46 @@ def test_bash(): #!/bin/bash [[ $# -eq 2 ]] && echo $(( $1 + $2 )) && exit $(( $1 + $2 )) """ - result = code_server.run_code(src, - 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - check_result(result) + # result = code_server.run_code(src, + # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + # check_result(result) src = """ #!/bin/bash [[ $# -eq 2 ]] && echo $(( $1 - $2 )) && exit $(( $1 - $2 )) """ - result = code_server.run_code(src, - 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - check_result(result, 'error') + #result = code_server.run_code(src, + # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + #check_result(result, 'error') src = """\ #!/bin/bash while [ 1 ] ; do echo "" > /dev/null ; done """ - result = code_server.run_code(src, - 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - check_result(result, 'more than ') + #result = code_server.run_code(src, + # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + #check_result(result, 'more than ') src = ''' #!/bin/bash while [ 1 ] ; do echo "" > /dev/null ''' - result = code_server.run_code(src, - 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - check_result(result, 'error') + #result = code_server.run_code(src, + # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + #check_result(result, 'error') src = '''# Enter your code here. #!/bin/bash while [ 1 ] ; do echo "" > /dev/null ''' - result = code_server.run_code(src, - 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - check_result(result, 'oserror') + #result = code_server.run_code(src, + # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + #check_result(result, 'oserror') if __name__ == '__main__': test_python() test_bash() test_c() + test_cpp() + test_java() + test_scilab() -- cgit From fc40c033a083e2a4027fdbeecfb5853e6770a7c6 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Wed, 5 Jun 2013 17:26:50 +0530 Subject: c and cpp files isolated --- testapp/test_server.py | 54 +++++++++----------------------------------------- 1 file changed, 9 insertions(+), 45 deletions(-) (limited to 'testapp/test_server.py') diff --git a/testapp/test_server.py b/testapp/test_server.py index fc6ec56..c63ee4b 100644 --- a/testapp/test_server.py +++ b/testapp/test_server.py @@ -47,14 +47,14 @@ def test_c(): int ad(int a, int b) {return a+b;} """ - result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") check_result(result) src = """ int add(int a, int b) {return a+b} """ - result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") check_result(result) src = """ @@ -62,14 +62,14 @@ def test_c(): {while(1>0){} return a+b;} """ - result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") check_result(result) src = """ int add(int a, int b) {return a+b;} """ - result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") check_result(result) src = """ @@ -77,7 +77,7 @@ def test_c(): int add(int a, int b) {printf("All Correct");} """ - result = code_server.run_code(src, '/tmp/main.c', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") check_result(result) @@ -89,7 +89,7 @@ def test_cpp(): return a+b } """ - result = code_server.run_code(src, 'docs/main.cpp', '/tmp', language="C++") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") check_result(result) src = """ @@ -98,7 +98,7 @@ def test_cpp(): return a+b; } """ - result = code_server.run_code(src, 'docs/main.cpp', '/tmp', language="C++") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") check_result(result) src = """ @@ -107,7 +107,7 @@ def test_cpp(): return a+b; } """ - result = code_server.run_code(src, 'docs/main.cpp', '/tmp', language="C++") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") check_result(result) src = """ @@ -118,43 +118,9 @@ def test_cpp(): return a+b; } """ - result = code_server.run_code(src, 'docs/main.cpp', '/tmp', language="C++") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") check_result(result) - -def test_java(): - """Test if server runs c code as expected.""" - src = """class Test -{ - public static void main(String arg[]) - { - int sum =Integer.parseInt(arg[0])+Integer.parseInt(arg[1]); - System.out.print(sum); - } -} - """ - -# result = code_server.run_code(src, 'docs/sample.java',\ -# '/tmp', language="Java") -# check_result(result) - - -def test_scilab(): - """Test if server runs c code as expected.""" - src = """ - -A=sciargs( -a=strtod(A(6)) -b=strtod(A(7)) -c=a+b -disp(c) -exit() - """ -# result = code_server.run_code(src, '/tmp/sample.sce',\ -# '/tmp', language="Scilab") -# check_result(result) - - def test_bash(): """Test if server runs Bash code as expected.""" src = """ @@ -202,5 +168,3 @@ if __name__ == '__main__': test_bash() test_c() test_cpp() - test_java() - test_scilab() -- cgit From 33366bfa87d62d433cc7541e4699b61be78e5024 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 1 Jul 2013 15:55:26 +0530 Subject: comments and print statement removed --- testapp/test_server.py | 70 ++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 37 deletions(-) (limited to 'testapp/test_server.py') diff --git a/testapp/test_server.py b/testapp/test_server.py index c63ee4b..f2ea84c 100644 --- a/testapp/test_server.py +++ b/testapp/test_server.py @@ -8,22 +8,19 @@ from exam.xmlrpc_clients import code_server def check_result(result, check='correct answer'): -# if check != 'correct answer': -# assert result[0] == False -# else: -# assert result[0] == True -# if "unable to connect" in result[1].lower(): -# assert result[0], result[1] -# assert check in result[1].lower(), result[1] - print result - + if check != 'correct answer': + assert result[0] == False + else: + assert result[0] == True + if "unable to connect" in result[1].lower(): + assert result[0], result[1] + assert check in result[1].lower(), result[1] def test_python(): """Test if server runs Python code as expected.""" src = 'while True: pass' - # result = code_server.run_code(src, '', '/tmp', language="python") - # check_result(result, 'more than ') - """ + result = code_server.run_code(src, '', '/tmp', language="python") + check_result(result, 'more than ') src = 'x = 1' result = code_server.run_code(src, 'assert x == 1', '/tmp', language="python") @@ -37,7 +34,6 @@ def test_python(): result = code_server.run_code(src, 'assert x == 0', '/tmp', language="python") check_result(result, 'nameerror') - """ def test_c(): @@ -48,14 +44,14 @@ def test_c(): {return a+b;} """ result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") - check_result(result) + check_result(result, 'error') src = """ int add(int a, int b) {return a+b} """ result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") - check_result(result) + check_result(result, 'compilation error') src = """ int add(int a, int b) @@ -63,14 +59,14 @@ def test_c(): return a+b;} """ result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") - check_result(result) + check_result(result, 'more than') src = """ int add(int a, int b) {return a+b;} """ result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") - check_result(result) + check_result(result, 'correct answer') src = """ #include @@ -78,7 +74,7 @@ def test_c(): {printf("All Correct");} """ result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") - check_result(result) + check_result(result, 'incorrect') def test_cpp(): @@ -90,7 +86,7 @@ def test_cpp(): } """ result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") - check_result(result) + check_result(result, 'compilation error') src = """ int add(int a, int b) @@ -99,7 +95,7 @@ def test_cpp(): } """ result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") - check_result(result) + check_result(result, 'correct answer') src = """ int dd(int a, int b) @@ -108,7 +104,7 @@ def test_cpp(): } """ result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") - check_result(result) + check_result(result, 'error') src = """ int add(int a, int b) @@ -119,7 +115,7 @@ def test_cpp(): } """ result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") - check_result(result) + check_result(result, 'more than') def test_bash(): """Test if server runs Bash code as expected.""" @@ -127,41 +123,41 @@ def test_bash(): #!/bin/bash [[ $# -eq 2 ]] && echo $(( $1 + $2 )) && exit $(( $1 + $2 )) """ - # result = code_server.run_code(src, - # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - # check_result(result) + result = code_server.run_code(src, + 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + check_result(result) src = """ #!/bin/bash [[ $# -eq 2 ]] && echo $(( $1 - $2 )) && exit $(( $1 - $2 )) """ - #result = code_server.run_code(src, - # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - #check_result(result, 'error') + result = code_server.run_code(src, + 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + check_result(result, 'error') src = """\ #!/bin/bash while [ 1 ] ; do echo "" > /dev/null ; done """ - #result = code_server.run_code(src, - # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - #check_result(result, 'more than ') + result = code_server.run_code(src, + 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + check_result(result, 'more than ') src = ''' #!/bin/bash while [ 1 ] ; do echo "" > /dev/null ''' - #result = code_server.run_code(src, - # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - #check_result(result, 'error') + result = code_server.run_code(src, + 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + check_result(result, 'error') src = '''# Enter your code here. #!/bin/bash while [ 1 ] ; do echo "" > /dev/null ''' - #result = code_server.run_code(src, - # 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") - #check_result(result, 'oserror') + result = code_server.run_code(src, + 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + check_result(result, 'oserror') if __name__ == '__main__': test_python() -- cgit From 781842c523ccf11b85e0be06e884b2cd585f3cb9 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Wed, 3 Jul 2013 12:41:00 +0530 Subject: clean code --- testapp/test_server.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'testapp/test_server.py') diff --git a/testapp/test_server.py b/testapp/test_server.py index f2ea84c..924a6c5 100644 --- a/testapp/test_server.py +++ b/testapp/test_server.py @@ -8,13 +8,13 @@ from exam.xmlrpc_clients import code_server def check_result(result, check='correct answer'): - if check != 'correct answer': - assert result[0] == False - else: - assert result[0] == True - if "unable to connect" in result[1].lower(): + if check != 'correct answer': + assert result[0] == False + else: + assert result[0] == True + if "unable to connect" in result[1].lower(): assert result[0], result[1] - assert check in result[1].lower(), result[1] + assert check in result[1].lower(), result[1] def test_python(): """Test if server runs Python code as expected.""" -- cgit From b522a1ba1c1edff7648c6c7d69fa2867a08b4100 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Wed, 3 Jul 2013 16:49:29 +0530 Subject: merged java --- testapp/test_server.py | 115 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 104 insertions(+), 11 deletions(-) (limited to 'testapp/test_server.py') diff --git a/testapp/test_server.py b/testapp/test_server.py index 924a6c5..2a17739 100644 --- a/testapp/test_server.py +++ b/testapp/test_server.py @@ -16,6 +16,7 @@ def check_result(result, check='correct answer'): assert result[0], result[1] assert check in result[1].lower(), result[1] + def test_python(): """Test if server runs Python code as expected.""" src = 'while True: pass' @@ -43,14 +44,16 @@ def test_c(): int ad(int a, int b) {return a+b;} """ - result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', + '/tmp', language="C") check_result(result, 'error') src = """ int add(int a, int b) {return a+b} """ - result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', + '/tmp', language="C") check_result(result, 'compilation error') src = """ @@ -58,14 +61,16 @@ def test_c(): {while(1>0){} return a+b;} """ - result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', + '/tmp', language="C") check_result(result, 'more than') src = """ int add(int a, int b) {return a+b;} """ - result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', + '/tmp', language="C") check_result(result, 'correct answer') src = """ @@ -73,7 +78,8 @@ def test_c(): int add(int a, int b) {printf("All Correct");} """ - result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', + '/tmp', language="C") check_result(result, 'incorrect') @@ -85,8 +91,9 @@ def test_cpp(): return a+b } """ - result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") - check_result(result, 'compilation error') + result = code_server.run_code(src, 'c_cpp_files/main.cpp', + '/tmp', language="C++") + check_result(result, 'error') src = """ int add(int a, int b) @@ -94,7 +101,8 @@ def test_cpp(): return a+b; } """ - result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', + '/tmp', language="C++") check_result(result, 'correct answer') src = """ @@ -103,7 +111,8 @@ def test_cpp(): return a+b; } """ - result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', + '/tmp', language="C++") check_result(result, 'error') src = """ @@ -114,9 +123,92 @@ def test_cpp(): return a+b; } """ - result = code_server.run_code(src, 'c_cpp_files/main.cpp', '/tmp', language="C++") + result = code_server.run_code(src, 'c_cpp_files/main.cpp', + '/tmp', language="C++") check_result(result, 'more than') + +def test_java(): + """Test if server runs java code as expected.""" + src = """ + class Test + { + int square_num(int a) + { + return a*a; + } + } + """ + result = code_server.run_code(src, 'java_files/main_square.java', + '/tmp', language="java") + check_result(result, 'correct answer') + + src = """ + class Test + { + int square_num(int a) + { + return b*b; + } + } + """ + result = code_server.run_code(src, 'java_files/main_square.java', + '/tmp', language="java") + check_result(result, 'error') + + src = """ + class Test + { + int square_nu(int a) + { + return a*a; + } + } + """ + result = code_server.run_code(src, 'java_files/main_square.java', + '/tmp', language="java") + check_result(result, 'error') + + src = """ + class Test + { + int square_num(int a) + { + while(0==0) + {} + } + } + """ + result = code_server.run_code(src, 'java_files/main_square.java', + '/tmp', language="java") + check_result(result, 'more than') + + src = """ + class Test + { + int square_num(int a) + { + return a+b + } + } + """ + result = code_server.run_code(src, 'java_files/main_square.java', + '/tmp', language="java") + check_result(result, 'error') + + src = """ + class Test + { + int square_num(int a) + { + return a+b + + """ + result = code_server.run_code(src, 'java_files/main_square.java', + '/tmp', language="java") + check_result(result, 'error') + + def test_bash(): """Test if server runs Bash code as expected.""" src = """ @@ -132,7 +224,7 @@ def test_bash(): [[ $# -eq 2 ]] && echo $(( $1 - $2 )) && exit $(( $1 - $2 )) """ result = code_server.run_code(src, - 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") + 'docs/sample.sh\ndocs/sample.args', '/tmp', language="bash") check_result(result, 'error') src = """\ @@ -164,3 +256,4 @@ if __name__ == '__main__': test_bash() test_c() test_cpp() + test_java() -- cgit