test_string.cpp 39 KB

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