-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtester.php
68 lines (56 loc) · 1.44 KB
/
tester.php
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
<?php
/**
* TODO
* Automaticaly tests files in most common languages (php, haskell, javascript, typescript, java)
* @author TENMAJKL
* @license MIT
*/
declare(strict_types=1);
const LANGUAGES = [
'php' => 'php %file < %data',
'hs' => 'ghc %file; ./%challenge/%name < %data',
'js' => 'node %file < %data',
// 'ts' => '', TODO
'java' => 'javac %file; java %challenge/%name.class',
];
function test(string $file, string $challenge, string $name, string $language): bool
{
if (!array_key_exists($language, LANGUAGES)) {
return true;
}
$sample = $challenge.'/sample.txt';
$result = $challenge.'/solution.txt';
if (!is_file($sample) || !is_file($result)) {
return true;
}
$command = str_replace([
'%file',
'%data',
'%name',
'%challenge'
], [
$file,
$sample,
$name,
$challenge
], LANGUAGES[$language]);
exec($command, $output);
$expected = file_get_contents($result);
return $expected === implode(PHP_EOL, $output).PHP_EOL;
}
function main($arguments): int
{
if (count($arguments) < 1) {
echo 'No arguments provided';
return -1;
}
foreach ($arguments as $file) {
if (preg_match('/^(\d+)\/(.+?)\.(\w+)$/', $file, $matches)) {
if (!test(...$matches)) {
return -1;
}
}
}
return 0;
}
exit(main(array_slice($argv, 1)));