From bbc9ab61a46175813b73bc4f67f2c2dd9e1a94b3 Mon Sep 17 00:00:00 2001 From: Yurun Date: Tue, 30 Jun 2026 19:48:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(tests):=20=E6=9B=B4=E6=96=B0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E9=A2=84=E6=9C=9F=E4=B8=8E=E6=94=B9=E8=BF=9B=E8=B7=A8?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 8 +- tests/aot/optimizations/int-calculation.phpt | 2 +- tests/aot/std-map/009.phpt | 43 +- tests/aot/std-map/010.phpt | 39 +- tests/aot/stdlib/dirname_basename.phpt | 26 +- tests/aot/stream_method/0.phpt | 3 +- tests/aot/string_method/string_functions.phpt | 5 +- tests/aot/string_method/to-str.phpt | 1 + tests/aot/var_convert/002.phpt | 2 +- tests/core/lang/inc_throw.inc | 5 + tests/std/core/002_class_exists.phpt | 28 +- tests/std/core/003_method_exists.phpt | 32 +- tests/std/core/004_is_a.phpt | 28 +- tests/std/core/006_get_parent_class.phpt | 18 +- tests/std/core/009_string_extra.phpt | 18 +- tests/std/core/010_fs_math.phpt | 2 +- tests/std/core/012_new_functions.phpt | 6 +- tests/std/dir/scandir_basic-win32-mb.phpt | 69 ++ tests/std/file/file.inc | 657 ++++++++++++++++++ tests/std/misc/exec_basic1.phpt | 2 +- .../in-de-crement/incdec_bool_exception.phpt | 10 +- 21 files changed, 891 insertions(+), 113 deletions(-) create mode 100644 tests/core/lang/inc_throw.inc create mode 100644 tests/std/dir/scandir_basic-win32-mb.phpt create mode 100644 tests/std/file/file.inc diff --git a/.gitignore b/.gitignore index b748d1ad..55aba248 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,10 @@ *.pdb *.class /swoole_compiler -/tpc \ No newline at end of file +/tpc +tests/**/*.diff +tests/**/*.exp +tests/**/*.log +tests/**/*.out +tests/**/*.php +tests/**/*.sh diff --git a/tests/aot/optimizations/int-calculation.phpt b/tests/aot/optimizations/int-calculation.phpt index 114db63f..3cd72ffa 100644 --- a/tests/aot/optimizations/int-calculation.phpt +++ b/tests/aot/optimizations/int-calculation.phpt @@ -15,4 +15,4 @@ function main(): void { ?> --EXPECTF-- 12 -9.22%dE+%d \ No newline at end of file +-9223372036854775797 \ No newline at end of file diff --git a/tests/aot/std-map/009.phpt b/tests/aot/std-map/009.phpt index a71162fe..7637c5b7 100644 --- a/tests/aot/std-map/009.phpt +++ b/tests/aot/std-map/009.phpt @@ -6,19 +6,38 @@ function main() { $map = std::map(complex_types::type_string, native_types::type_int); $map["alpha"] = 10; $map["beta"] = 20; - var_dump($map); + + // Windows and linux std::map implementation may have different iteration order, so we cannot verify the order of keys. Instead, we verify the count and existence of keys. + + // verify all expected keys exist with correct values + var_dump($map["alpha"] === 10); + var_dump($map["beta"] === 20); + // verify count is 2 + $found = []; + foreach ($map as $k => $v) { + $found[$k] = ($found[$k] ?? 0) + 1; + } + var_dump($found["alpha"] ?? 0); + var_dump($found["beta"] ?? 0); + var_dump(count($found) === 2); + unset($map["alpha"]); - var_dump($map); + // verify alpha gone, beta remains + $found = []; + foreach ($map as $k => $v) { + $found[$k] = ($found[$k] ?? 0) + 1; + } + var_dump(($found["alpha"] ?? 0) === 0); + var_dump(($found["beta"] ?? 0) === 1); + var_dump(count($found) === 1); } ?> --EXPECT-- -array(2) { - ["beta"]=> - int(20) - ["alpha"]=> - int(10) -} -array(1) { - ["beta"]=> - int(20) -} \ No newline at end of file +bool(true) +bool(true) +int(1) +int(1) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/aot/std-map/010.phpt b/tests/aot/std-map/010.phpt index 53924881..497336b4 100644 --- a/tests/aot/std-map/010.phpt +++ b/tests/aot/std-map/010.phpt @@ -8,25 +8,36 @@ function main() { $map["beta"] = 20; $map["gamma"] = 30; + // Windows and linux std::map implementation may have different iteration order, so we cannot verify the order of keys. Instead, we verify the count and existence of keys. + + // verify all keys exist with correct values + $found = []; foreach ($map as $k => $v) { - var_dump($k, $v); + $found[$k] = ($found[$k] ?? 0) + 1; } + var_dump(($found["alpha"] ?? 0) === 1); + var_dump(($found["beta"] ?? 0) === 1); + var_dump(($found["gamma"] ?? 0) === 1); + var_dump(count($found) === 3); + unset($map["beta"]); - echo "unset-------------\n"; + // verify beta gone, alpha and gamma remain + $found = []; foreach ($map as $k => $v) { - var_dump($k, $v); + $found[$k] = ($found[$k] ?? 0) + 1; } + var_dump(($found["alpha"] ?? 0) === 1); + var_dump(($found["beta"] ?? 0) === 0); + var_dump(($found["gamma"] ?? 0) === 1); + var_dump(count($found) === 2); } ?> --EXPECT-- -string(5) "gamma" -int(30) -string(4) "beta" -int(20) -string(5) "alpha" -int(10) -unset------------- -string(5) "gamma" -int(30) -string(5) "alpha" -int(10) \ No newline at end of file +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/tests/aot/stdlib/dirname_basename.phpt b/tests/aot/stdlib/dirname_basename.phpt index eab9be92..2e2a7e9a 100644 --- a/tests/aot/stdlib/dirname_basename.phpt +++ b/tests/aot/stdlib/dirname_basename.phpt @@ -20,16 +20,16 @@ function main() { var_dump(basename("")); } ?> ---EXPECT-- -string(10) "/usr/local" -string(10) "/usr/local" -string(4) "/usr" -string(1) "/" -string(1) "/" -string(1) "." -string(0) "" -string(3) "bin" -string(3) "bin" -string(0) "" -string(3) "usr" -string(0) "" +--EXPECTREGEX-- +string\(10\) "\/usr\/local" +string\(10\) "\/usr\/local" +string\(4\) "\/usr" +string\(1\) "[\\\/]" +string\(1\) "[\\\/]" +string\(1\) "\." +string\(0\) "" +string\(3\) "bin" +string\(3\) "bin" +string\(0\) "" +string\(3\) "usr" +string\(0\) "" diff --git a/tests/aot/stream_method/0.phpt b/tests/aot/stream_method/0.phpt index 974bea47..04d6c35b 100644 --- a/tests/aot/stream_method/0.phpt +++ b/tests/aot/stream_method/0.phpt @@ -5,7 +5,7 @@ stream_method: 0 write($rdata->base64Encode()), $rdata->length()); @@ -31,5 +31,6 @@ Assert::eq($line, fgets($fp)); Assert::true($fp->truncate(1000)); Assert::true($fp->close()); +unlink($filepath); ?> --EXPECT-- diff --git a/tests/aot/string_method/string_functions.phpt b/tests/aot/string_method/string_functions.phpt index 124a02e6..e4c21b39 100644 --- a/tests/aot/string_method/string_functions.phpt +++ b/tests/aot/string_method/string_functions.phpt @@ -58,8 +58,9 @@ var_dump($joined); $cmp1 = strcmp("abc", "abc"); var_dump($cmp1); +// Windows is -1, linux is -3 $cmp2 = strcmp("abc", "def"); -var_dump($cmp2); +var_dump($cmp2 < 0); $cmp3 = strcasecmp("ABC", "abc"); var_dump($cmp3); @@ -132,7 +133,7 @@ array(3) { } string(19) "apple-banana-orange" int(0) -int(-3) +bool(true) int(0) string(22) "Value: 42, Text: hello" array(4) { diff --git a/tests/aot/string_method/to-str.phpt b/tests/aot/string_method/to-str.phpt index 53cac6a4..bb2d1eeb 100644 --- a/tests/aot/string_method/to-str.phpt +++ b/tests/aot/string_method/to-str.phpt @@ -2,6 +2,7 @@ any --FILE-- toStream()->write('hello'); var_dump($pair[1]->toStream()->read(5)); diff --git a/tests/core/lang/inc_throw.inc b/tests/core/lang/inc_throw.inc new file mode 100644 index 00000000..1f032f7e --- /dev/null +++ b/tests/core/lang/inc_throw.inc @@ -0,0 +1,5 @@ + diff --git a/tests/std/core/002_class_exists.phpt b/tests/std/core/002_class_exists.phpt index 4c4c81c5..2709d355 100644 --- a/tests/std/core/002_class_exists.phpt +++ b/tests/std/core/002_class_exists.phpt @@ -8,23 +8,25 @@ interface MyInterface {} trait MyTrait {} enum MyEnum { case A; } -echo "class_exists:\n"; -echo class_exists("MyClass") ? "ok-true\n" : "fail\n"; -echo class_exists("NonExistentClass") ? "fail\n" : "ok-false\n"; +function main() { + echo "class_exists:\n"; + echo class_exists("MyClass") ? "ok-true\n" : "fail\n"; + echo class_exists("NonExistentClass") ? "fail\n" : "ok-false\n"; -echo "interface_exists:\n"; -echo interface_exists("MyInterface") ? "ok-true\n" : "fail\n"; -echo interface_exists("NonExistentInterface") ? "fail\n" : "ok-false\n"; + echo "interface_exists:\n"; + echo interface_exists("MyInterface") ? "ok-true\n" : "fail\n"; + echo interface_exists("NonExistentInterface") ? "fail\n" : "ok-false\n"; -echo "trait_exists:\n"; -echo trait_exists("MyTrait") ? "ok-true\n" : "fail\n"; -echo trait_exists("NonExistentTrait") ? "fail\n" : "ok-false\n"; + echo "trait_exists:\n"; + echo trait_exists("MyTrait") ? "ok-true\n" : "fail\n"; + echo trait_exists("NonExistentTrait") ? "fail\n" : "ok-false\n"; -echo "enum_exists:\n"; -echo enum_exists("MyEnum") ? "ok-true\n" : "fail\n"; -echo enum_exists("NonExistentEnum") ? "fail\n" : "ok-false\n"; + echo "enum_exists:\n"; + echo enum_exists("MyEnum") ? "ok-true\n" : "fail\n"; + echo enum_exists("NonExistentEnum") ? "fail\n" : "ok-false\n"; -echo "done\n"; + echo "done\n"; +} ?> --EXPECT-- class_exists: diff --git a/tests/std/core/003_method_exists.phpt b/tests/std/core/003_method_exists.phpt index 8e7cc7e8..6e92aa61 100644 --- a/tests/std/core/003_method_exists.phpt +++ b/tests/std/core/003_method_exists.phpt @@ -11,24 +11,26 @@ class TestClass { private function privMethod(): string { return "private"; } } -$obj = new TestClass(); +function main() { + $obj = new TestClass(); -echo "method_exists:\n"; -echo method_exists($obj, "pubMethod") ? "ok-obj-pub\n" : "fail\n"; -echo method_exists($obj, "privMethod") ? "ok-obj-priv\n" : "fail\n"; -echo method_exists($obj, "noSuchMethod") ? "fail\n" : "ok-obj-none\n"; -echo method_exists(TestClass::class, "pubMethod") ? "ok-cls-pub\n" : "fail\n"; -echo method_exists(TestClass::class, "noSuchMethod") ? "fail\n" : "ok-cls-none\n"; + echo "method_exists:\n"; + echo method_exists($obj, "pubMethod") ? "ok-obj-pub\n" : "fail\n"; + echo method_exists($obj, "privMethod") ? "ok-obj-priv\n" : "fail\n"; + echo method_exists($obj, "noSuchMethod") ? "fail\n" : "ok-obj-none\n"; + echo method_exists(TestClass::class, "pubMethod") ? "ok-cls-pub\n" : "fail\n"; + echo method_exists(TestClass::class, "noSuchMethod") ? "fail\n" : "ok-cls-none\n"; -echo "property_exists:\n"; -echo property_exists($obj, "pubProp") ? "ok-obj-pub\n" : "fail\n"; -echo property_exists($obj, "privProp") ? "ok-obj-priv\n" : "fail\n"; -echo property_exists($obj, "noSuchProp") ? "fail\n" : "ok-obj-none\n"; -echo property_exists(TestClass::class, "pubProp") ? "ok-cls-pub\n" : "fail\n"; -echo property_exists(TestClass::class, "privProp") ? "ok-cls-priv\n" : "fail\n"; -echo property_exists(TestClass::class, "noSuchProp") ? "fail\n" : "ok-cls-none\n"; + echo "property_exists:\n"; + echo property_exists($obj, "pubProp") ? "ok-obj-pub\n" : "fail\n"; + echo property_exists($obj, "privProp") ? "ok-obj-priv\n" : "fail\n"; + echo property_exists($obj, "noSuchProp") ? "fail\n" : "ok-obj-none\n"; + echo property_exists(TestClass::class, "pubProp") ? "ok-cls-pub\n" : "fail\n"; + echo property_exists(TestClass::class, "privProp") ? "ok-cls-priv\n" : "fail\n"; + echo property_exists(TestClass::class, "noSuchProp") ? "fail\n" : "ok-cls-none\n"; -echo "done\n"; + echo "done\n"; +} ?> --EXPECT-- method_exists: diff --git a/tests/std/core/004_is_a.phpt b/tests/std/core/004_is_a.phpt index f58c8d8a..b0c38159 100644 --- a/tests/std/core/004_is_a.phpt +++ b/tests/std/core/004_is_a.phpt @@ -8,22 +8,24 @@ class Child extends Base {} interface IFace {} class Impl implements IFace {} -$child = new Child(); -$impl = new Impl(); +function main() { + $child = new Child(); + $impl = new Impl(); -echo "is_a:\n"; -echo is_a($child, "Child") ? "ok-obj-class\n" : "fail\n"; -echo is_a($child, "Base") ? "ok-obj-parent\n" : "fail\n"; -echo is_a($child, "NoSuchClass") ? "fail\n" : "ok-obj-none\n"; + echo "is_a:\n"; + echo is_a($child, "Child") ? "ok-obj-class\n" : "fail\n"; + echo is_a($child, "Base") ? "ok-obj-parent\n" : "fail\n"; + echo is_a($child, "NoSuchClass") ? "fail\n" : "ok-obj-none\n"; -echo "is_subclass_of:\n"; -echo is_subclass_of($child, "Base") ? "ok-obj-parent\n" : "fail\n"; -echo is_subclass_of($child, "Child") ? "fail\n" : "ok-obj-same\n"; -echo is_subclass_of(Child::class, "Base") ? "ok-cls-parent\n" : "fail\n"; -echo is_subclass_of(Child::class, "Child") ? "fail\n" : "ok-cls-same\n"; -echo is_subclass_of($child, "NoSuchClass") ? "fail\n" : "ok-none\n"; + echo "is_subclass_of:\n"; + echo is_subclass_of($child, "Base") ? "ok-obj-parent\n" : "fail\n"; + echo is_subclass_of($child, "Child") ? "fail\n" : "ok-obj-same\n"; + echo is_subclass_of(Child::class, "Base") ? "ok-cls-parent\n" : "fail\n"; + echo is_subclass_of(Child::class, "Child") ? "fail\n" : "ok-cls-same\n"; + echo is_subclass_of($child, "NoSuchClass") ? "fail\n" : "ok-none\n"; -echo "done\n"; + echo "done\n"; +} ?> --EXPECT-- is_a: diff --git a/tests/std/core/006_get_parent_class.phpt b/tests/std/core/006_get_parent_class.phpt index b5679859..982a5300 100644 --- a/tests/std/core/006_get_parent_class.phpt +++ b/tests/std/core/006_get_parent_class.phpt @@ -7,16 +7,18 @@ class Base {} class Child extends Base {} class NoParent {} -$child = new Child(); -$noParent = new NoParent(); +function main() { + $child = new Child(); + $noParent = new NoParent(); -echo "get_parent_class:\n"; -echo get_parent_class($child) === "Base" ? "ok-obj\n" : "fail-obj\n"; -echo get_parent_class(Child::class) === "Base" ? "ok-cls\n" : "fail-cls\n"; -echo get_parent_class($noParent) === false ? "ok-obj-false\n" : "fail-obj-false\n"; -echo get_parent_class(NoParent::class) === false ? "ok-cls-false\n" : "fail-cls-false\n"; + echo "get_parent_class:\n"; + echo get_parent_class($child) === "Base" ? "ok-obj\n" : "fail-obj\n"; + echo get_parent_class(Child::class) === "Base" ? "ok-cls\n" : "fail-cls\n"; + echo get_parent_class($noParent) === false ? "ok-obj-false\n" : "fail-obj-false\n"; + echo get_parent_class(NoParent::class) === false ? "ok-cls-false\n" : "fail-cls-false\n"; -echo "done\n"; + echo "done\n"; +} ?> --EXPECT-- get_parent_class: diff --git a/tests/std/core/009_string_extra.phpt b/tests/std/core/009_string_extra.phpt index e293dfc1..8f06c727 100644 --- a/tests/std/core/009_string_extra.phpt +++ b/tests/std/core/009_string_extra.phpt @@ -20,17 +20,17 @@ echo strtr("hello world", "wo", "WO") . "\n"; echo strtr("abcdef", "abc", "123") . "\n"; ?> ---EXPECT-- -== dirname() == -/etc -/ -/ -relative/path -== basename() == +--EXPECTREGEX-- +== dirname\(\) == +\/etc +[\\\/] +[\\\/] +relative\/path +== basename\(\) == passwd passwd file -[] -== strtr() == +\[\] +== strtr\(\) == hellO WOrld 123def diff --git a/tests/std/core/010_fs_math.phpt b/tests/std/core/010_fs_math.phpt index c9e9ee30..79b06845 100644 --- a/tests/std/core/010_fs_math.phpt +++ b/tests/std/core/010_fs_math.phpt @@ -6,7 +6,7 @@ Filesystem and math functions - is_dir, is_file, round echo "== is_dir() ==\n"; var_dump(is_dir("/")); var_dump(is_dir("/nonexistent_path_xyz123")); -var_dump(is_dir("/etc")); +var_dump(is_dir(sys_get_temp_dir())); echo "== is_file() ==\n"; var_dump(is_file("/nonexistent_file_xyz123")); diff --git a/tests/std/core/012_new_functions.phpt b/tests/std/core/012_new_functions.phpt index 30b544dd..2f6ec55e 100644 --- a/tests/std/core/012_new_functions.phpt +++ b/tests/std/core/012_new_functions.phpt @@ -120,9 +120,9 @@ Array ) Array ( - [0] => 3 - [1] => 2 - [2] => 1 + [c] => 3 + [b] => 2 + [a] => 1 ) Array ( diff --git a/tests/std/dir/scandir_basic-win32-mb.phpt b/tests/std/dir/scandir_basic-win32-mb.phpt new file mode 100644 index 00000000..87e6e7e6 --- /dev/null +++ b/tests/std/dir/scandir_basic-win32-mb.phpt @@ -0,0 +1,69 @@ +--TEST-- +Test scandir() function : basic functionality +--SKIPIF-- + +--FILE-- + +--CLEAN-- + +--EXPECT-- +*** Testing scandir() : basic functionality *** + +-- scandir() with mandatory arguments -- +array(5) { + [0]=> + string(1) "." + [1]=> + string(2) ".." + [2]=> + string(45) "私はガラスを食べられますfile1.tmp" + [3]=> + string(45) "私はガラスを食べられますfile2.tmp" + [4]=> + string(45) "私はガラスを食べられますfile3.tmp" +} + +-- scandir() with all arguments -- +array(5) { + [0]=> + string(45) "私はガラスを食べられますfile3.tmp" + [1]=> + string(45) "私はガラスを食べられますfile2.tmp" + [2]=> + string(45) "私はガラスを食べられますfile1.tmp" + [3]=> + string(2) ".." + [4]=> + string(1) "." +} diff --git a/tests/std/file/file.inc b/tests/std/file/file.inc new file mode 100644 index 00000000..c972784f --- /dev/null +++ b/tests/std/file/file.inc @@ -0,0 +1,657 @@ + $fill_size ) { + $buffer = substr($tmp_buff, 0, $fill_size); + } else { + $buffer = $tmp_buff; + } + + return true; +} + +/* + Function : bool fill_file(resource $file_handle, string $fill_type, string $file_size); + Description: Fills the file with data as specified with requested size. + $file_handle = file handle, opened with write options, + $fill_type: + "text" = fills with string of size $file_size + "numeric" = fills with numeric value of size $file_size + "empty" = no fill operation performed, returns true + "text_with_new_line" = similar to "text" fill type but writes with new line + "alphanumeric" = fills with alphnumeric values + Returns: true on success, false on failure & invalid fill type +*/ + +function fill_file($file_handle, $fill_type, $file_size) { + + if ( $fill_type == "empty" ) { + // no fill required, return true + return true; + } if ( $fill_type == "text" ) { + $data = "text "; + $size_divider = strlen($data); + $add_value = strlen($data); + } else if ( $fill_type == "text_with_new_line" ) { + $data = "line\nline of text\n"; + $size_divider = strlen($data); + $add_value = strlen($data); + } else if ( $fill_type == "alphanumeric" ) { + $data = "ab12 "; + $size_divider = strlen($data); + $add_value = strlen($data); + } else if ( $fill_type == "numeric" ) { + $data = 2; + $size_divider = 1; + $add_value = 0; + } else { + // invalid fill type; + return false; + } + + // write in terms of a chunk of 1 K to avoid memory size overflow + $size = $file_size; + $chunk_size = 1024; + if ( $size > $chunk_size ) { + $loop_count = 1; + do { + $loop_count ++; + if ( $size <= $chunk_size ) { + $chunk_size = $size; + } + $num_values = str_repeat($data, (int) (($chunk_size/$size_divider) + $add_value) ); + $bytes_written = fwrite($file_handle, $num_values, $chunk_size); + if ( $bytes_written != $chunk_size ) { + return false; + } + $size -= $chunk_size; + } while ( $size > 0 ); + } else { + $num_values = str_repeat($data, (int) (($chunk_size/$size_divider) + $add_value) ); + $bytes_written = fwrite($file_handle, $num_values, $file_size); + if ( $bytes_written != $file_size ) { + return false; + } + } + + // successful, return true + return true; +} + +/* + Function: int change_file_perms(string $file_path, int $count = 1, int $perms = 0755, + string $name_prefix = "file", + string $name_suffix = 1, $file_extension = ".tmp"); + Description: changes file permission for given file(s). + $file_path = dir path where file exists + $count = no. of files, default is 1 + $perms = new permission of the file, similar to $mode args of chmod() call + $name_prefix = common name prefix, default is "file" + $name_suffix = suffix to end the common name given in name_prefix to create + a unique name. default is 1. + $file_extension = default is .tmp + Returns: + Integer, Count of total files permission changed. +*/ +function change_file_perms($file_path, + $count = 1, + $perms = 0755, + $name_prefix = "file", + $name_suffix = 1, + $file_extension = ".tmp" ) +{ + $changed = 0; + + if( $count <= 0 ) + return $changed; + + if ( $name_suffix <= 0) + $name_suffix = 1; + + for($loop_counter = 1; $loop_counter <= $count; $loop_counter++) { + $filename = $file_path."/".$name_prefix.$name_suffix.$file_extension; + if( chmod($filename, $perms) ) + $changed++; + $name_suffix++; + } + return $changed; +} + +/* + Function: array create_files( string $file_path, + int $count = 1, + string $content_type = "numeric", + int $permission = 0755, + int $size = 1, + string $mode = "w", + string $name_prefix = "file", + int $name_suffix = 1, + string $flag = "kilobytes" + string $file_extension = ".tmp" + ); + Description: Creates given number of files with specified mode and + permissions. File is filled with content of size specified. + $file_path = dir where files will be created + $name_prefix = prefix to be used for names, name is suffix with a + unique numeric value to make the file name unique, default = file + $name_suffix = suffix to be used for the name, default = 1 + $count = total no. of files to be created, default = 1 + $mode = file open mode as specified in fopen() call. Do not use + modes used for only reading the file. Default = "w" + $permission = An octal number, This should be similar to $mode + specified in chmod() call. + $content_type = Specify type of the content to fill in the file. + "numeric" = fill file with numeric values + "text" = fill file with regular text + "empty" = empty file + "text_with_new_line" = similar to text fill type, but writes with new line char + "alphanumeric" = fill file with alpha numeric text + If improper $content type is specified, file is created as empty + $size = size of the fill in terms of kilobyte, i.e size of the file. + if $flag is specified as "byte", then then given size is taken in bytes + $flag = specify if size has to be treated as no of total bytes or + multiple of KB. + "kilobytes" = take size in terms of multiple of KB + "byte" = take size in terms of bytes + $file_extension = default is .tmp + + Returns: + An array with following key value pair: + created => total file created + filled => total files filled + perms_changed => total files permission changed +*/ +function create_files( $file_path, + $count = 1, + $content_type = "numeric", + $permission = 0755, + $size = 1, + $mode = "w", + $name_prefix = "file", + $name_suffix = 1, + $flag = "kilobytes", + $file_extension = ".tmp" + ) +{ + $return_value = array('created' => 0, 'filled' => 0, 'perms_changed' => 0); + + //ensure that suffix is a +ve integer + if ($name_suffix <= 0) { + $name_suffix = 1; + } + + // check for proper size + if ( $size == 0 ) + return $return_value; + + // prepare the size based on flag + $file_size = $size; + if ( $flag == "kilobytes" ) { + $file_size = $file_size * 1024; + } + + $tmp_name_suffix = $name_suffix; + // create the files with specified mode and permission + for($file_created_count = 1; $file_created_count <= $count; $file_created_count ++) { + $filename = $file_path."/".$name_prefix.$tmp_name_suffix.$file_extension; + + $status = create_file($filename, $mode); + + $tmp_name_suffix++; + + if ($status == true) { + $return_value['created']++; + } + else { + return $return_value; + } + } + + if ( $content_type == "empty" ) { + $return_value['filled'] = $count; + } else { + // fill the file with specifiec type of data and size + $tmp_name_suffix = $name_suffix; + for($loop_counter = 1; $loop_counter <= $count; $loop_counter ++) { + $filename = $file_path."/".$name_prefix.$tmp_name_suffix.$file_extension; + $file_handle = fopen($filename, $mode); + if($file_handle == false) { + fclose($file_handle); + return $return_value; + } // end of if + + // call fill_file() to fill the file + if( fill_file($file_handle, $content_type, $file_size) ) + $return_value['filled']++; + + fclose($file_handle); + + $tmp_name_suffix++; + } // end of for + } + + // change all file's permissions + $return_value['perms_changed'] = change_file_perms($file_path, $count, $permission, $name_prefix, + $name_suffix, $file_extension); + + return $return_value; +} + + +/* + Function: function create_links( $file_path, + $filename, + $link_count = 1, + $link_type = "soft", + $link_size = 1024, + $link_name_prefix = "link", + $link_name_suffix = 1, + $link_file_content = "text", + $link_perms = 0755, + $link_file_extension = ".tmp" + ); + + Description: Creates given number of links with specified mode and + permissions.Link is filled with content of size specified. + $file_path = location of the file and where links need to be created + $link_name_prefix = prefix to be used for names, name is suffix with a + unique numeric value to make the file name unique, default = link + $link_name_suffix = suffix to be used for the name, default = 1 + $link_count = total no. of links to be created to given file, default = 1 + $link_perms = An octal number, This should be similar to $mode + specified in chmod() call. + $link_file_content = Type of the content to fill in the file. + numeric = fill file with numeric values + text = fill file with regular text + text_with_new_line = same as text but new lines are written + alphanumeric = fill with alphanumeric text + If imporper $content type is specified, file is created as empty + $size = size of the fill in terms of kilobyte, i.e size of the file. + $link_type = type of the link to be created + "soft" = soft link + "hard" = hard link + $filename = file used to create a link on + + Returns: + An array with following key value pair: + created => total file created + filled => total files filled, always returned as 1 + perms_changed => total files permission changed +*/ +function create_links($file_path, + $filename, + $link_count = 1, + $link_type = "soft", + $link_size = 1024, + $link_name_prefix = "link", + $link_name_suffix = 1, + $link_file_content = "text", + $link_perms = 0755, + $link_file_extension = ".tmp" + ) +{ + $return_value = array('created' => 0, 'filled' => 0, 'perms_changed' => 0); + $tmp_name_suffix = $link_name_suffix; + $src_filename = $file_path."/".$filename; + switch( $link_type ) { + default : + case "soft" : // create a soft link + for($link_created_count = 1; $link_created_count <= $link_count; $link_created_count++) { + $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension; + $status = symlink( $src_filename, $linkname); + $tmp_name_suffix++; + if ($status) { + $return_value['created']++; + } + else { + $return_value; + } + } + break; + + case "hard" : // create a hard link + for($link_created_count = 1; $link_created_count <= $link_count; $link_created_count++) { + $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension; + $status = link($src_filename, $linkname); + $tmp_name_suffix++; + if ($status) { + $return_value['created']++; + } + else { + $return_value; + } + } + break; + } + + if ( $link_file_content == "empty" ) { + $return_value['filled'] = 1; + return $return_value; + } + + // fill the file with specific type of data and size + $tmp_name_suffix = $link_name_suffix; + $linkname = $file_path."/".$link_name_prefix.$tmp_name_suffix.$link_file_extension; + $file_handle = fopen($linkname, "w"); + if($file_handle == false) { + return $return_value; + } // end of if + + // call fill_file() to fill the file + if( fill_file($file_handle, $link_file_content, $link_size) ) + $return_value['filled']++; + + // close the link + fclose($file_handle); + + // change the permission of the link file, only if hard link. + // this is not applicable to soft links + if( $link_type == "hard" ) { + $return_value['perms_changed'] = change_file_perms($file_path, + $link_count, + $link_perms, + $link_name_prefix, + $link_name_suffix, + $link_file_extension ); + } + + return $return_value; +} + +/* + Function: bool delete_file(string $filename); + Description: delete a given file if exists + Returns: true on success + false on failure + FILE_NOT_FOUND if file doesn't exist +*/ +function delete_file($filename) { + // check if file exists + if ( file_exists($filename) ) { + if ( unlink($filename) ) + return true; + else + return false; + } + return FILE_NOT_FOUND; +} + +/* + Function: array delete_files(string $file_path, int $count = 1, string $name_prefix = "file", + int name_suffix = 1, $file_extension = ".tmp" ); + Description: Deletes given number of files if exists. + $file_path = location of the files + $name_prefix = prefix for the filename, rest of the name is incremental(increment by 1 only) + numeric starting from suffix up to count + $count = number of files to be deleted + $name_suffix = first numeric suffix in the name + Returns: An array with following key/value pair + deleted = Number of files deleted. + notfound = Count of non existing file + failed = Count of failed to delete +*/ +function delete_files($file_path, + $count = 1, + $name_prefix = "file", + $name_suffix = 1, + $file_extension = ".tmp") +{ + $return_value = array ('deleted' => 0, 'notfound' => 0, 'failed' => 0); + + if ( $name_suffix < 1 ) + $name_suffix = 1; + for($loop_counter = 1; $loop_counter <= $count; $loop_counter++) { + $filename = $file_path."/".$name_prefix.$name_suffix.$file_extension; + $name_suffix++; + $status = delete_file($filename); + if($status == true) { + $return_value['deleted']++; + } else if($status == FILE_NOT_FOUND) { + $return_value['notfound']++; + } else { + $return_value['failed']++; + } + + } // end of for + return $return_value; +} + +/* + Function: array delete_links( $file_path, + $link_file_count, + $link_name_prefix, + $link_name_suffix, + $link_file_extension ); + Description: Deletes given number of links if exists. Uses delete_files() function + $file_path = location of link files + $link_file_count = Number of link files + $link_name_prefix = prefix for the linkname, rest of the name is incremental(increment by 1 only) + numeric starting from $link_name_suffix up to count + $link_name_suffix = first numeric suffix in the name + + Returns: An array with following key/value pair + deleted = Number of links deleted. + notfound = Count of non existing link + failed = Count of failed to delete +*/ +function delete_links($file_path, + $link_file_count = 1, + $link_name_prefix = "link", + $link_name_suffix = 1, + $link_file_extension = ".tmp") +{ + // call the delete files to delete links + $return_value = delete_files( $file_path, + $link_file_count, + $link_name_prefix, + $link_name_suffix, + $link_file_extension ); + return $return_value; +} + + + +/* + Prototype: + function compare_self_stat( array $stat ); + Description: + Compares the each of the first 13 values of the stat array with the + corresponding next 13 values of the same stat for equality + $stat = stat array + + Returns: true when all of them match, false otherwise +*/ +function compare_self_stat( array $stat ) +{ + //return value + $return_value = true; + + // named keys present in a stat + $string_keys = array("dev", "ino", "mode", "nlink", "uid", "gid", + "rdev", "size", "atime", "mtime", "ctime", + "blksize", "blocks"); + + // first numeric key + $key = 0; + + // compare the values in the stat, which are accessed using numeric key with + // values accessed using string keys + foreach($string_keys as $str_key) + { + if($stat[$key] != $stat[$str_key]) { + echo "Error: stat[$key] doesn't match with stat[$str_key]\n"; + $flag = false; + $key++; + } + else { + $key++; + } + } // end of foreach + + // if the $return_value is false, i.e all the element do not match then + // dump the stat array so that its easy to figure out the error + if ($return_value == false ) { + echo "\n Dumping stat array ...\n"; + var_dump($stat); + } + + return $return_value; +}// end of compare_self_stat + +/* +Prototype: + function compare_stats( array $stat1, array $stat2, array $fields, + [string $op = "==", [ bool $flag = false] ]); +Description: + Compares two stat values, stat value should be obtained by stat/lstat + $stat1 = first stat array + $stat2 = second stat array + $op = type of the comparison to be perform between elements of stat1 and stat2 + "!=" compare for not equal + "==" compare for equality + ">" if each element of stat1 is > than stat2 + "<" if each element of stat1 is < than stat2 + $fields = contains the key of the elements that needs to be compared. + type of the comparison is based on $op argument value + $flag = specify true to dump the stat1 and stat2 +*/ + +$all_stat_keys = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, + "dev", "ino", "mode", "nlink", "uid", "gid", + "rdev", "size", "atime", "mtime", "ctime", + "blksize", "blocks"); + +function compare_stats($stat1, $stat2, $fields, $op = "==", $flag = false ) { + $stat_time_diff_keys = array(8, 'atime'); + + // dump the stat if requested + if ( $flag == true ) { + var_dump($stat1); + var_dump($stat2); + } + + $result = true; + + // compare values of given key from each stat array + for($index = 0; $index < count($fields); $index++) + { + switch( $op ) + { + case "==": + if ( $stat1[ $fields[$index] ] != $stat2[ $fields[$index] ] ) { + if ( ! in_array( $fields[$index], $stat_time_diff_keys ) ) { + $result = false; + echo "Error: stat1 do not match with stat2 at key value: $fields[$index]\n"; + } elseif (abs($stat1[ $fields[$index] ] - $stat2[ $fields[$index] ]) > 2) { + $result = false; + echo "Error: stat1 differs too much from stat2 at key value: $fields[$index]\n"; + } + } + break; + + case "!=": + if ( $stat1[ $fields[$index] ] != $stat2[ $fields[$index] ] ) { + // do nothing as its not equal, else will take care of if equal + } else { + $result = false; + echo "Error: stat1 equals stat2 at key value: $fields[$index]\n"; + } + break; + + case ">": + if ( $stat1[ $fields[$index] ] <= $stat2[ $fields[$index] ] ) { + $result = false; + echo "Error: stat1 is not greater than stat2 at key value: $fields[$index]\n"; + } + break; + + case "<": + if ( $stat1[ $fields[$index] ] >= $stat2[ $fields[$index] ] ) { + $result = false; + echo "Error: stat1 is not lesser than stat2 at key value: $fields[$index]\n"; + } + break; + } + } + // if the result is false(i.e values are not as expected), + // dump the stat array so that easy to figure out the error + if ( $result == false ) { + echo "\n Dumping stat array 1...\n"; + var_dump($stat1); + echo "\n Dumping stat array 2...\n"; + var_dump($stat2); + } + + return $result; +} + +?> diff --git a/tests/std/misc/exec_basic1.phpt b/tests/std/misc/exec_basic1.phpt index db26e73b..14b5850c 100644 --- a/tests/std/misc/exec_basic1.phpt +++ b/tests/std/misc/exec_basic1.phpt @@ -3,7 +3,7 @@ exec, system, passthru — Basic command execution functions --SKIPIF-- --FILE-- ---EXPECT-- -Warning: Increment on type bool has no effect, this will change in the next major version of PHP in Unknown(0) : eval() on line 1 +--EXPECTF-- +Warning: Increment on type bool has no effect, this will change in the next major version of PHP in %s on line %d -Warning: Increment on type bool has no effect, this will change in the next major version of PHP in Unknown(0) : eval() on line 1 +Warning: Decrement on type bool has no effect, this will change in the next major version of PHP in %s on line %d -Warning: Increment on type bool has no effect, this will change in the next major version of PHP in Unknown(0) : eval() on line 1 +Warning: Increment on type bool has no effect, this will change in the next major version of PHP in %s on line %d -Warning: Increment on type bool has no effect, this will change in the next major version of PHP in Unknown(0) : eval() on line 1 +Warning: Decrement on type bool has no effect, this will change in the next major version of PHP in %s on line %d