xref: /PHP-7.4/ext/intl/tests/collator_asort.phpt (revision 782352c5)
1--TEST--
2asort()
3--SKIPIF--
4<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
5<?php if (version_compare(INTL_ICU_VERSION, '51.2') >=  0) die('skip for ICU < 51.2'); ?>
6--FILE--
7<?php
8
9/*
10 * Sort associative arrays using various locales.
11 */
12
13
14$test_num = 1;
15
16/*
17 * Sort various arrays in specified locale.
18 */
19function sort_arrays( $locale, $test_arrays, $sort_flag = Collator::SORT_REGULAR )
20{
21    $res_str = '';
22
23    $coll = ut_coll_create( $locale );
24
25    foreach( $test_arrays as $test_array )
26    {
27        // Try to sort test data.
28        $res_val = ut_coll_asort( $coll, $test_array, $sort_flag );
29
30        // Return output data.
31        $res_dump = "\n" . dump( $test_array ) .
32                    "\n Result: " . dump( $res_val );
33
34		// Preppend test signature to output string
35        $md5 = md5( $res_dump );
36
37        global $test_num;
38
39        $res_str .= "\n\n".
40                    "Test $test_num.$md5:" .
41                    $res_dump;
42        ++$test_num;
43    }
44
45    return $res_str;
46}
47
48/*
49 * Test main function.
50 */
51function ut_main()
52{
53    global $test_num;
54    $test_num = 1;
55    $res_str = '';
56
57    // Sort an array in SORT_REGULAR mode using en_US locale.
58    $test_params = array(
59        array( 'd' => 'y'  ,
60               'c' => 'i'  ,
61               'a' => 'k'  ),
62
63        array( 'a' => 'a'  ,
64               'b' => 'aaa',
65               'c' => 'aa' ),
66
67        array( 'a'  => 'a' ,
68               'aaa'=> 'a' ,
69               'aa' => 'a' ),
70
71        array( '1' => 'abc',
72               '5' => '!'  ,
73               '2' => null ,
74               '7' => ''   ),
75
76        array( '1' => '100',
77               '2' => '25' ,
78               '3' => '36' ),
79
80        array( '1' => 5    ,
81               '2' => '30' ,
82               '3' => 2    )
83    );
84
85    $res_str .= sort_arrays( 'en_US', $test_params );
86
87    // Sort an array in SORT_STRING mode using en_US locale.
88    $test_params = array(
89        array( '1' => '100',
90               '2' => '25' ,
91               '3' => '36' ),
92
93        array( '1' => 5    ,
94               '2' => '30' ,
95               '3' => 2    ),
96
97        array( '1' => 'd'  ,
98               '2' => ''   ,
99               '3' => ' a' ),
100
101        array( '1' => 'y'  ,
102               '2' => 'k'  ,
103               '3' => 'i'  )
104    );
105
106    $res_str .= sort_arrays( 'en_US', $test_params, Collator::SORT_STRING );
107
108    // Sort a non-ASCII array using ru_RU locale.
109    $test_params = array(
110        array( 'п' => 'у',
111               'б' => 'в',
112               'е' => 'а' ),
113
114        array( '1' => 'п',
115               '4' => '',
116               '7' => 'd',
117               '2' => 'пп' )
118    );
119
120    $res_str .= sort_arrays( 'ru_RU', $test_params );
121
122
123    // Sort an array using Lithuanian locale.
124    $test_params = array(
125        array( 'd' => 'y',
126               'c' => 'i',
127               'a' => 'k' )
128    );
129
130    $res_str .= sort_arrays( 'lt_LT', $test_params );
131
132    return $res_str . "\n";
133}
134
135include_once( 'ut_common.inc' );
136ut_run();
137?>
138--EXPECT--
139Test 1.162b81ac12878b817fc39063097e45b5:
140array (
141  'c' => 'i',
142  'a' => 'k',
143  'd' => 'y',
144)
145 Result: true
146
147Test 2.93d96e22f692d8a281b0a389f01f8d1e:
148array (
149  'a' => 'a',
150  'c' => 'aa',
151  'b' => 'aaa',
152)
153 Result: true
154
155Test 3.d5a9747a8bd4c32cb2a705f7e6d8a56f:
156array (
157  'a' => 'a',
158  'aaa' => 'a',
159  'aa' => 'a',
160)
161 Result: true
162
163Test 4.dcc371022fa1eac76e73b0455d70790a:
164array (
165  2 => NULL,
166  7 => '',
167  5 => '!',
168  1 => 'abc',
169)
170 Result: true
171
172Test 5.99dc71f405b286e03d489061b36e6900:
173array (
174  2 => '25',
175  3 => '36',
176  1 => '100',
177)
178 Result: true
179
180Test 6.bf5bba243307c9d12934e756ad4be190:
181array (
182  3 => 2,
183  1 => 5,
184  2 => '30',
185)
186 Result: true
187
188Test 7.e4ee7024c61476e9e7a6c28b5e47df6f:
189array (
190  1 => '100',
191  2 => '25',
192  3 => '36',
193)
194 Result: true
195
196Test 8.5fa7033dd43784be0db1474eb48b83c8:
197array (
198  3 => 2,
199  2 => '30',
200  1 => 5,
201)
202 Result: true
203
204Test 9.588cdf4692bc09aa92ffe7e48f9e4579:
205array (
206  2 => '',
207  3 => ' a',
208  1 => 'd',
209)
210 Result: true
211
212Test 10.be02641a47ebcccd23e4183ca3a415f7:
213array (
214  3 => 'i',
215  2 => 'k',
216  1 => 'y',
217)
218 Result: true
219
220Test 11.153d9b11d1e5936afc917a94a4e11f34:
221array (
222  'е' => 'а',
223  'б' => 'в',
224  'п' => 'у',
225)
226 Result: true
227
228Test 12.6eaea3fa21b3b7d006ca7dfa27d4e282:
229array (
230  4 => '',
231  7 => 'd',
232  1 => 'п',
233  2 => 'пп',
234)
235 Result: true
236
237Test 13.8800d48abb960a59002eef77f1d73ae0:
238array (
239  'c' => 'i',
240  'd' => 'y',
241  'a' => 'k',
242)
243 Result: true
244