test_string.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*************************************************************************/
  2. /* test_string.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "test_string.h"
  31. #include "core/io/ip_address.h"
  32. #include "core/os/os.h"
  33. #include "core/ustring.h"
  34. #include "modules/modules_enabled.gen.h"
  35. #ifdef MODULE_REGEX_ENABLED
  36. #include "modules/regex/regex.h"
  37. #endif
  38. #include <stdio.h>
  39. #include <wchar.h>
  40. namespace TestString {
  41. bool test_1() {
  42. OS::get_singleton()->print("\n\nTest 1: Assign from cstr\n");
  43. String s = "Hello";
  44. OS::get_singleton()->print("\tExpected: Hello\n");
  45. OS::get_singleton()->print("\tResulted: %ls\n", s.c_str());
  46. return (wcscmp(s.c_str(), L"Hello") == 0);
  47. }
  48. bool test_2() {
  49. OS::get_singleton()->print("\n\nTest 2: Assign from string (operator=)\n");
  50. String s = "Dolly";
  51. const String &t = s;
  52. OS::get_singleton()->print("\tExpected: Dolly\n");
  53. OS::get_singleton()->print("\tResulted: %ls\n", t.c_str());
  54. return (wcscmp(t.c_str(), L"Dolly") == 0);
  55. }
  56. bool test_3() {
  57. OS::get_singleton()->print("\n\nTest 3: Assign from c-string (copycon)\n");
  58. String s("Sheep");
  59. const String &t(s);
  60. OS::get_singleton()->print("\tExpected: Sheep\n");
  61. OS::get_singleton()->print("\tResulted: %ls\n", t.c_str());
  62. return (wcscmp(t.c_str(), L"Sheep") == 0);
  63. }
  64. bool test_4() {
  65. OS::get_singleton()->print("\n\nTest 4: Assign from c-widechar (operator=)\n");
  66. String s(L"Give me");
  67. OS::get_singleton()->print("\tExpected: Give me\n");
  68. OS::get_singleton()->print("\tResulted: %ls\n", s.c_str());
  69. return (wcscmp(s.c_str(), L"Give me") == 0);
  70. }
  71. bool test_5() {
  72. OS::get_singleton()->print("\n\nTest 5: Assign from c-widechar (copycon)\n");
  73. String s(L"Wool");
  74. OS::get_singleton()->print("\tExpected: Wool\n");
  75. OS::get_singleton()->print("\tResulted: %ls\n", s.c_str());
  76. return (wcscmp(s.c_str(), L"Wool") == 0);
  77. }
  78. bool test_6() {
  79. OS::get_singleton()->print("\n\nTest 6: comparisons (equal)\n");
  80. String s = "Test Compare";
  81. OS::get_singleton()->print("\tComparing to \"Test Compare\"\n");
  82. if (!(s == "Test Compare"))
  83. return false;
  84. if (!(s == L"Test Compare"))
  85. return false;
  86. if (!(s == String("Test Compare")))
  87. return false;
  88. return true;
  89. }
  90. bool test_7() {
  91. OS::get_singleton()->print("\n\nTest 7: comparisons (unequal)\n");
  92. String s = "Test Compare";
  93. OS::get_singleton()->print("\tComparing to \"Test Compare\"\n");
  94. if (!(s != "Peanut"))
  95. return false;
  96. if (!(s != L"Coconut"))
  97. return false;
  98. if (!(s != String("Butter")))
  99. return false;
  100. return true;
  101. }
  102. bool test_8() {
  103. OS::get_singleton()->print("\n\nTest 8: comparisons (operator<)\n");
  104. String s = "Bees";
  105. OS::get_singleton()->print("\tComparing to \"Bees\"\n");
  106. if (!(s < "Elephant"))
  107. return false;
  108. if (s < L"Amber")
  109. return false;
  110. if (s < String("Beatrix"))
  111. return false;
  112. return true;
  113. }
  114. bool test_9() {
  115. OS::get_singleton()->print("\n\nTest 9: Concatenation\n");
  116. String s;
  117. s += "Have";
  118. s += ' ';
  119. s += 'a';
  120. s += String(" ");
  121. s = s + L"Nice";
  122. s = s + " ";
  123. s = s + String("Day");
  124. OS::get_singleton()->print("\tComparing to \"Have a Nice Day\"\n");
  125. return (s == "Have a Nice Day");
  126. }
  127. bool test_10() {
  128. OS::get_singleton()->print("\n\nTest 10: Misc funcs (size/length/empty/etc)\n");
  129. if (!String("").empty())
  130. return false;
  131. if (String("Mellon").size() != 7)
  132. return false;
  133. if (String("Oranges").length() != 7)
  134. return false;
  135. return true;
  136. }
  137. bool test_11() {
  138. OS::get_singleton()->print("\n\nTest 11: Operator[]\n");
  139. String a = "Kugar Sane";
  140. a[0] = 'S';
  141. a[6] = 'C';
  142. if (a != "Sugar Cane")
  143. return false;
  144. if (a[1] != 'u')
  145. return false;
  146. return true;
  147. }
  148. bool test_12() {
  149. OS::get_singleton()->print("\n\nTest 12: case functions\n");
  150. String a = "MoMoNgA";
  151. if (a.to_upper() != "MOMONGA")
  152. return false;
  153. if (a.nocasecmp_to("momonga") != 0)
  154. return false;
  155. return true;
  156. }
  157. bool test_13() {
  158. OS::get_singleton()->print("\n\nTest 13: UTF8\n");
  159. /* how can i embed UTF in here? */
  160. static const CharType ustr[] = { 0x304A, 0x360F, 0x3088, 0x3046, 0 };
  161. //static const wchar_t ustr[] = { 'P', 0xCE, 'p',0xD3, 0 };
  162. String s = ustr;
  163. OS::get_singleton()->print("\tUnicode: %ls\n", ustr);
  164. s.parse_utf8(s.utf8().get_data());
  165. OS::get_singleton()->print("\tConvert/Parse UTF8: %ls\n", s.c_str());
  166. return (s == ustr);
  167. }
  168. bool test_14() {
  169. OS::get_singleton()->print("\n\nTest 14: ASCII\n");
  170. String s = L"Primero Leche";
  171. OS::get_singleton()->print("\tAscii: %s\n", s.ascii().get_data());
  172. String t = s.ascii().get_data();
  173. return (s == t);
  174. }
  175. bool test_15() {
  176. OS::get_singleton()->print("\n\nTest 15: substr\n");
  177. String s = "Killer Baby";
  178. OS::get_singleton()->print("\tsubstr(3,4) of \"%ls\" is \"%ls\"\n", s.c_str(), s.substr(3, 4).c_str());
  179. return (s.substr(3, 4) == "ler ");
  180. }
  181. bool test_16() {
  182. OS::get_singleton()->print("\n\nTest 16: find\n");
  183. String s = "Pretty Woman";
  184. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  185. OS::get_singleton()->print("\t\"tty\" is at %i pos.\n", s.find("tty"));
  186. OS::get_singleton()->print("\t\"Revenge of the Monster Truck\" is at %i pos.\n", s.find("Revenge of the Monster Truck"));
  187. if (s.find("tty") != 3)
  188. return false;
  189. if (s.find("Revenge of the Monster Truck") != -1)
  190. return false;
  191. return true;
  192. }
  193. bool test_17() {
  194. OS::get_singleton()->print("\n\nTest 17: find no case\n");
  195. String s = "Pretty Whale";
  196. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  197. OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n", s.findn("WHA"));
  198. OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n", s.findn("Revenge of the Monster Truck"));
  199. if (s.findn("WHA") != 7)
  200. return false;
  201. if (s.findn("Revenge of the Monster SawFish") != -1)
  202. return false;
  203. return true;
  204. }
  205. bool test_18() {
  206. OS::get_singleton()->print("\n\nTest 18: find no case\n");
  207. String s = "Pretty Whale";
  208. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  209. OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n", s.findn("WHA"));
  210. OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n", s.findn("Revenge of the Monster Truck"));
  211. if (s.findn("WHA") != 7)
  212. return false;
  213. if (s.findn("Revenge of the Monster SawFish") != -1)
  214. return false;
  215. return true;
  216. }
  217. bool test_19() {
  218. OS::get_singleton()->print("\n\nTest 19: Search & replace\n");
  219. String s = "Happy Birthday, Anna!";
  220. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  221. s = s.replace("Birthday", "Halloween");
  222. OS::get_singleton()->print("\tReplaced Birthday/Halloween: %ls.\n", s.c_str());
  223. return (s == "Happy Halloween, Anna!");
  224. }
  225. bool test_20() {
  226. OS::get_singleton()->print("\n\nTest 20: Insertion\n");
  227. String s = "Who is Frederic?";
  228. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  229. s = s.insert(s.find("?"), " Chopin");
  230. OS::get_singleton()->print("\tInserted Chopin: %ls.\n", s.c_str());
  231. return (s == "Who is Frederic Chopin?");
  232. }
  233. bool test_21() {
  234. OS::get_singleton()->print("\n\nTest 21: Number -> String\n");
  235. OS::get_singleton()->print("\tPi is %f\n", 33.141593);
  236. OS::get_singleton()->print("\tPi String is %ls\n", String::num(3.141593).c_str());
  237. return String::num(3.141593) == "3.141593";
  238. }
  239. bool test_22() {
  240. OS::get_singleton()->print("\n\nTest 22: String -> Int\n");
  241. static const char *nums[4] = { "1237461283", "- 22", "0", " - 1123412" };
  242. static const int num[4] = { 1237461283, -22, 0, -1123412 };
  243. for (int i = 0; i < 4; i++) {
  244. OS::get_singleton()->print("\tString: \"%s\" as Int is %i\n", nums[i], String(nums[i]).to_int());
  245. if (String(nums[i]).to_int() != num[i])
  246. return false;
  247. }
  248. return true;
  249. }
  250. bool test_23() {
  251. OS::get_singleton()->print("\n\nTest 23: String -> Float\n");
  252. static const char *nums[4] = { "-12348298412.2", "0.05", "2.0002", " -0.0001" };
  253. static const double num[4] = { -12348298412.2, 0.05, 2.0002, -0.0001 };
  254. for (int i = 0; i < 4; i++) {
  255. OS::get_singleton()->print("\tString: \"%s\" as Float is %f\n", nums[i], String(nums[i]).to_double());
  256. if (ABS(String(nums[i]).to_double() - num[i]) > 0.00001)
  257. return false;
  258. }
  259. return true;
  260. }
  261. bool test_24() {
  262. OS::get_singleton()->print("\n\nTest 24: Slicing\n");
  263. String s = "Mars,Jupiter,Saturn,Uranus";
  264. const char *slices[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
  265. OS::get_singleton()->print("\tSlicing \"%ls\" by \"%s\"..\n", s.c_str(), ",");
  266. for (int i = 0; i < s.get_slice_count(","); i++) {
  267. OS::get_singleton()->print("\t\t%i- %ls\n", i + 1, s.get_slice(",", i).c_str());
  268. if (s.get_slice(",", i) != slices[i])
  269. return false;
  270. }
  271. return true;
  272. }
  273. bool test_25() {
  274. OS::get_singleton()->print("\n\nTest 25: Erasing\n");
  275. String s = "Josephine is such a cute girl!";
  276. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  277. OS::get_singleton()->print("\tRemoving \"cute\"\n");
  278. s.erase(s.find("cute "), String("cute ").length());
  279. OS::get_singleton()->print("\tResult: %ls\n", s.c_str());
  280. return (s == "Josephine is such a girl!");
  281. }
  282. bool test_26() {
  283. OS::get_singleton()->print("\n\nTest 26: RegEx substitution\n");
  284. #ifndef MODULE_REGEX_ENABLED
  285. OS::get_singleton()->print("\tRegEx module disabled, can't run test.");
  286. return false;
  287. #else
  288. String s = "Double all the vowels.";
  289. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  290. OS::get_singleton()->print("\tRepeating instances of 'aeiou' once\n");
  291. RegEx re("(?<vowel>[aeiou])");
  292. s = re.sub(s, "$0$vowel", true);
  293. OS::get_singleton()->print("\tResult: %ls\n", s.c_str());
  294. return (s == "Doouublee aall thee vooweels.");
  295. #endif
  296. }
  297. struct test_27_data {
  298. char const *data;
  299. char const *begin;
  300. bool expected;
  301. };
  302. bool test_27() {
  303. OS::get_singleton()->print("\n\nTest 27: begins_with\n");
  304. test_27_data tc[] = {
  305. { "res://foobar", "res://", true },
  306. { "res", "res://", false },
  307. { "abc", "abc", true }
  308. };
  309. size_t count = sizeof(tc) / sizeof(tc[0]);
  310. bool state = true;
  311. for (size_t i = 0; state && i < count; ++i) {
  312. String s = tc[i].data;
  313. state = s.begins_with(tc[i].begin) == tc[i].expected;
  314. if (state) {
  315. String sb = tc[i].begin;
  316. state = s.begins_with(sb) == tc[i].expected;
  317. }
  318. if (!state) {
  319. OS::get_singleton()->print("\n\t Failure on:\n\t\tstring: %s\n\t\tbegin: %s\n\t\texpected: %s\n", tc[i].data, tc[i].begin, tc[i].expected ? "true" : "false");
  320. break;
  321. }
  322. };
  323. return state;
  324. };
  325. bool test_28() {
  326. OS::get_singleton()->print("\n\nTest 28: sprintf\n");
  327. bool success, state = true;
  328. char output_format[] = "\tTest:\t%ls => %ls (%s)\n";
  329. String format, output;
  330. Array args;
  331. bool error;
  332. // %%
  333. format = "fish %% frog";
  334. args.clear();
  335. output = format.sprintf(args, &error);
  336. success = (output == String("fish % frog") && !error);
  337. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  338. state = state && success;
  339. //////// INTS
  340. // Int
  341. format = "fish %d frog";
  342. args.clear();
  343. args.push_back(5);
  344. output = format.sprintf(args, &error);
  345. success = (output == String("fish 5 frog") && !error);
  346. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  347. state = state && success;
  348. // Int left padded with zeroes.
  349. format = "fish %05d frog";
  350. args.clear();
  351. args.push_back(5);
  352. output = format.sprintf(args, &error);
  353. success = (output == String("fish 00005 frog") && !error);
  354. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  355. state = state && success;
  356. // Int left padded with spaces.
  357. format = "fish %5d frog";
  358. args.clear();
  359. args.push_back(5);
  360. output = format.sprintf(args, &error);
  361. success = (output == String("fish 5 frog") && !error);
  362. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  363. state = state && success;
  364. // Int right padded with spaces.
  365. format = "fish %-5d frog";
  366. args.clear();
  367. args.push_back(5);
  368. output = format.sprintf(args, &error);
  369. success = (output == String("fish 5 frog") && !error);
  370. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  371. state = state && success;
  372. // Int with sign (positive).
  373. format = "fish %+d frog";
  374. args.clear();
  375. args.push_back(5);
  376. output = format.sprintf(args, &error);
  377. success = (output == String("fish +5 frog") && !error);
  378. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  379. state = state && success;
  380. // Negative int.
  381. format = "fish %d frog";
  382. args.clear();
  383. args.push_back(-5);
  384. output = format.sprintf(args, &error);
  385. success = (output == String("fish -5 frog") && !error);
  386. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  387. state = state && success;
  388. // Hex (lower)
  389. format = "fish %x frog";
  390. args.clear();
  391. args.push_back(45);
  392. output = format.sprintf(args, &error);
  393. success = (output == String("fish 2d frog") && !error);
  394. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  395. state = state && success;
  396. // Hex (upper)
  397. format = "fish %X frog";
  398. args.clear();
  399. args.push_back(45);
  400. output = format.sprintf(args, &error);
  401. success = (output == String("fish 2D frog") && !error);
  402. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  403. state = state && success;
  404. // Octal
  405. format = "fish %o frog";
  406. args.clear();
  407. args.push_back(99);
  408. output = format.sprintf(args, &error);
  409. success = (output == String("fish 143 frog") && !error);
  410. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  411. state = state && success;
  412. ////// REALS
  413. // Real
  414. format = "fish %f frog";
  415. args.clear();
  416. args.push_back(99.99);
  417. output = format.sprintf(args, &error);
  418. success = (output == String("fish 99.990000 frog") && !error);
  419. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  420. state = state && success;
  421. // Real left-padded
  422. format = "fish %11f frog";
  423. args.clear();
  424. args.push_back(99.99);
  425. output = format.sprintf(args, &error);
  426. success = (output == String("fish 99.990000 frog") && !error);
  427. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  428. state = state && success;
  429. // Real right-padded
  430. format = "fish %-11f frog";
  431. args.clear();
  432. args.push_back(99.99);
  433. output = format.sprintf(args, &error);
  434. success = (output == String("fish 99.990000 frog") && !error);
  435. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  436. state = state && success;
  437. // Real given int.
  438. format = "fish %f frog";
  439. args.clear();
  440. args.push_back(99);
  441. output = format.sprintf(args, &error);
  442. success = (output == String("fish 99.000000 frog") && !error);
  443. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  444. state = state && success;
  445. // Real with sign (positive).
  446. format = "fish %+f frog";
  447. args.clear();
  448. args.push_back(99.99);
  449. output = format.sprintf(args, &error);
  450. success = (output == String("fish +99.990000 frog") && !error);
  451. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  452. state = state && success;
  453. // Real with 1 decimals.
  454. format = "fish %.1f frog";
  455. args.clear();
  456. args.push_back(99.99);
  457. output = format.sprintf(args, &error);
  458. success = (output == String("fish 100.0 frog") && !error);
  459. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  460. state = state && success;
  461. // Real with 12 decimals.
  462. format = "fish %.12f frog";
  463. args.clear();
  464. args.push_back(99.99);
  465. output = format.sprintf(args, &error);
  466. success = (output == String("fish 99.990000000000 frog") && !error);
  467. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  468. state = state && success;
  469. // Real with no decimals.
  470. format = "fish %.f frog";
  471. args.clear();
  472. args.push_back(99.99);
  473. output = format.sprintf(args, &error);
  474. success = (output == String("fish 100 frog") && !error);
  475. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  476. state = state && success;
  477. /////// Strings.
  478. // String
  479. format = "fish %s frog";
  480. args.clear();
  481. args.push_back("cheese");
  482. output = format.sprintf(args, &error);
  483. success = (output == String("fish cheese frog") && !error);
  484. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  485. state = state && success;
  486. // String left-padded
  487. format = "fish %10s frog";
  488. args.clear();
  489. args.push_back("cheese");
  490. output = format.sprintf(args, &error);
  491. success = (output == String("fish cheese frog") && !error);
  492. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  493. state = state && success;
  494. // String right-padded
  495. format = "fish %-10s frog";
  496. args.clear();
  497. args.push_back("cheese");
  498. output = format.sprintf(args, &error);
  499. success = (output == String("fish cheese frog") && !error);
  500. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  501. state = state && success;
  502. ///// Characters
  503. // Character as string.
  504. format = "fish %c frog";
  505. args.clear();
  506. args.push_back("A");
  507. output = format.sprintf(args, &error);
  508. success = (output == String("fish A frog") && !error);
  509. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  510. state = state && success;
  511. // Character as int.
  512. format = "fish %c frog";
  513. args.clear();
  514. args.push_back(65);
  515. output = format.sprintf(args, &error);
  516. success = (output == String("fish A frog") && !error);
  517. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  518. state = state && success;
  519. ///// Dynamic width
  520. // String dynamic width
  521. format = "fish %*s frog";
  522. args.clear();
  523. args.push_back(10);
  524. args.push_back("cheese");
  525. output = format.sprintf(args, &error);
  526. success = (output == String("fish cheese frog") && !error);
  527. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  528. state = state && success;
  529. // Int dynamic width
  530. format = "fish %*d frog";
  531. args.clear();
  532. args.push_back(10);
  533. args.push_back(99);
  534. output = format.sprintf(args, &error);
  535. success = (output == String("fish 99 frog") && !error);
  536. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  537. state = state && success;
  538. // Float dynamic width
  539. format = "fish %*.*f frog";
  540. args.clear();
  541. args.push_back(10);
  542. args.push_back(3);
  543. args.push_back(99.99);
  544. output = format.sprintf(args, &error);
  545. success = (output == String("fish 99.990 frog") && !error);
  546. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  547. state = state && success;
  548. ///// Errors
  549. // More formats than arguments.
  550. format = "fish %s %s frog";
  551. args.clear();
  552. args.push_back("cheese");
  553. output = format.sprintf(args, &error);
  554. success = (output == "not enough arguments for format string" && error);
  555. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  556. state = state && success;
  557. // More arguments than formats.
  558. format = "fish %s frog";
  559. args.clear();
  560. args.push_back("hello");
  561. args.push_back("cheese");
  562. output = format.sprintf(args, &error);
  563. success = (output == "not all arguments converted during string formatting" && error);
  564. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  565. state = state && success;
  566. // Incomplete format.
  567. format = "fish %10";
  568. args.clear();
  569. args.push_back("cheese");
  570. output = format.sprintf(args, &error);
  571. success = (output == "incomplete format" && error);
  572. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  573. state = state && success;
  574. // Bad character in format string
  575. format = "fish %&f frog";
  576. args.clear();
  577. args.push_back("cheese");
  578. output = format.sprintf(args, &error);
  579. success = (output == "unsupported format character" && error);
  580. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  581. state = state && success;
  582. // Too many decimals.
  583. format = "fish %2.2.2f frog";
  584. args.clear();
  585. args.push_back(99.99);
  586. output = format.sprintf(args, &error);
  587. success = (output == "too many decimal points in format" && error);
  588. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  589. state = state && success;
  590. // * not a number
  591. format = "fish %*f frog";
  592. args.clear();
  593. args.push_back("cheese");
  594. args.push_back(99.99);
  595. output = format.sprintf(args, &error);
  596. success = (output == "* wants number" && error);
  597. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  598. state = state && success;
  599. // Character too long.
  600. format = "fish %c frog";
  601. args.clear();
  602. args.push_back("sc");
  603. output = format.sprintf(args, &error);
  604. success = (output == "%c requires number or single-character string" && error);
  605. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  606. state = state && success;
  607. // Character bad type.
  608. format = "fish %c frog";
  609. args.clear();
  610. args.push_back(Array());
  611. output = format.sprintf(args, &error);
  612. success = (output == "%c requires number or single-character string" && error);
  613. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  614. state = state && success;
  615. return state;
  616. }
  617. bool test_29() {
  618. bool state = true;
  619. IP_Address ip0("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  620. OS::get_singleton()->print("ip0 is %ls\n", String(ip0).c_str());
  621. IP_Address ip(0x0123, 0x4567, 0x89ab, 0xcdef, true);
  622. OS::get_singleton()->print("ip6 is %ls\n", String(ip).c_str());
  623. IP_Address ip2("fe80::52e5:49ff:fe93:1baf");
  624. OS::get_singleton()->print("ip6 is %ls\n", String(ip2).c_str());
  625. IP_Address ip3("::ffff:192.168.0.1");
  626. OS::get_singleton()->print("ip6 is %ls\n", String(ip3).c_str());
  627. String ip4 = "192.168.0.1";
  628. bool success = ip4.is_valid_ip_address();
  629. OS::get_singleton()->print("Is valid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL");
  630. state = state && success;
  631. ip4 = "192.368.0.1";
  632. success = (!ip4.is_valid_ip_address());
  633. OS::get_singleton()->print("Is invalid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL");
  634. state = state && success;
  635. String ip6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
  636. success = ip6.is_valid_ip_address();
  637. OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  638. state = state && success;
  639. ip6 = "2001:0db8:85j3:0000:0000:8a2e:0370:7334";
  640. success = (!ip6.is_valid_ip_address());
  641. OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  642. state = state && success;
  643. ip6 = "2001:0db8:85f345:0000:0000:8a2e:0370:7334";
  644. success = (!ip6.is_valid_ip_address());
  645. OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  646. state = state && success;
  647. ip6 = "2001:0db8::0:8a2e:370:7334";
  648. success = (ip6.is_valid_ip_address());
  649. OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  650. state = state && success;
  651. ip6 = "::ffff:192.168.0.1";
  652. success = (ip6.is_valid_ip_address());
  653. OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  654. state = state && success;
  655. return state;
  656. };
  657. bool test_30() {
  658. bool state = true;
  659. bool success = true;
  660. String input = "bytes2var";
  661. String output = "Bytes 2 Var";
  662. success = (input.capitalize() == output);
  663. state = state && success;
  664. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  665. input = "linear2db";
  666. output = "Linear 2 Db";
  667. success = (input.capitalize() == output);
  668. state = state && success;
  669. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  670. input = "vector3";
  671. output = "Vector 3";
  672. success = (input.capitalize() == output);
  673. state = state && success;
  674. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  675. input = "sha256";
  676. output = "Sha 256";
  677. success = (input.capitalize() == output);
  678. state = state && success;
  679. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  680. input = "2db";
  681. output = "2 Db";
  682. success = (input.capitalize() == output);
  683. state = state && success;
  684. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  685. input = "PascalCase";
  686. output = "Pascal Case";
  687. success = (input.capitalize() == output);
  688. state = state && success;
  689. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  690. input = "PascalPascalCase";
  691. output = "Pascal Pascal Case";
  692. success = (input.capitalize() == output);
  693. state = state && success;
  694. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  695. input = "snake_case";
  696. output = "Snake Case";
  697. success = (input.capitalize() == output);
  698. state = state && success;
  699. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  700. input = "snake_snake_case";
  701. output = "Snake Snake Case";
  702. success = (input.capitalize() == output);
  703. state = state && success;
  704. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  705. input = "sha256sum";
  706. output = "Sha 256 Sum";
  707. success = (input.capitalize() == output);
  708. state = state && success;
  709. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  710. input = "cat2dog";
  711. output = "Cat 2 Dog";
  712. success = (input.capitalize() == output);
  713. state = state && success;
  714. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  715. input = "function(name)";
  716. output = "Function(name)";
  717. success = (input.capitalize() == output);
  718. state = state && success;
  719. OS::get_singleton()->print("Capitalize %ls (existing incorrect behavior): %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  720. input = "snake_case_function(snake_case_arg)";
  721. output = "Snake Case Function(snake Case Arg)";
  722. success = (input.capitalize() == output);
  723. state = state && success;
  724. OS::get_singleton()->print("Capitalize %ls (existing incorrect behavior): %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  725. input = "snake_case_function( snake_case_arg )";
  726. output = "Snake Case Function( Snake Case Arg )";
  727. success = (input.capitalize() == output);
  728. state = state && success;
  729. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  730. return state;
  731. }
  732. bool test_31() {
  733. bool state = true;
  734. bool success;
  735. String a = "";
  736. success = a[0] == 0;
  737. OS::get_singleton()->print("Is 0 String[0]:, %s\n", success ? "OK" : "FAIL");
  738. if (!success)
  739. state = false;
  740. String b = "Godot";
  741. success = b[b.size()] == 0;
  742. OS::get_singleton()->print("Is 0 String[size()]:, %s\n", success ? "OK" : "FAIL");
  743. if (!success)
  744. state = false;
  745. const String c = "";
  746. success = c[0] == 0;
  747. OS::get_singleton()->print("Is 0 const String[0]:, %s\n", success ? "OK" : "FAIL");
  748. if (!success)
  749. state = false;
  750. const String d = "Godot";
  751. success = d[d.size()] == 0;
  752. OS::get_singleton()->print("Is 0 const String[size()]:, %s\n", success ? "OK" : "FAIL");
  753. if (!success)
  754. state = false;
  755. return state;
  756. };
  757. bool test_32() {
  758. #define STRIP_TEST(x) \
  759. { \
  760. bool success = x; \
  761. state = state && success; \
  762. if (!success) { \
  763. OS::get_singleton()->print("\tfailed at: %s\n", #x); \
  764. } \
  765. }
  766. OS::get_singleton()->print("\n\nTest 32: lstrip and rstrip\n");
  767. bool state = true;
  768. // strip none
  769. STRIP_TEST(String("abc").lstrip("") == "abc");
  770. STRIP_TEST(String("abc").rstrip("") == "abc");
  771. // strip one
  772. STRIP_TEST(String("abc").lstrip("a") == "bc");
  773. STRIP_TEST(String("abc").rstrip("c") == "ab");
  774. // strip lots
  775. STRIP_TEST(String("bababbababccc").lstrip("ab") == "ccc");
  776. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("cb") == "aaa");
  777. // strip empty string
  778. STRIP_TEST(String("").lstrip("") == "");
  779. STRIP_TEST(String("").rstrip("") == "");
  780. // strip to empty string
  781. STRIP_TEST(String("abcabcabc").lstrip("bca") == "");
  782. STRIP_TEST(String("abcabcabc").rstrip("bca") == "");
  783. // don't strip wrong end
  784. STRIP_TEST(String("abc").lstrip("c") == "abc");
  785. STRIP_TEST(String("abca").lstrip("a") == "bca");
  786. STRIP_TEST(String("abc").rstrip("a") == "abc");
  787. STRIP_TEST(String("abca").rstrip("a") == "abc");
  788. // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5)
  789. // and the same second as "ÿ" (\u00ff)
  790. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("µÿ")) == String::utf8("¿"));
  791. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("µÿ")) == String::utf8("¿"));
  792. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("µÿ")) == String::utf8("¿ÿ"));
  793. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("µÿ")) == String::utf8("µ¿"));
  794. // the above tests repeated with additional superfluous strip chars
  795. // strip none
  796. STRIP_TEST(String("abc").lstrip("qwjkl") == "abc");
  797. STRIP_TEST(String("abc").rstrip("qwjkl") == "abc");
  798. // strip one
  799. STRIP_TEST(String("abc").lstrip("qwajkl") == "bc");
  800. STRIP_TEST(String("abc").rstrip("qwcjkl") == "ab");
  801. // strip lots
  802. STRIP_TEST(String("bababbababccc").lstrip("qwabjkl") == "ccc");
  803. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("qwcbjkl") == "aaa");
  804. // strip empty string
  805. STRIP_TEST(String("").lstrip("qwjkl") == "");
  806. STRIP_TEST(String("").rstrip("qwjkl") == "");
  807. // strip to empty string
  808. STRIP_TEST(String("abcabcabc").lstrip("qwbcajkl") == "");
  809. STRIP_TEST(String("abcabcabc").rstrip("qwbcajkl") == "");
  810. // don't strip wrong end
  811. STRIP_TEST(String("abc").lstrip("qwcjkl") == "abc");
  812. STRIP_TEST(String("abca").lstrip("qwajkl") == "bca");
  813. STRIP_TEST(String("abc").rstrip("qwajkl") == "abc");
  814. STRIP_TEST(String("abca").rstrip("qwajkl") == "abc");
  815. // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5)
  816. // and the same second as "ÿ" (\u00ff)
  817. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  818. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  819. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿ÿ"));
  820. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("µ¿"));
  821. return state;
  822. #undef STRIP_TEST
  823. }
  824. bool test_33() {
  825. OS::get_singleton()->print("\n\nTest 33: parse_utf8(null, -1)\n");
  826. String empty;
  827. return empty.parse_utf8(nullptr, -1);
  828. }
  829. bool test_34() {
  830. OS::get_singleton()->print("\n\nTest 34: Cyrillic to_lower()\n");
  831. String upper = String::utf8("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ");
  832. String lower = String::utf8("абвгдеёжзийклмнопрстуфхцчшщъыьэюя");
  833. String test = upper.to_lower();
  834. bool state = test == lower;
  835. return state;
  836. }
  837. bool test_35() {
  838. #define COUNT_TEST(x) \
  839. { \
  840. bool success = x; \
  841. state = state && success; \
  842. if (!success) { \
  843. OS::get_singleton()->print("\tfailed at: %s\n", #x); \
  844. } \
  845. }
  846. OS::get_singleton()->print("\n\nTest 35: count and countn function\n");
  847. bool state = true;
  848. COUNT_TEST(String("").count("Test") == 0);
  849. COUNT_TEST(String("Test").count("") == 0);
  850. COUNT_TEST(String("Test").count("test") == 0);
  851. COUNT_TEST(String("Test").count("TEST") == 0);
  852. COUNT_TEST(String("TEST").count("TEST") == 1);
  853. COUNT_TEST(String("Test").count("Test") == 1);
  854. COUNT_TEST(String("aTest").count("Test") == 1);
  855. COUNT_TEST(String("Testa").count("Test") == 1);
  856. COUNT_TEST(String("TestTestTest").count("Test") == 3);
  857. COUNT_TEST(String("TestTestTest").count("TestTest") == 1);
  858. COUNT_TEST(String("TestGodotTestGodotTestGodot").count("Test") == 3);
  859. COUNT_TEST(String("TestTestTestTest").count("Test", 4, 8) == 1);
  860. COUNT_TEST(String("TestTestTestTest").count("Test", 4, 12) == 2);
  861. COUNT_TEST(String("TestTestTestTest").count("Test", 4, 16) == 3);
  862. COUNT_TEST(String("TestTestTestTest").count("Test", 4) == 3);
  863. COUNT_TEST(String("Test").countn("test") == 1);
  864. COUNT_TEST(String("Test").countn("TEST") == 1);
  865. COUNT_TEST(String("testTest-Testatest").countn("tEst") == 4);
  866. COUNT_TEST(String("testTest-TeStatest").countn("tEsT", 4, 16) == 2);
  867. return state;
  868. }
  869. typedef bool (*TestFunc)(void);
  870. TestFunc test_funcs[] = {
  871. test_1,
  872. test_2,
  873. test_3,
  874. test_4,
  875. test_5,
  876. test_6,
  877. test_7,
  878. test_8,
  879. test_9,
  880. test_10,
  881. test_11,
  882. test_12,
  883. test_13,
  884. test_14,
  885. test_15,
  886. test_16,
  887. test_17,
  888. test_18,
  889. test_19,
  890. test_20,
  891. test_21,
  892. test_22,
  893. test_23,
  894. test_24,
  895. test_25,
  896. test_26,
  897. test_27,
  898. test_28,
  899. test_29,
  900. test_30,
  901. test_31,
  902. test_32,
  903. test_33,
  904. test_34,
  905. test_35,
  906. nullptr
  907. };
  908. MainLoop *test() {
  909. /** A character length != wchar_t may be forced, so the tests won't work */
  910. static_assert(sizeof(CharType) == sizeof(wchar_t));
  911. int count = 0;
  912. int passed = 0;
  913. while (true) {
  914. if (!test_funcs[count])
  915. break;
  916. bool pass = test_funcs[count]();
  917. if (pass)
  918. passed++;
  919. OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED");
  920. count++;
  921. }
  922. OS::get_singleton()->print("\n\n\n");
  923. OS::get_singleton()->print("*************\n");
  924. OS::get_singleton()->print("***TOTALS!***\n");
  925. OS::get_singleton()->print("*************\n");
  926. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  927. return nullptr;
  928. }
  929. } // namespace TestString