添加更多测试,php std 标准库测试,支持 list 语法

pull/1/head
韩天峰 8 months ago
parent a54b04118d
commit 0033a243d0
  1. 2
      examples/list.php
  2. 24
      src/Php/Translator.php
  3. 25
      src/cpp/main.cc
  4. 14
      tests/aot/list.phpt
  5. 26
      tests/std/directory/directory_constants.phpt
  6. 42
      tests/std/serialize/002.phpt
  7. 33
      tests/std/time/001.phpt
  8. 34
      tests/std/time/bug38524.phpt
  9. 19
      tests/std/time/bug60222.phpt
  10. 33
      tests/std/time/idate.phpt
  11. 23
      tests/std/time/idate_iso.phpt
  12. 95
      tests/std/time/strptime_basic.phpt
  13. 25
      tests/std/time/strptime_error.phpt
  14. 69
      tests/std/time/strptime_parts.phpt

@ -0,0 +1,2 @@
<?php
list($micro, $time) = explode(" ", microtime());

@ -881,6 +881,30 @@ class Translator extends \PhpAot\Core\Translator
private function parseFinallyAssign($left, $right): string
{
if ($left instanceof Node\Expr\List_) {
$items = $left->items;
$code = '{';
$this->indentLevel++;
$tmpVar = $this->genTmpVarName();
$this->addLocalVar($tmpVar, self::TYPE_VAR);
$code .= $this->getIndent() . $tmpVar . ' = ' . $this->parseExpr($right) . '; ';
foreach ($items as $k => $item) {
if (!$item) {
continue;
}
if ($item instanceof Node\Expr\ArrayItem) {
$var = $this->parseIdentifier($item->value);
if (!$this->hasVar($var)) {
$this->addLocalVar($var, self::TYPE_VAR);
}
$code .= "$var = $tmpVar.offsetGet($k); ";
} else {
abort($item);
}
}
$this->indentLevel--;
return $code . '}';
}
$var = $this->parseIdentifier($left);
$expr = $this->parseExpr($right);

@ -4,6 +4,9 @@
#endif
#include <phpx.h>
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_void, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()
void php_main();
extern php::Var argc;
@ -13,21 +16,18 @@ extern void php_init_constant_vars();
extern void php_init_global_vars();
extern void php_unset_global_vars();
static void throw_exception(zend_object *ex) {
zend_bailout();
static ZEND_FUNCTION(main) {
php_main();
}
static const zend_function_entry ext_functions[] = {
ZEND_FE(main, arginfo_void)
ZEND_FE_END
};
int main(int cpp_argc, char **cpp_argv) {
php_embed_init(cpp_argc, cpp_argv);
zend_execute_data fake_execute_data;
memset(&fake_execute_data, 0, sizeof(zend_execute_data));
zend_function fake_func {};
fake_func.type = ZEND_INTERNAL_FUNCTION;
fake_execute_data.func = &fake_func;
EG(current_execute_data) = &fake_execute_data;
zend_throw_exception_hook = throw_exception;
zend_register_functions(nullptr, ext_functions, nullptr, 0);
int rc = 0;
#if PPROF_ON
@ -36,7 +36,7 @@ int main(int cpp_argc, char **cpp_argv) {
zend_first_try {
php_init_global_vars();
php_init_constant_vars();
php_main();
php::eval("main();");
}
zend_catch {
rc = EG(exit_status);
@ -51,5 +51,6 @@ int main(int cpp_argc, char **cpp_argv) {
php_unset_global_vars();
php::request_shutdown();
php_embed_shutdown();
return rc;
}

@ -0,0 +1,14 @@
--TEST--
strlen
--FILE--
<?php
list($micro, $time) = explode(" ", microtime());
var_dump($micro, $time);
list(, $time) = explode(" ", microtime());
var_dump($time);
?>
--EXPECTF--
string(%d) "%s"
string(%d) "%s"
string(%d) "%s"

@ -0,0 +1,26 @@
--TEST--
Test that the Directory extension constants are correctly defined.
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) == 'WIN') {
die('skip.. Not valid for Windows');
}
?>
--FILE--
<?php
echo DIRECTORY_SEPARATOR;
echo "\n";
echo PATH_SEPARATOR;
echo "\n";
echo "done";
?>
--EXPECT--
/
:
done

@ -0,0 +1,42 @@
--TEST--
Bug #25378 (unserialize() crashes with invalid data)
--FILE--
<?php
var_dump(unserialize('b:0;'));
var_dump(unserialize('b:1;'));
var_dump(unserialize('i:823;'));
var_dump(unserialize('s:0:"";'));
var_dump(unserialize('s:3:"foo";'));
var_dump(unserialize('a:1:{i:0;s:2:"12";}'));
var_dump(unserialize('a:2:{i:0;a:0:{}i:1;a:0:{}}'));
var_dump(unserialize('a:3:{i:0;s:3:"foo";i:1;s:3:"bar";i:2;s:3:"baz";}'));
var_dump(unserialize('O:8:"stdClass":0:{}'));
?>
--EXPECTF--
bool(false)
bool(true)
int(823)
string(0) ""
string(3) "foo"
array(1) {
[0]=>
string(2) "12"
}
array(2) {
[0]=>
array(0) {
}
[1]=>
array(0) {
}
}
array(3) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
}
object(stdClass)#%d (0) {
}

@ -0,0 +1,33 @@
--TEST--
microtime() function
--SKIPIF--
<?php
if (!function_exists('microtime')) die('skip microtime() not available');
?>
--FILE--
<?php
$passed = 0;
$failed = 0;
$last_m = 0;
$last_t = 0;
$result = '';
set_time_limit(0);
for ($i=1;$i<=100000;$i++) {
list($micro,$time)=explode(" ",microtime());
if ($time > $last_t || ($time == $last_t && $micro >= $last_m)) {
$passed++;
} else if ($failed++ <=10) {
$result .= sprintf('%06d', $i).": $time $micro < $last_t $last_m\n";
}
$last_m = $micro;
$last_t = $time;
}
echo "Passed: $passed\n";
echo "Failed: $failed\n";
echo $result;
?>
--EXPECT--
Passed: 100000
Failed: 0

@ -0,0 +1,34 @@
--TEST--
Bug #38524 (strptime() does not initialize the internal date storage structure)
--SKIPIF--
<?php
if (!function_exists('strptime')) echo "SKIP";
if (str_contains(PHP_OS, 'FreeBSD')) {
die("skip strptime() behaves differently on FreeBSD");
}
?>
--FILE--
<?php
var_dump(strptime('2006-08-20', '%Y-%m-%d'));
?>
--EXPECTF--
array(9) {
["tm_sec"]=>
int(0)
["tm_min"]=>
int(0)
["tm_hour"]=>
int(0)
["tm_mday"]=>
int(20)
["tm_mon"]=>
int(7)
["tm_year"]=>
int(106)
["tm_wday"]=>
int(0)
["tm_yday"]=>
int(%d)
["unparsed"]=>
string(0) ""
}

@ -0,0 +1,19 @@
--TEST--
Bug #60222 (time_nanosleep() does validate input params)
--FILE--
<?php
try {
time_nanosleep(-1, 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
try {
time_nanosleep(0, -1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
?>
--EXPECT--
time_nanosleep(): Argument #1 ($seconds) must be greater than or equal to 0
time_nanosleep(): Argument #2 ($nanoseconds) must be greater than or equal to 0

@ -0,0 +1,33 @@
--TEST--
idate() function
--FILE--
<?php
date_default_timezone_set('GMT0');
$tmp = "UYozymndjHGhgistwNLBIW";
for($a = 0;$a < strlen($tmp); $a++){
echo $tmp[$a], ': ', idate($tmp[$a], 1043324459)."\n";
}
?>
--EXPECT--
U: 1043324459
Y: 2003
o: 2003
z: 22
y: 3
m: 1
n: 1
d: 23
j: 23
H: 12
G: 12
h: 12
g: 12
i: 20
s: 59
t: 31
w: 4
N: 4
L: 0
B: 556
I: 0
W: 4

@ -0,0 +1,23 @@
--TEST--
idate() function: ISO Day-of-Week and ISO Year
--FILE--
<?php
date_default_timezone_set('UTC');
$tmp = "UwNW";
for ($a = 0; $a < strlen($tmp); $a++){
echo $tmp[$a], ': ', idate($tmp[$a], 1041808859), "\n";
}
$tmp = "UYoW";
for ($a = 0; $a < strlen($tmp); $a++){
echo $tmp[$a], ': ', idate($tmp[$a], 1072912859), "\n";
}
?>
--EXPECT--
U: 1041808859
w: 0
N: 7
W: 1
U: 1072912859
Y: 2003
o: 2004
W: 1

@ -0,0 +1,95 @@
--TEST--
Test strptime() function : basic functionality
--SKIPIF--
<?php
if (!function_exists('strptime')) {
die("skip - strptime() function not available in this build");
}
if (PHP_OS_FAMILY == 'Darwin' || PHP_OS_FAMILY == 'BSD') {
die("skip strptime() behaves differently on Darwin/BSD");
}
if (!@strftime('%Z')) die('skip strftime does not support %Z');
?>
--FILE--
<?php
$orig = setlocale(LC_ALL, 'C');
date_default_timezone_set("GMT");
echo "*** Testing strptime() : basic functionality ***\n";
$input = "10:00:00 AM July 2 1963";
$tstamp = strtotime($input);
$str = strftime("%r %B%e %Y %Z", $tstamp);
var_dump(strptime($str, '%H:%M:%S %p %B %d %Y'));
$str = strftime("%T %D", $tstamp);
var_dump(strptime($str, '%H:%M:%S %m/%d/%y'));
$str = strftime("%A %B %e %R", $tstamp);
var_dump(strptime($str, '%A %B %e %R'));
setlocale(LC_ALL, $orig);
?>
--EXPECTF--
*** Testing strptime() : basic functionality ***
array(9) {
["tm_sec"]=>
int(0)
["tm_min"]=>
int(0)
["tm_hour"]=>
int(10)
["tm_mday"]=>
int(2)
["tm_mon"]=>
int(6)
["tm_year"]=>
int(63)
["tm_wday"]=>
int(2)
["tm_yday"]=>
int(182)
["unparsed"]=>
string(4) " GMT"
}
array(9) {
["tm_sec"]=>
int(0)
["tm_min"]=>
int(0)
["tm_hour"]=>
int(10)
["tm_mday"]=>
int(2)
["tm_mon"]=>
int(6)
["tm_year"]=>
int(163)
["tm_wday"]=>
int(1)
["tm_yday"]=>
int(182)
["unparsed"]=>
string(0) ""
}
array(9) {
["tm_sec"]=>
int(0)
["tm_min"]=>
int(0)
["tm_hour"]=>
int(10)
["tm_mday"]=>
int(2)
["tm_mon"]=>
int(6)
["tm_year"]=>
int(0)
["tm_wday"]=>
int(2)
["tm_yday"]=>
int(182)
["unparsed"]=>
string(0) ""
}

@ -0,0 +1,25 @@
--TEST--
Test localtime() function : error conditions
--SKIPIF--
<?php
if (!function_exists('strptime')) {
echo "SKIP strptime function not available in build";
}
?>
--FILE--
<?php
//Set the default time zone
date_default_timezone_set("Europe/London");
echo "*** Testing strptime() : error conditions ***\n";
echo "\n-- Testing strptime() function on failure --\n";
$format = '%b %d %Y %H:%M:%S';
var_dump( strptime('foo', $format) );
?>
--EXPECTF--
*** Testing strptime() : error conditions ***
-- Testing strptime() function on failure --
bool(false)

@ -0,0 +1,69 @@
--TEST--
Test strptime() function : basic functionality
--SKIPIF--
<?php
if (!function_exists('strptime')) {
die("skip - strptime() function not available in this build");
}
if (!@strftime('%Z')) die('skip strftime does not support %Z');
?>
--FILE--
<?php
$orig = setlocale(LC_ALL, 'C');
date_default_timezone_set("GMT");
putenv("TZ=GMT");
echo "*** Testing strptime() : basic functionality ***\n";
$input = "10:01:20 AM July 2 1963";
$tstamp = strtotime($input);
$str = strftime("%r %B%e %Y %Z", $tstamp);
$res = strptime($str, '%H:%M:%S %p %B %d %Y %Z');
var_dump($res["tm_sec"]);
var_dump($res["tm_min"]);
var_dump($res["tm_hour"]);
var_dump($res["tm_mday"]);
var_dump($res["tm_mon"]);
var_dump($res["tm_year"]);
$str = strftime("%T %D", $tstamp);
$res = strptime($str, '%H:%M:%S %m/%d/%y');
var_dump($res["tm_sec"]);
var_dump($res["tm_min"]);
var_dump($res["tm_hour"]);
var_dump($res["tm_mday"]);
var_dump($res["tm_mon"]);
var_dump($res["tm_year"]);
$str = strftime("%A %B %e %R", $tstamp);
$res = strptime($str, '%A %B %e %R');
var_dump($res["tm_sec"]);
var_dump($res["tm_min"]);
var_dump($res["tm_hour"]);
var_dump($res["tm_mday"]);
var_dump($res["tm_mon"]);
var_dump($res["tm_year"]);
setlocale(LC_ALL, $orig);
?>
--EXPECTF--
*** Testing strptime() : basic functionality ***
int(20)
int(1)
int(10)
int(2)
int(6)
int(63)
int(20)
int(1)
int(10)
int(2)
int(6)
int(163)
int(0)
int(1)
int(10)
int(2)
int(6)
int(0)
Loading…
Cancel
Save