Annotation of embedaddon/php/ext/standard/tests/array/sort_basic.phpt, revision 1.1.1.2
1.1 misho 1: --TEST--
2: Test sort() function : basic functionality
3: --FILE--
4: <?php
5: /* Prototype : bool sort ( array &$array [, int $sort_flags] )
6: * Description: This function sorts an array.
7: Elements will be arranged from lowest to highest when this function has completed.
8: * Source code: ext/standard/array.c
9: */
10:
11: /*
12: * Testing sort() by providing arrays with default keys and assoc arrays
13: * to check the basic functionality with following flag values.
14: * flag value as defualt
15: * SORT_REGULAR - compare items normally
16: * SORT_NUMERIC - compare items numerically
17: * SORT_STRING - compare items as strings
18: */
19:
20: echo "*** Testing sort() : basic functionality ***\n";
21:
22: // associative array containing unsorted string values
1.1.1.2 ! misho 23: $unsorted_strings = array(
! 24: "l" => "lemon", "o" => "orange",
! 25: "O" => "Orange", "O1" => "Orange1", "o2" => "orange2", "O3" => "Orange3", "o20" => "orange20",
! 26: "b" => "banana",
! 27: );
1.1 misho 28:
29: // array with default keys containing unsorted numeric values
30: $unsorted_numerics = array( 100, 33, 555, 22 );
31:
32: echo "\n-- Testing sort() by supplying string array, 'flag' value is defualt --\n";
33: $temp_array = $unsorted_strings;
34: var_dump( sort($temp_array) ); // expecting : bool(true)
35: var_dump( $temp_array);
36:
37: echo "\n-- Testing sort() by supplying numeric array, 'flag' value is defualt --\n";
38: $temp_array = $unsorted_numerics;
39: var_dump( sort($temp_array) ); // expecting : bool(true)
40: var_dump( $temp_array);
41:
42: echo "\n-- Testing sort() by supplying string array, 'flag' = SORT_REGULAR --\n";
43: $temp_array = $unsorted_strings;
44: var_dump( sort($temp_array, SORT_REGULAR) ); // expecting : bool(true)
45: var_dump( $temp_array);
46:
47: echo "\n-- Testing sort() by supplying numeric array, 'flag' = SORT_REGULAR --\n";
48: $temp_array = $unsorted_numerics;
49: var_dump( sort($temp_array, SORT_REGULAR) ); // expecting : bool(true)
50: var_dump( $temp_array);
51:
52: echo "\n-- Testing sort() by supplying string array, 'flag' = SORT_STRING --\n";
53: $temp_array = $unsorted_strings;
54: var_dump( sort($temp_array, SORT_STRING) ); // expecting : bool(true)
55: var_dump( $temp_array);
56:
1.1.1.2 ! misho 57: echo "\n-- Testing sort() by supplying string array (case insensitive), 'flag' = SORT_STRING|SORT_FLAG_CASE --\n";
! 58: $temp_array = $unsorted_strings;
! 59: var_dump( sort($temp_array, SORT_STRING|SORT_FLAG_CASE) ); // expecting : bool(true)
! 60: var_dump( $temp_array);
! 61:
! 62: echo "\n-- Testing sort() by supplying string array (natural), 'flag' = SORT_NATURAL --\n";
! 63: $temp_array = $unsorted_strings;
! 64: var_dump( sort($temp_array, SORT_NATURAL) ); // expecting : bool(true)
! 65: var_dump( $temp_array);
! 66:
! 67: echo "\n-- Testing sort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE --\n";
! 68: $temp_array = $unsorted_strings;
! 69: var_dump( sort($temp_array, SORT_NATURAL|SORT_FLAG_CASE) ); // expecting : bool(true)
! 70: var_dump( $temp_array);
! 71:
1.1 misho 72: echo "\n-- Testing sort() by supplying numeric array, 'flag' = SORT_NUMERIC --\n";
73: $temp_array = $unsorted_numerics;
74: var_dump( sort($temp_array, SORT_NUMERIC) ); // expecting : bool(true)
75: var_dump( $temp_array);
76:
77: echo "Done\n";
78: ?>
79: --EXPECTF--
80: *** Testing sort() : basic functionality ***
81:
82: -- Testing sort() by supplying string array, 'flag' value is defualt --
83: bool(true)
1.1.1.2 ! misho 84: array(8) {
1.1 misho 85: [0]=>
1.1.1.2 ! misho 86: string(6) "Orange"
1.1 misho 87: [1]=>
1.1.1.2 ! misho 88: string(7) "Orange1"
1.1 misho 89: [2]=>
1.1.1.2 ! misho 90: string(7) "Orange3"
! 91: [3]=>
! 92: string(6) "banana"
! 93: [4]=>
! 94: string(5) "lemon"
! 95: [5]=>
1.1 misho 96: string(6) "orange"
1.1.1.2 ! misho 97: [6]=>
! 98: string(7) "orange2"
! 99: [7]=>
! 100: string(8) "orange20"
1.1 misho 101: }
102:
103: -- Testing sort() by supplying numeric array, 'flag' value is defualt --
104: bool(true)
105: array(4) {
106: [0]=>
107: int(22)
108: [1]=>
109: int(33)
110: [2]=>
111: int(100)
112: [3]=>
113: int(555)
114: }
115:
116: -- Testing sort() by supplying string array, 'flag' = SORT_REGULAR --
117: bool(true)
1.1.1.2 ! misho 118: array(8) {
1.1 misho 119: [0]=>
1.1.1.2 ! misho 120: string(6) "Orange"
1.1 misho 121: [1]=>
1.1.1.2 ! misho 122: string(7) "Orange1"
1.1 misho 123: [2]=>
1.1.1.2 ! misho 124: string(7) "Orange3"
! 125: [3]=>
! 126: string(6) "banana"
! 127: [4]=>
! 128: string(5) "lemon"
! 129: [5]=>
1.1 misho 130: string(6) "orange"
1.1.1.2 ! misho 131: [6]=>
! 132: string(7) "orange2"
! 133: [7]=>
! 134: string(8) "orange20"
1.1 misho 135: }
136:
137: -- Testing sort() by supplying numeric array, 'flag' = SORT_REGULAR --
138: bool(true)
139: array(4) {
140: [0]=>
141: int(22)
142: [1]=>
143: int(33)
144: [2]=>
145: int(100)
146: [3]=>
147: int(555)
148: }
149:
150: -- Testing sort() by supplying string array, 'flag' = SORT_STRING --
151: bool(true)
1.1.1.2 ! misho 152: array(8) {
! 153: [0]=>
! 154: string(6) "Orange"
! 155: [1]=>
! 156: string(7) "Orange1"
! 157: [2]=>
! 158: string(7) "Orange3"
! 159: [3]=>
! 160: string(6) "banana"
! 161: [4]=>
! 162: string(5) "lemon"
! 163: [5]=>
! 164: string(6) "orange"
! 165: [6]=>
! 166: string(7) "orange2"
! 167: [7]=>
! 168: string(8) "orange20"
! 169: }
! 170:
! 171: -- Testing sort() by supplying string array (case insensitive), 'flag' = SORT_STRING|SORT_FLAG_CASE --
! 172: bool(true)
! 173: array(8) {
1.1 misho 174: [0]=>
175: string(6) "banana"
176: [1]=>
177: string(5) "lemon"
178: [2]=>
179: string(6) "orange"
1.1.1.2 ! misho 180: [3]=>
! 181: string(6) "Orange"
! 182: [4]=>
! 183: string(7) "Orange1"
! 184: [5]=>
! 185: string(7) "orange2"
! 186: [6]=>
! 187: string(8) "orange20"
! 188: [7]=>
! 189: string(7) "Orange3"
! 190: }
! 191:
! 192: -- Testing sort() by supplying string array (natural), 'flag' = SORT_NATURAL --
! 193: bool(true)
! 194: array(8) {
! 195: [0]=>
! 196: string(6) "Orange"
! 197: [1]=>
! 198: string(7) "Orange1"
! 199: [2]=>
! 200: string(7) "Orange3"
! 201: [3]=>
! 202: string(6) "banana"
! 203: [4]=>
! 204: string(5) "lemon"
! 205: [5]=>
! 206: string(6) "orange"
! 207: [6]=>
! 208: string(7) "orange2"
! 209: [7]=>
! 210: string(8) "orange20"
! 211: }
! 212:
! 213: -- Testing sort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE --
! 214: bool(true)
! 215: array(8) {
! 216: [0]=>
! 217: string(6) "banana"
! 218: [1]=>
! 219: string(5) "lemon"
! 220: [2]=>
! 221: string(6) "orange"
! 222: [3]=>
! 223: string(6) "Orange"
! 224: [4]=>
! 225: string(7) "Orange1"
! 226: [5]=>
! 227: string(7) "orange2"
! 228: [6]=>
! 229: string(7) "Orange3"
! 230: [7]=>
! 231: string(8) "orange20"
1.1 misho 232: }
233:
234: -- Testing sort() by supplying numeric array, 'flag' = SORT_NUMERIC --
235: bool(true)
236: array(4) {
237: [0]=>
238: int(22)
239: [1]=>
240: int(33)
241: [2]=>
242: int(100)
243: [3]=>
244: int(555)
245: }
1.1.1.2 ! misho 246: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>