New testcases for join() function

This commit is contained in:
Raghubansh Kumar 2007-09-29 16:52:54 +00:00
parent 3253c3b6dd
commit b9ccaf44fb
8 changed files with 607 additions and 0 deletions

View file

@ -0,0 +1,43 @@
--TEST--
Test join() function : basic functionality
--FILE--
<?php
/* Prototype : string join( string $glue, array $pieces )
* Description: Join array elements with a string
* Source code: ext/standard/string.c
* Alias of function: implode()
*/
echo "*** Testing join() : basic functionality ***\n";
// Initialize all required variables
$glue = ',';
$pieces = array(1, 2, 3, 4);
// pieces as arry with numeric values
var_dump( join($glue, $pieces) );
// pieces as array with strings values
$glue = ", "; // multiple car as glue
$pieces = array("Red", "Green", "Blue", "Black", "White");
var_dump( join($glue, $pieces) );
// pices as associative array (numeric values)
$pieces = array("Hour" => 10, "Minute" => 20, "Second" => 40);
$glue = ':';
var_dump( join($glue, $pieces) );
// pices as associative array (string/numeric values)
$pieces = array("Day" => 'Friday', "Month" => "September", "Year" => 2007);
$glue = '/';
var_dump( join($glue, $pieces) );
echo "Done\n";
?>
--EXPECTF--
*** Testing join() : basic functionality ***
string(7) "1,2,3,4"
string(30) "Red, Green, Blue, Black, White"
string(8) "10:20:40"
string(21) "Friday/September/2007"
Done

View file

