-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.ps1
More file actions
266 lines (224 loc) · 9.99 KB
/
test_runner.ps1
File metadata and controls
266 lines (224 loc) · 9.99 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
# PowerShell Test Runner Script with Requirements Enforcement
# Usage: .\test_runner.ps1 [solution_file] [test_cases_dir]
# Example: .\test_runner.ps1 solution.cpp test_cases
param(
[string]$SolutionFile = "solution.cpp",
[string]$TestCasesDir = "test_cases",
[string]$Compiler = "C:\MinGW\bin\g++.exe",
[string]$RequirementsFile = "requirements.json"
)
# Colors for output
$Red = "Red"
$Green = "Green"
$Yellow = "Yellow"
$Blue = "Cyan"
# Load requirements if file exists
$TimeLimit = 4
$MemoryLimit = 512
if (Test-Path $RequirementsFile) {
try {
$Requirements = Get-Content $RequirementsFile | ConvertFrom-Json
$TimeLimit = $Requirements.time
$MemoryLimit = $Requirements.space
Write-Host "Loaded requirements: ${TimeLimit}s time, ${MemoryLimit}MB memory" -ForegroundColor $Blue
} catch {
Write-Host "Warning: Could not parse requirements.json, using defaults" -ForegroundColor $Yellow
}
}
Write-Host "=== C++ Solution Test Runner ===" -ForegroundColor $Blue
Write-Host "Solution: $SolutionFile" -ForegroundColor $Blue
Write-Host "Test Cases Directory: $TestCasesDir" -ForegroundColor $Blue
Write-Host "Time Limit: ${TimeLimit}s, Memory Limit: ${MemoryLimit}MB" -ForegroundColor $Blue
Write-Host ""
# Check if solution file exists
if (!(Test-Path $SolutionFile)) {
Write-Host "Error: Solution file '$SolutionFile' not found!" -ForegroundColor $Red
exit 1
}
# Check if test cases directory exists
if (!(Test-Path $TestCasesDir)) {
Write-Host "Error: Test cases directory '$TestCasesDir' not found!" -ForegroundColor $Red
exit 1
}
# Extract filename without extension for executable
$BaseName = [System.IO.Path]::GetFileNameWithoutExtension($SolutionFile)
$ExeFile = "$BaseName.exe"
# Compile the solution
Write-Host "Compiling $SolutionFile..." -ForegroundColor $Yellow
$CompileResult = & $Compiler -Wall -Wextra -g3 $SolutionFile -o $ExeFile 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "Compilation failed!" -ForegroundColor $Red
Write-Host $CompileResult -ForegroundColor $Red
exit 1
}
Write-Host "Compilation successful!" -ForegroundColor $Green
Write-Host ""
# Find all input test files
$InputFiles = Get-ChildItem -Path $TestCasesDir -Filter "*.in" | Sort-Object Name
if ($InputFiles.Count -eq 0) {
Write-Host "No test cases found in $TestCasesDir!" -ForegroundColor $Red
exit 1
}
$PassedTests = 0
$TotalTests = $InputFiles.Count
$FailedTests = @()
Write-Host "Running $TotalTests test cases..." -ForegroundColor $Blue
Write-Host ""
foreach ($InputFile in $InputFiles) {
$TestName = [System.IO.Path]::GetFileNameWithoutExtension($InputFile.Name)
$OutputFile = Join-Path $TestCasesDir "$TestName.out"
Write-Host "Test $TestName... " -NoNewline
# Check if expected output file exists
if (!(Test-Path $OutputFile)) {
Write-Host "SKIP (no expected output)" -ForegroundColor $Yellow
continue
}
# Run the solution with input and enforce time limit
try {
# Read input file content
$InputContent = Get-Content $InputFile.FullName -Raw
# Run the solution with input using proper PowerShell method
$StartTime = Get-Date
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
$ProcessInfo.FileName = ".\$ExeFile"
$ProcessInfo.RedirectStandardInput = $true
$ProcessInfo.RedirectStandardOutput = $true
$ProcessInfo.RedirectStandardError = $true
$ProcessInfo.UseShellExecute = $false
$ProcessInfo.CreateNoWindow = $true
$Process = New-Object System.Diagnostics.Process
$Process.StartInfo = $ProcessInfo
$Process.Start() | Out-Null
# Send input to the process
$Process.StandardInput.Write($InputContent)
$Process.StandardInput.Close()
# Wait with timeout
$TimeoutMs = $TimeLimit * 1000
$TimedOut = $false
if (-not $Process.WaitForExit($TimeoutMs)) {
$Process.Kill()
$TimedOut = $true
}
$EndTime = Get-Date
$ExecutionTime = ($EndTime - $StartTime).TotalMilliseconds
$ActualOutput = $Process.StandardOutput.ReadToEnd()
$ErrorOutput = $Process.StandardError.ReadToEnd()
# Get memory usage (approximate from process)
$MemoryUsageMB = 0
try {
if (-not $TimedOut -and $Process.HasExited -eq $false) {
$MemoryUsageMB = [Math]::Round($Process.WorkingSet64 / 1MB, 2)
}
} catch {
# Memory info not available
}
if ($TimedOut) {
Write-Host "TIME LIMIT EXCEEDED" -ForegroundColor $Red -NoNewline
Write-Host " (${ExecutionTime}ms)" -ForegroundColor $Blue
if ($MemoryUsageMB -gt 0) {
Write-Host " Memory used: ${MemoryUsageMB}MB" -ForegroundColor $Blue
}
$FailedTests += @{Name=$TestName; Reason="Time Limit Exceeded"; Time=$ExecutionTime}
continue
}
if ($Process.ExitCode -ne 0) {
Write-Host "RUNTIME ERROR" -ForegroundColor $Red -NoNewline
Write-Host " (${ExecutionTime}ms)" -ForegroundColor $Blue
if ($MemoryUsageMB -gt 0) {
Write-Host " Memory used: ${MemoryUsageMB}MB" -ForegroundColor $Blue
}
Write-Host " Error output: $ErrorOutput" -ForegroundColor $Red
$FailedTests += @{Name=$TestName; Reason="Runtime Error"; Output=$ErrorOutput; Time=$ExecutionTime}
continue
}
# Check memory limit
if ($MemoryUsageMB -gt $MemoryLimit) {
Write-Host "MEMORY LIMIT EXCEEDED" -ForegroundColor $Red -NoNewline
Write-Host " (${ExecutionTime}ms, ${MemoryUsageMB}MB)" -ForegroundColor $Blue
$FailedTests += @{Name=$TestName; Reason="Memory Limit Exceeded"; Time=$ExecutionTime; Memory=$MemoryUsageMB}
continue
}
# Read expected output
$ExpectedOutput = Get-Content $OutputFile -Raw
if ($ExpectedOutput -eq $null) { $ExpectedOutput = "" }
# Normalize whitespace and compare
$ActualOutput = $ActualOutput.Trim() -replace "`r`n", "`n"
$ExpectedOutput = $ExpectedOutput.Trim() -replace "`r`n", "`n"
# Handle empty output as "0" (valid answer when Odin cannot win)
if ([string]::IsNullOrWhiteSpace($ActualOutput)) {
$ActualOutput = "0"
}
# Check if actual output matches any of the expected valid answers
$IsValidAnswer = $false
# Split expected output by lines (for multiple test cases)
$ExpectedLines = if ([string]::IsNullOrWhiteSpace($ExpectedOutput)) { @("") } else { $ExpectedOutput -split "`n" }
$ActualLines = if ([string]::IsNullOrWhiteSpace($ActualOutput)) { @("0") } else { $ActualOutput -split "`n" }
if ($ExpectedLines.Count -eq $ActualLines.Count) {
$AllLinesValid = $true
for ($i = 0; $i -lt $ExpectedLines.Count; $i++) {
$ExpectedLine = if ([string]::IsNullOrWhiteSpace($ExpectedLines[$i])) { "0" } else { $ExpectedLines[$i].ToString().Trim() }
$ExpectedAnswers = @($ExpectedLine -split "\s+" | Where-Object { $_ -ne "" })
$ActualAnswer = $ActualLines[$i].ToString().Trim()
# If no expected answers, treat as "0"
if ($ExpectedAnswers.Count -eq 0) {
$ExpectedAnswers = @("0")
}
# Check if actual answer is in the list of valid expected answers
$LineValid = $false
foreach ($ValidAnswer in $ExpectedAnswers) {
$ValidAnswerStr = $ValidAnswer.ToString().Trim()
if ($ActualAnswer -eq $ValidAnswerStr) {
$LineValid = $true
break
}
}
if (-not $LineValid) {
$AllLinesValid = $false
break
}
}
$IsValidAnswer = $AllLinesValid
}
if ($IsValidAnswer) {
Write-Host "PASS" -ForegroundColor $Green -NoNewline
Write-Host " (${ExecutionTime}ms)" -ForegroundColor $Blue
$PassedTests++
} else {
Write-Host "FAIL" -ForegroundColor $Red
Write-Host " Expected (any of): '$ExpectedOutput'" -ForegroundColor $Red
Write-Host " Actual: '$ActualOutput'" -ForegroundColor $Red
$FailedTests += @{Name=$TestName; Expected=$ExpectedOutput; Actual=$ActualOutput}
}
}
catch {
Write-Host "ERROR" -ForegroundColor $Red
Write-Host "Exception: $($_.Exception.Message)" -ForegroundColor $Red
$FailedTests += @{Name=$TestName; Reason="Exception"; Error=$_.Exception.Message}
}
}
Write-Host ""
Write-Host "=== Test Results ===" -ForegroundColor $Blue
Write-Host "Passed: $PassedTests/$TotalTests" -ForegroundColor $(if ($PassedTests -eq $TotalTests) { $Green } else { $Yellow })
if ($FailedTests.Count -gt 0) {
Write-Host "Failed Tests:" -ForegroundColor $Red
foreach ($Failed in $FailedTests) {
Write-Host " - $($Failed.Name): $($Failed.Reason)" -ForegroundColor $Red
}
}
# Cleanup
try {
if (Test-Path $ExeFile) {
Start-Sleep -Milliseconds 100 # Brief pause to ensure process cleanup
Remove-Item $ExeFile -Force
}
} catch {
# Ignore cleanup errors
}
# Exit with appropriate code
if ($PassedTests -eq $TotalTests) {
Write-Host "All tests passed!" -ForegroundColor $Green
exit 0
} else {
Write-Host "Some tests failed!" -ForegroundColor $Red
exit 1
}