test_string.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 <stdio.h>
  36. #include "test_string.h"
  37. namespace TestString {
  38. bool test_1() {
  39. OS::get_singleton()->print("\n\nTest 1: Assign from cstr\n");
  40. String s = "Hello";
  41. OS::get_singleton()->print("\tExpected: Hello\n");
  42. OS::get_singleton()->print("\tResulted: %ls\n", s.c_str());
  43. return (wcscmp(s.c_str(), L"Hello") == 0);
  44. }
  45. bool test_2() {
  46. OS::get_singleton()->print("\n\nTest 2: Assign from string (operator=)\n");
  47. String s = "Dolly";
  48. String t = s;
  49. OS::get_singleton()->print("\tExpected: Dolly\n");
  50. OS::get_singleton()->print("\tResulted: %ls\n", t.c_str());
  51. return (wcscmp(t.c_str(), L"Dolly") == 0);
  52. }
  53. bool test_3() {
  54. OS::get_singleton()->print("\n\nTest 3: Assign from c-string (copycon)\n");
  55. String s("Sheep");
  56. String t(s);
  57. OS::get_singleton()->print("\tExpected: Sheep\n");
  58. OS::get_singleton()->print("\tResulted: %ls\n", t.c_str());
  59. return (wcscmp(t.c_str(), L"Sheep") == 0);
  60. }
  61. bool test_4() {
  62. OS::get_singleton()->print("\n\nTest 4: Assign from c-widechar (operator=)\n");
  63. String s(L"Give me");
  64. OS::get_singleton()->print("\tExpected: Give me\n");
  65. OS::get_singleton()->print("\tResulted: %ls\n", s.c_str());
  66. return (wcscmp(s.c_str(), L"Give me") == 0);
  67. }
  68. bool test_5() {
  69. OS::get_singleton()->print("\n\nTest 5: Assign from c-widechar (copycon)\n");
  70. String s(L"Wool");
  71. OS::get_singleton()->print("\tExpected: Wool\n");
  72. OS::get_singleton()->print("\tResulted: %ls\n", s.c_str());
  73. return (wcscmp(s.c_str(), L"Wool") == 0);
  74. }
  75. bool test_6() {
  76. OS::get_singleton()->print("\n\nTest 6: comparisons (equal)\n");
  77. String s = "Test Compare";
  78. OS::get_singleton()->print("\tComparing to \"Test Compare\"\n");
  79. if (!(s == "Test Compare"))
  80. return false;
  81. if (!(s == L"Test Compare"))
  82. return false;
  83. if (!(s == String("Test Compare")))
  84. return false;
  85. return true;
  86. }
  87. bool test_7() {
  88. OS::get_singleton()->print("\n\nTest 7: comparisons (unequal)\n");
  89. String s = "Test Compare";
  90. OS::get_singleton()->print("\tComparing to \"Test Compare\"\n");
  91. if (!(s != "Peanut"))
  92. return false;
  93. if (!(s != L"Coconut"))
  94. return false;
  95. if (!(s != String("Butter")))
  96. return false;
  97. return true;
  98. }
  99. bool test_8() {
  100. OS::get_singleton()->print("\n\nTest 8: comparisons (operator<)\n");
  101. String s = "Bees";
  102. OS::get_singleton()->print("\tComparing to \"Bees\"\n");
  103. if (!(s < "Elephant"))
  104. return false;
  105. if (s < L"Amber")
  106. return false;
  107. if (s < String("Beatrix"))
  108. return false;
  109. return true;
  110. }
  111. bool test_9() {
  112. OS::get_singleton()->print("\n\nTest 9: Concatenation\n");
  113. String s;
  114. s += "Have";
  115. s += ' ';
  116. s += 'a';
  117. s += String(" ");
  118. s = s + L"Nice";
  119. s = s + " ";
  120. s = s + String("Day");
  121. OS::get_singleton()->print("\tComparing to \"Have a Nice Day\"\n");
  122. return (s == "Have a Nice Day");
  123. }
  124. bool test_10() {
  125. OS::get_singleton()->print("\n\nTest 10: Misc funcs (size/length/empty/etc)\n");
  126. if (!String("").empty())
  127. return false;
  128. if (String("Mellon").size() != 7)
  129. return false;
  130. if (String("Oranges").length() != 7)
  131. return false;
  132. return true;
  133. }
  134. bool test_11() {
  135. OS::get_singleton()->print("\n\nTest 11: Operator[]\n");
  136. String a = "Kugar Sane";
  137. a[0] = 'S';
  138. a[6] = 'C';
  139. if (a != "Sugar Cane")
  140. return false;
  141. if (a[1] != 'u')
  142. return false;
  143. return true;
  144. }
  145. bool test_12() {
  146. OS::get_singleton()->print("\n\nTest 12: case functions\n");
  147. String a = "MoMoNgA";
  148. if (a.to_upper() != "MOMONGA")
  149. return false;
  150. if (a.nocasecmp_to("momonga") != 0)
  151. return false;
  152. return true;
  153. }
  154. bool test_13() {
  155. OS::get_singleton()->print("\n\nTest 13: UTF8\n");
  156. /* how can i embed UTF in here? */
  157. static const CharType ustr[] = { 0x304A, 0x360F, 0x3088, 0x3046, 0 };
  158. //static const wchar_t ustr[] = { 'P', 0xCE, 'p',0xD3, 0 };
  159. String s = ustr;
  160. OS::get_singleton()->print("\tUnicode: %ls\n", ustr);
  161. s.parse_utf8(s.utf8().get_data());
  162. OS::get_singleton()->print("\tConvert/Parse UTF8: %ls\n", s.c_str());
  163. return (s == ustr);
  164. }
  165. bool test_14() {
  166. OS::get_singleton()->print("\n\nTest 14: ASCII\n");
  167. String s = L"Primero Leche";
  168. OS::get_singleton()->print("\tAscii: %s\n", s.ascii().get_data());
  169. String t = s.ascii().get_data();
  170. return (s == t);
  171. }
  172. bool test_15() {
  173. OS::get_singleton()->print("\n\nTest 15: substr\n");
  174. String s = "Killer Baby";
  175. OS::get_singleton()->print("\tsubstr(3,4) of \"%ls\" is \"%ls\"\n", s.c_str(), s.substr(3, 4).c_str());
  176. return (s.substr(3, 4) == "ler ");
  177. }
  178. bool test_16() {
  179. OS::get_singleton()->print("\n\nTest 16: find\n");
  180. String s = "Pretty Woman";
  181. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  182. OS::get_singleton()->print("\t\"tty\" is at %i pos.\n", s.find("tty"));
  183. OS::get_singleton()->print("\t\"Revenge of the Monster Truck\" is at %i pos.\n", s.find("Revenge of the Monster Truck"));
  184. if (s.find("tty") != 3)
  185. return false;
  186. if (s.find("Revenge of the Monster Truck") != -1)
  187. return false;
  188. return true;
  189. }
  190. bool test_17() {
  191. OS::get_singleton()->print("\n\nTest 17: find no case\n");
  192. String s = "Pretty Whale";
  193. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  194. OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n", s.findn("WHA"));
  195. OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n", s.findn("Revenge of the Monster Truck"));
  196. if (s.findn("WHA") != 7)
  197. return false;
  198. if (s.findn("Revenge of the Monster SawFish") != -1)
  199. return false;
  200. return true;
  201. }
  202. bool test_18() {
  203. OS::get_singleton()->print("\n\nTest 18: find no case\n");
  204. String s = "Pretty Whale";
  205. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  206. OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n", s.findn("WHA"));
  207. OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n", s.findn("Revenge of the Monster Truck"));
  208. if (s.findn("WHA") != 7)
  209. return false;
  210. if (s.findn("Revenge of the Monster SawFish") != -1)
  211. return false;
  212. return true;
  213. }
  214. bool test_19() {
  215. OS::get_singleton()->print("\n\nTest 19: Search & replace\n");
  216. String s = "Happy Birthday, Anna!";
  217. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  218. s = s.replace("Birthday", "Halloween");
  219. OS::get_singleton()->print("\tReplaced Birthday/Halloween: %ls.\n", s.c_str());
  220. return (s == "Happy Halloween, Anna!");
  221. }
  222. bool test_20() {
  223. OS::get_singleton()->print("\n\nTest 20: Insertion\n");
  224. String s = "Who is Frederic?";
  225. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  226. s = s.insert(s.find("?"), " Chopin");
  227. OS::get_singleton()->print("\tInserted Chopin: %ls.\n", s.c_str());
  228. return (s == "Who is Frederic Chopin?");
  229. }
  230. bool test_21() {
  231. OS::get_singleton()->print("\n\nTest 21: Number -> String\n");
  232. OS::get_singleton()->print("\tPi is %f\n", 33.141593);
  233. OS::get_singleton()->print("\tPi String is %ls\n", String::num(3.141593).c_str());
  234. return String::num(3.141593) == "3.141593";
  235. }
  236. bool test_22() {
  237. OS::get_singleton()->print("\n\nTest 22: String -> Int\n");
  238. static const char *nums[4] = { "1237461283", "- 22", "0", " - 1123412" };
  239. static const int num[4] = { 1237461283, -22, 0, -1123412 };
  240. for (int i = 0; i < 4; i++) {
  241. OS::get_singleton()->print("\tString: \"%s\" as Int is %i\n", nums[i], String(nums[i]).to_int());
  242. if (String(nums[i]).to_int() != num[i])
  243. return false;
  244. }
  245. return true;
  246. }
  247. bool test_23() {
  248. OS::get_singleton()->print("\n\nTest 23: String -> Float\n");
  249. static const char *nums[4] = { "-12348298412.2", "0.05", "2.0002", " -0.0001" };
  250. static const double num[4] = { -12348298412.2, 0.05, 2.0002, -0.0001 };
  251. for (int i = 0; i < 4; i++) {
  252. OS::get_singleton()->print("\tString: \"%s\" as Float is %f\n", nums[i], String(nums[i]).to_double());
  253. if (ABS(String(nums[i]).to_double() - num[i]) > 0.00001)
  254. return false;
  255. }
  256. return true;
  257. }
  258. bool test_24() {
  259. OS::get_singleton()->print("\n\nTest 24: Slicing\n");
  260. String s = "Mars,Jupiter,Saturn,Uranus";
  261. const char *slices[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
  262. OS::get_singleton()->print("\tSlicing \"%ls\" by \"%s\"..\n", s.c_str(), ",");
  263. for (int i = 0; i < s.get_slice_count(","); i++) {
  264. OS::get_singleton()->print("\t\t%i- %ls\n", i + 1, s.get_slice(",", i).c_str());
  265. if (s.get_slice(",", i) != slices[i])
  266. return false;
  267. }
  268. return true;
  269. }
  270. bool test_25() {
  271. OS::get_singleton()->print("\n\nTest 25: Erasing\n");
  272. String s = "Josephine is such a cute girl!";
  273. OS::get_singleton()->print("\tString: %ls\n", s.c_str());
  274. OS::get_singleton()->print("\tRemoving \"cute\"\n");
  275. s.erase(s.find("cute "), String("cute ").length());
  276. OS::get_singleton()->print("\tResult: %ls\n", s.c_str());
  277. return (s == "Josephine is such a girl!");
  278. }
  279. bool test_26() {
  280. //TODO: Do replacement RegEx test
  281. return true;
  282. };
  283. struct test_27_data {
  284. char const *data;
  285. char const *begin;
  286. bool expected;
  287. };
  288. bool test_27() {
  289. OS::get_singleton()->print("\n\nTest 27: begins_with\n");
  290. test_27_data tc[] = {
  291. { "res://foobar", "res://", true },
  292. { "res", "res://", false },
  293. { "abc", "abc", true }
  294. };
  295. size_t count = sizeof(tc) / sizeof(tc[0]);
  296. bool state = true;
  297. for (size_t i = 0; state && i < count; ++i) {
  298. String s = tc[i].data;
  299. state = s.begins_with(tc[i].begin) == tc[i].expected;
  300. if (state) {
  301. String sb = tc[i].begin;
  302. state = s.begins_with(sb) == tc[i].expected;
  303. }
  304. if (!state) {
  305. OS::get_singleton()->print("\n\t Failure on:\n\t\tstring: ", tc[i].data, "\n\t\tbegin: ", tc[i].begin, "\n\t\texpected: ", tc[i].expected ? "true" : "false", "\n");
  306. break;
  307. }
  308. };
  309. return state;
  310. };
  311. bool test_28() {
  312. OS::get_singleton()->print("\n\nTest 28: sprintf\n");
  313. bool success, state = true;
  314. char output_format[] = "\tTest:\t%ls => %ls (%s)\n";
  315. String format, output;
  316. Array args;
  317. bool error;
  318. // %%
  319. format = "fish %% frog";
  320. args.clear();
  321. output = format.sprintf(args, &error);
  322. success = (output == String("fish % frog") && !error);
  323. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  324. state = state && success;
  325. //////// INTS
  326. // Int
  327. format = "fish %d frog";
  328. args.clear();
  329. args.push_back(5);
  330. output = format.sprintf(args, &error);
  331. success = (output == String("fish 5 frog") && !error);
  332. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  333. state = state && success;
  334. // Int left padded with zeroes.
  335. format = "fish %05d frog";
  336. args.clear();
  337. args.push_back(5);
  338. output = format.sprintf(args, &error);
  339. success = (output == String("fish 00005 frog") && !error);
  340. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  341. state = state && success;
  342. // Int left padded with spaces.
  343. format = "fish %5d frog";
  344. args.clear();
  345. args.push_back(5);
  346. output = format.sprintf(args, &error);
  347. success = (output == String("fish 5 frog") && !error);
  348. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  349. state = state && success;
  350. // Int right padded with spaces.
  351. format = "fish %-5d frog";
  352. args.clear();
  353. args.push_back(5);
  354. output = format.sprintf(args, &error);
  355. success = (output == String("fish 5 frog") && !error);
  356. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  357. state = state && success;
  358. // Int with sign (positive).
  359. format = "fish %+d frog";
  360. args.clear();
  361. args.push_back(5);
  362. output = format.sprintf(args, &error);
  363. success = (output == String("fish +5 frog") && !error);
  364. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  365. state = state && success;
  366. // Negative int.
  367. format = "fish %d frog";
  368. args.clear();
  369. args.push_back(-5);
  370. output = format.sprintf(args, &error);
  371. success = (output == String("fish -5 frog") && !error);
  372. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  373. state = state && success;
  374. // Hex (lower)
  375. format = "fish %x frog";
  376. args.clear();
  377. args.push_back(45);
  378. output = format.sprintf(args, &error);
  379. success = (output == String("fish 2d frog") && !error);
  380. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  381. state = state && success;
  382. // Hex (upper)
  383. format = "fish %X frog";
  384. args.clear();
  385. args.push_back(45);
  386. output = format.sprintf(args, &error);
  387. success = (output == String("fish 2D frog") && !error);
  388. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  389. state = state && success;
  390. // Octal
  391. format = "fish %o frog";
  392. args.clear();
  393. args.push_back(99);
  394. output = format.sprintf(args, &error);
  395. success = (output == String("fish 143 frog") && !error);
  396. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  397. state = state && success;
  398. ////// REALS
  399. // Real
  400. format = "fish %f frog";
  401. args.clear();
  402. args.push_back(99.99);
  403. output = format.sprintf(args, &error);
  404. success = (output == String("fish 99.990000 frog") && !error);
  405. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  406. state = state && success;
  407. // Real left-padded
  408. format = "fish %11f frog";
  409. args.clear();
  410. args.push_back(99.99);
  411. output = format.sprintf(args, &error);
  412. success = (output == String("fish 99.990000 frog") && !error);
  413. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  414. state = state && success;
  415. // Real right-padded
  416. format = "fish %-11f frog";
  417. args.clear();
  418. args.push_back(99.99);
  419. output = format.sprintf(args, &error);
  420. success = (output == String("fish 99.990000 frog") && !error);
  421. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  422. state = state && success;
  423. // Real given int.
  424. format = "fish %f frog";
  425. args.clear();
  426. args.push_back(99);
  427. output = format.sprintf(args, &error);
  428. success = (output == String("fish 99.000000 frog") && !error);
  429. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  430. state = state && success;
  431. // Real with sign (positive).
  432. format = "fish %+f frog";
  433. args.clear();
  434. args.push_back(99.99);
  435. output = format.sprintf(args, &error);
  436. success = (output == String("fish +99.990000 frog") && !error);
  437. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  438. state = state && success;
  439. // Real with 1 decimals.
  440. format = "fish %.1f frog";
  441. args.clear();
  442. args.push_back(99.99);
  443. output = format.sprintf(args, &error);
  444. success = (output == String("fish 100.0 frog") && !error);
  445. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  446. state = state && success;
  447. // Real with 12 decimals.
  448. format = "fish %.12f frog";
  449. args.clear();
  450. args.push_back(99.99);
  451. output = format.sprintf(args, &error);
  452. success = (output == String("fish 99.990000000000 frog") && !error);
  453. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  454. state = state && success;
  455. // Real with no decimals.
  456. format = "fish %.f frog";
  457. args.clear();
  458. args.push_back(99.99);
  459. output = format.sprintf(args, &error);
  460. success = (output == String("fish 100 frog") && !error);
  461. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  462. state = state && success;
  463. /////// Strings.
  464. // String
  465. format = "fish %s frog";
  466. args.clear();
  467. args.push_back("cheese");
  468. output = format.sprintf(args, &error);
  469. success = (output == String("fish cheese frog") && !error);
  470. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  471. state = state && success;
  472. // String left-padded
  473. format = "fish %10s frog";
  474. args.clear();
  475. args.push_back("cheese");
  476. output = format.sprintf(args, &error);
  477. success = (output == String("fish cheese frog") && !error);
  478. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  479. state = state && success;
  480. // String right-padded
  481. format = "fish %-10s frog";
  482. args.clear();
  483. args.push_back("cheese");
  484. output = format.sprintf(args, &error);
  485. success = (output == String("fish cheese frog") && !error);
  486. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  487. state = state && success;
  488. ///// Characters
  489. // Character as string.
  490. format = "fish %c frog";
  491. args.clear();
  492. args.push_back("A");
  493. output = format.sprintf(args, &error);
  494. success = (output == String("fish A frog") && !error);
  495. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  496. state = state && success;
  497. // Character as int.
  498. format = "fish %c frog";
  499. args.clear();
  500. args.push_back(65);
  501. output = format.sprintf(args, &error);
  502. success = (output == String("fish A frog") && !error);
  503. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  504. state = state && success;
  505. ///// Dynamic width
  506. // String dynamic width
  507. format = "fish %*s frog";
  508. args.clear();
  509. args.push_back(10);
  510. args.push_back("cheese");
  511. output = format.sprintf(args, &error);
  512. success = (output == String("fish cheese frog") && !error);
  513. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  514. state = state && success;
  515. // Int dynamic width
  516. format = "fish %*d frog";
  517. args.clear();
  518. args.push_back(10);
  519. args.push_back(99);
  520. output = format.sprintf(args, &error);
  521. success = (output == String("fish 99 frog") && !error);
  522. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  523. state = state && success;
  524. // Float dynamic width
  525. format = "fish %*.*f frog";
  526. args.clear();
  527. args.push_back(10);
  528. args.push_back(3);
  529. args.push_back(99.99);
  530. output = format.sprintf(args, &error);
  531. success = (output == String("fish 99.990 frog") && !error);
  532. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  533. state = state && success;
  534. ///// Errors
  535. // More formats than arguments.
  536. format = "fish %s %s frog";
  537. args.clear();
  538. args.push_back("cheese");
  539. output = format.sprintf(args, &error);
  540. success = (output == "not enough arguments for format string" && error);
  541. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  542. state = state && success;
  543. // More arguments than formats.
  544. format = "fish %s frog";
  545. args.clear();
  546. args.push_back("hello");
  547. args.push_back("cheese");
  548. output = format.sprintf(args, &error);
  549. success = (output == "not all arguments converted during string formatting" && error);
  550. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  551. state = state && success;
  552. // Incomplete format.
  553. format = "fish %10";
  554. args.clear();
  555. args.push_back("cheese");
  556. output = format.sprintf(args, &error);
  557. success = (output == "incomplete format" && error);
  558. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  559. state = state && success;
  560. // Bad character in format string
  561. format = "fish %&f frog";
  562. args.clear();
  563. args.push_back("cheese");
  564. output = format.sprintf(args, &error);
  565. success = (output == "unsupported format character" && error);
  566. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  567. state = state && success;
  568. // Too many decimals.
  569. format = "fish %2.2.2f frog";
  570. args.clear();
  571. args.push_back(99.99);
  572. output = format.sprintf(args, &error);
  573. success = (output == "too many decimal points in format" && error);
  574. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  575. state = state && success;
  576. // * not a number
  577. format = "fish %*f frog";
  578. args.clear();
  579. args.push_back("cheese");
  580. args.push_back(99.99);
  581. output = format.sprintf(args, &error);
  582. success = (output == "* wants number" && error);
  583. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  584. state = state && success;
  585. // Character too long.
  586. format = "fish %c frog";
  587. args.clear();
  588. args.push_back("sc");
  589. output = format.sprintf(args, &error);
  590. success = (output == "%c requires number or single-character string" && error);
  591. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  592. state = state && success;
  593. // Character bad type.
  594. format = "fish %c frog";
  595. args.clear();
  596. args.push_back(Array());
  597. output = format.sprintf(args, &error);
  598. success = (output == "%c requires number or single-character string" && error);
  599. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  600. state = state && success;
  601. return state;
  602. }
  603. bool test_29() {
  604. bool state = true;
  605. IP_Address ip0("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  606. OS::get_singleton()->print("ip0 is %ls\n", String(ip0).c_str());
  607. IP_Address ip(0x0123, 0x4567, 0x89ab, 0xcdef, true);
  608. OS::get_singleton()->print("ip6 is %ls\n", String(ip).c_str());
  609. IP_Address ip2("fe80::52e5:49ff:fe93:1baf");
  610. OS::get_singleton()->print("ip6 is %ls\n", String(ip2).c_str());
  611. IP_Address ip3("::ffff:192.168.0.1");
  612. OS::get_singleton()->print("ip6 is %ls\n", String(ip3).c_str());
  613. String ip4 = "192.168.0.1";
  614. bool success = ip4.is_valid_ip_address();
  615. OS::get_singleton()->print("Is valid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL");
  616. state = state && success;
  617. ip4 = "192.368.0.1";
  618. success = (!ip4.is_valid_ip_address());
  619. OS::get_singleton()->print("Is invalid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL");
  620. state = state && success;
  621. String ip6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
  622. success = ip6.is_valid_ip_address();
  623. OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  624. state = state && success;
  625. ip6 = "2001:0db8:85j3:0000:0000:8a2e:0370:7334";
  626. success = (!ip6.is_valid_ip_address());
  627. OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  628. state = state && success;
  629. ip6 = "2001:0db8:85f345:0000:0000:8a2e:0370:7334";
  630. success = (!ip6.is_valid_ip_address());
  631. OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  632. state = state && success;
  633. ip6 = "2001:0db8::0:8a2e:370:7334";
  634. success = (ip6.is_valid_ip_address());
  635. OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  636. state = state && success;
  637. ip6 = "::ffff:192.168.0.1";
  638. success = (ip6.is_valid_ip_address());
  639. OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  640. state = state && success;
  641. return state;
  642. };
  643. bool test_30() {
  644. bool state = true;
  645. bool success = true;
  646. String input = "bytes2var";
  647. String output = "Bytes 2 Var";
  648. success = (input.capitalize() == output);
  649. state = state && success;
  650. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  651. input = "linear2db";
  652. output = "Linear 2 Db";
  653. success = (input.capitalize() == output);
  654. state = state && success;
  655. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  656. input = "vector3";
  657. output = "Vector 3";
  658. success = (input.capitalize() == output);
  659. state = state && success;
  660. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  661. input = "sha256";
  662. output = "Sha 256";
  663. success = (input.capitalize() == output);
  664. state = state && success;
  665. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  666. input = "2db";
  667. output = "2 Db";
  668. success = (input.capitalize() == output);
  669. state = state && success;
  670. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  671. input = "PascalCase";
  672. output = "Pascal Case";
  673. success = (input.capitalize() == output);
  674. state = state && success;
  675. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  676. input = "PascalPascalCase";
  677. output = "Pascal Pascal Case";
  678. success = (input.capitalize() == output);
  679. state = state && success;
  680. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  681. input = "snake_case";
  682. output = "Snake Case";
  683. success = (input.capitalize() == output);
  684. state = state && success;
  685. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  686. input = "snake_snake_case";
  687. output = "Snake Snake Case";
  688. success = (input.capitalize() == output);
  689. state = state && success;
  690. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  691. input = "sha256sum";
  692. output = "Sha 256 Sum";
  693. success = (input.capitalize() == output);
  694. state = state && success;
  695. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  696. input = "cat2dog";
  697. output = "Cat 2 Dog";
  698. success = (input.capitalize() == output);
  699. state = state && success;
  700. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  701. input = "function(name)";
  702. output = "Function(name)";
  703. success = (input.capitalize() == output);
  704. state = state && success;
  705. OS::get_singleton()->print("Capitalize %ls (existing incorrect behavior): %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  706. input = "snake_case_function(snake_case_arg)";
  707. output = "Snake Case Function(snake Case Arg)";
  708. success = (input.capitalize() == output);
  709. state = state && success;
  710. OS::get_singleton()->print("Capitalize %ls (existing incorrect behavior): %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  711. input = "snake_case_function( snake_case_arg )";
  712. output = "Snake Case Function( Snake Case Arg )";
  713. success = (input.capitalize() == output);
  714. state = state && success;
  715. OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
  716. return state;
  717. };
  718. typedef bool (*TestFunc)(void);
  719. TestFunc test_funcs[] = {
  720. test_1,
  721. test_2,
  722. test_3,
  723. test_4,
  724. test_5,
  725. test_6,
  726. test_7,
  727. test_8,
  728. test_9,
  729. test_10,
  730. test_11,
  731. test_12,
  732. test_13,
  733. test_14,
  734. test_15,
  735. test_16,
  736. test_17,
  737. test_18,
  738. test_19,
  739. test_20,
  740. test_21,
  741. test_22,
  742. test_23,
  743. test_24,
  744. test_25,
  745. test_26,
  746. test_27,
  747. test_28,
  748. test_29,
  749. test_30,
  750. 0
  751. };
  752. MainLoop *test() {
  753. /** A character length != wchar_t may be forced, so the tests won't work */
  754. ERR_FAIL_COND_V(sizeof(CharType) != sizeof(wchar_t), NULL);
  755. int count = 0;
  756. int passed = 0;
  757. while (true) {
  758. if (!test_funcs[count])
  759. break;
  760. bool pass = test_funcs[count]();
  761. if (pass)
  762. passed++;
  763. OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED");
  764. count++;
  765. }
  766. OS::get_singleton()->print("\n\n\n");
  767. OS::get_singleton()->print("*************\n");
  768. OS::get_singleton()->print("***TOTALS!***\n");
  769. OS::get_singleton()->print("*************\n");
  770. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  771. return NULL;
  772. }
  773. } // namespace TestString