1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
from __future__ import unicode_literals
import unittest
from textwrap import dedent
import os
import tempfile
import shutil
from psutil import Process
from yaksh.grader import Grader
from yaksh.settings import SERVER_TIMEOUT
from yaksh.evaluator_tests.test_python_evaluation import EvaluatorBaseTest
class RAssertionEvaluationTestCase(EvaluatorBaseTest):
def setUp(self):
self.tmp_file = os.path.join(tempfile.gettempdir(), 'test.txt')
with open(self.tmp_file, 'wb') as f:
f.write('2'.encode('ascii'))
tmp_in_dir_path = tempfile.mkdtemp()
self.in_dir = tmp_in_dir_path
self.test_case = dedent(
'''
source("function.r")
check_empty = function(obj){
stopifnot(is.null(obj) == FALSE)
}
check = function(input, output){
stopifnot(input == output)
}
is_correct = function(){
if (count == 3){
quit("no", 31)
}
}
check_empty(odd_or_even(3))
check(odd_or_even(6), "EVEN")
check(odd_or_even(1), "ODD")
check(odd_or_even(10), "EVEN")
check(odd_or_even(777), "ODD")
check(odd_or_even(778), "EVEN")
count = 3
is_correct()
'''
)
self.test_case_data = [{"test_case": self.test_case,
"test_case_type": "standardtestcase",
"weight": 0.0
}]
self.timeout_msg = ("Code took more than {0} seconds to run. "
"You probably have an infinite loop in"
" your code.").format(SERVER_TIMEOUT)
self.file_paths = None
def tearDown(self):
os.remove(self.tmp_file)
shutil.rmtree(self.in_dir)
def test_correct_answer(self):
# Given
user_answer = dedent(
'''
odd_or_even <- function(n){
if(n %% 2 == 0){
return("EVEN")
}
return("ODD")
}
'''
)
kwargs = {'metadata': {
'user_answer': user_answer,
'file_paths': self.file_paths,
'partial_grading': False,
'language': 'r'},
'test_case_data': self.test_case_data,
}
# When
grader = Grader(self.in_dir)
result = grader.evaluate(kwargs)
# Then
self.assertTrue(result.get('success'))
def test_incorrect_answer(self):
# Given
user_answer = dedent(
'''
odd_or_even <- function(n){
if(n %% 2 == 0){
return("ODD")
}
return("EVEN")
}
'''
)
err = ['Error: input == output is not TRUE\nExecution halted\n']
kwargs = {'metadata': {
'user_answer': user_answer,
'file_paths': self.file_paths,
'partial_grading': False,
'language': 'r'},
'test_case_data': self.test_case_data,
}
# When
grader = Grader(self.in_dir)
result = grader.evaluate(kwargs)
errors = result.get('error')
# Then
self.assertFalse(result.get('success'))
self.assertEqual(errors, err)
def test_error_code(self):
# Given
user_answer = dedent(
'''
odd_or_even <- function(n){
a
}
'''
)
kwargs = {'metadata': {
'user_answer': user_answer,
'file_paths': self.file_paths,
'partial_grading': False,
'language': 'r'},
'test_case_data': self.test_case_data,
}
# When
grader = Grader(self.in_dir)
result = grader.evaluate(kwargs)
errors = result.get('error')
# Then
self.assertFalse(result.get("success"))
self.assertIn("object 'a' not found", errors[0])
def test_empty_function(self):
# Given
user_answer = dedent(
'''
odd_or_even <- function(n){
}
'''
)
kwargs = {'metadata': {
'user_answer': user_answer,
'file_paths': self.file_paths,
'partial_grading': False,
'language': 'r'},
'test_case_data': self.test_case_data,
}
# When
grader = Grader(self.in_dir)
result = grader.evaluate(kwargs)
errors = result.get('error')
# Then
self.assertFalse(result.get("success"))
self.assertIn("Error: is.null(obj) == FALSE is not TRUE", errors[0])
def test_infinite_loop(self):
# Given
user_answer = dedent(
'''
odd_or_even <- function(n){
while(0 == 0){
a <- 1
}
}
'''
)
kwargs = {'metadata': {
'user_answer': user_answer,
'file_paths': self.file_paths,
'partial_grading': False,
'language': 'r'},
'test_case_data': self.test_case_data,
}
# When
grader = Grader(self.in_dir)
result = grader.evaluate(kwargs)
# Then
self.assertFalse(result.get("success"))
self.assert_correct_output(self.timeout_msg,
result.get("error")[0]["message"]
)
parent_proc = Process(os.getpid()).children()
if parent_proc:
children_procs = Process(parent_proc[0].pid)
self.assertFalse(any(children_procs.children(recursive=True)))
|