test_string.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*************************************************************************/
  2. /* test_string.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "ustring.h"
  31. #include <wchar.h>
  32. //#include "math_funcs.h"
  33. #include "core/io/ip_address.h"
  34. #include "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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  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. if (!success) state = false;
  641. return state;
  642. };
  643. typedef bool (*TestFunc)(void);
  644. TestFunc test_funcs[] = {
  645. test_1,
  646. test_2,
  647. test_3,
  648. test_4,
  649. test_5,
  650. test_6,
  651. test_7,
  652. test_8,
  653. test_9,
  654. test_10,
  655. test_11,
  656. test_12,
  657. test_13,
  658. test_14,
  659. test_15,
  660. test_16,
  661. test_17,
  662. test_18,
  663. test_19,
  664. test_20,
  665. test_21,
  666. test_22,
  667. test_23,
  668. test_24,
  669. test_25,
  670. test_26,
  671. test_27,
  672. test_28,
  673. test_29,
  674. 0
  675. };
  676. MainLoop *test() {
  677. /** A character length != wchar_t may be forced, so the tests won't work */
  678. ERR_FAIL_COND_V(sizeof(CharType) != sizeof(wchar_t), NULL);
  679. int count = 0;
  680. int passed = 0;
  681. while (true) {
  682. if (!test_funcs[count])
  683. break;
  684. bool pass = test_funcs[count]();
  685. if (pass)
  686. passed++;
  687. OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED");
  688. count++;
  689. }
  690. OS::get_singleton()->print("\n\n\n");
  691. OS::get_singleton()->print("*************\n");
  692. OS::get_singleton()->print("***TOTALS!***\n");
  693. OS::get_singleton()->print("*************\n");
  694. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  695. return NULL;
  696. }
  697. } // namespace TestString