ut_common.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /*
  3. * Run unit test in OO- and in procedural mode.
  4. * Then compare the outputs.
  5. * It they're equal then show one of them.
  6. * Otherwise indicate an error.
  7. */
  8. function ut_run()
  9. {
  10. // Run unit test in OO mode.
  11. $GLOBALS['oo-mode'] = true;
  12. $oo_result = ut_main();
  13. // Run unit test in procedural mode.
  14. $GLOBALS['oo-mode'] = false;
  15. $proc_result = ut_main();
  16. // Show error if the APIs produce different results.
  17. if( $proc_result !== $oo_result )
  18. {
  19. echo "ERROR: OO- and procedural APIs produce different results!\n";
  20. echo "OO API output:\n";
  21. echo str_repeat( '=', 78 ) . "\n";
  22. echo $oo_result;
  23. echo str_repeat( '=', 78 ) . "\n";
  24. echo "procedural API output:\n";
  25. echo str_repeat( '=', 78 ) . "\n";
  26. echo $proc_result;
  27. echo str_repeat( '=', 78 ) . "\n";
  28. return;
  29. }
  30. // Else, if the results are equal, show one of them.
  31. echo $proc_result;
  32. }
  33. function dump( $val )
  34. {
  35. return var_export( $val, true );
  36. }
  37. /*
  38. * Wrappers around Collator methods to run them in either OO- or procedural mode.
  39. */
  40. function ut_coll_create( $locale )
  41. {
  42. return $GLOBALS['oo-mode'] ? Collator::create( $locale ) : collator_create( $locale );
  43. }
  44. function ut_coll_compare( $coll, $str1, $str2 )
  45. {
  46. return $GLOBALS['oo-mode'] ? $coll->compare( $str1, $str2 ) : collator_compare( $coll, $str1, $str2 );
  47. }
  48. function ut_coll_sort( $coll, &$arr, $sort_flag = Collator::SORT_REGULAR )
  49. {
  50. return $GLOBALS['oo-mode'] ? $coll->sort( $arr, $sort_flag ) : collator_sort( $coll, $arr, $sort_flag );
  51. }
  52. function ut_coll_sort_with_sort_keys( $coll, &$arr )
  53. {
  54. return $GLOBALS['oo-mode'] ? $coll->sortWithSortKeys( $arr ) : collator_sort_with_sort_keys( $coll, $arr );
  55. }
  56. function ut_coll_get_sort_key( $coll, $str )
  57. {
  58. return $GLOBALS['oo-mode'] ? $coll->getSortKey( $str ) : collator_get_sort_key( $coll, $str );
  59. }
  60. function ut_coll_asort( $coll, &$arr, $sort_flag = Collator::SORT_REGULAR )
  61. {
  62. return $GLOBALS['oo-mode'] ? $coll->asort( $arr, $sort_flag ) : collator_asort( $coll, $arr, $sort_flag );
  63. }
  64. function ut_coll_get_locale( $coll, $type )
  65. {
  66. return $GLOBALS['oo-mode'] ? $coll->getLocale( $type ) : collator_get_locale( $coll, $type );
  67. }
  68. function ut_coll_get_display_name( $obj_loc, $disp_loc )
  69. {
  70. return $GLOBALS['oo-mode'] ? Collator::getDisplayName( $obj_loc, $disp_loc ) : collator_get_display_name( $obj_loc, $disp_loc );
  71. }
  72. function ut_coll_get_available_locales()
  73. {
  74. return $GLOBALS['oo-mode'] ? Collator::getAvailableLocales() : collator_get_available_locales();
  75. }
  76. function ut_coll_get_attribute( $coll, $attr )
  77. {
  78. return $GLOBALS['oo-mode'] ? $coll->getAttribute( $attr ) : collator_get_attribute( $coll, $attr );
  79. }
  80. function ut_coll_get_strength( $coll )
  81. {
  82. return $GLOBALS['oo-mode'] ? $coll->getStrength() : collator_get_strength( $coll );
  83. }
  84. function ut_coll_set_strength( $coll, $strength )
  85. {
  86. return $GLOBALS['oo-mode'] ? $coll->setStrength( $strength ) : collator_set_strength( $coll, $strength );
  87. }
  88. function ut_coll_set_attribute( $coll, $attr, $val )
  89. {
  90. return $GLOBALS['oo-mode'] ? $coll->setAttribute( $attr, $val ) : collator_set_attribute( $coll, $attr, $val );
  91. }
  92. function ut_coll_get_variable_top( $coll )
  93. {
  94. return $GLOBALS['oo-mode'] ? $coll->getVariableTop() : collator_get_variable_top( $coll );
  95. }
  96. function ut_coll_set_variable_top( $coll, $var_top )
  97. {
  98. return $GLOBALS['oo-mode'] ? $coll->setVariableTop( $var_top ) : collator_set_variable_top( $coll, $var_top );
  99. }
  100. function ut_coll_restore_variable_top( $coll, $var_top )
  101. {
  102. return $GLOBALS['oo-mode'] ? $coll->restoreVariableTop( $var_top ) : collator_restore_variable_top( $coll, $var_top );
  103. }
  104. function ut_coll_get_error_code( $coll )
  105. {
  106. return $GLOBALS['oo-mode'] ? $coll->getErrorCode() : collator_get_error_code( $coll );
  107. }
  108. function ut_coll_get_error_message( $coll )
  109. {
  110. return $GLOBALS['oo-mode'] ? $coll->getErrorMessage() : collator_get_error_message( $coll );
  111. }
  112. function ut_coll_get_default()
  113. {
  114. return $GLOBALS['oo-mode'] ? Collator::getDefault() : collator_get_default();
  115. }
  116. function ut_coll_set_default( $coll )
  117. {
  118. return $GLOBALS['oo-mode'] ? Collator::setDefault( $coll ) : collator_set_default( $coll );
  119. }
  120. /*
  121. * Wrappers around NumberFormatter methods to run them in either OO- or procedural mode.
  122. */
  123. // FIXME: incomplete list
  124. function ut_nfmt_create( $locale, $style, $pattern = null )
  125. {
  126. if ($GLOBALS['oo-mode']) {
  127. try {
  128. return new NumberFormatter( $locale, $style, $pattern );
  129. } catch (Exception $e) {
  130. return NULL;
  131. }
  132. } else {
  133. return numfmt_create( $locale, $style, $pattern );
  134. }
  135. }
  136. function ut_nfmt_format( $fmt, $number, $type = NumberFormatter::TYPE_DEFAULT )
  137. {
  138. return $GLOBALS['oo-mode'] ? $fmt->format( $number, $type ) : numfmt_format( $fmt, $number, $type );
  139. }
  140. function ut_nfmt_parse( $fmt, $string, $type = NumberFormatter::TYPE_DOUBLE, &$position = null )
  141. {
  142. if(is_null($position)) {
  143. return $GLOBALS['oo-mode'] ? $fmt->parse( $string, $type ) : numfmt_parse( $fmt, $string, $type );
  144. } else {
  145. return $GLOBALS['oo-mode'] ? $fmt->parse( $string, $type, $position ) : numfmt_parse( $fmt, $string, $type, $position );
  146. }
  147. }
  148. function ut_nfmt_format_currency( $fmt, $number, $currency )
  149. {
  150. return $GLOBALS['oo-mode'] ? $fmt->formatCurrency( $number, $currency ) : numfmt_format_currency( $fmt, $number, $currency );
  151. }
  152. function ut_nfmt_parse_currency( $fmt, $string, &$currency, &$position = null )
  153. {
  154. if(is_null($position)) {
  155. return $GLOBALS['oo-mode'] ? $fmt->parseCurrency( $string, $currency ) : numfmt_parse_currency( $fmt, $string, $currency );
  156. } else {
  157. return $GLOBALS['oo-mode'] ? $fmt->parseCurrency( $string, $currency, $position ) : numfmt_parse_currency( $fmt, $string, $currency, $position );
  158. }
  159. }
  160. function ut_nfmt_set_attribute( $fmt, $attribute, $value )
  161. {
  162. return $GLOBALS['oo-mode'] ? $fmt->setAttribute( $attribute, $value ) : numfmt_set_attribute( $fmt, $attribute, $value );
  163. }
  164. function ut_nfmt_set_text_attribute( $fmt, $attribute, $value )
  165. {
  166. return $GLOBALS['oo-mode'] ? $fmt->setTextAttribute( $attribute, $value ) : numfmt_set_text_attribute( $fmt, $attribute, $value );
  167. }
  168. function ut_nfmt_set_symbol( $fmt, $attribute, $value )
  169. {
  170. return $GLOBALS['oo-mode'] ? $fmt->setSymbol( $attribute, $value ) : numfmt_set_symbol( $fmt, $attribute, $value );
  171. }
  172. function ut_nfmt_set_pattern( $fmt, $pattern )
  173. {
  174. return $GLOBALS['oo-mode'] ? $fmt->setPattern( $pattern ) : numfmt_set_pattern( $fmt, $pattern );
  175. }
  176. function ut_nfmt_get_attribute( $fmt, $attribute )
  177. {
  178. return $GLOBALS['oo-mode'] ? $fmt->getAttribute( $attribute ) : numfmt_get_attribute( $fmt, $attribute );
  179. }
  180. function ut_nfmt_get_text_attribute( $fmt, $attribute )
  181. {
  182. return $GLOBALS['oo-mode'] ? $fmt->getTextAttribute( $attribute ) : numfmt_get_text_attribute( $fmt, $attribute );
  183. }
  184. function ut_nfmt_get_symbol( $fmt, $attribute )
  185. {
  186. return $GLOBALS['oo-mode'] ? $fmt->getSymbol( $attribute ) : numfmt_get_symbol( $fmt, $attribute );
  187. }
  188. function ut_nfmt_get_pattern( $fmt )
  189. {
  190. return $GLOBALS['oo-mode'] ? $fmt->getPattern() : numfmt_get_pattern( $fmt );
  191. }
  192. function ut_nfmt_get_locale( $fmt, $type = 0 )
  193. {
  194. return $GLOBALS['oo-mode'] ? $fmt->getLocale( $type ) : numfmt_get_locale( $fmt, $type );
  195. }
  196. function ut_nfmt_get_error_code( $fmt )
  197. {
  198. return $GLOBALS['oo-mode'] ? $fmt->getErrorCode() : numfmt_get_error_code( $fmt );
  199. }
  200. function ut_nfmt_get_error_message( $fmt )
  201. {
  202. return $GLOBALS['oo-mode'] ? $fmt->getErrorMessage() : numfmt_get_error_message( $fmt );
  203. }
  204. function ut_norm_normalize( $str, $form )
  205. {
  206. return $GLOBALS['oo-mode'] ? Normalizer::normalize( $str, $form ) : normalizer_normalize( $str, $form );
  207. }
  208. function ut_norm_is_normalized( $str, $form )
  209. {
  210. return $GLOBALS['oo-mode'] ? Normalizer::isNormalized( $str, $form ) : normalizer_is_normalized( $str, $form );
  211. }
  212. function ut_norm_get_raw_decomposition( $str, $form )
  213. {
  214. return $GLOBALS['oo-mode'] ? Normalizer::getRawDecomposition( $str, $form ) : normalizer_get_raw_decomposition( $str, $form );
  215. }
  216. /*
  217. * Wrappers around Collator methods to run them in either OO- or procedural mode.
  218. */
  219. function ut_loc_get_default( )
  220. {
  221. return $GLOBALS['oo-mode'] ? Locale::getDefault( ) : locale_get_default();
  222. }
  223. function ut_loc_set_default( $locale )
  224. {
  225. return $GLOBALS['oo-mode'] ? Locale::setDefault( $locale ) : locale_set_default( $locale );
  226. }
  227. function ut_loc_get_primary_language( $locale )
  228. {
  229. return $GLOBALS['oo-mode'] ? Locale::getPrimaryLanguage( $locale ) : locale_get_primary_language( $locale );
  230. }
  231. function ut_loc_get_script( $locale )
  232. {
  233. return $GLOBALS['oo-mode'] ? Locale::getScript( $locale ) : locale_get_script( $locale );
  234. }
  235. function ut_loc_get_region( $locale )
  236. {
  237. return $GLOBALS['oo-mode'] ? Locale::getRegion( $locale ) : locale_get_region( $locale );
  238. }
  239. function ut_loc_get_keywords( $locale )
  240. {
  241. return $GLOBALS['oo-mode'] ? Locale::getKeywords( $locale ) : locale_get_keywords( $locale );
  242. }
  243. function ut_loc_get_display_name( $locale , $dispLocale )
  244. {
  245. return $GLOBALS['oo-mode'] ? Locale::getDisplayName( $locale , $dispLocale ) : locale_get_display_name( $locale , $dispLocale );
  246. }
  247. function ut_loc_get_display_language( $locale , $dispLocale )
  248. {
  249. return $GLOBALS['oo-mode'] ? Locale::getDisplayLanguage( $locale , $dispLocale ) : locale_get_display_language( $locale , $dispLocale );
  250. }
  251. function ut_loc_get_display_script( $locale , $dispLocale )
  252. {
  253. return $GLOBALS['oo-mode'] ? Locale::getDisplayScript( $locale , $dispLocale ) : locale_get_display_script( $locale , $dispLocale );
  254. }
  255. function ut_loc_get_display_region( $locale, $dispLocale )
  256. {
  257. return $GLOBALS['oo-mode'] ? Locale::getDisplayRegion( $locale, $dispLocale ) : locale_get_display_region( $locale, $dispLocale );
  258. }
  259. function ut_loc_get_display_variant( $locale , $dispLocale )
  260. {
  261. return $GLOBALS['oo-mode'] ? Locale::getDisplayVariant( $locale , $dispLocale ) : locale_get_display_variant( $locale, $dispLocale );
  262. }
  263. function ut_loc_locale_compose( $loc_parts_arr )
  264. {
  265. return $GLOBALS['oo-mode'] ? Locale::composeLocale( $loc_parts_arr ) : locale_compose( $loc_parts_arr );
  266. }
  267. function ut_loc_locale_parse( $locale )
  268. {
  269. return $GLOBALS['oo-mode'] ? Locale::parseLocale( $locale ) : locale_parse($locale );
  270. }
  271. function ut_loc_locale_get_all_variants( $locale )
  272. {
  273. return $GLOBALS['oo-mode'] ? Locale::getAllVariants( $locale ) : locale_get_all_variants( $locale );
  274. }
  275. function ut_loc_locale_filter_matches( $lang_tag,$loc_range ,$isCanonical)
  276. {
  277. return $GLOBALS['oo-mode'] ? Locale::filterMatches( $lang_tag,$loc_range ,$isCanonical) : locale_filter_matches( $lang_tag,$loc_range ,$isCanonical);
  278. }
  279. function ut_loc_canonicalize( $locale )
  280. {
  281. return $GLOBALS['oo-mode'] ? Locale::canonicalize( $locale ) : locale_canonicalize( $locale );
  282. }
  283. function ut_loc_locale_lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc)
  284. {
  285. return $GLOBALS['oo-mode'] ? Locale::lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc ) : locale_lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc );
  286. }
  287. function ut_loc_accept_http($http) {
  288. return $GLOBALS['oo-mode'] ? Locale::acceptFromHttp($http):locale_accept_from_http($http);
  289. }
  290. /* MessageFormatter functions */
  291. function ut_msgfmt_create( $locale, $pattern)
  292. {
  293. return $GLOBALS['oo-mode'] ? MessageFormatter::create( $locale, $pattern ) : msgfmt_create( $locale, $pattern );
  294. }
  295. function ut_msgfmt_format( $fmt, $args )
  296. {
  297. return $GLOBALS['oo-mode'] ? $fmt->format( $args ) : msgfmt_format( $fmt, $args);
  298. }
  299. function ut_msgfmt_parse( $fmt, $string)
  300. {
  301. return $GLOBALS['oo-mode'] ? $fmt->parse( $string) : msgfmt_parse( $fmt, $string);
  302. }
  303. function ut_msgfmt_format_message( $locale, $pattern, $args )
  304. {
  305. return $GLOBALS['oo-mode'] ? MessageFormatter::formatMessage( $locale, $pattern, $args ) : msgfmt_format_message( $locale, $pattern, $args );
  306. }
  307. function ut_msgfmt_parse_message( $locale, $pattern, $string )
  308. {
  309. return $GLOBALS['oo-mode'] ? MessageFormatter::parseMessage( $locale, $pattern, $string ) : msgfmt_parse_message( $locale, $pattern, $string );
  310. }
  311. function ut_msgfmt_set_pattern( $fmt, $pattern )
  312. {
  313. return $GLOBALS['oo-mode'] ? $fmt->setPattern( $pattern ) : msgfmt_set_pattern( $fmt, $pattern );
  314. }
  315. function ut_msgfmt_get_pattern( $fmt )
  316. {
  317. return $GLOBALS['oo-mode'] ? $fmt->getPattern() : msgfmt_get_pattern( $fmt );
  318. }
  319. function ut_msgfmt_get_locale( $fmt )
  320. {
  321. return $GLOBALS['oo-mode'] ? $fmt->getLocale( ) : msgfmt_get_locale( $fmt );
  322. }
  323. function ut_msgfmt_get_error_code( $fmt )
  324. {
  325. return $GLOBALS['oo-mode'] ? $fmt->getErrorCode() : msgfmt_get_error_code( $fmt );
  326. }
  327. function ut_msgfmt_get_error_message( $fmt )
  328. {
  329. return $GLOBALS['oo-mode'] ? $fmt->getErrorMessage() : msgfmt_get_error_message( $fmt );
  330. }
  331. /* IntlDateFormatter functions */
  332. function ut_datefmt_create( $locale, $datetype, $timetype, $timezone = null, $calendar = null ,$pattern = null)
  333. {
  334. return $GLOBALS['oo-mode'] ? datefmt_create( $locale, $datetype, $timetype, $timezone, $calendar ,$pattern ) : datefmt_create( $locale, $datetype, $timetype, $timezone, $calendar ,$pattern);
  335. }
  336. function ut_datefmt_get_datetype( $fmt )
  337. {
  338. return $GLOBALS['oo-mode'] ? $fmt->getDateType( ) : datefmt_get_datetype( $fmt );
  339. }
  340. function ut_datefmt_get_timetype( $fmt )
  341. {
  342. return $GLOBALS['oo-mode'] ? $fmt->getTimeType( ) : datefmt_get_timetype( $fmt );
  343. }
  344. function ut_datefmt_get_calendar( $fmt )
  345. {
  346. return $GLOBALS['oo-mode'] ? $fmt->getCalendar( ) : datefmt_get_calendar( $fmt );
  347. }
  348. function ut_datefmt_set_calendar( $fmt ,$calendar )
  349. {
  350. return $GLOBALS['oo-mode'] ? $fmt->setCalendar( $calendar ) : datefmt_set_calendar( $fmt , $calendar );
  351. }
  352. function ut_datefmt_get_timezone_id( $fmt )
  353. {
  354. return $GLOBALS['oo-mode'] ? $fmt->getTimeZoneId( ) : datefmt_get_timezone_id( $fmt );
  355. }
  356. function ut_datefmt_set_timezone_id( $fmt ,$timezone_id )
  357. {
  358. return $GLOBALS['oo-mode'] ? $fmt->setTimeZone( $timezone_id ) : datefmt_set_timezone( $fmt ,$timezone_id);
  359. }
  360. function ut_datefmt_get_pattern( $fmt )
  361. {
  362. return $GLOBALS['oo-mode'] ? $fmt->getPattern() : datefmt_get_pattern( $fmt );
  363. }
  364. function ut_datefmt_set_pattern( $fmt , $pattern )
  365. {
  366. return $GLOBALS['oo-mode'] ? $fmt->setPattern( $pattern ) : datefmt_set_pattern( $fmt , $pattern);
  367. }
  368. function ut_datefmt_get_locale( $fmt ,$type=ULOC_ACTUAL_LOCALE)
  369. {
  370. return $GLOBALS['oo-mode'] ? $fmt->getLocale($type ) : datefmt_get_locale( $fmt ,$type);
  371. }
  372. function ut_datefmt_is_lenient( $fmt )
  373. {
  374. return $GLOBALS['oo-mode'] ? $fmt->isLenient() : datefmt_is_lenient( $fmt );
  375. }
  376. function ut_datefmt_set_lenient( $fmt , $lenient )
  377. {
  378. return $GLOBALS['oo-mode'] ? $fmt->setLenient( $lenient ) : datefmt_set_lenient( $fmt , $lenient);
  379. }
  380. function ut_datefmt_format( $fmt , $value )
  381. {
  382. return $GLOBALS['oo-mode'] ? $fmt->format( $value ) : datefmt_format( $fmt , $value);
  383. }
  384. function ut_datefmt_parse( $fmt , $value , &$parse_pos=0 )
  385. {
  386. return $GLOBALS['oo-mode'] ? $fmt->parse( $value ,$parse_pos ) : datefmt_parse( $fmt , $value,$parse_pos);
  387. }
  388. function ut_datefmt_localtime( $fmt , $value , &$parse_pos=0 )
  389. {
  390. return $GLOBALS['oo-mode'] ? $fmt->localtime( $value , $parse_pos ) : datefmt_localtime( $fmt , $value , $parse_pos );
  391. }
  392. function ut_resourcebundle_create( $locale, $bundle, $fallback=true )
  393. {
  394. if ($GLOBALS['oo-mode']) {
  395. try {
  396. return new ResourceBundle($locale, $bundle, $fallback);
  397. } catch (Exception $e) {
  398. return NULL;
  399. }
  400. } else {
  401. return resourcebundle_create($locale, $bundle, $fallback);
  402. }
  403. }
  404. function ut_resourcebundle_count($bundle )
  405. {
  406. return $GLOBALS['oo-mode'] ? $bundle->count():resourcebundle_count($bundle);
  407. }
  408. function ut_resourcebundle_locales($bundle )
  409. {
  410. return $GLOBALS['oo-mode'] ? ResourceBundle::getLocales($bundle):resourcebundle_locales($bundle);
  411. }
  412. function ut_resourcebundle_get($bundle, $idx )
  413. {
  414. return $GLOBALS['oo-mode'] ? $bundle->get($idx):resourcebundle_get($bundle, $idx);
  415. }
  416. function ut_resourcebundle_get_error_code($bundle )
  417. {
  418. return $GLOBALS['oo-mode'] ? $bundle->getErrorCode():resourcebundle_get_error_code($bundle);
  419. }
  420. function ut_resourcebundle_get_error_message($bundle )
  421. {
  422. return $GLOBALS['oo-mode'] ? $bundle->getErrorMessage():resourcebundle_get_error_message($bundle);
  423. }