Return to end.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / array |
1.1 misho 1: --TEST-- 2: Test end() function 3: --SKIPIF-- 4: <?php 5: if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only"); 6: ?> 7: --INI-- 8: precision=14 9: --FILE-- 10: <?php 11: /* Prototype: mixed end ( array &$array ); 12: Description: Advances internal pointer of array to last element, and returns its value. 13: */ 14: 15: $arrays = array ( 16: array( 0 ), 17: range(1, 100 ), 18: range('a', 'z', 2 ), 19: array("a" => "A", 2 => "B", "C" => 3, 4 => 4, "one" => 1, "" => NULL ), 20: array(1, array(1, 2 => 3 ), "one" => 1, "5" => 5 ), 21: array(-1, -2, -3, -4, "-0.005" => "neg0.005", 2.0 => "float2", "neg.9" => -.9 ), 22: array(1.0005, 2.000000, -3.000000, -4.9999999 ), 23: array(true, false), 24: array("PHP", "Web2.0", "SOA"), 25: array(1, array() ), 26: array(1, 2, "" ), 27: array(" "), 28: array(2147483647, 2147483648, -2147483647, -2147483648 ), 29: array(0x7FFFFFFF, -0x80000000, 017777777777, -020000000000 ), 30: array(-.6700000E+3, -4.10003E+3, 1e-5, -1E+5, 000002.00 ) 31: ); 32: /* loop through $arrays to print the last element of each sub-array */ 33: echo "*** Testing end() on different arrays ***\n"; 34: $counter = 1; 35: foreach ($arrays as $sub_array){ 36: echo "-- Iteration $counter --\n"; 37: var_dump( end($sub_array) ); 38: /* ensure that internal pointer is moved to last element */ 39: var_dump( current($sub_array) ); 40: $counter++; 41: } 42: 43: /* checking for end() on sub-arrays */ 44: echo "\n*** Testing end() with sub-arrays ***\n"; 45: $test_array = array(1, array(1 => "one", "two" => 2, "" => "f") ); 46: var_dump( end($test_array) ); 47: var_dump( end($test_array[1]) ); 48: 49: /* checking working of end() when array elements are deleted */ 50: echo "\n*** Testing end() when array elements are deleted ***\n"; 51: $array_test = array("a", "b", "d", 7, "u" => "U", -4, "-.008" => "neg.008"); 52: 53: // remove first element from array 54: echo "\n-- Remove first element from array --\n"; 55: unset($array_test[0]); 56: var_dump( end($array_test) ); 57: 58: // remove last element from array, rewind and check end() 59: echo "\n-- Remove last element from array --\n"; 60: unset($array_test['-.008']); 61: var_dump( end($array_test) ); 62: reset( $array_test ); 63: var_dump( end($array_test) ); 64: 65: // remove any element !first, !last, rewind and check end() 66: echo "\n-- Remove any element from array apart from first and last element --\n"; 67: unset($array_test[7]); 68: var_dump( end($array_test) ); 69: var_dump( reset($array_test) ); 70: var_dump( end($array_test) ); 71: 72: /* Checking on OBJECTS type */ 73: echo "\n*** Testing end() on objects ***\n"; 74: class foo 75: { 76: function __toString() { 77: return "Object"; 78: } 79: } 80: class foo1 81: { 82: function __toString() { 83: return "Object1"; 84: } 85: } 86: 87: $object1 = new foo(); //new object created 88: $object2 = new foo1(); 89: 90: $array_object = array(); 91: $array_object[0] = &$object1; 92: $array_object[1] = &$object2; 93: var_dump( end($array_object) ); 94: var_dump($array_object); 95: 96: /* Checking on RESOURCE type */ 97: echo "\n*** Testing end() on resource type ***\n"; 98: //file type resource 99: $file_handle = fopen(__FILE__, "r"); 100: 101: //directory type resource 102: $dir_handle = opendir( dirname(__FILE__) ); 103: 104: //store resources in array 105: $resources = array($file_handle, $dir_handle); 106: var_dump( end($resources) ); 107: var_dump( current($resources) ); 108: 109: echo "\n*** Testing error conditions ***\n"; 110: /* checking for unexpected number of arguments */ 111: var_dump( end() ); 112: var_dump( end($array[0], $array[0]) ); 113: 114: /* checking for unexpected type of arguments */ 115: $var=1; 116: $var1="string"; 117: var_dump( end($var) ); 118: var_dump( end($var1) ); 119: 120: /* checking null array */ 121: $null_array = array(); 122: var_dump( end($null_array) ); 123: 124: echo "Done\n"; 125: 126: 127: /* cleaning resource handles */ 128: fclose( $file_handle ); //file resource handle deleted 129: closedir( $dir_handle ); //dir resource handle deleted 130: 131: ?> 132: --EXPECTF-- 133: *** Testing end() on different arrays *** 134: -- Iteration 1 -- 135: int(0) 136: int(0) 137: -- Iteration 2 -- 138: int(100) 139: int(100) 140: -- Iteration 3 -- 141: string(1) "y" 142: string(1) "y" 143: -- Iteration 4 -- 144: NULL 145: NULL 146: -- Iteration 5 -- 147: int(5) 148: int(5) 149: -- Iteration 6 -- 150: float(-0.9) 151: float(-0.9) 152: -- Iteration 7 -- 153: float(-4.9999999) 154: float(-4.9999999) 155: -- Iteration 8 -- 156: bool(false) 157: bool(false) 158: -- Iteration 9 -- 159: string(3) "SOA" 160: string(3) "SOA" 161: -- Iteration 10 -- 162: array(0) { 163: } 164: array(0) { 165: } 166: -- Iteration 11 -- 167: string(0) "" 168: string(0) "" 169: -- Iteration 12 -- 170: string(1) " " 171: string(1) " " 172: -- Iteration 13 -- 173: float(-2147483648) 174: float(-2147483648) 175: -- Iteration 14 -- 176: float(-2147483648) 177: float(-2147483648) 178: -- Iteration 15 -- 179: float(2) 180: float(2) 181: 182: *** Testing end() with sub-arrays *** 183: array(3) { 184: [1]=> 185: string(3) "one" 186: ["two"]=> 187: int(2) 188: [""]=> 189: string(1) "f" 190: } 191: string(1) "f" 192: 193: *** Testing end() when array elements are deleted *** 194: 195: -- Remove first element from array -- 196: string(7) "neg.008" 197: 198: -- Remove last element from array -- 199: int(-4) 200: int(-4) 201: 202: -- Remove any element from array apart from first and last element -- 203: int(-4) 204: string(1) "b" 205: int(-4) 206: 207: *** Testing end() on objects *** 208: object(foo1)#%d (0) { 209: } 210: array(2) { 211: [0]=> 212: &object(foo)#%d (0) { 213: } 214: [1]=> 215: &object(foo1)#%d (0) { 216: } 217: } 218: 219: *** Testing end() on resource type *** 220: resource(%d) of type (stream) 221: resource(%d) of type (stream) 222: 223: *** Testing error conditions *** 224: 225: Warning: end() expects exactly 1 parameter, 0 given in %s on line %d 226: NULL 227: 228: Warning: end() expects exactly 1 parameter, 2 given in %s on line %d 229: NULL 230: 231: Warning: end() expects parameter 1 to be array, integer given in %s on line %d 232: NULL 233: 234: Warning: end() expects parameter 1 to be array, string given in %s on line %d 235: NULL 236: bool(false) 237: Done