Annotation of embedaddon/php/ext/standard/tests/array/009.phpt, revision 1.1.1.2

1.1       misho       1: --TEST--
                      2: Test key(), current(), next() & reset() functions
                      3: --FILE--
                      4: <?php
                      5: /* Prototype & Usage: 
                      6:    mixed key ( array &$array ) -> returns the index element of the current array position
                      7:    mixed current ( array &$array ) -> returns the current element in the array
                      8:    mixed next ( array &$array ) -> similar to current() but advances the internal pointer to next element
                      9:    mixed reset ( array &$array ) -> Reset the internal pointer to first element
                     10: */
                     11: 
                     12: $basic_arrays = array (
                     13:   array(0),  // array with element as 0
                     14:   array(1),  // array with single element
                     15:   array(1,2, 3, -1, -2, -3),               // array of integers
                     16:   array(1.1, 2.2, 3.3, -1.1, -2.2, -3.3),  // array of floats
                     17:   array('a', 'b', 'c', "ab", "ac", "ad"),  // string array
                     18:   array("a" => "apple", "b" => "book", "c" => "cook"),  // associative array
                     19:   array('d' => 'drink', 'p' => 'port', 's' => 'set'),   // another associative array
                     20:   array(1 => 'One', 2 => 'two', 3 => "three")           // associative array with key as integers
                     21: );
                     22:             
                     23: $varient_arrays = array (
                     24:    array(),    // empty array
                     25:    array(""),  // array with null string
                     26:    array(NULL),// array with NULL 
                     27:    array(null),// array with null
                     28:    array(NULL, true, null, "", 1), // mixed array
                     29:    array(-1.5 => "test", -2 => "rest", 2.5 => "two", 
                     30:          "" => "string", 0 => "zero", "" => "" ) // mixed array
                     31: );  
                     32: 
                     33: echo "*** Testing basic operations ***\n";
                     34: $loop_count = 1;
                     35: foreach ($basic_arrays as $sub_array )  {
                     36:   echo "-- Iteration $loop_count --\n";
                     37:   $loop_count++;
                     38:   $c = count ($sub_array);
                     39:   $c++; // increment by one to create the situation of accessing beyond array size
                     40:   while ( $c ) {
                     41:     var_dump( current($sub_array)); // current element
                     42:     var_dump( key($sub_array) );    // key of the current element
                     43:     var_dump( next($sub_array) );   // move to next element
                     44:     $c --;
                     45:   }
                     46:   var_dump( reset($sub_array) );    // reset the internal pointer to first element
                     47:   var_dump( key($sub_array) );      // access the array after reset
                     48:   var_dump( $sub_array );           // dump the array to see that its intact
                     49: 
                     50:   echo "\n";
                     51: }
                     52: 
                     53: echo "\n*** Testing possible variations ***\n";
                     54: $loop_count = 1;
                     55: foreach ($varient_arrays as $sub_array )  {
                     56:   echo "-- Iteration $loop_count --\n";
                     57:   $loop_count++;
                     58:   $c = count ($sub_array);
                     59:   $c++; // increment by one to create the situation of accessing beyond array size
                     60:   while ( $c ) {
                     61:     var_dump( current($sub_array)); // current element
                     62:     var_dump( key($sub_array) );    // key of the current element
                     63:     var_dump( next($sub_array) );   // move to next element
                     64:     $c --;
                     65:   }
                     66:   var_dump( reset($sub_array) );    // reset the internal pointer to first element
                     67:   var_dump( key($sub_array) );      // access the array after reset
                     68:   var_dump( $sub_array );           // dump the array to see that its intact
                     69:   echo "\n";
                     70: }
                     71: 
                     72: /*test these functions on array which is already unset */ 
                     73: echo "\n-- Testing variation: when array is unset --\n";
                     74: $unset_array = array (1);
                     75: unset($unset_array);
                     76: 
                     77: var_dump( current($unset_array) );
                     78: var_dump( key($unset_array) );
                     79: var_dump( next($unset_array) );
                     80: var_dump( reset($unset_array) );
                     81: 
                     82: 
                     83: echo "\n*** Testing error conditions ***\n";
                     84: //Zero argument, expected 1 argument
                     85: var_dump( key() );
                     86: var_dump( current() );
                     87: var_dump( reset() );
                     88: var_dump( next() );
                     89: 
                     90: // args more than expected, expected 1 argument
                     91: $temp_array = array(1);
                     92: var_dump( key($temp_array, $temp_array) );
                     93: var_dump( current($temp_array, $temp_array) );
                     94: var_dump( reset($temp_array, $temp_array) );
                     95: var_dump( next($temp_array, $temp_array) );
                     96: 
