PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/test_ext_string.cpp

https://github.com/kevlund/hiphop-php
C++ | 835 lines | 704 code | 114 blank | 17 comment | 9 complexity | 57283beebce46cc50665fd3420bf832b MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <test/test_ext_string.h>
  17. #include <runtime/ext/ext_string.h>
  18. ///////////////////////////////////////////////////////////////////////////////
  19. bool TestExtString::RunTests(const std::string &which) {
  20. bool ret = true;
  21. RUN_TEST(test_addcslashes);
  22. RUN_TEST(test_stripcslashes);
  23. RUN_TEST(test_addslashes);
  24. RUN_TEST(test_stripslashes);
  25. RUN_TEST(test_bin2hex);
  26. RUN_TEST(test_hex2bin);
  27. RUN_TEST(test_nl2br);
  28. RUN_TEST(test_quotemeta);
  29. RUN_TEST(test_str_shuffle);
  30. RUN_TEST(test_strrev);
  31. RUN_TEST(test_strtolower);
  32. RUN_TEST(test_strtoupper);
  33. RUN_TEST(test_ucfirst);
  34. RUN_TEST(test_ucwords);
  35. RUN_TEST(test_strip_tags);
  36. RUN_TEST(test_trim);
  37. RUN_TEST(test_ltrim);
  38. RUN_TEST(test_rtrim);
  39. RUN_TEST(test_chop);
  40. RUN_TEST(test_explode);
  41. RUN_TEST(test_implode);
  42. RUN_TEST(test_join);
  43. RUN_TEST(test_str_split);
  44. RUN_TEST(test_chunk_split);
  45. RUN_TEST(test_strtok);
  46. RUN_TEST(test_str_replace);
  47. RUN_TEST(test_str_ireplace);
  48. RUN_TEST(test_substr_replace);
  49. RUN_TEST(test_substr);
  50. RUN_TEST(test_str_pad);
  51. RUN_TEST(test_str_repeat);
  52. RUN_TEST(test_wordwrap);
  53. RUN_TEST(test_html_entity_decode);
  54. RUN_TEST(test_htmlentities);
  55. RUN_TEST(test_htmlspecialchars_decode);
  56. RUN_TEST(test_htmlspecialchars);
  57. RUN_TEST(test_quoted_printable_encode);
  58. RUN_TEST(test_quoted_printable_decode);
  59. RUN_TEST(test_convert_uudecode);
  60. RUN_TEST(test_convert_uuencode);
  61. RUN_TEST(test_str_rot13);
  62. RUN_TEST(test_crc32);
  63. RUN_TEST(test_crypt);
  64. RUN_TEST(test_md5);
  65. RUN_TEST(test_sha1);
  66. RUN_TEST(test_strtr);
  67. RUN_TEST(test_convert_cyr_string);
  68. RUN_TEST(test_get_html_translation_table);
  69. RUN_TEST(test_hebrev);
  70. RUN_TEST(test_hebrevc);
  71. RUN_TEST(test_setlocale);
  72. RUN_TEST(test_localeconv);
  73. RUN_TEST(test_nl_langinfo);
  74. RUN_TEST(test_printf);
  75. RUN_TEST(test_vprintf);
  76. RUN_TEST(test_sprintf);
  77. RUN_TEST(test_vsprintf);
  78. RUN_TEST(test_sscanf);
  79. RUN_TEST(test_chr);
  80. RUN_TEST(test_ord);
  81. RUN_TEST(test_money_format);
  82. RUN_TEST(test_number_format);
  83. RUN_TEST(test_strcmp);
  84. RUN_TEST(test_strncmp);
  85. RUN_TEST(test_strnatcmp);
  86. RUN_TEST(test_strcasecmp);
  87. RUN_TEST(test_strncasecmp);
  88. RUN_TEST(test_strnatcasecmp);
  89. RUN_TEST(test_strcoll);
  90. RUN_TEST(test_substr_compare);
  91. RUN_TEST(test_strchr);
  92. RUN_TEST(test_strrchr);
  93. RUN_TEST(test_strstr);
  94. RUN_TEST(test_stristr);
  95. RUN_TEST(test_strpbrk);
  96. RUN_TEST(test_strpos);
  97. RUN_TEST(test_stripos);
  98. RUN_TEST(test_strrpos);
  99. RUN_TEST(test_strripos);
  100. RUN_TEST(test_substr_count);
  101. RUN_TEST(test_strspn);
  102. RUN_TEST(test_strcspn);
  103. RUN_TEST(test_strlen);
  104. RUN_TEST(test_count_chars);
  105. RUN_TEST(test_str_word_count);
  106. RUN_TEST(test_levenshtein);
  107. RUN_TEST(test_similar_text);
  108. RUN_TEST(test_soundex);
  109. RUN_TEST(test_metaphone);
  110. RUN_TEST(test_parse_str);
  111. return ret;
  112. }
  113. ///////////////////////////////////////////////////////////////////////////////
  114. bool TestExtString::test_addcslashes() {
  115. VS(f_addcslashes("ABCDEFGH\n", "A..D\n"), "\\A\\B\\C\\DEFGH\\n");
  116. VS(f_addcslashes(String("\x00\x0D\n", 3, AttachLiteral), null_string),
  117. "\\000\\r\\n");
  118. return Count(true);
  119. }
  120. bool TestExtString::test_stripcslashes() {
  121. VS(f_stripcslashes("\\A\\B\\C\\DEFGH\\n"), "ABCDEFGH\n");
  122. return Count(true);
  123. }
  124. bool TestExtString::test_addslashes() {
  125. VS(f_addslashes("'\"\\\n"), "\\'\\\"\\\\\n");
  126. return Count(true);
  127. }
  128. bool TestExtString::test_stripslashes() {
  129. VS(f_stripslashes("\\'\\\"\\\\\n"), "'\"\\\n");
  130. return Count(true);
  131. }
  132. bool TestExtString::test_bin2hex() {
  133. VS(f_bin2hex("ABC\n"), "4142430a");
  134. return Count(true);
  135. }
  136. bool TestExtString::test_hex2bin() {
  137. VS(f_hex2bin("4142430a"), "ABC\n");
  138. return Count(true);
  139. }
  140. bool TestExtString::test_nl2br() {
  141. VS(f_nl2br("A\nB"), "A<br />\nB");
  142. return Count(true);
  143. }
  144. bool TestExtString::test_quotemeta() {
  145. VS(f_quotemeta(". \\ + * ? [ ^ ] ( $ )"),
  146. "\\. \\\\ \\+ \\* \\? \\[ \\^ \\] \\( \\$ \\)");
  147. return Count(true);
  148. }
  149. bool TestExtString::test_str_shuffle() {
  150. VERIFY(f_str_shuffle("ABC").size() == 3);
  151. return Count(true);
  152. }
  153. bool TestExtString::test_strrev() {
  154. VS(f_strrev("ABC"), "CBA");
  155. return Count(true);
  156. }
  157. bool TestExtString::test_strtolower() {
  158. VS(f_strtolower("ABC"), "abc");
  159. return Count(true);
  160. }
  161. bool TestExtString::test_strtoupper() {
  162. VS(f_strtoupper("abc"), "ABC");
  163. return Count(true);
  164. }
  165. bool TestExtString::test_ucfirst() {
  166. VS(f_ucfirst("abc"), "Abc");
  167. return Count(true);
  168. }
  169. bool TestExtString::test_ucwords() {
  170. VS(f_ucwords("abc def"), "Abc Def");
  171. return Count(true);
  172. }
  173. bool TestExtString::test_strip_tags() {
  174. String text = "<p>Test paragraph.</p><!-- Comment --> "
  175. "<a href=\"#fragment\">Other text</a>";
  176. VS(f_strip_tags(text), "Test paragraph. Other text");
  177. VS(f_strip_tags(text, "<p><a>"),
  178. "<p>Test paragraph.</p> <a href=\"#fragment\">Other text</a>");
  179. return Count(true);
  180. }
  181. bool TestExtString::test_trim() {
  182. VS(f_trim(" abc "), "abc");
  183. return Count(true);
  184. }
  185. bool TestExtString::test_ltrim() {
  186. VS(f_ltrim(" abc "), "abc ");
  187. return Count(true);
  188. }
  189. bool TestExtString::test_rtrim() {
  190. VS(f_rtrim(" abc "), " abc");
  191. return Count(true);
  192. }
  193. bool TestExtString::test_chop() {
  194. VS(f_chop(" abc "), " abc");
  195. return Count(true);
  196. }
  197. bool TestExtString::test_explode() {
  198. {
  199. String metric = "create stuff";
  200. Array pieces = f_explode(" ", metric, 1);
  201. VS(pieces.size(), 1);
  202. VS(pieces[0], "create stuff");
  203. }
  204. {
  205. String metric = "create stuff";
  206. Array pieces = f_explode(" ", metric, 0);
  207. VS(pieces.size(), 1);
  208. VS(pieces[0], "create stuff");
  209. }
  210. {
  211. String pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
  212. Array pieces = f_explode(" ", pizza);
  213. VS(pieces[0], "piece1");
  214. VS(pieces[1], "piece2");
  215. }
  216. {
  217. String str = "one|two|three|four";
  218. Array ret = f_explode("|", str, 2);
  219. VERIFY(ret.size() == 2);
  220. VS(ret[0], "one");
  221. VS(ret[1], "two|three|four");
  222. }
  223. {
  224. String str = "one|two|three|four";
  225. Array ret = f_explode("|", str, -1);
  226. VERIFY(ret.size() == 3);
  227. VS(ret[0], "one");
  228. VS(ret[1], "two");
  229. VS(ret[2], "three");
  230. }
  231. {
  232. String str = "ab";
  233. Array ret = f_explode("b", str);
  234. VS(ret[0], "a");
  235. VS(ret[1], "");
  236. }
  237. {
  238. String str = "b";
  239. Array ret = f_explode("b", str);
  240. VS(ret[0], "");
  241. VS(ret[1], "");
  242. }
  243. {
  244. String str = "bb";
  245. Array ret = f_explode("b", str);
  246. VS(ret[0], "");
  247. VS(ret[1], "");
  248. VS(ret[2], "");
  249. }
  250. {
  251. String str = "";
  252. Array ret = f_explode("b", str);
  253. VS(ret[0], "");
  254. }
  255. return Count(true);
  256. }
  257. bool TestExtString::test_implode() {
  258. {
  259. Array arr = CREATE_VECTOR3("lastname", "email", "phone");
  260. VS(f_implode(",", arr), "lastname,email,phone");
  261. }
  262. {
  263. Array arr = CREATE_VECTOR3("lastname", "", "phone");
  264. VS(f_implode(",", arr), "lastname,,phone");
  265. }
  266. {
  267. Array arr = CREATE_VECTOR3("", "email", "phone");
  268. VS(f_implode(",", arr), ",email,phone");
  269. }
  270. {
  271. Array arr = CREATE_VECTOR3("", "", "");
  272. VS(f_implode(",", arr), ",,");
  273. }
  274. {
  275. Array arr = Array::Create();
  276. VS(f_implode(",", arr), "");
  277. }
  278. return Count(true);
  279. }
  280. bool TestExtString::test_join() {
  281. Array arr = CREATE_VECTOR3("lastname", "email", "phone");
  282. VS(f_join(",", arr), "lastname,email,phone");
  283. return Count(true);
  284. }
  285. bool TestExtString::test_str_split() {
  286. String str = "Hello Friend";
  287. Array arr1 = f_str_split(str);
  288. VERIFY(arr1.size() == str.size());
  289. VS(arr1[0], "H");
  290. VS(arr1[1], "e");
  291. Array arr2 = f_str_split(str, 3);
  292. VERIFY(arr2.size() == 4);
  293. VS(arr2[0], "Hel");
  294. VS(arr2[1], "lo ");
  295. return Count(true);
  296. }
  297. bool TestExtString::test_chunk_split() {
  298. String ret = f_chunk_split("ABCD", 2);
  299. VS(ret, "AB\r\nCD\r\n");
  300. return Count(true);
  301. }
  302. bool TestExtString::test_strtok() {
  303. String s = "This is\tan ";
  304. Array tokens;
  305. Variant tok = f_strtok(s, " \n\t");
  306. while (tok) {
  307. tokens.append(tok);
  308. tok = f_strtok(" \n\t");
  309. }
  310. VS(tokens, CREATE_VECTOR3("This", "is", "an"));
  311. return Count(true);
  312. }
  313. bool TestExtString::test_str_replace() {
  314. {
  315. VS(f_str_replace("%body%", "black", "<body text='%body%'>"),
  316. "<body text='black'>");
  317. }
  318. {
  319. Array vowels;
  320. vowels.append("a");
  321. vowels.append("e");
  322. vowels.append("i");
  323. vowels.append("o");
  324. vowels.append("u");
  325. vowels.append("A");
  326. vowels.append("E");
  327. vowels.append("I");
  328. vowels.append("O");
  329. vowels.append("U");
  330. VS(f_str_replace(vowels, "", "Hello World of PHP"), "Hll Wrld f PHP");
  331. }
  332. {
  333. String phrase = "You should eat fruits, vegetables, and fiber every day.";
  334. Array healthy = CREATE_VECTOR3("fruits", "vegetables", "fiber");
  335. Array yummy = CREATE_VECTOR3("pizza", "beer", "ice cream");
  336. VS(f_str_replace(healthy, yummy, phrase),
  337. "You should eat pizza, beer, and ice cream every day.");
  338. }
  339. {
  340. Variant count;
  341. Variant str = f_str_replace("ll", "", "good golly miss molly!",
  342. ref(count));
  343. VS(count, 2);
  344. }
  345. {
  346. Array letters = CREATE_VECTOR2("a", "p");
  347. Array fruit = CREATE_VECTOR2("apple", "pear");
  348. String text = "a p";
  349. VS(f_str_replace(letters, fruit, text), "apearpearle pear");
  350. }
  351. return Count(true);
  352. }
  353. bool TestExtString::test_str_ireplace() {
  354. VS(f_str_ireplace("%body%", "black", "<body text='%BODY%'>"),
  355. "<body text='black'>");
  356. VS(f_str_ireplace("%body%", "Black", "<body Text='%BODY%'>"),
  357. "<body Text='Black'>");
  358. return Count(true);
  359. }
  360. bool TestExtString::test_substr_replace() {
  361. String var = "ABCDEFGH:/MNRPQR/";
  362. VS(f_substr_replace(var, "bob", 0), "bob");
  363. VS(f_substr_replace(var, "bob", 0, f_strlen(var)), "bob");
  364. VS(f_substr_replace(var, "bob", 0, 0), "bobABCDEFGH:/MNRPQR/");
  365. VS(f_substr_replace(var, "bob", 10, -1), "ABCDEFGH:/bob/");
  366. VS(f_substr_replace(var, "bob", -7, -1), "ABCDEFGH:/bob/");
  367. VS(f_substr_replace(var, "", 10, -1), "ABCDEFGH://");
  368. return Count(true);
  369. }
  370. bool TestExtString::test_substr() {
  371. VS(f_substr("abcdef", 1), "bcdef");
  372. VS(f_substr("abcdef", 1, 3), "bcd");
  373. VS(f_substr("abcdef", 0, 4), "abcd");
  374. VS(f_substr("abcdef", 0, 8), "abcdef");
  375. VS(f_substr("abcdef", -1, 1), "f");
  376. VS(f_substr("abcdef", 6), false);
  377. VS(f_substr("abcdef", 3, 0), "");
  378. return Count(true);
  379. }
  380. bool TestExtString::test_str_pad() {
  381. String input = "Alien";
  382. VS(f_str_pad(input, 10), "Alien ");
  383. VS(f_str_pad(input, 10, "-=", k_STR_PAD_LEFT), "-=-=-Alien");
  384. VS(f_str_pad(input, 10, "_", k_STR_PAD_BOTH), "__Alien___");
  385. VS(f_str_pad(input, 6 , "___"), "Alien_");
  386. VS(f_str_pad(input, 6 , String("\0", 1, AttachLiteral)),
  387. String("Alien\0", 6, AttachLiteral));
  388. return Count(true);
  389. }
  390. bool TestExtString::test_str_repeat() {
  391. VS(f_str_repeat("-=", 10), "-=-=-=-=-=-=-=-=-=-=");
  392. return Count(true);
  393. }
  394. bool TestExtString::test_wordwrap() {
  395. {
  396. String text = "The quick brown fox jumped over the lazy dog.";
  397. VS(f_wordwrap(text, 20, "<br />\n"),
  398. "The quick brown fox<br />\njumped over the lazy<br />\ndog.");
  399. }
  400. {
  401. String text = "A very long woooooooooooord.";
  402. VS(f_wordwrap(text, 8, "\n", true), "A very\nlong\nwooooooo\nooooord.");
  403. }
  404. return Count(true);
  405. }
  406. bool TestExtString::test_html_entity_decode() {
  407. String orig = "I'll \"walk\" the <b>dog</b> now";
  408. String a = f_htmlentities(orig);
  409. VS(a, "I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now");
  410. VS(f_html_entity_decode(a), orig);
  411. VS(f_bin2hex(f_html_entity_decode("&nbsp;", 3)), "a0");
  412. VS(f_bin2hex(f_html_entity_decode("&nbsp;", 3, "")), "c2a0");
  413. VS(f_bin2hex(f_html_entity_decode("&nbsp;", 3, "UTF-8")), "c2a0");
  414. VS(f_html_entity_decode("&amp; & &amp;", k_ENT_QUOTES, "UTF-8"), "& & &");
  415. VS(f_html_entity_decode("&#00000000000000097; test &amp;", k_ENT_QUOTES, "UTF-8"), "a test &");
  416. VS(f_bin2hex(f_html_entity_decode("&Egrave;")), "c8");
  417. VS(f_bin2hex(f_html_entity_decode("&Egrave;", 3, "UTF-8")), "c388");
  418. VS(f_html_entity_decode("&Alpha;"), "&Alpha;");
  419. VS(f_bin2hex(f_html_entity_decode("&Alpha;", 3, "UTF-8")), "ce91");
  420. return Count(true);
  421. }
  422. bool TestExtString::test_htmlentities() {
  423. String str = "A 'quote' is <b>bold</b>";
  424. VS(f_htmlentities(str), "A 'quote' is &lt;b&gt;bold&lt;/b&gt;");
  425. VS(f_htmlentities(str, k_ENT_QUOTES),
  426. "A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;");
  427. VS(f_htmlentities("\xA0", k_ENT_COMPAT), "&nbsp;");
  428. VS(f_htmlentities("\xc2\xA0", k_ENT_COMPAT, ""), "&nbsp;");
  429. VS(f_htmlentities("\xc2\xA0", k_ENT_COMPAT, "UTF-8"), "&nbsp;");
  430. return Count(true);
  431. }
  432. bool TestExtString::test_htmlspecialchars_decode() {
  433. String str = "<p>this -&gt; &quot;</p>";
  434. VS(f_htmlspecialchars_decode(str), "<p>this -> \"</p>");
  435. VS(f_htmlspecialchars_decode("&lt;"), "<");
  436. VS(f_htmlspecialchars_decode("&nbsp;"), "&nbsp;");
  437. VS(f_htmlspecialchars_decode("&amp; &Eacute; &Alpha; &#039;"),
  438. "& &Eacute; &Alpha; '");
  439. return Count(true);
  440. }
  441. bool TestExtString::test_htmlspecialchars() {
  442. VS(f_htmlspecialchars("<a href='test'>Test</a>", k_ENT_QUOTES),
  443. "&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;");
  444. VS(f_bin2hex(f_htmlspecialchars("\xA0", k_ENT_COMPAT)), "a0");
  445. VS(f_bin2hex(f_htmlspecialchars("\xc2\xA0", k_ENT_COMPAT, "")), "c2a0");
  446. VS(f_bin2hex(f_htmlspecialchars("\xc2\xA0", k_ENT_COMPAT, "UTF-8")), "c2a0");
  447. return Count(true);
  448. }
  449. bool TestExtString::test_quoted_printable_encode() {
  450. VS(f_quoted_printable_encode("egfe \015\t"), "egfe=20=0D=09");
  451. return Count(true);
  452. }
  453. bool TestExtString::test_quoted_printable_decode() {
  454. VS(f_quoted_printable_decode("=65=67=66=65="), "egfe");
  455. return Count(true);
  456. }
  457. bool TestExtString::test_convert_uudecode() {
  458. VS(f_convert_uudecode("+22!L;W9E(%!(4\"$`\n`"), "I love PHP!");
  459. return Count(true);
  460. }
  461. bool TestExtString::test_convert_uuencode() {
  462. VS(f_convert_uuencode("test\ntext text\r\n"),
  463. "0=&5S=`IT97AT('1E>'0-\"@``\n`\n");
  464. return Count(true);
  465. }
  466. bool TestExtString::test_str_rot13() {
  467. VS(f_str_rot13("PHP 4.3.0"), "CUC 4.3.0");
  468. return Count(true);
  469. }
  470. bool TestExtString::test_crc32() {
  471. VS(f_crc32("The quick brown fox jumped over the lazy dog."), 2191738434LL);
  472. return Count(true);
  473. }
  474. bool TestExtString::test_crypt() {
  475. VERIFY(!f_crypt("mypassword").empty());
  476. return Count(true);
  477. }
  478. bool TestExtString::test_md5() {
  479. VS(f_md5("apple"), "1f3870be274f6c49b3e31a0c6728957f");
  480. return Count(true);
  481. }
  482. bool TestExtString::test_sha1() {
  483. VS(f_sha1("apple"), "d0be2dc421be4fcd0172e5afceea3970e2f3d940");
  484. return Count(true);
  485. }
  486. bool TestExtString::test_strtr() {
  487. Array trans = CREATE_MAP2("hello", "hi", "hi", "hello");
  488. VS(f_strtr("hi all, I said hello", trans), "hello all, I said hi");
  489. return Count(true);
  490. }
  491. bool TestExtString::test_convert_cyr_string() {
  492. VS(f_convert_cyr_string("abc", "a", "d"), "abc"); // sanity
  493. return Count(true);
  494. }
  495. bool TestExtString::test_get_html_translation_table() {
  496. VERIFY(!f_get_html_translation_table(k_HTML_ENTITIES).empty());
  497. return Count(true);
  498. }
  499. bool TestExtString::test_hebrev() {
  500. VS(f_hebrev("test"), "test"); // sanity
  501. return Count(true);
  502. }
  503. bool TestExtString::test_hebrevc() {
  504. VS(f_hebrevc("test"), "test"); // sanity
  505. return Count(true);
  506. }
  507. bool TestExtString::test_setlocale() {
  508. VERIFY(!f_setlocale(0, k_LC_ALL, 0).toString().empty());
  509. return Count(true);
  510. }
  511. bool TestExtString::test_localeconv() {
  512. VERIFY(!f_localeconv().empty());
  513. return Count(true);
  514. }
  515. bool TestExtString::test_nl_langinfo() {
  516. VS(f_nl_langinfo(k_AM_STR), "AM");
  517. return Count(true);
  518. }
  519. bool TestExtString::test_printf() {
  520. g_context->obStart();
  521. f_printf(2, "A%sB%dC", CREATE_VECTOR2("test", 10));
  522. String output = g_context->obCopyContents();
  523. g_context->obEnd();
  524. VS(output, "AtestB10C");
  525. g_context->obStart();
  526. f_printf(2, "test %2$d %d", CREATE_VECTOR2(1, 10));
  527. output = g_context->obCopyContents();
  528. g_context->obEnd();
  529. VS(output, "test 10 1");
  530. return Count(true);
  531. }
  532. bool TestExtString::test_vprintf() {
  533. g_context->obStart();
  534. f_vprintf("A%sB%dC", CREATE_VECTOR2("test", 10));
  535. String output = g_context->obCopyContents();
  536. g_context->obEnd();
  537. VS(output, "AtestB10C");
  538. return Count(true);
  539. }
  540. bool TestExtString::test_sprintf() {
  541. VS(f_sprintf(2, "A%sB%dC", CREATE_VECTOR2("test", 10)), "AtestB10C");
  542. VS(f_sprintf(2, "%010s", CREATE_VECTOR1("1101")), "0000001101");
  543. VS(f_sprintf(2, "%02d", CREATE_VECTOR1("09")), "09");
  544. return Count(true);
  545. }
  546. bool TestExtString::test_vsprintf() {
  547. VS(f_vsprintf("A%sB%dC", CREATE_VECTOR2("test", 10)), "AtestB10C");
  548. return Count(true);
  549. }
  550. bool TestExtString::test_sscanf() {
  551. VS(f_sscanf(0, "SN/2350001", "SN/%d"), CREATE_VECTOR1(2350001));
  552. Variant out;
  553. VS(f_sscanf(0, "SN/2350001", "SN/%d", CREATE_VECTOR1(ref(out))), 1);
  554. VS(out, 2350001);
  555. return Count(true);
  556. }
  557. bool TestExtString::test_chr() {
  558. VS(f_chr(92), "\\");
  559. return Count(true);
  560. }
  561. bool TestExtString::test_ord() {
  562. VS(f_ord("\\"), 92);
  563. return Count(true);
  564. }
  565. bool TestExtString::test_money_format() {
  566. VS(f_money_format("%i", 1234.56), "1234.56");
  567. return Count(true);
  568. }
  569. bool TestExtString::test_number_format() {
  570. VS(f_number_format(1234.56), "1,235");
  571. return Count(true);
  572. }
  573. bool TestExtString::test_strcmp() {
  574. VERIFY(f_strcmp("a", "b") < 0);
  575. VERIFY(f_strcmp("a", "A") > 0);
  576. return Count(true);
  577. }
  578. bool TestExtString::test_strncmp() {
  579. VERIFY(f_strncmp("a", "ab", 1) == 0);
  580. return Count(true);
  581. }
  582. bool TestExtString::test_strnatcmp() {
  583. VERIFY(f_strnatcmp("a", "b") < 0);
  584. return Count(true);
  585. }
  586. bool TestExtString::test_strcasecmp() {
  587. VERIFY(f_strcasecmp("a", "A") == 0);
  588. return Count(true);
  589. }
  590. bool TestExtString::test_strncasecmp() {
  591. VERIFY(f_strncasecmp("a", "Ab", 1) == 0);
  592. return Count(true);
  593. }
  594. bool TestExtString::test_strnatcasecmp() {
  595. VERIFY(f_strnatcasecmp("a", "Ab") < 0);
  596. return Count(true);
  597. }
  598. bool TestExtString::test_strcoll() {
  599. VERIFY(f_strcoll("a", "b") < 0);
  600. VERIFY(f_strcoll("a", "A") > 0);
  601. return Count(true);
  602. }
  603. bool TestExtString::test_substr_compare() {
  604. VS(f_substr_compare("abcde", "bc", 1, 2), 0);
  605. VS(f_substr_compare("abcde", "de", -2, 2), 0);
  606. VS(f_substr_compare("abcde", "bcg", 1, 2), 0);
  607. VS(f_substr_compare("abcde", "BC", 1, 2, true), 0);
  608. VS(f_substr_compare("abcde", "bc", 1, 3), 1);
  609. VS(f_substr_compare("abcde", "cd", 1, 2), -1);
  610. return Count(true);
  611. }
  612. bool TestExtString::test_strchr() {
  613. String email = "name@example.com";
  614. VS(f_strchr(email, "@"), "@example.com");
  615. return Count(true);
  616. }
  617. bool TestExtString::test_strrchr() {
  618. String text = "Line 1\nLine 2\nLine 3";
  619. VS(f_strrchr(text, 10), "\nLine 3");
  620. return Count(true);
  621. }
  622. bool TestExtString::test_strstr() {
  623. String email = "name@example.com";
  624. VS(f_strstr(email, "@"), "@example.com");
  625. return Count(true);
  626. }
  627. bool TestExtString::test_stristr() {
  628. VS(f_stristr("Hello World!", "earth"), false);
  629. VS(f_stristr("APPLE", 97), "APPLE");
  630. return Count(true);
  631. }
  632. bool TestExtString::test_strpbrk() {
  633. String text = "This is a Simple text.";
  634. VS(f_strpbrk(text, "mi"), "is is a Simple text.");
  635. VS(f_strpbrk(text, "S"), "Simple text.");
  636. return Count(true);
  637. }
  638. bool TestExtString::test_strpos() {
  639. VS(f_strpos("abcdef abcdef", "a"), 0);
  640. VS(f_strpos("abcdef abcdef", "a", 1), 7);
  641. VS(f_strpos("abcdef abcdef", "A", 1), false);
  642. VS(f_strpos("abcdef abcdef", "", 0), false);
  643. return Count(true);
  644. }
  645. bool TestExtString::test_stripos() {
  646. VS(f_stripos("abcdef abcdef", "A", 1), 7);
  647. return Count(true);
  648. }
  649. bool TestExtString::test_strrpos() {
  650. VS(f_strrpos("abcdef abcdef", "a"), 7);
  651. VS(f_strrpos("0123456789a123456789b123456789c", "7", -5), 17);
  652. VS(f_strrpos("0123456789a123456789b123456789c", "7", 20), 27);
  653. VS(f_strrpos("0123456789a123456789b123456789c", "7", 28), false);
  654. return Count(true);
  655. }
  656. bool TestExtString::test_strripos() {
  657. VS(f_strripos("abcdef abcdef", "A"), 7);
  658. return Count(true);
  659. }
  660. bool TestExtString::test_substr_count() {
  661. String text = "This is a test";
  662. VS(f_substr_count(text, "is"), 2);
  663. VS(f_substr_count(text, "is", 3), 1);
  664. VS(f_substr_count(text, "is", 3, 3), 0);
  665. VS(f_substr_count("gcdgcdgcd", "gcdgcd"), 1);
  666. return Count(true);
  667. }
  668. bool TestExtString::test_strspn() {
  669. VS(f_strspn("foo", "o", 1, 2), 2);
  670. return Count(true);
  671. }
  672. bool TestExtString::test_strcspn() {
  673. VS(f_strcspn("foo", "o", 1, 2), 0);
  674. return Count(true);
  675. }
  676. bool TestExtString::test_strlen() {
  677. VS(f_strlen("test"), 4);
  678. return Count(true);
  679. }
  680. bool TestExtString::test_count_chars() {
  681. Array ret = f_count_chars("Two Ts and one F.");
  682. VS(ret[f_ord("T")], 2);
  683. return Count(true);
  684. }
  685. bool TestExtString::test_str_word_count() {
  686. VS(f_str_word_count("Two Ts and one F."), 5);
  687. return Count(true);
  688. }
  689. bool TestExtString::test_levenshtein() {
  690. VS(f_levenshtein("carrrot", "carrot"), 1);
  691. return Count(true);
  692. }
  693. bool TestExtString::test_similar_text() {
  694. VS(f_similar_text("carrrot", "carrot"), 6);
  695. return Count(true);
  696. }
  697. bool TestExtString::test_soundex() {
  698. VS(f_soundex("carrot"), "C630");
  699. return Count(true);
  700. }
  701. bool TestExtString::test_metaphone() {
  702. VS(f_metaphone("carrot"), "KRT");
  703. return Count(true);
  704. }
  705. bool TestExtString::test_parse_str() {
  706. {
  707. Variant output;
  708. f_parse_str("first=value&arr[]=foo+bar&arr[]=baz", ref(output));
  709. VS(output["first"], "value");
  710. VS(output["arr"][0], "foo bar");
  711. VS(output["arr"][1], "baz");
  712. }
  713. {
  714. Variant output;
  715. f_parse_str("a[2][i]=3&a[4][i]=5", ref(output));
  716. VS(output["a"][2]["i"], "3");
  717. VS(output["a"][4]["i"], "5");
  718. }
  719. return Count(true);
  720. }