Created
April 18, 2014 11:46
-
-
Save tairov/11039717 to your computer and use it in GitHub Desktop.
run tests script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
function en() {echo join(' ', func_get_args()),PHP_EOL;} | |
function eln() {echo join(PHP_EOL, func_get_args()),PHP_EOL;} | |
function runCase($num, $exe, $input, $output, $inData, $expected) { | |
global $haveErrors; | |
if ($input != 'stdin') { | |
$tmpInput = $input; | |
} else { | |
$tmpInput = '_tmp' . time() . '.in'; | |
} | |
file_put_contents($tmpInput, $inData); | |
$cmd = $exe; | |
if ($input == 'stdin') { | |
$cmd .= ' < ' . $tmpInput; | |
} | |
//eln($cmd); | |
if ($output != 'stdout' && file_exists($output)) { | |
file_put_contents($output, ''); | |
} | |
$time = microtime(true); | |
exec($cmd, $tmpOut, $returnVal); | |
$time = microtime(true) - $time; | |
echo 'Case #' . $num . ': '; | |
if ($returnVal > 0) { | |
en('Warning! ReturnVal is ' . $returnVal . ' > 0.'); | |
} | |
if ($output != 'stdout') { | |
$tmpOut = trim(file_get_contents($output)); | |
} else { | |
$tmpOut = trim(join(PHP_EOL, $tmpOut)); | |
} | |
if ($tmpOut !== trim($expected)) { | |
en('WRONG ANSWER!' . PHP_EOL. 'Expected:', substr(trim($expected), 0, 50) . PHP_EOL . 'Return:', substr($tmpOut, 0, 100)); | |
en('case: ' . PHP_EOL . substr(trim($inData), 0, 100)); | |
$haveErrors++; | |
} else { | |
en('OK! Time:', sprintf("%0.4f", $time) ); | |
} | |
unlink($tmpInput); | |
} | |
$separator = '>>>'; | |
$resultSeprator = '==='; | |
$input = 'stdin'; | |
$output = 'stdout'; | |
$tests = 'tests.txt'; | |
$haveErrors = 0; | |
if ($argc < 2) { | |
eln('Usage: run.php ./a.out in input.txt out stdout tests NN.in'); | |
eln('Ex.: ' . 'g++ a.cpp && ./run ./a.out tests test_a.txt'); | |
exit(0); | |
} | |
for ($i = 1; $i < $argc; $i++) { | |
$val = $argv[$i]; | |
if ($i == 1) { | |
$exe = $val; | |
} else { | |
switch ($val) { | |
case 'in': | |
$i++; | |
$input = $argv[$i]; | |
break; | |
case 'out': | |
$i++; | |
$output = $argv[$i]; | |
break; | |
case 'tests': | |
$i++; | |
$tests = $argv[$i]; | |
break; | |
default: | |
eln('Unknown param: ', $val); | |
break; | |
} | |
} | |
} | |
$content = ''; | |
if (!file_exists($tests)) { | |
eln('Warning! No tests file available!'); | |
exit('asfd'); | |
} else { | |
$content = trim(file_get_contents($tests)); | |
} | |
$cases = explode($separator, $content); | |
if (!empty($cases)) { | |
$case = 1; | |
foreach ($cases as $c) { | |
list($data, $result) = explode($resultSeprator, trim($c)); | |
runCase($case++ , $exe, $input, $output, $data, $result); | |
} | |
} | |
if ($haveErrors) { | |
en('We have errors:', $haveErrors); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment