test_string.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. bool test_31() {
  719. bool state = true;
  720. bool success;
  721. String a = "";
  722. success = a[0] == 0;
  723. OS::get_singleton()->print("Is 0 String[0]:, %s\n", success ? "OK" : "FAIL");
  724. if (!success) state = false;
  725. String b = "Godot";
  726. success = b[b.size()] == 0;
  727. OS::get_singleton()->print("Is 0 String[size()]:, %s\n", success ? "OK" : "FAIL");
  728. if (!success) state = false;
  729. const String c = "";
  730. success = c[0] == 0;
  731. OS::get_singleton()->print("Is 0 const String[0]:, %s\n", success ? "OK" : "FAIL");
  732. if (!success) state = false;
  733. const String d = "Godot";
  734. success = d[d.size()] == 0;
  735. OS::get_singleton()->print("Is 0 const String[size()]:, %s\n", success ? "OK" : "FAIL");
  736. if (!success) state = false;
  737. return state;
  738. };
  739. bool test_32() {
  740. #define STRIP_TEST(x) \
  741. { \
  742. bool success = x; \
  743. state = state && success; \
  744. if (!success) { \
  745. OS::get_singleton()->print("\tfailed at: %s\n", #x); \
  746. } \
  747. }
  748. OS::get_singleton()->print("\n\nTest 32: lstrip and rstrip\n");
  749. bool state = true;
  750. // strip none
  751. STRIP_TEST(String("abc").lstrip("") == "abc");
  752. STRIP_TEST(String("abc").rstrip("") == "abc");
  753. // strip one
  754. STRIP_TEST(String("abc").lstrip("a") == "bc");
  755. STRIP_TEST(String("abc").rstrip("c") == "ab");
  756. // strip lots
  757. STRIP_TEST(String("bababbababccc").lstrip("ab") == "ccc");
  758. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("cb") == "aaa");
  759. // strip empty string
  760. STRIP_TEST(String("").lstrip("") == "");
  761. STRIP_TEST(String("").rstrip("") == "");
  762. // strip to empty string
  763. STRIP_TEST(String("abcabcabc").lstrip("bca") == "");
  764. STRIP_TEST(String("abcabcabc").rstrip("bca") == "");
  765. // don't strip wrong end
  766. STRIP_TEST(String("abc").lstrip("c") == "abc");
  767. STRIP_TEST(String("abca").lstrip("a") == "bca");
  768. STRIP_TEST(String("abc").rstrip("a") == "abc");
  769. STRIP_TEST(String("abca").rstrip("a") == "abc");
  770. // in utf-8 "¿" has the same first byte as "µ"
  771. // and the same second as "ÿ"
  772. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("µÿ")) == String::utf8("¿"));
  773. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("µÿ")) == String::utf8("¿"));
  774. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("µÿ")) == String::utf8("¿ÿ"));
  775. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("µÿ")) == String::utf8("µ¿"));
  776. // the above tests repeated with additional superfluous strip chars
  777. // strip none
  778. STRIP_TEST(String("abc").lstrip("qwjkl") == "abc");
  779. STRIP_TEST(String("abc").rstrip("qwjkl") == "abc");
  780. // strip one
  781. STRIP_TEST(String("abc").lstrip("qwajkl") == "bc");
  782. STRIP_TEST(String("abc").rstrip("qwcjkl") == "ab");
  783. // strip lots
  784. STRIP_TEST(String("bababbababccc").lstrip("qwabjkl") == "ccc");
  785. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("qwcbjkl") == "aaa");
  786. // strip empty string
  787. STRIP_TEST(String("").lstrip("qwjkl") == "");
  788. STRIP_TEST(String("").rstrip("qwjkl") == "");
  789. // strip to empty string
  790. STRIP_TEST(String("abcabcabc").lstrip("qwbcajkl") == "");
  791. STRIP_TEST(String("abcabcabc").rstrip("qwbcajkl") == "");
  792. // don't strip wrong end
  793. STRIP_TEST(String("abc").lstrip("qwcjkl") == "abc");
  794. STRIP_TEST(String("abca").lstrip("qwajkl") == "bca");
  795. STRIP_TEST(String("abc").rstrip("qwajkl") == "abc");
  796. STRIP_TEST(String("abca").rstrip("qwajkl") == "abc");
  797. // in utf-8 "¿" has the same first byte as "µ"
  798. // and the same second as "ÿ"
  799. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  800. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  801. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿ÿ"));
  802. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("µ¿"));
  803. return state;
  804. #undef STRIP_TEST
  805. }
  806. typedef bool (*TestFunc)(void);
  807. TestFunc test_funcs[] = {
  808. test_1,
  809. test_2,
  810. test_3,
  811. test_4,
  812. test_5,
  813. test_6,
  814. test_7,
  815. test_8,
  816. test_9,
  817. test_10,
  818. test_11,
  819. test_12,
  820. test_13,
  821. test_14,
  822. test_15,
  823. test_16,
  824. test_17,
  825. test_18,
  826. test_19,
  827. test_20,
  828. test_21,
  829. test_22,
  830. test_23,
  831. test_24,
  832. test_25,
  833. test_26,
  834. test_27,
  835. test_28,
  836. test_29,
  837. test_30,
  838. test_31,
  839. test_32,
  840. 0
  841. };
  842. MainLoop *test() {
  843. /** A character length != wchar_t may be forced, so the tests won't work */
  844. ERR_FAIL_COND_V(sizeof(CharType) != sizeof(wchar_t), NULL);
  845. int count = 0;
  846. int passed = 0;
  847. while (true) {
  848. if (!test_funcs[count])
  849. break;
  850. bool pass = test_funcs[count]();
  851. if (pass)
  852. passed++;
  853. OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED");
  854. count++;
  855. }
  856. OS::get_singleton()->print("\n\n\n");
  857. OS::get_singleton()->print("*************\n");
  858. OS::get_singleton()->print("***TOTALS!***\n");
  859. OS::get_singleton()->print("*************\n");
  860. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  861. return NULL;
  862. }
  863. } // namespace TestString