test_string.cpp 35 KB

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