Unittest fixes when running with icu-53.1
[hiphop-php.git] / hphp / test / slow / ext_collator / 1810.php
bloba497a118902d46e43307a71e24c6a1e920ab5c86
1 <?php
3 function ut_run($mainFunc) {
4 $GLOBALS['oo-mode'] = true;
5 $oo_result = $mainFunc();
6 $GLOBALS['oo-mode'] = false;
7 $proc_result = $mainFunc();
8 if($proc_result !== $oo_result) {
9 echo "ERROR: OO- and procedural APIs produce different results!\n";
10 echo "OO API output:\n";
11 echo str_repeat( '=', 78 ) . "\n";
12 echo $oo_result;
13 echo str_repeat( '=', 78 ) . "\n";
14 echo "procedural API output:\n";
15 echo str_repeat( '=', 78 ) . "\n";
16 echo $proc_result;
17 echo str_repeat( '=', 78 ) . "\n";
18 return;
20 echo $oo_result;
22 function dump($val) {
23 return var_export( $val, true );
25 function ut_coll_create( $locale )
27 return $GLOBALS['oo-mode'] ? Collator::create( $locale ) : collator_create( $locale );
29 function ut_coll_compare( $coll, $str1, $str2 )
31 return $GLOBALS['oo-mode'] ?
32 $coll->compare( $str1, $str2 ) : collator_compare( $coll, $str1, $str2 );
34 function ut_coll_sort_with_sort_keys( $coll, &$arr )
36 return $GLOBALS['oo-mode'] ?
37 $coll->sortWithSortKeys( $arr ) : collator_sort_with_sort_keys( $coll, $arr );
39 function ut_coll_sort( $coll, &$arr, $sort_flag = Collator::SORT_REGULAR )
41 return $GLOBALS['oo-mode'] ?
42 $coll->sort( $arr, $sort_flag ) : collator_sort( $coll, $arr, $sort_flag );
44 function ut_coll_asort( $coll, &$arr, $sort_flag = Collator::SORT_REGULAR )
46 return $GLOBALS['oo-mode'] ?
47 $coll->asort( $arr, $sort_flag ) : collator_asort( $coll, $arr, $sort_flag );
49 function ut_coll_get_locale( $coll, $type )
51 return $GLOBALS['oo-mode'] ?
52 $coll->getLocale( $type ) : collator_get_locale( $coll, $type );
54 function ut_coll_set_strength( $coll, $strength )
56 return $GLOBALS['oo-mode'] ?
57 $coll->setStrength( $strength ) : collator_set_strength( $coll, $strength );
59 function ut_coll_set_attribute( $coll, $attr, $val )
61 return $GLOBALS['oo-mode'] ?
62 $coll->setAttribute( $attr, $val ) : collator_set_attribute( $coll, $attr, $val );
64 function ut_coll_set_default( $coll )
66 return $GLOBALS['oo-mode'] ? Collator::setDefault( $coll ) : collator_set_default( $coll );
68 $test_num = 1;
69 function sort_arrays( $locale, $arrays, $sort_flag = Collator::SORT_REGULAR )
71 $res_str = '';
72 $coll = ut_coll_create( $locale );
73 foreach( $arrays as $array )
75 // Sort array values
76 $res_val = ut_coll_sort( $coll, $array, $sort_flag );
77 // Concatenate the sorted array and function result
78 // with output string.
79 $res_dump = "\n" . dump( $array ) .
80 "\n Result: " . dump( $res_val );
81 // Preppend test signature to output string
82 $md5 = md5( $res_dump );
83 global $test_num;
85 $res_str .= "\n\n".
86 "Test $test_num.$md5:" .
87 $res_dump;
88 ++$test_num;
90 return $res_str;
92 function ut_main1()
94 global $test_num;
95 $test_num = 1;
96 $res_str = '';
97 // Sort an array in SORT_REGULAR mode using en_US locale.
98 $test_params = array(
99 array( 'abc', 'abd', 'aaa' ),
100 array( 'm' , '1' , '_' ),
101 array( 'a' , 'aaa', 'aa' ),
102 array( 'ba' , 'b' , 'ab' ),
103 array( 'e' , 'c' , 'a' ),
104 array( '100', '25' , '36' ), // test 6
105 array( 5 , '30' , 2 ),
106 array( 'd' , '' , ' a' ),
107 array( 'd ' , 'f ' , ' a' ),
108 array( 'a' , null , '3' ),
109 array( 'y' , 'k' , 'i' )
111 $res_str .= sort_arrays( 'en_US', $test_params );
112 $test_params = array(
113 array( '100', '25' , '36' ),
114 array( 5 , '30' , 2 ), // test 13
115 array( 'd' , '' , ' a' ),
116 array( 'y' , 'k' , 'i' )
118 // Sort in en_US locale with SORT_STRING flag
119 $res_str .= sort_arrays( 'en_US', $test_params, Collator::SORT_STRING );
120 // Sort a non-ASCII array using ru_RU locale.
121 $test_params = array(
122 array( 'абг', 'абв', 'ааа', 'абв' ),
123 array( 'аа', 'ааа', 'а' )
125 $res_str .= sort_arrays( 'ru_RU', $test_params );
126 // Sort an array using Lithuanian locale.
127 $test_params = array(
128 array( 'y' , 'k' , 'i' )
130 $res_str .= sort_arrays( 'lt_LT', $test_params );
131 return $res_str;
133 ut_run('ut_main1');
134 function ut_main2() {
135 $obj = ut_coll_create('en_US');
136 $arr0 = array( 100, 25, 36, '30.2', '30.12' );
137 // test 6
138 $arr1 = array( '100', '25', '36' );
139 // test 6
140 $arr2 = array( 11, 5, '2', 64, 17, '30', 10, 2, '54' );
141 // strcmp 17 and 30, ret = 1
142 // Comparing values 17 and 30, ret = 1
143 $arr3 = array( 11, 5, 2, 64, 17, 30, 10, 2, 54 );
144 $arrA = $arr0;
145 $arrB = $arr0;
146 $arrC = $arr0;
147 ut_coll_sort($obj, $arrA, Collator::SORT_REGULAR);
148 ut_coll_sort($obj, $arrB, Collator::SORT_STRING);
149 ut_coll_sort($obj, $arrC, Collator::SORT_NUMERIC);
150 var_dump($arrA, $arrB, $arrC);
151 $arrA = $arr1;
152 $arrB = $arr1;
153 $arrC = $arr1;
154 ut_coll_sort($obj, $arrA, Collator::SORT_REGULAR);
155 ut_coll_sort($obj, $arrB, Collator::SORT_STRING);
156 ut_coll_sort($obj, $arrC, Collator::SORT_NUMERIC);
157 var_dump($arrA, $arrB, $arrC);
158 $arrA = $arr2;
159 $arrB = $arr2;
160 $arrC = $arr2;
161 ut_coll_sort($obj, $arrA, Collator::SORT_REGULAR);
162 ut_coll_sort($obj, $arrB, Collator::SORT_STRING);
163 ut_coll_sort($obj, $arrC, Collator::SORT_NUMERIC);
164 var_dump($arrA, $arrB, $arrC);
165 $arrA = $arr3;
166 $arrB = $arr3;
167 $arrC = $arr3;
168 ut_coll_sort($obj, $arrA, Collator::SORT_REGULAR);
169 ut_coll_sort($obj, $arrB, Collator::SORT_STRING);
170 ut_coll_sort($obj, $arrC, Collator::SORT_NUMERIC);
171 var_dump($arrA, $arrB, $arrC);
173 ut_run('ut_main2');
174 function ut_main3()
176 $res_str = '';
177 $locales = array(
178 'EN-US-ODESSA',
179 'UK_UA_ODESSA',
181 'root',
182 'uk@currency=EURO'
184 foreach( $locales as $locale )
186 // Create Collator with the current locale.
187 $coll = ut_coll_create( $locale );
188 if( !is_object($coll) )
190 $res_str .= "Error creating collator with '$locale' locale: " .
191 intl_get_error_message() . "\n";
192 continue;
194 // Get the requested, valid and actual locales.
195 $vloc = ut_coll_get_locale( $coll, 1 ); // was Locale::VALID_LOCALE
196 // Show them.
197 $res_str .= "Locale: '$locale'\n" .
198 " ULOC_VALID_LOCALE = '$vloc'\n";
200 return $res_str;
202 ut_run('ut_main3');
203 function test_COW( $locale, $test_array )
205 $res_str = '';
206 $coll = ut_coll_create( $locale );
207 // Create two copies of the given array.
208 $copy1 = $test_array;
209 $copy2 = $test_array;
210 // Sort given array and the first copy of it.
211 ut_coll_sort( $coll, $test_array );
212 ut_coll_sort( $coll, $copy1 );
213 // Return contents of all the arrays.
214 // The second copy should remain unsorted.
215 $res_str .= dump( $test_array ) . "\n";
216 $res_str .= dump( $copy1 ) . "\n";
217 $res_str .= dump( $copy2 ) . "\n";
218 return $res_str;
220 function ut_main4()
222 $res_str = '';
223 $a1 = array( 'b', 'a', 'c' );
224 $a2 = array( 'б', 'а', 'в' );
225 $res_str .= test_COW( 'en_US', $a1 );
226 $res_str .= test_COW( 'ru_RU', $a2 );
227 return $res_str;
229 ut_run('ut_main4');
230 function cmp_array( &$coll, $a )
232 $res = '';
233 $prev = null;
234 foreach( $a as $i )
236 if( is_null( $prev ) )
237 $res .= "$i";
238 else
240 $eqrc = ut_coll_compare( $coll, $prev, $i );
241 $eq = $eqrc < 0 ? "<" : ( $eqrc > 0 ? ">" : "=" );
242 $res .= " $eq $i";
244 $prev = $i;
246 $res .= "\n";
247 return $res;
249 function check_alternate_handling( &$coll )
251 $res = '';
252 ut_coll_set_strength( $coll, Collator::TERTIARY );
253 ut_coll_set_attribute( $coll, Collator::ALTERNATE_HANDLING, Collator::NON_IGNORABLE );
254 $res .= cmp_array( $coll, array( 'di Silva', 'Di Silva', 'diSilva', 'U.S.A.', 'USA' ) );
255 ut_coll_set_attribute( $coll, Collator::ALTERNATE_HANDLING, Collator::SHIFTED );
256 $res .= cmp_array( $coll, array( 'di Silva', 'diSilva', 'Di Silva', 'U.S.A.', 'USA' ) );
257 ut_coll_set_strength( $coll, Collator::QUATERNARY );
258 $res .= cmp_array( $coll, array( 'di Silva', 'diSilva', 'Di Silva', 'U.S.A.', 'USA' ) );
259 $res .= "\n";
260 return $res;
262 function ut_main5()
264 $coll = ut_coll_create( 'en_US' );
265 return
266 check_alternate_handling( $coll );
268 ut_run('ut_main5');
269 function sort_arrays_with_sort_keys( $locale, $arrays )
271 $res_str = '';
272 $coll = ut_coll_create( $locale );
273 foreach( $arrays as $array )
275 // Sort array values
276 $res_val = ut_coll_sort_with_sort_keys( $coll, $array );
277 // Concatenate the sorted array and function result
278 // with output string.
279 $res_dump = "\n" . dump( $array ) .
280 "\n Result: " . dump( $res_val );
283 // Preppend test signature to output string
284 $md5 = md5( $res_dump );
285 global $test_num;
286 $res_str .= "\n\n".
287 "Test $test_num.$md5:" .
288 $res_dump;
289 ++$test_num;
291 return $res_str;
293 function ut_main6()
295 global $test_num;
296 $test_num = 1;
297 $res_str = '';
298 // Sort an array in SORT_REGULAR mode using en_US locale.
299 $test_params = array(
300 array( 'abc', 'abd', 'aaa' ),
301 array( 'm' , '1' , '_' ),
302 array( 'a' , 'aaa', 'aa' ),
303 array( 'ba' , 'b' , 'ab' ),
304 array( 'e' , 'c' , 'a' ),
305 array( 'd' , '' , ' a' ),
306 array( 'd ' , 'f ' , ' a' ),
307 array( 'a' , null , '3' ),
308 array( 'y' , 'i' , 'k' )
310 $res_str .= sort_arrays_with_sort_keys( 'en_US', $test_params );
311 // Sort a non-ASCII array using ru_RU locale.
312 $test_params = array(
313 array( 'абг', 'абв', 'ааа', 'абв' ),
314 array( 'аа', 'ааа', 'а' )
316 $res_str .= sort_arrays_with_sort_keys( 'ru_RU', $test_params );
317 // Array with data for sorting.
318 $test_params = array(
319 array( 'y' , 'i' , 'k' )
321 // Sort an array using Lithuanian locale.
322 $res_str .= sort_arrays_with_sort_keys( 'lt_LT', $test_params );
323 return $res_str . "\n";
325 ut_run('ut_main6');