@ -0,0 +1,50 @@
--TEST--
Test join() function : error conditions(Bug#42789)
--FILE--
<?php
/* Prototype : string join( string $glue, array $pieces )
* Description: Join array elements with a string
* Source code: ext/standard/string.c
* Alias of function: implode()
*/
echo "*** Testing join() : error conditions ***\n";
// Zero argument
echo "\n-- Testing join() function with Zero arguments --\n";
var_dump( join() );
// More than expected number of arguments
echo "\n-- Testing join() function with more than expected no. of arguments --\n";
$glue = 'string_val';
$pieces = array(1, 2);
$extra_arg = 10;
var_dump( join($glue, $pieces, $extra_arg) );
// Less than expected number of arguments
echo "\n-- Testing join() with less than expected no. of arguments --\n";
$glue = 'string_val';
var_dump( join($glue));
echo "Done\n";
?>
--EXPECTF--
*** Testing join() : error conditions ***
-- Testing join() function with Zero arguments --
Warning: Wrong parameter count for join() in %s on line %d
NULL
-- Testing join() function with more than expected no. of arguments --
Warning: Wrong parameter count for join() in %s on line %d
NULL
-- Testing join() with less than expected no. of arguments --
Warning: join(): Argument to join must be an array in %s on line %d
NULL
Done

View file

@ -0,0 +1,167 @@
--TEST--
Test join() function : usage variations - unexpected values for 'glue' argument
--FILE--
<?php
/* Prototype : string join( string $glue, array $pieces )
* Description: Join array elements with a string
* Source code: ext/standard/string.c
* Alias of function: implode()
*/
/*
* testing join() by passing different unexpected value for glue argument
*/
echo "*** Testing join() : usage variations ***\n";
// initialize all required variables
$pieces = array("element1", "element2");
// get an unset variable
$unset_var = 'string_val';
unset($unset_var);
// get a resource variable
$fp = fopen(__FILE__, "r");
// define a class
class test
{
var $t = 10;
function __toString() {
return "testObject";
}
}
// array with different values
$values = array (
// integer values
0,
1,
12345,
-2345,
// float values
10.5,
-10.5,
10.5e10,
10.6E-10,
.5,
// array values
array(),
array(0),
array(1),
array(1, 2),
array('color' => 'red', 'item' => 'pen'),
// boolean values
true,
false,
TRUE,
FALSE,
// objects
new test(),
// empty string
"",
'',
// null vlaues
NULL,
null,
// resource variable
$fp,
// undefined variable
@$undefined_var,
// unset variable
@$unset_var
);
// loop through each element of the array and check the working of join()
// when $glue arugment is supplied with different values
echo "\n--- Testing join() by supplying different values for 'glue' argument ---\n";
$counter = 1;
for($index = 0; $index < count($values); $index ++) {
echo "-- Iteration $counter --\n";
$glue = $values [$index];
var_dump( join($glue, $pieces) );
$counter ++;
}
echo "Done\n";
?>
--EXPECTF--
*** Testing join() : usage variations ***
--- Testing join() by supplying different values for 'glue' argument ---
-- Iteration 1 --
string(17) "element10element2"
-- Iteration 2 --
string(17) "element11element2"
-- Iteration 3 --
string(21) "element112345element2"
-- Iteration 4 --
string(21) "element1-2345element2"
-- Iteration 5 --
string(20) "element110.5element2"
-- Iteration 6 --
string(21) "element1-10.5element2"
-- Iteration 7 --
string(28) "element1105000000000element2"
-- Iteration 8 --
string(23) "element11.06E-9element2"
-- Iteration 9 --
string(19) "element10.5element2"
-- Iteration 10 --
Notice: Array to string conversion in %s on line %d
string(0) ""
-- Iteration 11 --
Notice: Array to string conversion in %s on line %d
string(1) "0"
-- Iteration 12 --
Notice: Array to string conversion in %s on line %d
string(1) "1"
-- Iteration 13 --
Notice: Array to string conversion in %s on line %d
string(7) "1Array2"
-- Iteration 14 --
Notice: Array to string conversion in %s on line %d
string(11) "redArraypen"
-- Iteration 15 --
string(17) "element11element2"
-- Iteration 16 --
string(16) "element1element2"
-- Iteration 17 --
string(17) "element11element2"
-- Iteration 18 --
string(16) "element1element2"
-- Iteration 19 --
string(26) "element1testObjectelement2"
-- Iteration 20 --
string(16) "element1element2"
-- Iteration 21 --
string(16) "element1element2"
-- Iteration 22 --
string(16) "element1element2"
-- Iteration 23 --
string(16) "element1element2"
-- Iteration 24 --
string(30) "element1Resource id #%delement2"
-- Iteration 25 --
string(16) "element1element2"
-- Iteration 26 --
string(16) "element1element2"
Done

View file

@ -0,0 +1,198 @@
--TEST--
Test join() function : usage variations - unexpected values for 'pieces' argument(Bug#42789)
--FILE--
<?php
/* Prototype : string join( string $glue, array $pieces )
* Description: Join array elements with a string
* Source code: ext/standard/string.c
* Alias of function: implode()
*/
/*
* test join() by passing different unexpected value for pieces argument
*/
echo "*** Testing join() : usage variations ***\n";
// initialize all required variables
$glue = '::';
// get an unset variable
$unset_var = array(1, 2);
unset($unset_var);
// get a resouce variable
$fp = fopen(__FILE__, "r");
// define a class
class test
{
var $t = 10;
var $p = 10;
function __toString() {
return "testObject";
}
}
// array with different values
$values = array (
// integer values
0,
1,
12345,
-2345,
// float values
10.5,
-10.5,
10.5e10,
10.6E-10,
.5,
// boolean values
true,
false,
TRUE,
FALSE,
// string values
"string",
'string',
// objects
new test(),
// empty string
"",
'',
// null vlaues
NULL,
null,
// resource variable
$fp,
// undefined variable
@$undefined_var,
// unset variable
@$unset_var
);
// loop through each element of the array and check the working of join()
// when $pieces arugment is supplied with different values
echo "\n--- Testing join() by supplying different values for 'pieces' argument ---\n";
$counter = 1;
for($index = 0; $index < count($values); $index ++) {
echo "-- Iteration $counter --\n";
$pieces = $values [$index];
var_dump( join($glue, $pieces) );
$counter ++;
}
// close the resources used
fclose($fp);
echo "Done\n";
?>
--EXPECTF--
*** Testing join() : usage variations ***
--- Testing join() by supplying different values for 'pieces' argument ---
-- Iteration 1 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 2 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 3 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 4 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 5 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 6 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 7 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 8 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 9 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 10 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 11 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 12 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 13 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 14 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 15 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 16 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 17 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 18 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 19 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 20 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 21 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 22 --
Warning: join(): Bad arguments in %s on line %d
NULL
-- Iteration 23 --
Warning: join(): Bad arguments in %s on line %d
NULL
Done

View file

@ -0,0 +1,99 @@
--TEST--
Test join() function : usage variations - different values for 'pieces' argument
--FILE--
<?php
/* Prototype : string join( string $glue, array $pieces )
* Description: Join array elements with a string
* Source code: ext/standard/string.c
* Alias of function: implode()
*/
/*
* test join() by giving different pieces values
*/
echo "*** Testing join() : usage variations ***\n";
$pieces_arrays = array (
array(1, 2), // array with default keys and numrice values
array(1.1, 2.2), // array with default keys & float values
array( array(2), array(1)), // sub arrays
array(false,true), // array with default keys and boolean values
array(), // empty array
array(NULL), // array with NULL
array("a","aaaa","b","bbbb","c","ccccc"),
// associative arrays
array(1 => "one", 2 => "two", 3 => "three"), // explicit numeric keys, string values
array("one" => 1, "two" => 2, "three" => 3 ), // string keys & numeric values
array( 1 => 10, 2 => 20, 4 => 40, 3 => 30), // explicit numeric keys and numeric values
array( "one" => "ten", "two" => "twenty", "three" => "thirty"), // string key/value
array("one" => 1, 2 => "two", 4 => "four"), //mixed
// associative array, containing null/empty/boolean values as key/value
array(NULL => "NULL", null => "null", "NULL" => NULL, "null" => null),
array(true => "true", false => "false", "false" => false, "true" => true),
array("" => "emptyd", '' => 'emptys', "emptyd" => "", 'emptys' => ''),
array(1 => '', 2 => "", 3 => NULL, 4 => null, 5 => false, 6 => true),
array('' => 1, "" => 2, NULL => 3, null => 4, false => 5, true => 6),
// array with repetative keys
array("One" => 1, "two" => 2, "One" => 10, "two" => 20, "three" => 3)
);
// a multichar glue value
$glue = "], [";
// loop through each $pieces_arrays element and call join()
$iteration = 1;
for($index = 0; $index < count($pieces_arrays); $index ++) {
echo "-- Iteration $iteration --\n";
var_dump( join($glue, $pieces_arrays[$index]) );
$iteration ++;
}
echo "Done\n";
?>
--EXPECTF--
*** Testing join() : usage variations ***
-- Iteration 1 --
string(6) "1], [2"
-- Iteration 2 --
string(10) "1.1], [2.2"
-- Iteration 3 --
Notice: Array to string conversion in %s on line %d
Notice: Array to string conversion in %s on line %d
string(14) "Array], [Array"
-- Iteration 4 --
string(5) "], [1"
-- Iteration 5 --
string(0) ""
-- Iteration 6 --
string(0) ""
-- Iteration 7 --
string(36) "a], [aaaa], [b], [bbbb], [c], [ccccc"
-- Iteration 8 --
string(19) "one], [two], [three"
-- Iteration 9 --
string(11) "1], [2], [3"
-- Iteration 10 --
string(20) "10], [20], [40], [30"
-- Iteration 11 --
string(23) "ten], [twenty], [thirty"
-- Iteration 12 --
string(16) "1], [two], [four"
-- Iteration 13 --
string(12) "null], [], ["
-- Iteration 14 --
string(22) "true], [false], [], [1"
-- Iteration 15 --
string(14) "emptys], [], ["
-- Iteration 16 --
string(21) "], [], [], [], [], [1"
-- Iteration 17 --
string(11) "4], [5], [6"
-- Iteration 18 --
string(13) "10], [20], [3"
Done

Binary file not shown.

View file

@ -0,0 +1,50 @@
--TEST--
Test join() function : usage variations - sub array as argument
--FILE--
<?php
/* Prototype : string join( string $glue, array $pieces )
* Description: Join array elements with a string
* Source code: ext/standard/string.c
* Alias of function: implode()
*/
/*
* test join() by passing pieces as array containing sub array(s)
*/
echo "*** Testing implode() : usage variations - sub arrays ***\n";
$sub_array = array(array(1,2,3,4), array(1 => "one", 2 => "two"), "PHP", 50);
// pieces as array containing sub array
var_dump( join("TEST", $sub_array) );
// glue as array & pieces as array containing sub array
var_dump( join(array(1, 2, 3, 4), $sub_array) );
// numeric value as glue, pieces as array containg sub array
var_dump( join(2, $sub_array) );
// using directly the sub_array as pieces
var_dump( join(", ", $sub_array[0]) );
var_dump( join(", ", $sub_array[1]) );
echo "Done\n";
?>
--EXPECTF--
*** Testing implode() : usage variations - sub arrays ***
Notice: Array to string conversion in %s on line %d
Notice: Array to string conversion in %s on line %d
string(27) "ArrayTESTArrayTESTPHPTEST50"
Notice: Array to string conversion in %s on line %d
string(19) "1Array2Array3Array4"
Notice: Array to string conversion in %s on line %d
Notice: Array to string conversion in %s on line %d
string(18) "Array2Array2PHP250"
string(10) "1, 2, 3, 4"
string(8) "one, two"
Done

Binary file not shown.