PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/slow/ext_collator/1810.php

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