1.1.1.2 ! misho      97: // invalid args type, valid argument: array 
1.1       misho      98: $int_var = 1;
                     99: $float_var = 1.5;
                    100: $string = "string";
                    101: var_dump( key($int_var) );
                    102: var_dump( key($float_var) );
                    103: var_dump( key($string) );
                    104: 
                    105: var_dump( current($int_var) );
                    106: var_dump( current($float_var) );
                    107: var_dump( current($string) );
                    108: 
                    109: var_dump( next($int_var) );
                    110: var_dump( next($float_var) );
                    111: var_dump( next($string) );
                    112: 
                    113: var_dump( reset($int_var) );
                    114: var_dump( reset($float_var) );
                    115: var_dump( reset($string) );
                    116: 
                    117: echo "Done\n";
                    118: ?>
                    119: --EXPECTF--    
                    120: *** Testing basic operations ***
                    121: -- Iteration 1 --
                    122: int(0)
                    123: int(0)
                    124: bool(false)
                    125: bool(false)
                    126: NULL
                    127: bool(false)
                    128: int(0)
                    129: int(0)
                    130: array(1) {
                    131:   [0]=>
                    132:   int(0)
                    133: }
                    134: 
                    135: -- Iteration 2 --
                    136: int(1)
                    137: int(0)
                    138: bool(false)
                    139: bool(false)
                    140: NULL
                    141: bool(false)
                    142: int(1)
                    143: int(0)
                    144: array(1) {
                    145:   [0]=>
                    146:   int(1)
                    147: }
                    148: 
                    149: -- Iteration 3 --
                    150: int(1)
                    151: int(0)
                    152: int(2)
                    153: int(2)
                    154: int(1)
                    155: int(3)
                    156: int(3)
                    157: int(2)
                    158: int(-1)
                    159: int(-1)
                    160: int(3)
                    161: int(-2)
                    162: int(-2)
                    163: int(4)
                    164: int(-3)
                    165: int(-3)
                    166: int(5)
                    167: bool(false)
                    168: bool(false)
                    169: NULL
                    170: bool(false)
                    171: int(1)
                    172: int(0)
                    173: array(6) {
                    174:   [0]=>
                    175:   int(1)
                    176:   [1]=>
                    177:   int(2)
                    178:   [2]=>
                    179:   int(3)
                    180:   [3]=>
                    181:   int(-1)
                    182:   [4]=>
                    183:   int(-2)
                    184:   [5]=>
                    185:   int(-3)
                    186: }
                    187: 
                    188: -- Iteration 4 --
                    189: float(1.1)
                    190: int(0)
                    191: float(2.2)
                    192: float(2.2)
                    193: int(1)
                    194: float(3.3)
                    195: float(3.3)
                    196: int(2)
                    197: float(-1.1)
                    198: float(-1.1)
                    199: int(3)
                    200: float(-2.2)
                    201: float(-2.2)
                    202: int(4)
                    203: float(-3.3)
                    204: float(-3.3)
                    205: int(5)
                    206: bool(false)
                    207: bool(false)
                    208: NULL
                    209: bool(false)
                    210: float(1.1)
                    211: int(0)
                    212: array(6) {
                    213:   [0]=>
                    214:   float(1.1)
                    215:   [1]=>
                    216:   float(2.2)
                    217:   [2]=>
                    218:   float(3.3)
                    219:   [3]=>
                    220:   float(-1.1)
                    221:   [4]=>
                    222:   float(-2.2)
                    223:   [5]=>
                    224:   float(-3.3)
                    225: }
                    226: 
                    227: -- Iteration 5 --
                    228: string(1) "a"
                    229: int(0)
                    230: string(1) "b"
                    231: string(1) "b"
                    232: int(1)
                    233: string(1) "c"
                    234: string(1) "c"
                    235: int(2)
                    236: string(2) "ab"
                    237: string(2) "ab"
                    238: int(3)
                    239: string(2) "ac"
                    240: string(2) "ac"
                    241: int(4)
                    242: string(2) "ad"
                    243: string(2) "ad"
                    244: int(5)
                    245: bool(false)
                    246: bool(false)
                    247: NULL
                    248: bool(false)
                    249: string(1) "a"
                    250: int(0)
                    251: array(6) {
                    252:   [0]=>
                    253:   string(1) "a"
                    254:   [1]=>
                    255:   string(1) "b"
                    256:   [2]=>
                    257:   string(1) "c"
                    258:   [3]=>
                    259:   string(2) "ab"
                    260:   [4]=>
                    261:   string(2) "ac"
                    262:   [5]=>
                    263:   string(2) "ad"
                    264: }
                    265: 
                    266: -- Iteration 6 --
                    267: string(5) "apple"
                    268: string(1) "a"
                    269: string(4) "book"
                    270: string(4) "book"
                    271: string(1) "b"
                    272: string(4) "cook"
                    273: string(4) "cook"
                    274: string(1) "c"
                    275: bool(false)
                    276: bool(false)
                    277: NULL
                    278: bool(false)
                    279: string(5) "apple"
                    280: string(1) "a"
                    281: array(3) {
                    282:   ["a"]=>
                    283:   string(5) "apple"
                    284:   ["b"]=>
                    285:   string(4) "book"
                    286:   ["c"]=>
                    287:   string(4) "cook"
                    288: }
                    289: 
                    290: -- Iteration 7 --
                    291: string(5) "drink"
                    292: string(1) "d"
                    293: string(4) "port"
                    294: string(4) "port"
                    295: string(1) "p"
                    296: string(3) "set"
                    297: string(3) "set"
                    298: string(1) "s"
                    299: bool(false)
                    300: bool(false)
                    301: NULL
                    302: bool(false)
                    303: string(5) "drink"
                    304: string(1) "d"
                    305: array(3) {
                    306:   ["d"]=>
                    307:   string(5) "drink"
                    308:   ["p"]=>
                    309:   string(4) "port"
                    310:   ["s"]=>
                    311:   string(3) "set"
                    312: }
                    313: 
                    314: -- Iteration 8 --
                    315: string(3) "One"
                    316: int(1)
                    317: string(3) "two"
                    318: string(3) "two"
                    319: int(2)
                    320: string(5) "three"
                    321: string(5) "three"
                    322: int(3)
                    323: bool(false)
                    324: bool(false)
                    325: NULL
                    326: bool(false)
                    327: string(3) "One"
                    328: int(1)
                    329: array(3) {
                    330:   [1]=>
                    331:   string(3) "One"
                    332:   [2]=>
                    333:   string(3) "two"
                    334:   [3]=>
                    335:   string(5) "three"
                    336: }
                    337: 
                    338: 
                    339: *** Testing possible variations ***
                    340: -- Iteration 1 --
                    341: bool(false)
                    342: NULL
                    343: bool(false)
                    344: bool(false)
                    345: NULL
                    346: array(0) {
                    347: }
                    348: 
                    349: -- Iteration 2 --
                    350: string(0) ""
                    351: int(0)
                    352: bool(false)
                    353: bool(false)
                    354: NULL
                    355: bool(false)
                    356: string(0) ""
                    357: int(0)
                    358: array(1) {
                    359:   [0]=>
                    360:   string(0) ""
                    361: }
                    362: 
                    363: -- Iteration 3 --
                    364: NULL
                    365: int(0)
                    366: bool(false)
                    367: bool(false)
                    368: NULL
                    369: bool(false)
                    370: NULL
                    371: int(0)
                    372: array(1) {
                    373:   [0]=>
                    374:   NULL
                    375: }
                    376: 
                    377: -- Iteration 4 --
                    378: NULL
                    379: int(0)
                    380: bool(false)
                    381: bool(false)
                    382: NULL
                    383: bool(false)
                    384: NULL
                    385: int(0)
                    386: array(1) {
                    387:   [0]=>
                    388:   NULL
                    389: }
                    390: 
                    391: -- Iteration 5 --
                    392: NULL
                    393: int(0)
                    394: bool(true)
                    395: bool(true)
                    396: int(1)
                    397: NULL
                    398: NULL
                    399: int(2)
                    400: string(0) ""
                    401: string(0) ""
                    402: int(3)
                    403: int(1)
                    404: int(1)
                    405: int(4)
                    406: bool(false)
                    407: bool(false)
                    408: NULL
                    409: bool(false)
                    410: NULL
                    411: int(0)
                    412: array(5) {
                    413:   [0]=>
                    414:   NULL
                    415:   [1]=>
                    416:   bool(true)
                    417:   [2]=>
                    418:   NULL
                    419:   [3]=>
                    420:   string(0) ""
                    421:   [4]=>
                    422:   int(1)
                    423: }
                    424: 
                    425: -- Iteration 6 --
                    426: string(4) "test"
                    427: int(-1)
                    428: string(4) "rest"
                    429: string(4) "rest"
                    430: int(-2)
                    431: string(3) "two"
                    432: string(3) "two"
                    433: int(2)
                    434: string(0) ""
                    435: string(0) ""
                    436: string(0) ""
                    437: string(4) "zero"
                    438: string(4) "zero"
                    439: int(0)
                    440: bool(false)
                    441: bool(false)
                    442: NULL
                    443: bool(false)
                    444: string(4) "test"
                    445: int(-1)
                    446: array(5) {
                    447:   [-1]=>
                    448:   string(4) "test"
                    449:   [-2]=>
                    450:   string(4) "rest"
                    451:   [2]=>
                    452:   string(3) "two"
                    453:   [""]=>
                    454:   string(0) ""
                    455:   [0]=>
                    456:   string(4) "zero"
                    457: }
                    458: 
                    459: 
                    460: -- Testing variation: when array is unset --
                    461: 
                    462: Warning: current() expects parameter 1 to be array, null given in %s on line %d
                    463: NULL
                    464: 
                    465: Warning: key() expects parameter 1 to be array, null given in %s on line %d
                    466: NULL
                    467: 
                    468: Warning: next() expects parameter 1 to be array, null given in %s on line %d
                    469: NULL
                    470: 
                    471: Warning: reset() expects parameter 1 to be array, null given in %s on line %d
                    472: NULL
                    473: 
                    474: *** Testing error conditions ***
                    475: 
                    476: Warning: key() expects exactly 1 parameter, 0 given in %s on line %d
                    477: NULL
                    478: 
                    479: Warning: current() expects exactly 1 parameter, 0 given in %s on line %d
                    480: NULL
                    481: 
                    482: Warning: reset() expects exactly 1 parameter, 0 given in %s on line %d
                    483: NULL
                    484: 
                    485: Warning: next() expects exactly 1 parameter, 0 given in %s on line %d
                    486: NULL
                    487: 
                    488: Warning: key() expects exactly 1 parameter, 2 given in %s on line %d
                    489: NULL
                    490: 
                    491: Warning: current() expects exactly 1 parameter, 2 given in %s on line %d
                    492: NULL
                    493: 
                    494: Warning: reset() expects exactly 1 parameter, 2 given in %s on line %d
                    495: NULL
                    496: 
                    497: Warning: next() expects exactly 1 parameter, 2 given in %s on line %d
                    498: NULL
                    499: 
                    500: Warning: key() expects parameter 1 to be array, integer given in %s on line %d
                    501: NULL
                    502: 
                    503: Warning: key() expects parameter 1 to be array, double given in %s on line %d
                    504: NULL
                    505: 
                    506: Warning: key() expects parameter 1 to be array, string given in %s on line %d
                    507: NULL
                    508: 
                    509: Warning: current() expects parameter 1 to be array, integer given in %s on line %d
                    510: NULL
                    511: 
                    512: Warning: current() expects parameter 1 to be array, double given in %s on line %d
                    513: NULL
                    514: 
                    515: Warning: current() expects parameter 1 to be array, string given in %s on line %d
                    516: NULL
                    517: 
                    518: Warning: next() expects parameter 1 to be array, integer given in %s on line %d
                    519: NULL
                    520: 
                    521: Warning: next() expects parameter 1 to be array, double given in %s on line %d
                    522: NULL
                    523: 
                    524: Warning: next() expects parameter 1 to be array, string given in %s on line %d
                    525: NULL
                    526: 
                    527: Warning: reset() expects parameter 1 to be array, integer given in %s on line %d
                    528: NULL
                    529: 
                    530: Warning: reset() expects parameter 1 to be array, double given in %s on line %d
                    531: NULL
                    532: 
                    533: Warning: reset() expects parameter 1 to be array, string given in %s on line %d
                    534: NULL
                    535: Done

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>