123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- --TEST--
- Test key(), current(), next() & reset() functions
- --FILE--
- <?php
- /* Prototype & Usage:
- mixed key ( array &$array ) -> returns the index element of the current array position
- mixed current ( array &$array ) -> returns the current element in the array
- mixed next ( array &$array ) -> similar to current() but advances the internal pointer to next element
- mixed reset ( array &$array ) -> Reset the internal pointer to first element
- */
- $basic_arrays = array (
- array(0), // array with element as 0
- array(1), // array with single element
- array(1,2, 3, -1, -2, -3), // array of integers
- array(1.1, 2.2, 3.3, -1.1, -2.2, -3.3), // array of floats
- array('a', 'b', 'c', "ab", "ac", "ad"), // string array
- array("a" => "apple", "b" => "book", "c" => "cook"), // associative array
- array('d' => 'drink', 'p' => 'port', 's' => 'set'), // another associative array
- array(1 => 'One', 2 => 'two', 3 => "three") // associative array with key as integers
- );
-
- $varient_arrays = array (
- array(), // empty array
- array(""), // array with null string
- array(NULL),// array with NULL
- array(null),// array with null
- array(NULL, true, null, "", 1), // mixed array
- array(-1.5 => "test", -2 => "rest", 2.5 => "two",
- "" => "string", 0 => "zero", "" => "" ) // mixed array
- );
- echo "*** Testing basic operations ***\n";
- $loop_count = 1;
- foreach ($basic_arrays as $sub_array ) {
- echo "-- Iteration $loop_count --\n";
- $loop_count++;
- $c = count ($sub_array);
- $c++; // increment by one to create the situation of accessing beyond array size
- while ( $c ) {
- var_dump( current($sub_array)); // current element
- var_dump( key($sub_array) ); // key of the current element
- var_dump( next($sub_array) ); // move to next element
- $c --;
- }
- var_dump( reset($sub_array) ); // reset the internal pointer to first element
- var_dump( key($sub_array) ); // access the array after reset
- var_dump( $sub_array ); // dump the array to see that its intact
- echo "\n";
- }
- echo "\n*** Testing possible variations ***\n";
- $loop_count = 1;
- foreach ($varient_arrays as $sub_array ) {
- echo "-- Iteration $loop_count --\n";
- $loop_count++;
- $c = count ($sub_array);
- $c++; // increment by one to create the situation of accessing beyond array size
- while ( $c ) {
- var_dump( current($sub_array)); // current element
- var_dump( key($sub_array) ); // key of the current element
- var_dump( next($sub_array) ); // move to next element
- $c --;
- }
- var_dump( reset($sub_array) ); // reset the internal pointer to first element
- var_dump( key($sub_array) ); // access the array after reset
- var_dump( $sub_array ); // dump the array to see that its intact
- echo "\n";
- }
- /*test these functions on array which is already unset */
- echo "\n-- Testing variation: when array is unset --\n";
- $unset_array = array (1);
- unset($unset_array);
- var_dump( current($unset_array) );
- var_dump( key($unset_array) );
- var_dump( next($unset_array) );
- var_dump( reset($unset_array) );
- echo "\n*** Testing error conditions ***\n";
- //Zero argument, expected 1 argument
- var_dump( key() );
- var_dump( current() );
- var_dump( reset() );
- var_dump( next() );
- // args more than expected, expected 1 argument
- $temp_array = array(1);
- var_dump( key($temp_array, $temp_array) );
- var_dump( current($temp_array, $temp_array) );
- var_dump( reset($temp_array, $temp_array) );
- var_dump( next($temp_array, $temp_array) );
- // invalid args type, valid argument: array
- $int_var = 1;
- $float_var = 1.5;
- $string = "string";
- var_dump( key($int_var) );
- var_dump( key($float_var) );
- var_dump( key($string) );
- var_dump( current($int_var) );
- var_dump( current($float_var) );
- var_dump( current($string) );
- var_dump( next($int_var) );
- var_dump( next($float_var) );
- var_dump( next($string) );
- var_dump( reset($int_var) );
- var_dump( reset($float_var) );
- var_dump( reset($string) );
- echo "Done\n";
- ?>
- --EXPECTF--
- *** Testing basic operations ***
- -- Iteration 1 --
- int(0)
- int(0)
- bool(false)
- bool(false)
- NULL
- bool(false)
- int(0)
- int(0)
- array(1) {
- [0]=>
- int(0)
- }
- -- Iteration 2 --
- int(1)
- int(0)
- bool(false)
- bool(false)
- NULL
- bool(false)
- int(1)
- int(0)
- array(1) {
- [0]=>
- int(1)
- }
- -- Iteration 3 --
- int(1)
- int(0)
- int(2)
- int(2)
- int(1)
- int(3)
- int(3)
- int(2)
- int(-1)
- int(-1)
- int(3)
- int(-2)
- int(-2)
- int(4)
- int(-3)
- int(-3)
- int(5)
- bool(false)
- bool(false)
- NULL
- bool(false)
- int(1)
- int(0)
- array(6) {
- [0]=>
- int(1)
- [1]=>
- int(2)
- [2]=>
- int(3)
- [3]=>
- int(-1)
- [4]=>
- int(-2)
- [5]=>
- int(-3)
- }
- -- Iteration 4 --
- float(1.1)
- int(0)
- float(2.2)
- float(2.2)
- int(1)
- float(3.3)
- float(3.3)
- int(2)
- float(-1.1)
- float(-1.1)
- int(3)
- float(-2.2)
- float(-2.2)
- int(4)
- float(-3.3)
- float(-3.3)
- int(5)
- bool(false)
- bool(false)
- NULL
- bool(false)
- float(1.1)
- int(0)
- array(6) {
- [0]=>
- float(1.1)
- [1]=>
- float(2.2)
- [2]=>
- float(3.3)
- [3]=>
- float(-1.1)
- [4]=>
- float(-2.2)
- [5]=>
- float(-3.3)
- }
- -- Iteration 5 --
- string(1) "a"
- int(0)
- string(1) "b"
- string(1) "b"
- int(1)
- string(1) "c"
- string(1) "c"
- int(2)
- string(2) "ab"
- string(2) "ab"
- int(3)
- string(2) "ac"
- string(2) "ac"
- int(4)
- string(2) "ad"
- string(2) "ad"
- int(5)
- bool(false)
- bool(false)
- NULL
- bool(false)
- string(1) "a"
- int(0)
- array(6) {
- [0]=>
- string(1) "a"
- [1]=>
- string(1) "b"
- [2]=>
- string(1) "c"
- [3]=>
- string(2) "ab"
- [4]=>
- string(2) "ac"
- [5]=>
- string(2) "ad"
- }
- -- Iteration 6 --
- string(5) "apple"
- string(1) "a"
- string(4) "book"
- string(4) "book"
- string(1) "b"
- string(4) "cook"
- string(4) "cook"
- string(1) "c"
- bool(false)
- bool(false)
- NULL
- bool(false)
- string(5) "apple"
- string(1) "a"
- array(3) {
- ["a"]=>
- string(5) "apple"
- ["b"]=>
- string(4) "book"
- ["c"]=>
- string(4) "cook"
- }
- -- Iteration 7 --
- string(5) "drink"
- string(1) "d"
- string(4) "port"
- string(4) "port"
- string(1) "p"
- string(3) "set"
- string(3) "set"
- string(1) "s"
- bool(false)
- bool(false)
- NULL
- bool(false)
- string(5) "drink"
- string(1) "d"
- array(3) {
- ["d"]=>
- string(5) "drink"
- ["p"]=>
- string(4) "port"
- ["s"]=>
- string(3) "set"
- }
- -- Iteration 8 --
- string(3) "One"
- int(1)
- string(3) "two"
- string(3) "two"
- int(2)
- string(5) "three"
- string(5) "three"
- int(3)
- bool(false)
- bool(false)
- NULL
- bool(false)
- string(3) "One"
- int(1)
- array(3) {
- [1]=>
- string(3) "One"
- [2]=>
- string(3) "two"
- [3]=>
- string(5) "three"
- }
- *** Testing possible variations ***
- -- Iteration 1 --
- bool(false)
- NULL
- bool(false)
- bool(false)
- NULL
- array(0) {
- }
- -- Iteration 2 --
- string(0) ""
- int(0)
- bool(false)
- bool(false)
- NULL
- bool(false)
- string(0) ""
- int(0)
- array(1) {
- [0]=>
- string(0) ""
- }
- -- Iteration 3 --
- NULL
- int(0)
- bool(false)
- bool(false)
- NULL
- bool(false)
- NULL
- int(0)
- array(1) {
- [0]=>
- NULL
- }
- -- Iteration 4 --
- NULL
- int(0)
- bool(false)
- bool(false)
- NULL
- bool(false)
- NULL
- int(0)
- array(1) {
- [0]=>
- NULL
- }
- -- Iteration 5 --
- NULL
- int(0)
- bool(true)
- bool(true)
- int(1)
- NULL
- NULL
- int(2)
- string(0) ""
- string(0) ""
- int(3)
- int(1)
- int(1)
- int(4)
- bool(false)
- bool(false)
- NULL
- bool(false)
- NULL
- int(0)
- array(5) {
- [0]=>
- NULL
- [1]=>
- bool(true)
- [2]=>
- NULL
- [3]=>
- string(0) ""
- [4]=>
- int(1)
- }
- -- Iteration 6 --
- string(4) "test"
- int(-1)
- string(4) "rest"
- string(4) "rest"
- int(-2)
- string(3) "two"
- string(3) "two"
- int(2)
- string(0) ""
- string(0) ""
- string(0) ""
- string(4) "zero"
- string(4) "zero"
- int(0)
- bool(false)
- bool(false)
- NULL
- bool(false)
- string(4) "test"
- int(-1)
- array(5) {
- [-1]=>
- string(4) "test"
- [-2]=>
- string(4) "rest"
- [2]=>
- string(3) "two"
- [""]=>
- string(0) ""
- [0]=>
- string(4) "zero"
- }
- -- Testing variation: when array is unset --
- Warning: current() expects parameter 1 to be array, null given in %s on line %d
- NULL
- Warning: key() expects parameter 1 to be array, null given in %s on line %d
- NULL
- Warning: next() expects parameter 1 to be array, null given in %s on line %d
- NULL
- Warning: reset() expects parameter 1 to be array, null given in %s on line %d
- NULL
- *** Testing error conditions ***
- Warning: key() expects exactly 1 parameter, 0 given in %s on line %d
- NULL
- Warning: current() expects exactly 1 parameter, 0 given in %s on line %d
- NULL
- Warning: reset() expects exactly 1 parameter, 0 given in %s on line %d
- NULL
- Warning: next() expects exactly 1 parameter, 0 given in %s on line %d
- NULL
- Warning: key() expects exactly 1 parameter, 2 given in %s on line %d
- NULL
- Warning: current() expects exactly 1 parameter, 2 given in %s on line %d
- NULL
- Warning: reset() expects exactly 1 parameter, 2 given in %s on line %d
- NULL
- Warning: next() expects exactly 1 parameter, 2 given in %s on line %d
- NULL
- Warning: key() expects parameter 1 to be array, integer given in %s on line %d
- NULL
- Warning: key() expects parameter 1 to be array, double given in %s on line %d
- NULL
- Warning: key() expects parameter 1 to be array, string given in %s on line %d
- NULL
- Warning: current() expects parameter 1 to be array, integer given in %s on line %d
- NULL
- Warning: current() expects parameter 1 to be array, double given in %s on line %d
- NULL
- Warning: current() expects parameter 1 to be array, string given in %s on line %d
- NULL
- Warning: next() expects parameter 1 to be array, integer given in %s on line %d
- NULL
- Warning: next() expects parameter 1 to be array, double given in %s on line %d
- NULL
- Warning: next() expects parameter 1 to be array, string given in %s on line %d
- NULL
- Warning: reset() expects parameter 1 to be array, integer given in %s on line %d
- NULL
- Warning: reset() expects parameter 1 to be array, double given in %s on line %d
- NULL
- Warning: reset() expects parameter 1 to be array, string given in %s on line %d
- NULL
- Done
|