ir/ir-test.php

232 lines
6.1 KiB
PHP
Raw Permalink Normal View History

2022-04-05 23:19:23 +02:00
<?php
2022-11-08 09:32:46 +01:00
/*
* IR - Lightweight JIT Compilation Framework
* (Test driver)
* Copyright (C) 2022 Zend by Perforce.
* Authors: Dmitry Stogov <dmitry@php.net>
*/
2022-04-05 23:19:23 +02:00
function parse_test($test, &$name, &$code, &$expect, &$args, &$target, &$xfail) {
$sections = array();
2022-04-05 23:19:23 +02:00
$text = @file_get_contents($test);
if (!$text || !preg_match_all("/^--[A-Z]+--$/m", $text, $r, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
2022-04-05 23:19:23 +02:00
return false;
}
foreach($r as $sect) {
if (isset($sect_name)) {
$sections[$sect_name] = trim(substr($text, $sect_offset, $sect[0][1] - $sect_offset));
2022-05-25 16:38:22 +02:00
}
$sect_name = $sect[0][0];
$sect_offset = $sect[0][1] + strlen($sect_name);
2022-05-25 16:38:22 +02:00
}
if (isset($sect_name)) {
$sections[$sect_name] = trim(substr($text, $sect_offset));
2022-05-25 16:38:22 +02:00
}
if (!isset($sections['--TEST--']) || !isset($sections['--CODE--']) || !isset($sections['--EXPECT--'])) {
return false;
}
$name = $sections['--TEST--'];
$code = $sections['--CODE--'];
$expect = $sections['--EXPECT--'];
$args = isset($sections['--ARGS--']) ? $sections['--ARGS--'] : "--save";
$target = isset($sections['--TARGET--']) ? $sections['--TARGET--'] : null;
$xfail = isset($sections['--XFAIL--']) ? $sections['--XFAIL--'] : null;
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;
}
if (PHP_OS_FAMILY != "Windows") {
$cmd = "$build_dir/ir $input $args >$output 2>&1";
} else {
$cmd = "$build_dir\\ir $input $args --no-abort-fault >$output 2>&1";
}
$ret = @system($cmd, $result_code);
if ($ret === false) {
return false;
}
if ($result_code) {
}
2022-04-05 23:19:23 +02:00
$out = @file_get_contents($output);
if ($out === false) {
return false;
}
if ($result_code) {
$out = "\nExit Code = $result_code\n";
file_put_contents($output, $out);
}
2022-04-05 23:19:23 +02:00
$out = trim($out);
$out = str_replace("\r", "", $out);
if ($out !== $expect) {
if (!@file_put_contents("$base.exp", "$expect\n")) {
return false;
}
if (PHP_OS_FAMILY != "Windows") {
$cmd = "diff -u $base.exp $output > $base.diff";
} else {
/* Diff somehow resets terminal and breaks "cooring" */
$cmd = "diff --strip-trailing-cr -u $base.exp $output > $base.diff 2>&1";
}
if (@system($cmd) != 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() {
global $show_diff, $colorize;
$build_dir = getenv("BUILD_DIR") ?? ".";
$src_dir = getenv("SRC_DIR") ?? ".";
2022-05-25 16:38:22 +02:00
$skiped = 0;
if (PHP_OS_FAMILY != "Windows") {
$cmd = "$build_dir/ir --target";
} else {
$cmd = "$build_dir\\ir --target";
}
$target = @system($cmd);
if ($target === false) {
echo "Cannot run '$cmd'\n";
return 1;
}
$tests = find_tests("$src_dir/tests");
2022-04-05 23:19:23 +02:00
$bad = array();
$failed = array();
$xfailed = 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++;
if (parse_test($test, $name, $code, $expect, $opt, $test_target, $xfail)) {
2022-05-25 16:38:22 +02:00
if ($test_target !== null && $target != $test_target) {
if ($colorize) {
echo "\r\e[1;33mSKIP\e[0m: $name [$test]\n";
} else {
echo "\rSKIP: $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);
echo str_repeat(" ", $len);
2022-06-01 17:16:32 +02:00
if ($ret) {
if ($colorize) {
echo "\r\e[1;32mPASS\e[0m: $name [$test]\n";
} else {
echo "\rPASS: $name [$test]\n";
}
} else if (isset($xfail)) {
if ($colorize) {
echo "\r\e[1;31mXFAIL\e[0m: $name [$test] XFAIL REASON: $xfail\n";
} else {
echo "\rXFAIL: $name [$test] XFAIL REASON: $xfail\n";
}
$xfailed[$test] = array($name, $xfail);
2022-06-01 17:16:32 +02:00
} else {
if ($colorize) {
echo "\r\e[1;31mFAIL\e[0m: $name [$test]\n";
} else {
echo "\rFAIL: $name [$test]\n";
}
2022-04-05 23:19:23 +02:00
$failed[$test] = $name;
2022-11-19 16:26:16 +01:00
if ($show_diff) {
$base = substr($test, 0, -4);
echo file_get_contents("$base.diff");
}
2022-04-05 23:19:23 +02:00
}
} else {
if ($colorize) {
echo "\r\e[1;31mBROK\e[0m: $name [$test]\n";
} else {
echo "\rBROK: $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";
echo "Expected fail: " . count($xfailed) . "\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";
if (count($xfailed) > 0) {
echo "-------------------------------\n";
echo "EXPECTED FAILED TESTS\n";
echo "-------------------------------\n";
foreach ($xfailed as $test => list($name, $xfail)) {
echo "$name [$test] XFAIL REASON: $xfail\n";
}
}
2022-04-05 23:19:23 +02:00
if (count($failed) > 0) {
echo "-------------------------------\n";
echo "FAILED TESTS\n";
2022-04-05 23:19:23 +02:00
echo "-------------------------------\n";
foreach ($failed as $test => $name) {
echo "$name [$test]\n";
}
}
echo "-------------------------------\n";
2022-11-19 16:26:16 +01:00
return count($failed) > 0 ? 1 : 0;
2022-04-05 23:19:23 +02:00
}
$colorize = true;
if (function_exists('sapi_windows_vt100_support') && !sapi_windows_vt100_support(STDOUT, true)) {
$colorize = false;
}
if (in_array('--no-color', $argv, true)) {
$colorize = false;
}
2022-11-19 16:26:16 +01:00
$show_diff = in_array('--show-diff', $argv, true);
exit(run_tests());