ir/ir-test.php

169 lines
4.3 KiB
PHP
Raw Normal View History

2022-04-05 23:19:23 +02:00
<?php
2022-05-25 16:38:22 +02:00
function parse_test($test, &$name, &$code, &$expect, &$args, &$target) {
2022-04-05 23:19:23 +02:00
$text = @file_get_contents($test);
if (!$text) {
return false;
}
$p1 = strpos($text, '--TEST--');
2022-05-25 16:38:22 +02:00
$p_args = strpos($text, '--ARGS--');
$p_target = strpos($text, '--TARGET--');
2022-04-05 23:19:23 +02:00
$p3 = strpos($text, '--CODE--');
$p4 = strpos($text, '--EXPECT--');
if ($p1 === false || $p3 === false || $p4 === false || $p1 > $p3 || $p3 > $p4) {
return false;
}
$code = trim(substr($text, $p3 + strlen('--CODE--'), $p4 - $p3 - strlen('--CODE--')));
$expect = trim(substr($text, $p4 + strlen('--EXPECT--')));
$expect = str_replace("\r", "", $expect);
2022-05-25 16:38:22 +02:00
$end_name = $p3;
$args = "--save";
$target = null;
if ($p_args !== false ) {
$end = ($p_target !== false && $p_target > $p_args) ? $p_target : $p3;
if ($p_args < $p1 || $p_args > $end) {
return false;
}
if ($p_args < $end_name) {
$end_name = $p_args;
}
$args = trim(substr($text, $p_args + strlen('--ARGS--'), $end - $p_args - strlen('--ARGS--')));
}
if ($p_target !== false ) {
$end = ($p_args !== false && $p_args > $p_target) ? $p_args : $p3;
if ($p_target < $p1 || $p_target > $end) {
return false;
}
if ($p_target < $end_name) {
$end_name = $p_target;
}
$target = trim(substr($text, $p_target + strlen('--TARGET--'), $end - $p_target - strlen('--TARGET--')));
}
$name = trim(substr($text, $p1 + strlen('--TEST--'), $end_name - $p1 - strlen('--TEST--')));
2022-04-05 23:19:23 +02:00
return true;
}
function run_test($build_dir, $test, $name, $code, $expect, $args) {
2022-04-05 23:19:23 +02:00
$base = substr($test, 0, -4);
$input = $base . ".ir";
$output = $base . ".out";
@unlink($input);
@unlink($output);
@unlink("$base.exp");
@unlink("$base.diff");
if (!@file_put_contents($input, $code)) {
return false;
}
@system("$build_dir/ir $input $args >$output 2>&1");
// if (@system("$build_dir/ir $input $args 2>&1 >$output") != 0) {
2022-04-05 23:19:23 +02:00
// return false;
// }
$out = @file_get_contents($output);
if ($out === false) {
return false;
}
$out = trim($out);
$out = str_replace("\r", "", $out);
if ($out !== $expect) {
if (!@file_put_contents("$base.exp", "$expect\n")) {
return false;
}
2022-05-13 11:14:21 +02:00
if (@system("diff -u $base.exp $output > $base.diff") != 0) {
2022-04-05 23:19:23 +02:00
return false;
}
return false;
}
@unlink($input);
@unlink($output);
return true;
}
2022-04-07 22:24:29 +02:00
function find_tests_in_dir($dir, &$tests) {
2022-04-07 22:31:03 +02:00
$d = opendir($dir);
if ($d !== false) {
while (($name = readdir($d)) !== false) {
if ($name === '.' || $name === '..') continue;
$fn = "$dir/$name";
if (is_dir($fn)) {
find_tests_in_dir($fn, $tests);
} else if (substr($name, -4) === '.irt') {
$tests[] = $fn;
}
}
closedir($d);
}
2022-04-07 22:24:29 +02:00
}
function find_tests($dir) {
$tests = [];
find_tests_in_dir($dir, $tests);
sort($tests);
2022-04-07 22:24:29 +02:00
return $tests;
}
2022-04-05 23:19:23 +02:00
function run_tests() {
$build_dir = getenv("BUILD_DIR") ?? ".";
$src_dir = getenv("SRC_DIR") ?? ".";
2022-05-25 16:38:22 +02:00
$skiped = 0;
$target = @system("$build_dir/ir --target");
$tests = find_tests("$src_dir/tests");
2022-04-05 23:19:23 +02:00
$bad = array();
$failed = array();
2022-06-01 17:16:32 +02:00
$total = count($tests);
$count = 0;
2022-04-05 23:19:23 +02:00
foreach($tests as $test) {
2022-06-01 17:16:32 +02:00
$count++;
2022-05-25 16:38:22 +02:00
if (parse_test($test, $name, $code, $expect, $opt, $test_target)) {
if ($test_target !== null && $target != $test_target) {
2022-06-01 17:16:32 +02:00
echo "\r\e[1;33mSKIP\e[0m: $name [$test]\n";
2022-05-25 16:38:22 +02:00
$skiped++;
continue;
2022-06-01 17:16:32 +02:00
}
$str = "TEST: $count/$total $name [$test]\r";
$len = strlen($str);
echo $str;
flush();
$ret = run_test($build_dir, $test, $name, $code, $expect, $opt);
2022-06-01 17:16:32 +02:00
echo str_repeat(" ", $len);
if ($ret) {
echo "\r\e[1;32mPASS\e[0m: $name [$test]\n";
} else {
echo "\r\e[1;31mFAIL\e[0m: $name [$test]\n";
2022-04-05 23:19:23 +02:00
$failed[$test] = $name;
}
} else {
2022-06-01 17:16:32 +02:00
echo "\r\e[1;31mBROK\e[0m: $name [$test]\n";
2022-04-05 23:19:23 +02:00
$bad[] = $test;
}
}
echo "-------------------------------\n";
echo "Test Summary\n";
echo "-------------------------------\n";
if (count($bad) > 0) {
echo "Bad tests: " . count($bad) . "\n";
echo "-------------------------------\n";
foreach ($bad as $test) {
echo "$test\n";
}
echo "-------------------------------\n";
}
echo "Total: " . count($tests) . "\n";
2022-05-25 16:38:22 +02:00
echo "Passed: " . (count($tests) - count($failed) - $skiped) . "\n";
2022-04-05 23:19:23 +02:00
echo "Failed: " . count($failed) . "\n";
2022-05-25 16:38:22 +02:00
echo "Skiped: " . $skiped . "\n";
2022-04-05 23:19:23 +02:00
if (count($failed) > 0) {
echo "-------------------------------\n";
foreach ($failed as $test => $name) {
echo "$name [$test]\n";
}
}
echo "-------------------------------\n";
}
run_tests();