Annotation of embedaddon/php/ext/standard/tests/array/005.phpt, revision 1.1.1.1

1.1       misho       1: --TEST--
                      2: Test array_shift() function
                      3: --FILE--
                      4: <?php
                      5: /* Prototype: mixed array_shift( array &array );
                      6:  * Description: Shifts the first value of the array off and returns it.
                      7:  */
                      8: 
                      9: array_shift($GLOBALS);
                     10: 
                     11: $empty_array = array();
                     12: $number = 5;
                     13: $str = "abc";
                     14: 
                     15: 
                     16: /* Various combinations of arrays to be used for the test */
                     17: $mixed_array = array(
                     18:   array(),
                     19:   array( 1,2,3,4,5,6,7,8,9 ), 
                     20:   array( "One", "_Two", "Three", "Four", "Five" ),
                     21:   array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ),
                     22:   array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ),
                     23:   array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ),
                     24:   array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ),
                     25:   array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2.4 => "float", "F" => "FFF",
                     26:          "blank" => "", 3.7 => 3.7, 5.4 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ),
                     27:   array( 12, "name", 'age', '45' ),
                     28:   array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ),
                     29:   array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6,
                     30:                               5.4 => 54, 5.7 => 57, "5.4" => 554, "5.7" => 557 )
                     31: );
                     32: 
                     33: /* Testing Error Conditions */
                     34: echo "\n*** Testing Error Conditions ***\n";
                     35: 
                     36: /* Zero argument  */
                     37: var_dump( array_shift() );
                     38: 
                     39: /* Scalar argument */
                     40: var_dump( array_shift($number) );
                     41: 
                     42: /* String argument */
                     43: var_dump( array_shift($str) );
                     44: 
                     45: /* Invalid Number of arguments */
                     46: var_dump( array_shift($mixed_array[1],$mixed_array[2]) );
                     47: 
                     48: /* Empty Array as argument */
                     49: var_dump( array_shift($empty_array) );
                     50: 
                     51: /* Loop to test normal functionality with different arrays inputs */
                     52: echo "\n*** Testing with various array inputs ***\n";
                     53: 
                     54: $counter = 1;
                     55: foreach( $mixed_array as $sub_array ) { 
                     56:   echo "\n-- Input Array for Iteration $counter is -- \n";
                     57:   print_r( $sub_array );
                     58:   echo "\nOutput after shift is :\n";
                     59:   var_dump( array_shift($sub_array) );
                     60:   $counter++;
                     61: } 
                     62: 
                     63: /*Checking for internal array pointer beint reset when shift is called */
                     64: 
                     65: echo"\n*** Checking for internal array pointer being reset when shift is called ***\n";
                     66: 
                     67: echo "\nCurrent Element is : ";
                     68: var_dump( current($mixed_array[1]) );
                     69: 
                     70: echo "\nNext Element is : ";
                     71: var_dump( next($mixed_array[1]) );
                     72: 
                     73: echo "\nNext Element is : ";
                     74: var_dump( next($mixed_array[1]) );
                     75: 
                     76: echo "\nshifted Element is : ";
                     77: var_dump( array_shift($mixed_array[1]) );
                     78: 
                     79: echo "\nCurrent Element after shift operation is: ";
                     80: var_dump( current($mixed_array[1]) );
                     81: 
                     82: echo"Done";
                     83: ?>
                     84: --EXPECTF--
                     85: *** Testing Error Conditions ***
                     86: 
                     87: Warning: array_shift() expects exactly 1 parameter, 0 given in %s on line %d
                     88: NULL
                     89: 
                     90: Warning: array_shift() expects parameter 1 to be array, integer given in %s on line %d
                     91: NULL
                     92: 
                     93: Warning: array_shift() expects parameter 1 to be array, string given in %s on line %d
                     94: NULL
                     95: 
                     96: Warning: array_shift() expects exactly 1 parameter, 2 given in %s on line %d
                     97: NULL
                     98: NULL
                     99: 
                    100: *** Testing with various array inputs ***
                    101: 
                    102: -- Input Array for Iteration 1 is -- 
                    103: Array
                    104: (
                    105: )
                    106: 
                    107: Output after shift is :
                    108: NULL
                    109: 
                    110: -- Input Array for Iteration 2 is -- 
                    111: Array
                    112: (
                    113:     [0] => 1
                    114:     [1] => 2
                    115:     [2] => 3
                    116:     [3] => 4
                    117:     [4] => 5
                    118:     [5] => 6
                    119:     [6] => 7
                    120:     [7] => 8
                    121:     [8] => 9
                    122: )
                    123: 
                    124: Output after shift is :
                    125: int(1)
                    126: 
                    127: -- Input Array for Iteration 3 is -- 
                    128: Array
                    129: (
                    130:     [0] => One
                    131:     [1] => _Two
                    132:     [2] => Three
                    133:     [3] => Four
                    134:     [4] => Five
                    135: )
                    136: 
                    137: Output after shift is :
                    138: string(3) "One"
                    139: 
                    140: -- Input Array for Iteration 4 is -- 
                    141: Array
                    142: (
                    143:     [0] => 6
                    144:     [1] => six
                    145:     [2] => 7
                    146:     [3] => seven
                    147:     [4] => 8
                    148:     [5] => eight
                    149:     [6] => 9
                    150:     [7] => nine
                    151: )
                    152: 
                    153: Output after shift is :
                    154: int(6)
                    155: 
                    156: -- Input Array for Iteration 5 is -- 
                    157: Array
                    158: (
                    159:     [a] => aaa
                    160:     [A] => AAA
                    161:     [c] => ccc
                    162:     [d] => ddd
                    163:     [e] => eee
                    164: )
                    165: 
                    166: Output after shift is :
                    167: string(3) "aaa"
                    168: 
                    169: -- Input Array for Iteration 6 is -- 
                    170: Array
                    171: (
                    172:     [1] => one
                    173:     [2] => two
                    174:     [3] => three
                    175:     [4] => four
                    176:     [5] => five
                    177: )
                    178: 
                    179: Output after shift is :
                    180: string(3) "one"
                    181: 
                    182: -- Input Array for Iteration 7 is -- 
                    183: Array
                    184: (
                    185:     [1] => one
                    186:     [2] => two
                    187:     [3] => 7
                    188:     [4] => four
                    189:     [5] => five
                    190: )
                    191: 
                    192: Output after shift is :
                    193: string(3) "one"
                    194: 
                    195: -- Input Array for Iteration 8 is -- 
                    196: Array
                    197: (
                    198:     [f] => fff
                    199:     [1] => one
                    200:     [4] => 6
                    201:     [] => 3
                    202:     [2] => float
                    203:     [F] => FFF
                    204:     [blank] => 
                    205:     [3] => 3.7
                    206:     [5] => Five
                    207:     [6] => 8.6
                    208:     [4name] => jonny
                    209:     [a] => 
                    210: )
                    211: 
                    212: Output after shift is :
                    213: string(3) "fff"
                    214: 
                    215: -- Input Array for Iteration 9 is -- 
                    216: Array
                    217: (
                    218:     [0] => 12
                    219:     [1] => name
                    220:     [2] => age
                    221:     [3] => 45
                    222: )
                    223: 
                    224: Output after shift is :
                    225: int(12)
                    226: 
                    227: -- Input Array for Iteration 10 is -- 
                    228: Array
                    229: (
                    230:     [0] => Array
                    231:         (
                    232:             [0] => oNe
                    233:             [1] => tWo
                    234:             [2] => 4
                    235:         )
                    236: 
                    237:     [1] => Array
                    238:         (
                    239:             [0] => 10
                    240:             [1] => 20
                    241:             [2] => 30
                    242:             [3] => 40
                    243:             [4] => 50
                    244:         )
                    245: 
                    246:     [2] => Array
                    247:         (
                    248:         )
                    249: 
                    250: )
                    251: 
                    252: Output after shift is :
                    253: array(3) {
                    254:   [0]=>
                    255:   string(3) "oNe"
                    256:   [1]=>
                    257:   string(3) "tWo"
                    258:   [2]=>
                    259:   int(4)
                    260: }
                    261: 
                    262: -- Input Array for Iteration 11 is -- 
                    263: Array
                    264: (
                    265:     [one] => 2
                    266:     [three] => 3
                    267:     [0] => 3
                    268:     [1] => 4
                    269:     [3] => 33
                    270:     [4] => 44
                    271:     [5] => 57
                    272:     [6] => 6
                    273:     [5.4] => 554
                    274:     [5.7] => 557
                    275: )
                    276: 
                    277: Output after shift is :
                    278: int(2)
                    279: 
                    280: *** Checking for internal array pointer being reset when shift is called ***
                    281: 
                    282: Current Element is : int(1)
                    283: 
                    284: Next Element is : int(2)
                    285: 
                    286: Next Element is : int(3)
                    287: 
                    288: shifted Element is : int(1)
                    289: 
                    290: Current Element after shift operation is: int(2)
                    291: Done

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