-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_evaluate_python.py
More file actions
274 lines (227 loc) · 12.3 KB
/
test_evaluate_python.py
File metadata and controls
274 lines (227 loc) · 12.3 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python3
"""
Test suite for Python program evaluation function using unittest framework.
This test validates that the evaluate function in synthesizer_python.py correctly
handles various types of errors and scenarios:
1. Syntax errors
2. Indentation errors
3. Runtime errors
4. Timeout errors
5. Partial test failures
6. Complete success
"""
import unittest
import sys
import os
from dataset import ProgramSynthesisDatapoint
from synthesizer_python import PythonProgramSynthesizer
class TestPythonEvaluation(unittest.TestCase):
"""Test cases for Python program evaluation."""
def setUp(self):
"""Set up test fixtures before each test method."""
self.synthesizer = PythonProgramSynthesizer()
self.simple_datapoint = self._create_simple_datapoint()
def _create_simple_datapoint(self):
"""Create a simple test datapoint for testing."""
return ProgramSynthesisDatapoint({
"description": "Sum all numbers in a list",
"input_from": "standard input",
"output_to": "standard output",
"time_limit": 2.0,
"memory_limit": "256 megabytes",
"input_spec": "First line contains n, then n integers on the next line",
"output_spec": "Print the sum of all integers",
"notes": "Simple test case",
"sample_inputs": ["1 2 3", "10 20", "5"], # Just the numbers, no count
"sample_outputs": ["6", "30", "5"],
"tags": ["implementation", "math"],
"src_uid": "test_001",
"difficulty": 100
})
def test_syntax_error(self):
"""Test evaluation with syntax error."""
# Program with syntax error (missing colon)
bad_program = """
numbers = list(map(int, input().split()))
sum_val = sum(numbers)
print(sum_val) # Missing colon after if statement
if sum_val > 0
print("Positive")
"""
report = self.synthesizer.evaluate(self.simple_datapoint, bad_program)
# Validate results
self.assertFalse(report.compiles, "Should not compile due to syntax error")
self.assertFalse(report.executes, "Should not execute if compilation fails")
self.assertTrue(report.has_syntax_errors, "Should have syntax errors")
self.assertGreater(len(report.compiler_errors), 0, "Should have compiler errors")
self.assertEqual(report.overall_status, "failed", "Overall status should be failed")
print(f"✅ Syntax error test passed - {len(report.compiler_errors)} compiler errors")
def test_indentation_error(self):
"""Test evaluation with indentation error."""
# Program with indentation error
bad_program = """
numbers = list(map(int, input().split()))
sum_val = sum(numbers)
print(sum_val)
if sum_val > 0: # Incorrect indentation
print("Positive")
"""
report = self.synthesizer.evaluate(self.simple_datapoint, bad_program)
# Validate results
self.assertFalse(report.compiles, "Should not compile due to indentation error")
self.assertFalse(report.executes, "Should not execute if compilation fails")
self.assertTrue(report.has_syntax_errors, "Should have syntax errors")
self.assertGreater(len(report.compiler_errors), 0, "Should have compiler errors")
self.assertEqual(report.overall_status, "failed", "Overall status should be failed")
print(f"✅ Indentation error test passed - {len(report.compiler_errors)} compiler errors")
def test_runtime_error(self):
"""Test evaluation with runtime error."""
# Program that compiles but has runtime error (division by zero)
bad_program = """
numbers = list(map(int, input().split()))
sum_val = sum(numbers)
result = sum_val / 0 # Division by zero will cause runtime error
print(result)
"""
report = self.synthesizer.evaluate(self.simple_datapoint, bad_program)
# Validate results
self.assertTrue(report.compiles, "Should compile successfully")
self.assertTrue(report.executes, "Should attempt execution")
self.assertTrue(report.has_runtime_errors, "Should have runtime errors")
self.assertGreater(len(report.runtime_errors), 0, "Should have runtime errors")
self.assertIn(report.overall_status, ["failed", "partial"], "Overall status should be failed or partial")
print(f"✅ Runtime error test passed - {len(report.runtime_errors)} runtime errors, {report.passed_tests}/{report.total_tests} tests passed")
def test_timeout(self):
"""Test evaluation with timeout."""
# Create a datapoint with very short timeout
timeout_datapoint = ProgramSynthesisDatapoint({
"description": "Sum all numbers in a list",
"input_from": "standard input",
"output_to": "standard output",
"time_limit": 0.1, # Very short timeout
"memory_limit": "256 megabytes",
"input_spec": "First line contains n, then n integers on the next line",
"output_spec": "Print the sum of all integers",
"notes": "Simple test case",
"sample_inputs": ["1 2 3"],
"sample_outputs": ["6"],
"tags": ["implementation", "math"],
"src_uid": "test_001",
"difficulty": 100
})
# Program that will timeout (infinite loop)
bad_program = """
numbers = list(map(int, input().split()))
sum_val = sum(numbers)
while True: # Infinite loop to cause timeout
pass
print(sum_val)
"""
report = self.synthesizer.evaluate(timeout_datapoint, bad_program)
# Validate results
self.assertTrue(report.compiles, "Should compile successfully")
self.assertTrue(report.executes, "Should attempt execution")
self.assertGreater(len(report.runtime_errors), 0, "Should have timeout errors")
self.assertTrue(any("timed out" in error.lower() for error in report.runtime_errors), "Should have timeout error")
self.assertIn(report.overall_status, ["failed", "partial"], "Overall status should be failed or partial")
print(f"✅ Timeout test passed - {len(report.runtime_errors)} runtime errors, {report.passed_tests}/{report.total_tests} tests passed")
def test_partial_failure(self):
"""Test evaluation with partial test failure."""
# Program that works for some inputs but not others (only works for even-length arrays)
bad_program = """
numbers = list(map(int, input().split()))
sum_val = sum(numbers)
if len(numbers) % 2 == 0:
print(sum_val)
else:
print(sum_val + 1) # Add 1 for odd-length arrays
"""
report = self.synthesizer.evaluate(self.simple_datapoint, bad_program)
# Validate results
self.assertTrue(report.compiles, "Should compile successfully")
self.assertTrue(report.executes, "Should execute successfully")
self.assertFalse(report.has_syntax_errors, "Should not have syntax errors")
self.assertFalse(report.has_runtime_errors, "Should not have runtime errors")
self.assertLess(report.passed_tests, report.total_tests, "Should have some failed tests")
self.assertGreater(report.passed_tests, 0, "Should have some passed tests")
self.assertEqual(report.overall_status, "partial", "Overall status should be partial")
self.assertGreater(report.success_rate, 0, "Success rate should be greater than 0")
self.assertLess(report.success_rate, 1.0, "Success rate should be less than 1.0")
print(f"✅ Partial failure test passed - {report.passed_tests}/{report.total_tests} tests passed ({report.success_rate:.2%})")
# Print test details
for i, test in enumerate(report.test_results):
status_icon = "✅" if test["status"] == "passed" else "❌"
print(f" Test {i+1}: {status_icon} Input: {test['input']} | Expected: {test['expected_output']} | Got: {test['actual_output']}")
def test_success(self):
"""Test evaluation with complete success."""
# Correct program
good_program = """
numbers = list(map(int, input().split()))
sum_val = sum(numbers)
print(sum_val)
"""
report = self.synthesizer.evaluate(self.simple_datapoint, good_program)
# Validate results
self.assertTrue(report.compiles, "Should compile successfully")
self.assertTrue(report.executes, "Should execute successfully")
self.assertFalse(report.has_syntax_errors, "Should not have syntax errors")
self.assertFalse(report.has_runtime_errors, "Should not have runtime errors")
self.assertEqual(report.passed_tests, report.total_tests, "Should pass all tests")
self.assertEqual(report.overall_status, "success", "Overall status should be success")
self.assertEqual(report.success_rate, 1.0, "Success rate should be 100%")
print(f"✅ Complete success test passed - {report.passed_tests}/{report.total_tests} tests passed ({report.success_rate:.2%})")
def test_name_error(self):
"""Test evaluation with NameError (undefined variable)."""
# Program with undefined variable
bad_program = """
numbers = list(map(int, input().split()))
sum_val = sum(numbers)
print(undefined_variable) # NameError: name 'undefined_variable' is not defined
"""
report = self.synthesizer.evaluate(self.simple_datapoint, bad_program)
# Validate results
self.assertTrue(report.compiles, "Should compile successfully (syntax is valid)")
self.assertTrue(report.executes, "Should attempt execution")
self.assertTrue(report.has_runtime_errors, "Should have runtime errors")
self.assertGreater(len(report.runtime_errors), 0, "Should have runtime errors")
self.assertIn(report.overall_status, ["failed", "partial"], "Overall status should be failed or partial")
print(f"✅ NameError test passed - {len(report.runtime_errors)} runtime errors, {report.passed_tests}/{report.total_tests} tests passed")
def test_type_error(self):
"""Test evaluation with TypeError."""
# Program with type error
bad_program = """
numbers = list(map(int, input().split()))
sum_val = sum(numbers)
result = sum_val + "invalid" # TypeError: unsupported operand type(s)
print(result)
"""
report = self.synthesizer.evaluate(self.simple_datapoint, bad_program)
# Validate results
self.assertTrue(report.compiles, "Should compile successfully (syntax is valid)")
self.assertTrue(report.executes, "Should attempt execution")
self.assertTrue(report.has_runtime_errors, "Should have runtime errors")
self.assertGreater(len(report.runtime_errors), 0, "Should have runtime errors")
self.assertIn(report.overall_status, ["failed", "partial"], "Overall status should be failed or partial")
print(f"✅ TypeError test passed - {len(report.runtime_errors)} runtime errors, {report.passed_tests}/{report.total_tests} tests passed")
def test_value_error(self):
"""Test evaluation with ValueError."""
# Program with value error (invalid input parsing)
bad_program = """
numbers = list(map(int, input().split()))
sum_val = sum(numbers)
result = int("not_a_number") # ValueError: invalid literal for int()
print(result)
"""
report = self.synthesizer.evaluate(self.simple_datapoint, bad_program)
# Validate results
self.assertTrue(report.compiles, "Should compile successfully (syntax is valid)")
self.assertTrue(report.executes, "Should attempt execution")
self.assertTrue(report.has_runtime_errors, "Should have runtime errors")
self.assertGreater(len(report.runtime_errors), 0, "Should have runtime errors")
self.assertIn(report.overall_status, ["failed", "partial"], "Overall status should be failed or partial")
print(f"✅ ValueError test passed - {len(report.runtime_errors)} runtime errors, {report.passed_tests}/{report.total_tests} tests passed")
if __name__ == "__main__":
loader = unittest.TestLoader()
suite = loader.loadTestsFromTestCase(TestPythonEvaluation)
runner = unittest.TextTestRunner(verbosity=2, stream=sys.stdout)
result = runner.run(suite)