-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.py
More file actions
425 lines (358 loc) · 14.1 KB
/
Copy pathtest_runner.py
File metadata and controls
425 lines (358 loc) · 14.1 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python3
"""
Nightpass Survivor Test Runner
Compile, test, compare, and grade your solution automatically.
Usage:
python test_runner.py # Test all cases
python test_runner.py --type type1 # Test only type1 cases
python test_runner.py --type type2 # Test only type2 cases
python test_runner.py --verbose # Show detailed diffs
python test_runner.py --benchmark # Benchmark mode: only measure times
"""
import os
import sys
import subprocess
import time
import argparse
import glob
import filecmp
from pathlib import Path
# Configuration
SRC_DIR = "src"
OUTPUT_DIR = "output"
TESTCASE_INPUT_DIR = "testcase_inputs"
TESTCASE_OUTPUT_DIR = "testcase_outputs"
MAIN_CLASS = "Main"
# Colors for cross-platform output
class Colors:
def __init__(self):
# Enable colors on Windows if supported
if os.name == 'nt':
try:
import colorama
colorama.init()
self.enabled = True
except ImportError:
self.enabled = False
else:
# Unix-like systems generally support ANSI colors
self.enabled = True
def __getattr__(self, name):
colors = {
'GREEN': '\033[0;32m',
'RED': '\033[0;31m',
'YELLOW': '\033[1;33m',
'BLUE': '\033[0;34m',
'NC': '\033[0m' # No Color
}
if self.enabled and name in colors:
return colors[name]
return ''
colors = Colors()
def log_info(message):
"""Print info message with blue color"""
print(f"{colors.BLUE}{message}{colors.NC}")
def log_success(message):
"""Print success message with green color"""
print(f"{colors.GREEN}{message}{colors.NC}")
def log_warning(message):
"""Print warning message with yellow color"""
print(f"{colors.YELLOW}{message}{colors.NC}")
def log_error(message):
"""Print error message with red color"""
print(f"{colors.RED}{message}{colors.NC}")
def ensure_directory(path):
"""Create directory if it doesn't exist"""
Path(path).mkdir(parents=True, exist_ok=True)
def compile_java():
"""Compile Java sources"""
log_info("Compiling Java sources...")
if not os.path.exists(SRC_DIR):
log_error(f"✗ Source directory '{SRC_DIR}' not found")
return False
# Find all .java files
java_files = glob.glob(os.path.join(SRC_DIR, "*.java"))
if not java_files:
log_error(f"✗ No Java files found in '{SRC_DIR}'")
return False
try:
# Compile all Java files
cmd = ["javac"] + [os.path.basename(f) for f in java_files]
result = subprocess.run(cmd, cwd=SRC_DIR, capture_output=True, text=True)
if result.returncode != 0:
log_error("✗ Compilation failed:")
if result.stderr:
print("Compilation errors:")
print(result.stderr)
if result.stdout:
print(result.stdout)
return False
log_success("✓ Compilation successful")
return True
except FileNotFoundError:
log_error("✗ javac not found. Please ensure Java SDK is installed and in PATH")
return False
except Exception as e:
log_error(f"✗ Compilation error: {e}")
return False
def get_test_files(test_type=None):
"""Get list of test input files, optionally filtered by type"""
if not os.path.exists(TESTCASE_INPUT_DIR):
return []
pattern = "*.txt"
if test_type:
pattern = f"{test_type}_*.txt"
input_files = glob.glob(os.path.join(TESTCASE_INPUT_DIR, pattern))
return sorted(input_files)
def run_single_test(input_file, verbose=False, benchmark=False):
"""Run a single test case and return result"""
basename = os.path.splitext(os.path.basename(input_file))[0]
expected_file = os.path.join(TESTCASE_OUTPUT_DIR, f"{basename}.txt")
actual_file = os.path.join(OUTPUT_DIR, f"{basename}.txt")
result = {
'name': basename,
'input_file': input_file,
'expected_file': expected_file,
'actual_file': actual_file,
'status': 'unknown',
'duration': 0,
'error_message': ''
}
# In benchmark mode, skip expected output check
if not benchmark and not os.path.exists(expected_file):
result['status'] = 'skip'
result['error_message'] = 'No expected output file'
return result
try:
# Run the Java program
cmd = ["java", MAIN_CLASS, f"../{input_file}", f"../{actual_file}"]
start_time = time.time()
process_result = subprocess.run(
cmd,
cwd=SRC_DIR,
capture_output=True,
text=True,
timeout=30 # 30 second timeout
)
end_time = time.time()
result['duration'] = end_time - start_time
# Check exit code
if process_result.returncode != 0:
result['status'] = 'runtime_error'
result['error_message'] = f"Exit code: {process_result.returncode}"
if process_result.stderr:
result['error_message'] += f"\nStderr: {process_result.stderr.strip()}"
return result
# Check if output file was created
if not os.path.exists(actual_file):
result['status'] = 'no_output'
result['error_message'] = 'No output file generated'
return result
# In benchmark mode, just mark as completed without comparison
if benchmark:
result['status'] = 'benchmark_complete'
else:
# Compare files
if filecmp.cmp(expected_file, actual_file, shallow=False):
result['status'] = 'pass'
else:
result['status'] = 'wrong_output'
# Generate diff for verbose mode
if verbose:
try:
with open(expected_file, 'r') as f:
expected_content = f.read()
with open(actual_file, 'r') as f:
actual_content = f.read()
import difflib
diff = list(difflib.unified_diff(
expected_content.splitlines(keepends=True),
actual_content.splitlines(keepends=True),
fromfile='expected',
tofile='actual',
n=3
))
result['diff'] = ''.join(diff[:20]) # Limit diff output
except Exception:
result['diff'] = "Could not generate diff"
return result
except subprocess.TimeoutExpired:
result['status'] = 'timeout'
result['error_message'] = 'Test timed out (30s limit)'
return result
except Exception as e:
result['status'] = 'error'
result['error_message'] = str(e)
return result
def run_tests(test_type=None, verbose=False, benchmark=False):
"""Run all tests and return summary"""
if benchmark:
log_info("Starting benchmark execution...")
log_warning("Benchmark mode: measuring execution times only, no output comparison")
else:
log_info("Starting test execution...")
if test_type:
log_warning(f"Filtering tests for type: {test_type}")
# Get test files
input_files = get_test_files(test_type)
if not input_files:
log_warning("⚠ No test cases found")
return {'total': 0, 'passed': 0, 'failed': 0, 'skipped': 0, 'completed': 0}
if benchmark:
log_info(f"Benchmarking {len(input_files)} test cases")
else:
log_info(f"Testing {len(input_files)} test cases")
if not verbose:
print("----------------------------------------")
else:
print("========================================")
# Ensure output directory exists
ensure_directory(OUTPUT_DIR)
# Run tests
results = []
passed = 0
failed = 0
skipped = 0
completed = 0 # For benchmark mode
for i, input_file in enumerate(input_files, 1):
result = run_single_test(input_file, verbose, benchmark)
results.append(result)
if verbose:
action = "Benchmarking" if benchmark else "Testing"
print(f"{colors.BLUE}[{i}] {action}: {result['name']}{colors.NC}")
else:
action = "Benchmarking" if benchmark else "Testing"
print(f"{action} {result['name']} ... ", end='', flush=True)
if result['status'] == 'skip':
if verbose:
log_warning(f"⚠ SKIP: {result['error_message']}")
print()
else:
log_warning(f"⚠ SKIP: {result['name']} ({result['error_message']})")
skipped += 1
elif result['status'] == 'benchmark_complete':
if verbose:
log_success(f"✓ COMPLETED (Time: {result['duration']:.3f}s)")
print()
else:
log_success(f"✓ {result['duration']:.3f}s")
completed += 1
elif result['status'] == 'pass':
if verbose:
log_success(f"✓ RESULT: PASS (Time: {result['duration']:.1f}s)")
print()
else:
log_success(f"✓ PASS ({result['duration']:.1f}s)")
passed += 1
else: # Any failure status
failed += 1
if verbose:
log_error(f"✗ RESULT: {result['status'].upper().replace('_', ' ')} (Time: {result['duration']:.1f}s)")
if result['error_message']:
print(f" {result['error_message']}")
if result.get('diff'):
log_warning("Expected vs Actual diff:")
print(result['diff'])
print()
else:
log_error(f"✗ {result['status'].upper().replace('_', ' ')} ({result['duration']:.1f}s)")
# Print summary
if verbose:
print("========================================")
summary_title = "Final Benchmark Summary:" if benchmark else "Final Test Summary:"
log_info(summary_title)
else:
print("----------------------------------------")
summary_title = "Benchmark Summary:" if benchmark else "Test Summary:"
log_info(summary_title)
total = len(input_files)
print(f" Total: {total}")
if benchmark:
print(f" {colors.GREEN}Completed: {completed}{colors.NC}")
print(f" {colors.RED}Failed: {failed}{colors.NC}")
if skipped > 0:
print(f" {colors.YELLOW}Skipped: {skipped}{colors.NC}")
# Calculate stats for benchmark mode
if completed > 0:
times = [r['duration'] for r in results if r['status'] == 'benchmark_complete']
if times:
avg_time = sum(times) / len(times)
min_time = min(times)
max_time = max(times)
print(f" Average time: {avg_time:.3f}s")
print(f" Min time: {min_time:.3f}s")
print(f" Max time: {max_time:.3f}s")
if failed == 0 and completed > 0:
log_success("🎉 All benchmarks completed!")
elif total == 0:
log_warning("⚠ No test cases found")
elif failed > 0:
log_error("❌ Some benchmarks failed")
else:
print(f" {colors.GREEN}Passed: {passed}{colors.NC}")
print(f" {colors.RED}Failed: {failed}{colors.NC}")
if skipped > 0:
print(f" {colors.YELLOW}Skipped: {skipped}{colors.NC}")
if failed == 0 and total > 0:
if verbose:
log_success("🎉 ALL TESTS PASSED!")
else:
log_success("🎉 All tests passed!")
elif total == 0:
log_warning("⚠ No test cases found")
else:
if verbose:
log_error("❌ SOME TESTS FAILED")
else:
log_error("❌ Some tests failed")
return {
'total': total,
'passed': passed,
'failed': failed,
'skipped': skipped,
'completed': completed,
'results': results
}
def clean_outputs():
"""Clean generated output files automatically"""
import glob
# Remove old .class files
for f in glob.glob(os.path.join(SRC_DIR, "*.class")):
try:
os.remove(f)
except:
pass
# Remove old output files
ensure_directory(OUTPUT_DIR)
for f in glob.glob(os.path.join(OUTPUT_DIR, "*.txt")):
try:
os.remove(f)
except:
pass
def main():
parser = argparse.ArgumentParser(description="Nightpass Survivor Test Runner")
parser.add_argument('--type', choices=['type1', 'type2'], help='Filter tests by type (type1 or type2)')
parser.add_argument('--verbose', '-v', action='store_true', help='Show detailed output and diffs')
parser.add_argument('--benchmark', '-b', action='store_true', help='Benchmark mode: only measure execution times, no output comparison')
args = parser.parse_args()
# Change to script directory
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
log_info("Nightpass Survivor Test Runner")
print("=" * 40)
# Always clean before testing
clean_outputs()
# Compile Java sources
if not compile_java():
sys.exit(1)
# Run tests and grade
summary = run_tests(args.type, args.verbose, args.benchmark)
# Exit with appropriate code
if args.benchmark:
# In benchmark mode, success if any tests completed
sys.exit(0 if summary['completed'] > 0 or summary['total'] == 0 else 1)
else:
# In normal mode, success if no failures and at least one test
sys.exit(0 if summary['failed'] == 0 and summary['total'] > 0 else 1)
if __name__ == '__main__':
main()