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