Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 36 additions & 27 deletions yaksh/cpp_code_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from os.path import isfile
import subprocess
from textwrap import dedent

# Local imports
from .file_utils import copy_files, delete_files
Expand Down Expand Up @@ -49,16 +50,42 @@ def set_file_paths(self):

return user_output_path, ref_output_path

def append_test_case_to_answer(self):
content = dedent('''
{0}
#include <cassert>
int main(){{
{1};
return 0;
}}
'''.format(self.user_answer, self.test_case))
return content

def get_commands(self, clean_ref_code_path, user_output_path,
ref_output_path):
compile_command = 'g++ {0} -c -o {1}'.format(
compile_command = 'g++ {0} -c -o {1}'.format(
self.submit_code_path, user_output_path)
compile_main = 'g++ {0} {1} -o {2}'.format(
clean_ref_code_path, user_output_path,
ref_output_path
compile_main = 'g++ {0} -o {1}'.format(
clean_ref_code_path, ref_output_path
)
return compile_command, compile_main

def trim_error(self, error_type, error):
err = error_type
try:
error_lines = error.splitlines()
for e in error_lines:
if ':' in e:
if error_type == 'Assertion Error:':
err = '{0} \n {1}'.format(err, e.split(':')[-1])
else:
err = '{0} \n {1}'.format(err, e.split(':', 1)[1])
else:
err = '{0} \n {1}'.format(err, e)
except Exception:
return '{0} \n {1}'.format(err, main_err)
return err

def compile_code(self):
if self.compiled_user_answer and self.compiled_test_code:
return None
Expand All @@ -67,7 +94,8 @@ def compile_code(self):
self.test_code_path = self.create_submit_code_file('main.c')
self.write_to_submit_code_file(self.submit_code_path,
self.user_answer)
self.write_to_submit_code_file(self.test_code_path, self.test_case)
main_content = self.append_test_case_to_answer()
self.write_to_submit_code_file(self.test_code_path, main_content)
clean_ref_code_path = self.test_code_path
if self.file_paths:
self.files = copy_files(self.file_paths)
Expand All @@ -90,7 +118,6 @@ def compile_code(self):
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)

self.compiled_test_code = self._run_command(
self.compile_main,
shell=True,
Expand Down Expand Up @@ -141,31 +168,13 @@ def check_code(self):
success, err = True, None
mark_fraction = 1.0 if self.partial_grading else 0.0
else:
err = "{0} \n {1}".format(stdout, stderr)
err = self.trim_error('Assertion Error:', stderr)
raise AssertionError(err)
else:
err = "Test case Error:"
try:
error_lines = main_err.splitlines()
for e in error_lines:
if ':' in e:
err = "{0} \n {1}".format(err, e.split(":", 1)[1])
else:
err = "{0} \n {1}".format(err, e)
except Exception:
err = "{0} \n {1}".format(err, main_err)
err = self.trim_error('Test case Error:', main_err)
raise TestCaseError(err)
else:
err = "Compilation Error:"
try:
error_lines = stdnt_stderr.splitlines()
for e in error_lines:
if ':' in e:
err = "{0} \n {1}".format(err, e.split(":", 1)[1])
else:
err = "{0} \n {1}".format(err, e)
except Exception:
err = "{0} \n {1}".format(err, stdnt_stderr)
err = self.trim_error('Compilation Error:', stdnt_stderr)
raise CompilationError(err)

return success, err, mark_fraction
135 changes: 4 additions & 131 deletions yaksh/evaluator_tests/test_c_cpp_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,7 @@ def setUp(self):
with open(self.f_path, 'wb') as f:
f.write('2'.encode('ascii'))
tmp_in_dir_path = tempfile.mkdtemp()
self.tc_data = dedent("""
#include <stdio.h>
#include <stdlib.h>

extern int add(int, int);

template <class T>

void check(T expect, T result)
{
if (expect == result)
{
printf("Correct: Expected %d got %d ",expect,result);
}
else
{
printf("Incorrect: Expected %d got %d ",expect,result);
exit (1);
}
}

int main(void)
{
int result;
result = add(0,0);
printf("Input submitted to the function: 0, 0");
check(0, result);
result = add(2,3);
printf("Input submitted to the function: 2 3");
check(5,result);
printf("All Correct");
return 0;
}
""")
self.tc_data = 'assert(add(2, 3) == 5)'
self.test_case_data = [{"test_case": self.tc_data,
"test_case_type": "standardtestcase",
"weight": 0.0
Expand Down Expand Up @@ -157,33 +124,7 @@ def test_infinite_loop(self):
def test_file_based_assert(self):
# Given
self.file_paths = [(self.f_path, False)]
self.tc_data = dedent("""
#include <stdio.h>
#include <stdlib.h>

extern int ans();

template <class T>
void check(T expect,T result)
{
if (expect == result)
{
printf("Correct: Expected %d got %d ",expect,result);
}
else
{
printf("Incorrect: Expected %d got %d ",expect,result);
exit (0);
}
}

int main(void)
{
int result;
result = ans();
check(50, result);
}
""")
self.tc_data = 'assert(ans() == 50)'
self.test_case_data = [{"test_case": self.tc_data,
"test_case_type": "standardtestcase",
"weight": 0.0
Expand Down Expand Up @@ -218,40 +159,7 @@ def test_file_based_assert(self):

def test_incorrect_testcase(self):
# Given
self.tc_data = dedent("""
#include <stdio.h>
#include <stdlib.h>

extern int add(int, int);

template <class T>

void check(T expect, T result)
{
if (expect == result)
{
printf("Correct: Expected %d got %d ",expect,result);
}
else
{
printf("Incorrect: Expected %d got %d ",expect,result);
exit (1);
}
}

int main(void)
{
int result;
result = add(0,0);
printf("Input submitted to the function: 0, 0");
check(0, result);
result = add(2,3);
printf("Input submitted to the function: 2 3");
check(5,result)
printf("All Correct");
return 0;
}
""")
self.tc_data = 'assert(add(0, 1) == 1'
user_answer = dedent("""\
int add(int a, int b)
{
Expand Down Expand Up @@ -828,42 +736,7 @@ def test_assert_with_hook(self):
# Given
user_answer = "int add(int a, int b)\n{return a+b;}"

assert_test_case = dedent("""\
#include <stdio.h>
#include <stdlib.h>

extern int add(int, int);

template <class T>

void check(T expect, T result)
{
if (expect == result)
{
printf("Correct: Expected %d got %d ",
expect,result);
}
else
{
printf("Incorrect: Expected %d got %d ",
expect,result);
exit (1);
}
}

int main(void)
{
int result;
result = add(0,0);
printf("Input submitted 0, 0");
check(0, result);
result = add(2,3);
printf("Input submitted 2 3");
check(5,result);
printf("All Correct");
return 0;
}
""")
assert_test_case = 'assert(add(2, 3) == 5)'

hook_code = dedent("""\
def check_answer(user_answer):
Expand Down
Binary file modified yaksh/fixtures/demo_questions.zip
Binary file not shown.