Return to array_rand_variation2.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / array |
1.1 misho 1: --TEST-- 2: Test array_rand() function : usage variations - unexpected values for 'num_req' parameter 3: --FILE-- 4: <?php 5: /* Prototype : mixed array_rand(array input [, int num_req]) 6: * Description: Return key/keys for random entry/entries in the array 7: * Source code: ext/standard/array.c 8: */ 9: 10: /* 11: * Test array_rand() with different types of values other than int passed to 'num_req' argument 12: * to see that function works with unexpeced data and generates warning message as required. 13: */ 14: 15: echo "*** Testing array_rand() : unexpected values for 'num_req' parameter ***\n"; 16: 17: // Initialise function arguments 18: $input = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); 19: 20: //get an unset variable 21: $unset_var = 10; 22: unset ($unset_var); 23: 24: //define a class 25: class test 26: { 27: var $t = 10; 28: function __toString() 29: { 30: return "3object"; 31: } 32: } 33: 34: //array of values to iterate over 35: $values = array( 36: 37: // int data 38: /*1*/ 0, 39: 1, 40: 12345, 41: -2345, 42: 43: // float data 44: /*5*/ 10.5, 45: -10.5, 46: 12.3456789000e10, 47: 12.3456789000E-10, 48: .5, 49: 50: // null data 51: /*10*/ NULL, 52: null, 53: 54: // boolean data 55: /*12*/ true, 56: false, 57: TRUE, 58: FALSE, 59: 60: // empty data 61: /*16*/ "", 62: '', 63: 64: // string data 65: /*18*/ "string", 66: 'string', 67: 68: // object data 69: /*20*/ new test(), 70: 71: // undefined data 72: /*21*/ @$undefined_var, 73: 74: // unset data 75: /*22*/ @$unset_var, 76: ); 77: 78: 79: // loop through each element of the array for different values for 'num_req' argument 80: $count = 1; 81: foreach($values as $value) { 82: echo "\n-- Iteration $count --\n"; 83: var_dump( array_rand($input,$value) ); 84: $count++; 85: }; 86: 87: echo "Done"; 88: ?> 89: --EXPECTF-- 90: *** Testing array_rand() : unexpected values for 'num_req' parameter *** 91: 92: -- Iteration 1 -- 93: 94: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 95: NULL 96: 97: -- Iteration 2 -- 98: int(%d) 99: 100: -- Iteration 3 -- 101: 102: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 103: NULL 104: 105: -- Iteration 4 -- 106: 107: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 108: NULL 109: 110: -- Iteration 5 -- 111: array(10) { 112: [0]=> 113: int(%d) 114: [1]=> 115: int(%d) 116: [2]=> 117: int(%d) 118: [3]=> 119: int(%d) 120: [4]=> 121: int(%d) 122: [5]=> 123: int(%d) 124: [6]=> 125: int(%d) 126: [7]=> 127: int(%d) 128: [8]=> 129: int(%d) 130: [9]=> 131: int(%d) 132: } 133: 134: -- Iteration 6 -- 135: 136: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 137: NULL 138: 139: -- Iteration 7 -- 140: 141: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 142: NULL 143: 144: -- Iteration 8 -- 145: 146: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 147: NULL 148: 149: -- Iteration 9 -- 150: 151: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 152: NULL 153: 154: -- Iteration 10 -- 155: 156: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 157: NULL 158: 159: -- Iteration 11 -- 160: 161: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 162: NULL 163: 164: -- Iteration 12 -- 165: int(%d) 166: 167: -- Iteration 13 -- 168: 169: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 170: NULL 171: 172: -- Iteration 14 -- 173: int(%d) 174: 175: -- Iteration 15 -- 176: 177: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 178: NULL 179: 180: -- Iteration 16 -- 181: 182: Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d 183: NULL 184: 185: -- Iteration 17 -- 186: 187: Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d 188: NULL 189: 190: -- Iteration 18 -- 191: 192: Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d 193: NULL 194: 195: -- Iteration 19 -- 196: 197: Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d 198: NULL 199: 200: -- Iteration 20 -- 201: 202: Warning: array_rand() expects parameter 2 to be long, object given in %s on line %d 203: NULL 204: 205: -- Iteration 21 -- 206: 207: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 208: NULL 209: 210: -- Iteration 22 -- 211: 212: Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d 213: NULL 214: Done 215: