Return to arsort_variation3.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / array |
1.1 misho 1: --TEST-- 2: Test arsort() function : usage variations - sort integer/float values 3: --FILE-- 4: <?php 5: /* Prototype : bool arsort ( array &$array [, int $sort_flags] ) 6: * Description: Sort an array and maintain index association 7: Elements will be arranged from highest to lowest when this function has completed. 8: * Source code: ext/standard/array.c 9: */ 10: 11: /* 12: * Testing arsort() by providing different integer/float value arrays for $array argument with following values 13: * 1. flag value as defualt 14: * 2. SORT_REGULAR - compare items normally 15: * 3. SORT_NUMERIC - compare items numerically 16: */ 17: 18: echo "*** Testing arsort() : usage variations ***\n"; 19: 20: // group of various arrays with indices 21: $various_arrays = array( 22: // negative/posative integer array 23: array(1 => 11, 2 => -11, 3 => 21, 4 => -21, 5 => 31, 6 => -31, 7 => 0, 8 => 41, 10 =>-41), 24: 25: // float value array 26: array(1 => 10.5, 2 => -10.5, 3 => 10.5e2, 4 => 10.6E-2, 5 => .5, 6 => .0001, 7 => -.1), 27: 28: // mixed value array 29: array(1 => .0001, 2 => .0021, 3 => -.01, 4 => -1, 5 => 0, 6 => .09, 7 => 2, 8 => -.9, 9 => 10.6E-2, 10 => -10.6E-2, 11 => 33), 30: 31: // array values contains minimum and maximum ranges 32: array(1 => 2147483647, 2 => 2147483648, 3 => -2147483647, 4 => -2147483648, 5 => -0, 6 => 0, 7 => -2147483649) 33: ); 34: 35: // set of possible flag values 36: $flag_value = array("SORT_REGULAR" => SORT_REGULAR, "SORT_NUMERIC" => SORT_NUMERIC); 37: 38: $count = 1; 39: echo "\n-- Testing arsort() by supplying various integer/float arrays --\n"; 40: 41: // loop through to test arsort() with different arrays 42: foreach ($various_arrays as $array) { 43: echo "\n-- Iteration $count --\n"; 44: 45: echo "- With default sort_flag -\n"; 46: $temp_array = $array; 47: var_dump(arsort($temp_array) ); 48: var_dump($temp_array); 49: 50: // loop through $flag_value array and setting all possible flag values 51: foreach($flag_value as $key => $flag){ 52: echo "- Sort_flag = $key -\n"; 53: $temp_array = $array; 54: var_dump(arsort($temp_array, $flag) ); 55: var_dump($temp_array); 56: } 57: $count++; 58: } 59: 60: echo "Done\n"; 61: ?> 62: --EXPECTF-- 63: *** Testing arsort() : usage variations *** 64: 65: -- Testing arsort() by supplying various integer/float arrays -- 66: 67: -- Iteration 1 -- 68: - With default sort_flag - 69: bool(true) 70: array(9) { 71: [8]=> 72: int(41) 73: [5]=> 74: int(31) 75: [3]=> 76: int(21) 77: [1]=> 78: int(11) 79: [7]=> 80: int(0) 81: [2]=> 82: int(-11) 83: [4]=> 84: int(-21) 85: [6]=> 86: int(-31) 87: [10]=> 88: int(-41) 89: } 90: - Sort_flag = SORT_REGULAR - 91: bool(true) 92: array(9) { 93: [8]=> 94: int(41) 95: [5]=> 96: int(31) 97: [3]=> 98: int(21) 99: [1]=> 100: int(11) 101: [7]=> 102: int(0) 103: [2]=> 104: int(-11) 105: [4]=> 106: int(-21) 107: [6]=> 108: int(-31) 109: [10]=> 110: int(-41) 111: } 112: - Sort_flag = SORT_NUMERIC - 113: bool(true) 114: array(9) { 115: [8]=> 116: int(41) 117: [5]=> 118: int(31) 119: [3]=> 120: int(21) 121: [1]=> 122: int(11) 123: [7]=> 124: int(0) 125: [2]=> 126: int(-11) 127: [4]=> 128: int(-21) 129: [6]=> 130: int(-31) 131: [10]=> 132: int(-41) 133: } 134: 135: -- Iteration 2 -- 136: - With default sort_flag - 137: bool(true) 138: array(7) { 139: [3]=> 140: float(1050) 141: [1]=> 142: float(10.5) 143: [5]=> 144: float(0.5) 145: [4]=> 146: float(0.106) 147: [6]=> 148: float(0.0001) 149: [7]=> 150: float(-0.1) 151: [2]=> 152: float(-10.5) 153: } 154: - Sort_flag = SORT_REGULAR - 155: bool(true) 156: array(7) { 157: [3]=> 158: float(1050) 159: [1]=> 160: float(10.5) 161: [5]=> 162: float(0.5) 163: [4]=> 164: float(0.106) 165: [6]=> 166: float(0.0001) 167: [7]=> 168: float(-0.1) 169: [2]=> 170: float(-10.5) 171: } 172: - Sort_flag = SORT_NUMERIC - 173: bool(true) 174: array(7) { 175: [3]=> 176: float(1050) 177: [1]=> 178: float(10.5) 179: [5]=> 180: float(0.5) 181: [4]=> 182: float(0.106) 183: [6]=> 184: float(0.0001) 185: [7]=> 186: float(-0.1) 187: [2]=> 188: float(-10.5) 189: } 190: 191: -- Iteration 3 -- 192: - With default sort_flag - 193: bool(true) 194: array(11) { 195: [11]=> 196: int(33) 197: [7]=> 198: int(2) 199: [9]=> 200: float(0.106) 201: [6]=> 202: float(0.09) 203: [2]=> 204: float(0.0021) 205: [1]=> 206: float(0.0001) 207: [5]=> 208: int(0) 209: [3]=> 210: float(-0.01) 211: [10]=> 212: float(-0.106) 213: [8]=> 214: float(-0.9) 215: [4]=> 216: int(-1) 217: } 218: - Sort_flag = SORT_REGULAR - 219: bool(true) 220: array(11) { 221: [11]=> 222: int(33) 223: [7]=> 224: int(2) 225: [9]=> 226: float(0.106) 227: [6]=> 228: float(0.09) 229: [2]=> 230: float(0.0021) 231: [1]=> 232: float(0.0001) 233: [5]=> 234: int(0) 235: [3]=> 236: float(-0.01) 237: [10]=> 238: float(-0.106) 239: [8]=> 240: float(-0.9) 241: [4]=> 242: int(-1) 243: } 244: - Sort_flag = SORT_NUMERIC - 245: bool(true) 246: array(11) { 247: [11]=> 248: int(33) 249: [7]=> 250: int(2) 251: [9]=> 252: float(0.106) 253: [6]=> 254: float(0.09) 255: [2]=> 256: float(0.0021) 257: [1]=> 258: float(0.0001) 259: [5]=> 260: int(0) 261: [3]=> 262: float(-0.01) 263: [10]=> 264: float(-0.106) 265: [8]=> 266: float(-0.9) 267: [4]=> 268: int(-1) 269: } 270: 271: -- Iteration 4 -- 272: - With default sort_flag - 273: bool(true) 274: array(7) { 275: [2]=> 276: %s(2147483648) 277: [1]=> 278: int(2147483647) 279: [6]=> 280: int(0) 281: [5]=> 282: int(0) 283: [3]=> 284: int(-2147483647) 285: [4]=> 286: %s(-2147483648) 287: [7]=> 288: %s(-2147483649) 289: } 290: - Sort_flag = SORT_REGULAR - 291: bool(true) 292: array(7) { 293: [2]=> 294: %s(2147483648) 295: [1]=> 296: int(2147483647) 297: [6]=> 298: int(0) 299: [5]=> 300: int(0) 301: [3]=> 302: int(-2147483647) 303: [4]=> 304: %s(-2147483648) 305: [7]=> 306: %s(-2147483649) 307: } 308: - Sort_flag = SORT_NUMERIC - 309: bool(true) 310: array(7) { 311: [2]=> 312: %s(2147483648) 313: [1]=> 314: int(2147483647) 315: [6]=> 316: int(0) 317: [5]=> 318: int(0) 319: [3]=> 320: int(-2147483647) 321: [4]=> 322: %s(-2147483648) 323: [7]=> 324: %s(-2147483649) 325: } 326: Done