mirror of
https://github.com/php/php-src.git
synced 2024-09-22 18:37:25 +00:00
New trim() tests. Tested on Windows, Linux and Linux 64 bit
This commit is contained in:
parent
cf84f2c5f5
commit
98076f58db
45
ext/standard/tests/strings/trim_basic.phpt
Normal file
45
ext/standard/tests/strings/trim_basic.phpt
Normal file
@ -0,0 +1,45 @@
|
||||
--TEST--
|
||||
Test trim() function : basic functionality
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
/* Prototype : string trim ( string $str [, string $charlist ] )
|
||||
* Description: Strip whitespace (or other characters) from the beginning and end of a string.
|
||||
* Source code: ext/standard/string.c
|
||||
*/
|
||||
|
||||
echo "*** Testing trim() : basic functionality ***\n";
|
||||
|
||||
$text = " \t\r\n\0\x0B ---These are a few words--- \t\r\n\0\x0B ";
|
||||
$hello = "!===Hello World===!";
|
||||
$binary = "\x0A\x0DExample string\x0A\x0D";
|
||||
|
||||
echo "\n-- Trim string with all white space characters --\n";
|
||||
var_dump(trim($text));
|
||||
|
||||
echo "\n-- Trim non-whitespace from a string --\n";
|
||||
var_dump(trim($hello, "=!"));
|
||||
|
||||
echo "\n-- Trim some non-white space characters from a string --\n";
|
||||
var_dump(trim($hello, "Hdle"));
|
||||
|
||||
echo "\n-- Trim the ASCII control characters at the beginning of a string --\n";
|
||||
var_dump(trim($binary, "\x00..\x1F"));
|
||||
|
||||
?>
|
||||
===DONE===
|
||||
--EXPECT--
|
||||
*** Testing trim() : basic functionality ***
|
||||
|
||||
-- Trim string with all white space characters --
|
||||
string(27) "---These are a few words---"
|
||||
|
||||
-- Trim non-whitespace from a string --
|
||||
string(11) "Hello World"
|
||||
|
||||
-- Trim some non-white space characters from a string --
|
||||
string(19) "!===Hello World===!"
|
||||
|
||||
-- Trim the ASCII control characters at the beginning of a string --
|
||||
string(14) "Example string"
|
||||
===DONE===
|
61
ext/standard/tests/strings/trim_error.phpt
Normal file
61
ext/standard/tests/strings/trim_error.phpt
Normal file
@ -0,0 +1,61 @@
|
||||
--TEST--
|
||||
Test trim() function : error conditions
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
/* Prototype : string trim ( string $str [, string $charlist ] )
|
||||
* Description: Strip whitespace (or other characters) from the begining and end of a string.
|
||||
* Source code: ext/standard/string.c
|
||||
*/
|
||||
|
||||
|
||||
echo "*** Testing trim() : error conditions ***\n";
|
||||
|
||||
echo "\n-- Testing trim() function with no arguments --\n";
|
||||
var_dump( trim() );
|
||||
|
||||
echo "\n-- Testing trim() function with more than expected no. of arguments --\n";
|
||||
$extra_arg = 10;
|
||||
var_dump( trim("Hello World", "Heo", $extra_arg) );
|
||||
|
||||
|
||||
$hello = " Hello World\n";
|
||||
echo "\n-- Test trim function with various invalid charlists --\n";
|
||||
var_dump(trim($hello, "..a"));
|
||||
var_dump(trim($hello, "a.."));
|
||||
var_dump(trim($hello, "z..a"));
|
||||
var_dump(trim($hello, "a..b..c"));
|
||||
|
||||
?>
|
||||
===DONE===
|
||||
--EXPECTF--
|
||||
*** Testing trim() : error conditions ***
|
||||
|
||||
-- Testing trim() function with no arguments --
|
||||
|
||||
Warning: trim() expects at least 1 parameter, 0 given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Testing trim() function with more than expected no. of arguments --
|
||||
|
||||
Warning: trim() expects at most 2 parameters, 3 given in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Test trim function with various invalid charlists --
|
||||
|
||||
Warning: trim(): Invalid '..'-range, no character to the left of '..' in %s on line %d
|
||||
string(14) " Hello World
|
||||
"
|
||||
|
||||
Warning: trim(): Invalid '..'-range, no character to the right of '..' in %s on line %d
|
||||
string(14) " Hello World
|
||||
"
|
||||
|
||||
Warning: trim(): Invalid '..'-range, '..'-range needs to be incrementing in %s on line %d
|
||||
string(14) " Hello World
|
||||
"
|
||||
|
||||
Warning: trim(): Invalid '..'-range in %s on line %d
|
||||
string(14) " Hello World
|
||||
"
|
||||
===DONE===
|
138
ext/standard/tests/strings/trim_variation1.phpt
Normal file
138
ext/standard/tests/strings/trim_variation1.phpt
Normal file
@ -0,0 +1,138 @@
|
||||
--TEST--
|
||||
Test trim() function : usage variations - test values for $str argument
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
/* Prototype : string trim ( string $str [, string $charlist ] )
|
||||
* Description: Strip whitespace (or other characters) from the begining and end of a string.
|
||||
* Source code: ext/standard/string.c
|
||||
*/
|
||||
|
||||
echo "*** Testing trim() function: with unexpected inputs for 'str' argument ***\n";
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = ' !--string_val--! ';
|
||||
unset($unset_var);
|
||||
|
||||
//defining a class
|
||||
class sample {
|
||||
public function __toString() {
|
||||
return " !---sample object---! ";
|
||||
}
|
||||
}
|
||||
|
||||
//getting the resource
|
||||
$file_handle = fopen(__FILE__, "r");
|
||||
|
||||
// array with different values for $input
|
||||
$inputs = array (
|
||||
|
||||
// integer values
|
||||
/*1*/ 0,
|
||||
1,
|
||||
255,
|
||||
256,
|
||||
2147483647,
|
||||
-2147483648,
|
||||
|
||||
// float values
|
||||
/*7*/ 10.5,
|
||||
-20.5,
|
||||
10.1234567e10,
|
||||
|
||||
// array values
|
||||
/*10*/ array(),
|
||||
array(0),
|
||||
array(1, 2),
|
||||
|
||||
// boolean values
|
||||
/*13*/ true,
|
||||
false,
|
||||
TRUE,
|
||||
FALSE,
|
||||
|
||||
// null values
|
||||
/*17*/ NULL,
|
||||
null,
|
||||
|
||||
// objects
|
||||
/*19*/ new sample(),
|
||||
|
||||
// resource
|
||||
/*20*/ $file_handle,
|
||||
|
||||
// undefined variable
|
||||
/*21*/ @$undefined_var,
|
||||
|
||||
// unset variable
|
||||
/*22*/ @$unset_var
|
||||
);
|
||||
|
||||
// loop through with each element of the $inputs array to test trim() function
|
||||
$count = 1;
|
||||
foreach($inputs as $input) {
|
||||
echo "-- Iteration $count --\n";
|
||||
// strip white space and any "minus" signs
|
||||
var_dump( trim($input, " !-") );
|
||||
$count ++;
|
||||
}
|
||||
|
||||
fclose($file_handle); //closing the file handle
|
||||
|
||||
?>
|
||||
===DONE===
|
||||
--EXPECTF--
|
||||
*** Testing trim() function: with unexpected inputs for 'str' argument ***
|
||||
-- Iteration 1 --
|
||||
string(1) "0"
|
||||
-- Iteration 2 --
|
||||
string(1) "1"
|
||||
-- Iteration 3 --
|
||||
string(3) "255"
|
||||
-- Iteration 4 --
|
||||
string(3) "256"
|
||||
-- Iteration 5 --
|
||||
string(10) "2147483647"
|
||||
-- Iteration 6 --
|
||||
string(10) "2147483648"
|
||||
-- Iteration 7 --
|
||||
string(4) "10.5"
|
||||
-- Iteration 8 --
|
||||
string(4) "20.5"
|
||||
-- Iteration 9 --
|
||||
string(12) "101234567000"
|
||||
-- Iteration 10 --
|
||||
|
||||
Warning: trim() expects parameter 1 to be string, array given in %s on line %d
|
||||
NULL
|
||||
-- Iteration 11 --
|
||||
|
||||
Warning: trim() expects parameter 1 to be string, array given in %s on line %d
|
||||
NULL
|
||||
-- Iteration 12 --
|
||||
|
||||
Warning: trim() expects parameter 1 to be string, array given in %s on line %d
|
||||
NULL
|
||||
-- Iteration 13 --
|
||||
string(1) "1"
|
||||
-- Iteration 14 --
|
||||
string(0) ""
|
||||
-- Iteration 15 --
|
||||
string(1) "1"
|
||||
-- Iteration 16 --
|
||||
string(0) ""
|
||||
-- Iteration 17 --
|
||||
string(0) ""
|
||||
-- Iteration 18 --
|
||||
string(0) ""
|
||||
-- Iteration 19 --
|
||||
string(13) "sample object"
|
||||
-- Iteration 20 --
|
||||
|
||||
Warning: trim() expects parameter 1 to be string, resource given in %s on line %d
|
||||
NULL
|
||||
-- Iteration 21 --
|
||||
string(0) ""
|
||||
-- Iteration 22 --
|
||||
string(0) ""
|
||||
===DONE===
|
138
ext/standard/tests/strings/trim_variation2.phpt
Normal file
138
ext/standard/tests/strings/trim_variation2.phpt
Normal file
@ -0,0 +1,138 @@
|
||||
--TEST--
|
||||
Test trim() function : usage variations - test values for $charlist argument
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
/* Prototype : string trim ( string $str [, string $charlist ] )
|
||||
* Description: Strip whitespace (or other characters) from the begining and end of a string.
|
||||
* Source code: ext/standard/string.c
|
||||
*/
|
||||
|
||||
echo "*** Testing trim() function: with unexpected inputs for 'charlist' argument ***\n";
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = ' string_val ';
|
||||
unset($unset_var);
|
||||
|
||||
//defining a class
|
||||
class sample {
|
||||
public function __toString() {
|
||||
return " sample object ";
|
||||
}
|
||||
}
|
||||
|
||||
//getting the resource
|
||||
$file_handle = fopen(__FILE__, "r");
|
||||
|
||||
// array with different values for $input
|
||||
$inputs = array (
|
||||
|
||||
// integer values
|
||||
/*1*/ 0,
|
||||
1,
|
||||
255,
|
||||
256,
|
||||
2147483647,
|
||||
-2147483648,
|
||||
|
||||
// float values
|
||||
/*7*/ 10.5,
|
||||
-20.5,
|
||||
10.1234567e10,
|
||||
|
||||
// array values
|
||||
/*10*/ array(),
|
||||
array(0),
|
||||
array(1, 2),
|
||||
|
||||
// boolean values
|
||||
/*13*/ true,
|
||||
false,
|
||||
TRUE,
|
||||
FALSE,
|
||||
|
||||
// null values
|
||||
/*17*/ NULL,
|
||||
null,
|
||||
|
||||
// objects
|
||||
/*19*/ new sample(),
|
||||
|
||||
// resource
|
||||
/*20*/ $file_handle,
|
||||
|
||||
// undefined variable
|
||||
/*21*/ @$undefined_var,
|
||||
|
||||
// unset variable
|
||||
/*22*/ @$unset_var
|
||||
);
|
||||
|
||||
// loop through with each element of the $inputs array to test trim() function
|
||||
$count = 1;
|
||||
foreach($inputs as $charlist) {
|
||||
echo "-- Iteration $count --\n";
|
||||
// strip white space and any "minus" signs
|
||||
var_dump( trim("!---Hello World---!", $charlist) );
|
||||
$count ++;
|
||||
}
|
||||
|
||||
fclose($file_handle); //closing the file handle
|
||||
|
||||
?>
|
||||
===DONE===
|
||||
--EXPECTF--
|
||||
*** Testing trim() function: with unexpected inputs for 'charlist' argument ***
|
||||
-- Iteration 1 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 2 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 3 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 4 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 5 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 6 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 7 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 8 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 9 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 10 --
|
||||
|
||||
Warning: trim() expects parameter 2 to be string, array given in %s on line %d
|
||||
NULL
|
||||
-- Iteration 11 --
|
||||
|
||||
Warning: trim() expects parameter 2 to be string, array given in %s on line %d
|
||||
NULL
|
||||
-- Iteration 12 --
|
||||
|
||||
Warning: trim() expects parameter 2 to be string, array given in %s on line %d
|
||||
NULL
|
||||
-- Iteration 13 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 14 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 15 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 16 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 17 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 18 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 19 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 20 --
|
||||
|
||||
Warning: trim() expects parameter 2 to be string, resource given in %s on line %d
|
||||
NULL
|
||||
-- Iteration 21 --
|
||||
string(19) "!---Hello World---!"
|
||||
-- Iteration 22 --
|
||||
string(19) "!---Hello World---!"
|
||||
===DONE===
|
Loading…
Reference in New Issue
Block a user