xref: /PHP-8.4/ext/intl/tests/collator_sort.phpt (revision 74859783)
1--TEST--
2sort()
3--EXTENSIONS--
4intl
5--SKIPIF--
6<?php if (version_compare(INTL_ICU_VERSION, '51.2') >=  0) die('skip for ICU < 51.2'); ?>
7--FILE--
8<?php
9
10/*
11 * Sort arrays using various locales.
12 */
13
14
15$test_num = 1;
16
17/*
18 * Sort arrays in the given list using specified locale.
19 */
20function sort_arrays( $locale, $arrays, $sort_flag = Collator::SORT_REGULAR )
21{
22    $res_str = '';
23
24    $coll = ut_coll_create( $locale );
25
26    foreach( $arrays as $array )
27    {
28        // Sort array values
29        $res_val = ut_coll_sort( $coll, $array, $sort_flag );
30
31        // Concatenate the sorted array and function result
32        // with output string.
33        $res_dump = "\n" . dump( $array ) .
34                    "\n Result: " . dump( $res_val );
35
36        // Preppend test signature to output string
37        $md5 = md5( $res_dump );
38
39        global $test_num;
40
41        $res_str .= "\n\n".
42                    "Test $test_num.$md5:" .
43                    $res_dump;
44        ++$test_num;
45    }
46
47    return $res_str;
48}
49
50function ut_main()
51{
52    global $test_num;
53    $test_num = 1;
54    $res_str = '';
55
56    // Sort an array in SORT_REGULAR mode using en_US locale.
57    $test_params = array(
58        array( 'abc', 'abd', 'aaa' ),
59        array( 'm'  , '1'  , '_'   ),
60        array( 'a'  , 'aaa', 'aa'  ),
61        array( 'ba' , 'b'  , 'ab'  ),
62        array( 'e'  , 'c'  , 'a'   ),
63        array( '100', '25' , '36'  ),
64        array( 5    , '30' , 2     ),
65        array( 'd'  , ''   , ' a'  ),
66        array( 'd ' , 'f ' , ' a'  ),
67        array( 'a'  , null , '3'   ),
68        array( 'y'  , 'k'  , 'i' )
69    );
70
71    $res_str .= sort_arrays( 'en_US', $test_params );
72
73    $test_params = array(
74        array( '100', '25' , '36'  ),
75        array( 5    , '30' , 2     ),
76        array( 'd'  , ''   , ' a'  ),
77        array( 'y'  , 'k'  , 'i' )
78    );
79
80    // Sort in en_US locale with SORT_STRING flag
81    $res_str .= sort_arrays( 'en_US', $test_params, Collator::SORT_STRING );
82
83
84    // Sort a non-ASCII array using ru_RU locale.
85    $test_params = array(
86        array( 'абг', 'абв', 'ааа', 'abc' ),
87        array( 'аа', 'ааа' , 'а' )
88    );
89
90    $res_str .= sort_arrays( 'ru_RU', $test_params );
91
92    // Sort an array using Lithuanian locale.
93    $test_params = array(
94        array( 'y'  , 'k'  , 'i' )
95    );
96
97    $res_str .= sort_arrays( 'lt_LT', $test_params );
98
99    return $res_str;
100}
101
102include_once( 'ut_common.inc' );
103ut_run();
104?>
105--EXPECT--
106Test 1.e8f1cd28133d79ecd660002f1c660d0e:
107array (
108  0 => 'aaa',
109  1 => 'abc',
110  2 => 'abd',
111)
112 Result: true
113
114Test 2.c2ded12173dd2996927378cae37eb275:
115array (
116  0 => '_',
117  1 => '1',
118  2 => 'm',
119)
120 Result: true
121
122Test 3.54071c968d71cb98c5d379145f8d7d38:
123array (
124  0 => 'a',
125  1 => 'aa',
126  2 => 'aaa',
127)
128 Result: true
129
130Test 4.19abe63d6f6dfef65b0e3c9ab4826b07:
131array (
132  0 => 'ab',
133  1 => 'b',
134  2 => 'ba',
135)
136 Result: true
137
138Test 5.9a8dc0a9bc771368c2f1fc3d02754610:
139array (
140  0 => 'a',
141  1 => 'c',
142  2 => 'e',
143)
144 Result: true
145
146Test 6.ab530b060e5e54a65bfb8b9f8fc61870:
147array (
148  0 => '25',
149  1 => '36',
150  2 => '100',
151)
152 Result: true
153
154Test 7.0718dd838509017bded2ed307a6e785f:
155array (
156  0 => 2,
157  1 => 5,
158  2 => '30',
159)
160 Result: true
161
162Test 8.923d65739c5219c634616ffd100a50e4:
163array (
164  0 => '',
165  1 => ' a',
166  2 => 'd',
167)
168 Result: true
169
170Test 9.289bc2f28e87d3201ec9d7e8477ae1b0:
171array (
172  0 => ' a',
173  1 => 'd ',
174  2 => 'f ',
175)
176 Result: true
177
178Test 10.de0fd958484f2377a645835d7fbcf124:
179array (
180  0 => NULL,
181  1 => '3',
182  2 => 'a',
183)
184 Result: true
185
186Test 11.dd2b8f0adb37c45d528cad1a0cc0f361:
187array (
188  0 => 'i',
189  1 => 'k',
190  2 => 'y',
191)
192 Result: true
193
194Test 12.1e6b4d6f7df9d4580317634ea46d8208:
195array (
196  0 => '100',
197  1 => '25',
198  2 => '36',
199)
200 Result: true
201
202Test 13.cec115dc9850b98dfbdf102efa09e61b:
203array (
204  0 => 2,
205  1 => '30',
206  2 => 5,
207)
208 Result: true
209
210Test 14.923d65739c5219c634616ffd100a50e4:
211array (
212  0 => '',
213  1 => ' a',
214  2 => 'd',
215)
216 Result: true
217
218Test 15.dd2b8f0adb37c45d528cad1a0cc0f361:
219array (
220  0 => 'i',
221  1 => 'k',
222  2 => 'y',
223)
224 Result: true
225
226Test 16.ca0e38a2e3147dd97070f2128f140934:
227array (
228  0 => 'abc',
229  1 => 'ааа',
230  2 => 'абв',
231  3 => 'абг',
232)
233 Result: true
234
235Test 17.91480b10473a0c96a4cd6d88c23c577a:
236array (
237  0 => 'а',
238  1 => 'аа',
239  2 => 'ааа',
240)
241 Result: true
242
243Test 18.fdd3fe3981476039164aa000bf9177f2:
244array (
245  0 => 'i',
246  1 => 'y',
247  2 => 'k',
248)
249 Result: true
250