test_string.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /*************************************************************************/
  2. /* test_string.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #ifndef TEST_STRING_H
  31. #define TEST_STRING_H
  32. #include <inttypes.h>
  33. #include <stdio.h>
  34. #include <wchar.h>
  35. #include "core/io/ip_address.h"
  36. #include "core/os/main_loop.h"
  37. #include "core/os/os.h"
  38. #include "core/string/ustring.h"
  39. #include "core/variant/variant.h"
  40. #include "tests/test_macros.h"
  41. namespace TestString {
  42. int u32scmp(const char32_t *l, const char32_t *r) {
  43. for (; *l == *r && *l && *r; l++, r++)
  44. ;
  45. return *l - *r;
  46. }
  47. TEST_CASE("[String] Assign Latin-1 char string") {
  48. String s = "Hello";
  49. CHECK(u32scmp(s.get_data(), U"Hello") == 0);
  50. }
  51. TEST_CASE("[String] Assign from Latin-1 char string (operator=)") {
  52. String s = "Dolly";
  53. const String &t = s;
  54. CHECK(u32scmp(t.get_data(), U"Dolly") == 0);
  55. }
  56. TEST_CASE("[String] Assign from Latin-1 char string (copycon)") {
  57. String s("Sheep");
  58. const String &t1(s);
  59. CHECK(u32scmp(t1.get_data(), U"Sheep") == 0);
  60. String t2 = String("Sheep", 3);
  61. CHECK(u32scmp(t2.get_data(), U"She") == 0);
  62. }
  63. TEST_CASE("[String] Assign from wchar_t string (operator=)") {
  64. String s = L"Give me";
  65. CHECK(u32scmp(s.get_data(), U"Give me") == 0);
  66. }
  67. TEST_CASE("[String] Assign from wchar_t string (copycon)") {
  68. String s(L"Wool");
  69. CHECK(u32scmp(s.get_data(), U"Wool") == 0);
  70. }
  71. TEST_CASE("[String] Assign from char32_t string (operator=)") {
  72. String s = U"Give me";
  73. CHECK(u32scmp(s.get_data(), U"Give me") == 0);
  74. }
  75. TEST_CASE("[String] Assign from char32_t string (copycon)") {
  76. String s(U"Wool");
  77. CHECK(u32scmp(s.get_data(), U"Wool") == 0);
  78. }
  79. TEST_CASE("[String] UTF8") {
  80. /* how can i embed UTF in here? */
  81. static const char32_t u32str[] = { 0x0045, 0x0020, 0x304A, 0x360F, 0x3088, 0x3046, 0x1F3A4, 0 };
  82. static const uint8_t u8str[] = { 0x45, 0x20, 0xE3, 0x81, 0x8A, 0xE3, 0x98, 0x8F, 0xE3, 0x82, 0x88, 0xE3, 0x81, 0x86, 0xF0, 0x9F, 0x8E, 0xA4, 0 };
  83. String s = u32str;
  84. bool err = s.parse_utf8(s.utf8().get_data());
  85. CHECK(!err);
  86. CHECK(s == u32str);
  87. err = s.parse_utf8((const char *)u8str);
  88. CHECK(!err);
  89. CHECK(s == u32str);
  90. CharString cs = (const char *)u8str;
  91. CHECK(String::utf8(cs) == s);
  92. }
  93. TEST_CASE("[String] UTF16") {
  94. /* how can i embed UTF in here? */
  95. static const char32_t u32str[] = { 0x0045, 0x0020, 0x304A, 0x360F, 0x3088, 0x3046, 0x1F3A4, 0 };
  96. static const char16_t u16str[] = { 0x0045, 0x0020, 0x304A, 0x360F, 0x3088, 0x3046, 0xD83C, 0xDFA4, 0 };
  97. String s = u32str;
  98. bool err = s.parse_utf16(s.utf16().get_data());
  99. CHECK(!err);
  100. CHECK(s == u32str);
  101. err = s.parse_utf16(u16str);
  102. CHECK(!err);
  103. CHECK(s == u32str);
  104. Char16String cs = u16str;
  105. CHECK(String::utf16(cs) == s);
  106. }
  107. TEST_CASE("[String] UTF8 with BOM") {
  108. /* how can i embed UTF in here? */
  109. static const char32_t u32str[] = { 0x0045, 0x0020, 0x304A, 0x360F, 0x3088, 0x3046, 0x1F3A4, 0 };
  110. static const uint8_t u8str[] = { 0xEF, 0xBB, 0xBF, 0x45, 0x20, 0xE3, 0x81, 0x8A, 0xE3, 0x98, 0x8F, 0xE3, 0x82, 0x88, 0xE3, 0x81, 0x86, 0xF0, 0x9F, 0x8E, 0xA4, 0 };
  111. String s;
  112. bool err = s.parse_utf8((const char *)u8str);
  113. CHECK(!err);
  114. CHECK(s == u32str);
  115. CharString cs = (const char *)u8str;
  116. CHECK(String::utf8(cs) == s);
  117. }
  118. TEST_CASE("[String] UTF16 with BOM") {
  119. /* how can i embed UTF in here? */
  120. static const char32_t u32str[] = { 0x0020, 0x0045, 0x304A, 0x360F, 0x3088, 0x3046, 0x1F3A4, 0 };
  121. static const char16_t u16str[] = { 0xFEFF, 0x0020, 0x0045, 0x304A, 0x360F, 0x3088, 0x3046, 0xD83C, 0xDFA4, 0 };
  122. static const char16_t u16str_swap[] = { 0xFFFE, 0x2000, 0x4500, 0x4A30, 0x0F36, 0x8830, 0x4630, 0x3CD8, 0xA4DF, 0 };
  123. String s;
  124. bool err = s.parse_utf16(u16str);
  125. CHECK(!err);
  126. CHECK(s == u32str);
  127. err = s.parse_utf16(u16str_swap);
  128. CHECK(!err);
  129. CHECK(s == u32str);
  130. Char16String cs = u16str;
  131. CHECK(String::utf16(cs) == s);
  132. cs = u16str_swap;
  133. CHECK(String::utf16(cs) == s);
  134. }
  135. TEST_CASE("[String] Invalid UTF8") {
  136. ERR_PRINT_OFF
  137. static const uint8_t u8str[] = { 0x45, 0xE3, 0x81, 0x8A, 0x8F, 0xE3, 0xE3, 0x98, 0x8F, 0xE3, 0x82, 0x88, 0xE3, 0x81, 0x86, 0xF0, 0x9F, 0x8E, 0xA4, 0 };
  138. String s;
  139. bool err = s.parse_utf8((const char *)u8str);
  140. CHECK(err);
  141. CHECK(s == String());
  142. CharString cs = (const char *)u8str;
  143. CHECK(String::utf8(cs) == String());
  144. ERR_PRINT_ON
  145. }
  146. TEST_CASE("[String] Invalid UTF16") {
  147. ERR_PRINT_OFF
  148. static const char16_t u16str[] = { 0x0045, 0x304A, 0x3088, 0x3046, 0xDFA4, 0 };
  149. String s;
  150. bool err = s.parse_utf16(u16str);
  151. CHECK(err);
  152. CHECK(s == String());
  153. Char16String cs = u16str;
  154. CHECK(String::utf16(cs) == String());
  155. ERR_PRINT_ON
  156. }
  157. TEST_CASE("[String] ASCII") {
  158. String s = U"Primero Leche";
  159. String t = s.ascii(false).get_data();
  160. CHECK(s == t);
  161. t = s.ascii(true).get_data();
  162. CHECK(s == t);
  163. }
  164. TEST_CASE("[String] Comparisons (equal)") {
  165. String s = "Test Compare";
  166. CHECK(s == "Test Compare");
  167. CHECK(s == U"Test Compare");
  168. CHECK(s == L"Test Compare");
  169. CHECK(s == String("Test Compare"));
  170. }
  171. TEST_CASE("[String] Comparisons (not equal)") {
  172. String s = "Test Compare";
  173. CHECK(s != "Peanut");
  174. CHECK(s != U"Coconut");
  175. CHECK(s != L"Coconut");
  176. CHECK(s != String("Butter"));
  177. }
  178. TEST_CASE("[String] Comparisons (operator <)") {
  179. String s = "Bees";
  180. CHECK(s < "Elephant");
  181. CHECK(!(s < U"Amber"));
  182. CHECK(!(s < L"Amber"));
  183. CHECK(!(s < String("Beatrix")));
  184. }
  185. TEST_CASE("[String] Concatenation") {
  186. String s;
  187. s += "Have";
  188. s += ' ';
  189. s += 'a';
  190. s += String(" ");
  191. s = s + U"Nice";
  192. s = s + " ";
  193. s = s + String("Day");
  194. CHECK(s == "Have a Nice Day");
  195. }
  196. TEST_CASE("[String] Testing size and length of string") {
  197. // todo: expand this test to do more tests on size() as it is complicated under the hood.
  198. CHECK(String("Mellon").size() == 7);
  199. CHECK(String("Mellon1").size() == 8);
  200. // length works fine and is easier to test
  201. CHECK(String("Mellon").length() == 6);
  202. CHECK(String("Mellon1").length() == 7);
  203. CHECK(String("Mellon2").length() == 7);
  204. CHECK(String("Mellon3").length() == 7);
  205. }
  206. TEST_CASE("[String] Testing for empty string") {
  207. CHECK(!String("Mellon").is_empty());
  208. // do this more than once, to check for string corruption
  209. CHECK(String("").is_empty());
  210. CHECK(String("").is_empty());
  211. CHECK(String("").is_empty());
  212. }
  213. TEST_CASE("[String] Test chr") {
  214. CHECK(String::chr('H') == "H");
  215. CHECK(String::chr(0x3012)[0] == 0x3012);
  216. ERR_PRINT_OFF
  217. CHECK(String::chr(0xd812)[0] == 0xfffd); // Unpaired UTF-16 surrogate
  218. CHECK(String::chr(0x20d812)[0] == 0xfffd); // Outside UTF-32 range
  219. ERR_PRINT_ON
  220. }
  221. TEST_CASE("[String] Operator []") {
  222. String a = "Kugar Sane";
  223. a[0] = 'S';
  224. a[6] = 'C';
  225. CHECK(a == "Sugar Cane");
  226. CHECK(a[1] == 'u');
  227. CHECK(a.unicode_at(1) == 'u');
  228. }
  229. TEST_CASE("[String] Case function test") {
  230. String a = "MoMoNgA";
  231. CHECK(a.to_upper() == "MOMONGA");
  232. CHECK(a.to_lower() == "momonga");
  233. }
  234. TEST_CASE("[String] Case compare function test") {
  235. String a = "MoMoNgA";
  236. CHECK(a.casecmp_to("momonga") != 0);
  237. CHECK(a.nocasecmp_to("momonga") == 0);
  238. }
  239. TEST_CASE("[String] Natural compare function test") {
  240. String a = "img2.png";
  241. CHECK(a.nocasecmp_to("img10.png") > 0);
  242. CHECK(a.naturalnocasecmp_to("img10.png") < 0);
  243. }
  244. TEST_CASE("[String] hex_encode_buffer") {
  245. static const uint8_t u8str[] = { 0x45, 0xE3, 0x81, 0x8A, 0x8F, 0xE3 };
  246. String s = String::hex_encode_buffer(u8str, 6);
  247. CHECK(s == U"45e3818a8fe3");
  248. }
  249. TEST_CASE("[String] Substr") {
  250. String s = "Killer Baby";
  251. CHECK(s.substr(3, 4) == "ler ");
  252. CHECK(s.substr(3) == "ler Baby");
  253. }
  254. TEST_CASE("[String] Find") {
  255. String s = "Pretty Woman Woman";
  256. CHECK(s.find("tty") == 3);
  257. CHECK(s.find("Wo", 9) == 13);
  258. CHECK(s.find("Revenge of the Monster Truck") == -1);
  259. CHECK(s.rfind("man") == 15);
  260. }
  261. TEST_CASE("[String] Find no case") {
  262. String s = "Pretty Whale Whale";
  263. CHECK(s.findn("WHA") == 7);
  264. CHECK(s.findn("WHA", 9) == 13);
  265. CHECK(s.findn("Revenge of the Monster SawFish") == -1);
  266. CHECK(s.rfindn("WHA") == 13);
  267. }
  268. TEST_CASE("[String] Find MK") {
  269. Vector<String> keys;
  270. keys.push_back("sty");
  271. keys.push_back("tty");
  272. keys.push_back("man");
  273. String s = "Pretty Woman";
  274. int key = 0;
  275. CHECK(s.findmk(keys, 0, &key) == 3);
  276. CHECK(key == 1);
  277. CHECK(s.findmk(keys, 5, &key) == 9);
  278. CHECK(key == 2);
  279. }
  280. TEST_CASE("[String] Find and replace") {
  281. String s = "Happy Birthday, Anna!";
  282. s = s.replace("Birthday", "Halloween");
  283. CHECK(s == "Happy Halloween, Anna!");
  284. s = s.replace_first("H", "W");
  285. CHECK(s == "Wappy Halloween, Anna!");
  286. }
  287. TEST_CASE("[String] Insertion") {
  288. String s = "Who is Frederic?";
  289. s = s.insert(s.find("?"), " Chopin");
  290. CHECK(s == "Who is Frederic Chopin?");
  291. }
  292. TEST_CASE("[String] Number to string") {
  293. CHECK(String::num(0) == "0");
  294. CHECK(String::num(0.0) == "0"); // No trailing zeros.
  295. CHECK(String::num(-0.0) == "-0"); // Includes sign even for zero.
  296. CHECK(String::num(3.141593) == "3.141593");
  297. CHECK(String::num(3.141593, 3) == "3.142");
  298. CHECK(String::num_scientific(30000000) == "3e+07");
  299. CHECK(String::num_int64(3141593) == "3141593");
  300. CHECK(String::num_int64(0xA141593, 16) == "a141593");
  301. CHECK(String::num_int64(0xA141593, 16, true) == "A141593");
  302. CHECK(String::num(42.100023, 4) == "42.1"); // No trailing zeros.
  303. // String::num_real tests.
  304. CHECK(String::num_real(3.141593) == "3.141593");
  305. CHECK(String::num_real(3.141) == "3.141"); // No trailing zeros.
  306. #ifdef REAL_T_IS_DOUBLE
  307. CHECK_MESSAGE(String::num_real(Math_PI) == "3.14159265358979", "Prints the appropriate amount of digits for real_t = double.");
  308. CHECK_MESSAGE(String::num_real(3.1415f) == "3.14149999618530", "Prints more digits of 32-bit float when real_t = double (ones that would be reliable for double).");
  309. #else
  310. CHECK_MESSAGE(String::num_real(Math_PI) == "3.141593", "Prints the appropriate amount of digits for real_t = float.");
  311. CHECK_MESSAGE(String::num_real(3.1415f) == "3.1415", "Prints only reliable digits of 32-bit float when real_t = float.");
  312. #endif // REAL_T_IS_DOUBLE
  313. // Checks doubles with many decimal places.
  314. CHECK(String::num(0.0000012345432123454321, -1) == "0.00000123454321"); // -1 uses 14 as sane default.
  315. CHECK(String::num(0.0000012345432123454321) == "0.00000123454321"); // -1 is the default value.
  316. CHECK(String::num(-0.0000012345432123454321) == "-0.00000123454321");
  317. CHECK(String::num(-10000.0000012345432123454321) == "-10000.0000012345");
  318. CHECK(String::num(0.0000000000012345432123454321) == "0.00000000000123");
  319. CHECK(String::num(0.0000000000012345432123454321, 3) == "0");
  320. // Note: When relevant (remainder > 0.5), the last digit gets rounded up,
  321. // which can also lead to not include a trailing zero, e.g. "...89" -> "...9".
  322. CHECK(String::num(0.0000056789876567898765) == "0.00000567898766"); // Should round last digit.
  323. CHECK(String::num(10000.000005678999999999) == "10000.000005679"); // We cut at ...789|99 which is rounded to ...79, so only 13 decimals.
  324. CHECK(String::num(42.12999999, 6) == "42.13"); // Also happens with lower decimals count.
  325. // 32 is MAX_DECIMALS. We can't reliably store that many so we can't compare against a string,
  326. // but we can check that the string length is 34 (32 + 2 for "0.").
  327. CHECK(String::num(0.00000123456789987654321123456789987654321, 32).length() == 34);
  328. CHECK(String::num(0.00000123456789987654321123456789987654321, 42).length() == 34); // Should enforce MAX_DECIMALS.
  329. CHECK(String::num(10000.00000123456789987654321123456789987654321, 42).length() == 38); // 32 decimals + "10000.".
  330. }
  331. TEST_CASE("[String] String to integer") {
  332. static const char *nums[4] = { "1237461283", "- 22", "0", " - 1123412" };
  333. static const int num[4] = { 1237461283, -22, 0, -1123412 };
  334. for (int i = 0; i < 4; i++) {
  335. CHECK(String(nums[i]).to_int() == num[i]);
  336. }
  337. }
  338. TEST_CASE("[String] Hex to integer") {
  339. static const char *nums[4] = { "0xFFAE", "22", "0", "AADDAD" };
  340. static const int64_t num[4] = { 0xFFAE, 0x22, 0, 0xAADDAD };
  341. for (int i = 0; i < 4; i++) {
  342. CHECK(String(nums[i]).hex_to_int() == num[i]);
  343. }
  344. }
  345. TEST_CASE("[String] String to float") {
  346. static const char *nums[4] = { "-12348298412.2", "0.05", "2.0002", " -0.0001" };
  347. static const double num[4] = { -12348298412.2, 0.05, 2.0002, -0.0001 };
  348. for (int i = 0; i < 4; i++) {
  349. CHECK(!(ABS(String(nums[i]).to_float() - num[i]) > 0.00001));
  350. }
  351. }
  352. TEST_CASE("[String] CamelCase to underscore") {
  353. CHECK(String("TestTestStringGD").camelcase_to_underscore(false) == String("Test_Test_String_GD"));
  354. CHECK(String("TestTestStringGD").camelcase_to_underscore(true) == String("test_test_string_gd"));
  355. }
  356. TEST_CASE("[String] Slicing") {
  357. String s = "Mars,Jupiter,Saturn,Uranus";
  358. const char *slices[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
  359. for (int i = 0; i < s.get_slice_count(","); i++) {
  360. CHECK(s.get_slice(",", i) == slices[i]);
  361. }
  362. }
  363. TEST_CASE("[String] Splitting") {
  364. String s = "Mars,Jupiter,Saturn,Uranus";
  365. Vector<String> l;
  366. const char *slices_l[3] = { "Mars", "Jupiter", "Saturn,Uranus" };
  367. const char *slices_r[3] = { "Mars,Jupiter", "Saturn", "Uranus" };
  368. l = s.split(",", true, 2);
  369. CHECK(l.size() == 3);
  370. for (int i = 0; i < l.size(); i++) {
  371. CHECK(l[i] == slices_l[i]);
  372. }
  373. l = s.rsplit(",", true, 2);
  374. CHECK(l.size() == 3);
  375. for (int i = 0; i < l.size(); i++) {
  376. CHECK(l[i] == slices_r[i]);
  377. }
  378. s = "Mars Jupiter Saturn Uranus";
  379. const char *slices_s[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
  380. l = s.split_spaces();
  381. for (int i = 0; i < l.size(); i++) {
  382. CHECK(l[i] == slices_s[i]);
  383. }
  384. s = "1.2;2.3 4.5";
  385. const double slices_d[3] = { 1.2, 2.3, 4.5 };
  386. Vector<float> f;
  387. f = s.split_floats(";");
  388. CHECK(f.size() == 2);
  389. for (int i = 0; i < f.size(); i++) {
  390. CHECK(ABS(f[i] - slices_d[i]) <= 0.00001);
  391. }
  392. Vector<String> keys;
  393. keys.push_back(";");
  394. keys.push_back(" ");
  395. f = s.split_floats_mk(keys);
  396. CHECK(f.size() == 3);
  397. for (int i = 0; i < f.size(); i++) {
  398. CHECK(ABS(f[i] - slices_d[i]) <= 0.00001);
  399. }
  400. s = "1;2 4";
  401. const int slices_i[3] = { 1, 2, 4 };
  402. Vector<int> ii;
  403. ii = s.split_ints(";");
  404. CHECK(ii.size() == 2);
  405. for (int i = 0; i < ii.size(); i++) {
  406. CHECK(ii[i] == slices_i[i]);
  407. }
  408. ii = s.split_ints_mk(keys);
  409. CHECK(ii.size() == 3);
  410. for (int i = 0; i < ii.size(); i++) {
  411. CHECK(ii[i] == slices_i[i]);
  412. }
  413. }
  414. TEST_CASE("[String] Erasing") {
  415. String s = "Josephine is such a cute girl!";
  416. s.erase(s.find("cute "), String("cute ").length());
  417. CHECK(s == "Josephine is such a girl!");
  418. }
  419. struct test_27_data {
  420. char const *data;
  421. char const *part;
  422. bool expected;
  423. };
  424. TEST_CASE("[String] Begins with") {
  425. test_27_data tc[] = {
  426. { "res://foobar", "res://", true },
  427. { "res", "res://", false },
  428. { "abc", "abc", true }
  429. };
  430. size_t count = sizeof(tc) / sizeof(tc[0]);
  431. bool state = true;
  432. for (size_t i = 0; state && i < count; ++i) {
  433. String s = tc[i].data;
  434. state = s.begins_with(tc[i].part) == tc[i].expected;
  435. if (state) {
  436. String sb = tc[i].part;
  437. state = s.begins_with(sb) == tc[i].expected;
  438. }
  439. CHECK(state);
  440. if (!state) {
  441. break;
  442. }
  443. };
  444. CHECK(state);
  445. }
  446. TEST_CASE("[String] Ends with") {
  447. test_27_data tc[] = {
  448. { "res://foobar", "foobar", true },
  449. { "res", "res://", false },
  450. { "abc", "abc", true }
  451. };
  452. size_t count = sizeof(tc) / sizeof(tc[0]);
  453. bool state = true;
  454. for (size_t i = 0; state && i < count; ++i) {
  455. String s = tc[i].data;
  456. state = s.ends_with(tc[i].part) == tc[i].expected;
  457. if (state) {
  458. String sb = tc[i].part;
  459. state = s.ends_with(sb) == tc[i].expected;
  460. }
  461. CHECK(state);
  462. if (!state) {
  463. break;
  464. }
  465. };
  466. CHECK(state);
  467. }
  468. TEST_CASE("[String] format") {
  469. const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";
  470. Dictionary value_dictionary;
  471. value_dictionary["red"] = 10;
  472. value_dictionary["green"] = 20;
  473. value_dictionary["blue"] = "bla";
  474. value_dictionary["alpha"] = 0.4;
  475. String value = value_format.format(value_dictionary, "$_");
  476. CHECK(value == "red=\"10\" green=\"20\" blue=\"bla\" alpha=\"0.4\"");
  477. }
  478. TEST_CASE("[String] sprintf") {
  479. String format, output;
  480. Array args;
  481. bool error;
  482. // %%
  483. format = "fish %% frog";
  484. args.clear();
  485. output = format.sprintf(args, &error);
  486. REQUIRE(error == false);
  487. CHECK(output == String("fish % frog"));
  488. //////// INTS
  489. // Int
  490. format = "fish %d frog";
  491. args.clear();
  492. args.push_back(5);
  493. output = format.sprintf(args, &error);
  494. REQUIRE(error == false);
  495. CHECK(output == String("fish 5 frog"));
  496. // Int left padded with zeroes.
  497. format = "fish %05d frog";
  498. args.clear();
  499. args.push_back(5);
  500. output = format.sprintf(args, &error);
  501. REQUIRE(error == false);
  502. CHECK(output == String("fish 00005 frog"));
  503. // Int left padded with spaces.
  504. format = "fish %5d frog";
  505. args.clear();
  506. args.push_back(5);
  507. output = format.sprintf(args, &error);
  508. REQUIRE(error == false);
  509. CHECK(output == String("fish 5 frog"));
  510. // Int right padded with spaces.
  511. format = "fish %-5d frog";
  512. args.clear();
  513. args.push_back(5);
  514. output = format.sprintf(args, &error);
  515. REQUIRE(error == false);
  516. CHECK(output == String("fish 5 frog"));
  517. // Int with sign (positive).
  518. format = "fish %+d frog";
  519. args.clear();
  520. args.push_back(5);
  521. output = format.sprintf(args, &error);
  522. REQUIRE(error == false);
  523. CHECK(output == String("fish +5 frog"));
  524. // Negative int.
  525. format = "fish %d frog";
  526. args.clear();
  527. args.push_back(-5);
  528. output = format.sprintf(args, &error);
  529. REQUIRE(error == false);
  530. CHECK(output == String("fish -5 frog"));
  531. // Hex (lower)
  532. format = "fish %x frog";
  533. args.clear();
  534. args.push_back(45);
  535. output = format.sprintf(args, &error);
  536. REQUIRE(error == false);
  537. CHECK(output == String("fish 2d frog"));
  538. // Hex (upper)
  539. format = "fish %X frog";
  540. args.clear();
  541. args.push_back(45);
  542. output = format.sprintf(args, &error);
  543. REQUIRE(error == false);
  544. CHECK(output == String("fish 2D frog"));
  545. // Octal
  546. format = "fish %o frog";
  547. args.clear();
  548. args.push_back(99);
  549. output = format.sprintf(args, &error);
  550. REQUIRE(error == false);
  551. CHECK(output == String("fish 143 frog"));
  552. ////// REALS
  553. // Real
  554. format = "fish %f frog";
  555. args.clear();
  556. args.push_back(99.99);
  557. output = format.sprintf(args, &error);
  558. REQUIRE(error == false);
  559. CHECK(output == String("fish 99.990000 frog"));
  560. // Real left-padded
  561. format = "fish %11f frog";
  562. args.clear();
  563. args.push_back(99.99);
  564. output = format.sprintf(args, &error);
  565. REQUIRE(error == false);
  566. CHECK(output == String("fish 99.990000 frog"));
  567. // Real right-padded
  568. format = "fish %-11f frog";
  569. args.clear();
  570. args.push_back(99.99);
  571. output = format.sprintf(args, &error);
  572. REQUIRE(error == false);
  573. CHECK(output == String("fish 99.990000 frog"));
  574. // Real given int.
  575. format = "fish %f frog";
  576. args.clear();
  577. args.push_back(99);
  578. output = format.sprintf(args, &error);
  579. REQUIRE(error == false);
  580. CHECK(output == String("fish 99.000000 frog"));
  581. // Real with sign (positive).
  582. format = "fish %+f frog";
  583. args.clear();
  584. args.push_back(99.99);
  585. output = format.sprintf(args, &error);
  586. REQUIRE(error == false);
  587. CHECK(output == String("fish +99.990000 frog"));
  588. // Real with 1 decimals.
  589. format = "fish %.1f frog";
  590. args.clear();
  591. args.push_back(99.99);
  592. output = format.sprintf(args, &error);
  593. REQUIRE(error == false);
  594. CHECK(output == String("fish 100.0 frog"));
  595. // Real with 12 decimals.
  596. format = "fish %.12f frog";
  597. args.clear();
  598. args.push_back(99.99);
  599. output = format.sprintf(args, &error);
  600. REQUIRE(error == false);
  601. CHECK(output == String("fish 99.990000000000 frog"));
  602. // Real with no decimals.
  603. format = "fish %.f frog";
  604. args.clear();
  605. args.push_back(99.99);
  606. output = format.sprintf(args, &error);
  607. REQUIRE(error == false);
  608. CHECK(output == String("fish 100 frog"));
  609. /////// Strings.
  610. // String
  611. format = "fish %s frog";
  612. args.clear();
  613. args.push_back("cheese");
  614. output = format.sprintf(args, &error);
  615. REQUIRE(error == false);
  616. CHECK(output == String("fish cheese frog"));
  617. // String left-padded
  618. format = "fish %10s frog";
  619. args.clear();
  620. args.push_back("cheese");
  621. output = format.sprintf(args, &error);
  622. REQUIRE(error == false);
  623. CHECK(output == String("fish cheese frog"));
  624. // String right-padded
  625. format = "fish %-10s frog";
  626. args.clear();
  627. args.push_back("cheese");
  628. output = format.sprintf(args, &error);
  629. REQUIRE(error == false);
  630. CHECK(output == String("fish cheese frog"));
  631. ///// Characters
  632. // Character as string.
  633. format = "fish %c frog";
  634. args.clear();
  635. args.push_back("A");
  636. output = format.sprintf(args, &error);
  637. REQUIRE(error == false);
  638. CHECK(output == String("fish A frog"));
  639. // Character as int.
  640. format = "fish %c frog";
  641. args.clear();
  642. args.push_back(65);
  643. output = format.sprintf(args, &error);
  644. REQUIRE(error == false);
  645. CHECK(output == String("fish A frog"));
  646. ///// Dynamic width
  647. // String dynamic width
  648. format = "fish %*s frog";
  649. args.clear();
  650. args.push_back(10);
  651. args.push_back("cheese");
  652. output = format.sprintf(args, &error);
  653. REQUIRE(error == false);
  654. REQUIRE(output == String("fish cheese frog"));
  655. // Int dynamic width
  656. format = "fish %*d frog";
  657. args.clear();
  658. args.push_back(10);
  659. args.push_back(99);
  660. output = format.sprintf(args, &error);
  661. REQUIRE(error == false);
  662. REQUIRE(output == String("fish 99 frog"));
  663. // Float dynamic width
  664. format = "fish %*.*f frog";
  665. args.clear();
  666. args.push_back(10);
  667. args.push_back(3);
  668. args.push_back(99.99);
  669. output = format.sprintf(args, &error);
  670. REQUIRE(error == false);
  671. CHECK(output == String("fish 99.990 frog"));
  672. ///// Errors
  673. // More formats than arguments.
  674. format = "fish %s %s frog";
  675. args.clear();
  676. args.push_back("cheese");
  677. output = format.sprintf(args, &error);
  678. REQUIRE(error);
  679. CHECK(output == "not enough arguments for format string");
  680. // More arguments than formats.
  681. format = "fish %s frog";
  682. args.clear();
  683. args.push_back("hello");
  684. args.push_back("cheese");
  685. output = format.sprintf(args, &error);
  686. REQUIRE(error);
  687. CHECK(output == "not all arguments converted during string formatting");
  688. // Incomplete format.
  689. format = "fish %10";
  690. args.clear();
  691. args.push_back("cheese");
  692. output = format.sprintf(args, &error);
  693. REQUIRE(error);
  694. CHECK(output == "incomplete format");
  695. // Bad character in format string
  696. format = "fish %&f frog";
  697. args.clear();
  698. args.push_back("cheese");
  699. output = format.sprintf(args, &error);
  700. REQUIRE(error);
  701. CHECK(output == "unsupported format character");
  702. // Too many decimals.
  703. format = "fish %2.2.2f frog";
  704. args.clear();
  705. args.push_back(99.99);
  706. output = format.sprintf(args, &error);
  707. REQUIRE(error);
  708. CHECK(output == "too many decimal points in format");
  709. // * not a number
  710. format = "fish %*f frog";
  711. args.clear();
  712. args.push_back("cheese");
  713. args.push_back(99.99);
  714. output = format.sprintf(args, &error);
  715. REQUIRE(error);
  716. CHECK(output == "* wants number");
  717. // Character too long.
  718. format = "fish %c frog";
  719. args.clear();
  720. args.push_back("sc");
  721. output = format.sprintf(args, &error);
  722. REQUIRE(error);
  723. CHECK(output == "%c requires number or single-character string");
  724. // Character bad type.
  725. format = "fish %c frog";
  726. args.clear();
  727. args.push_back(Array());
  728. output = format.sprintf(args, &error);
  729. REQUIRE(error);
  730. CHECK(output == "%c requires number or single-character string");
  731. }
  732. TEST_CASE("[String] is_numeric") {
  733. CHECK(String("12").is_numeric());
  734. CHECK(String("1.2").is_numeric());
  735. CHECK(!String("AF").is_numeric());
  736. CHECK(String("-12").is_numeric());
  737. CHECK(String("-1.2").is_numeric());
  738. }
  739. TEST_CASE("[String] pad") {
  740. String s = String("test");
  741. CHECK(s.lpad(10, "x") == U"xxxxxxtest");
  742. CHECK(s.rpad(10, "x") == U"testxxxxxx");
  743. s = String("10.10");
  744. CHECK(s.pad_decimals(4) == U"10.1000");
  745. CHECK(s.pad_zeros(4) == U"0010.10");
  746. }
  747. TEST_CASE("[String] is_subsequence_of") {
  748. String a = "is subsequence of";
  749. CHECK(String("sub").is_subsequence_of(a));
  750. CHECK(!String("Sub").is_subsequence_of(a));
  751. CHECK(String("Sub").is_subsequence_ofi(a));
  752. }
  753. TEST_CASE("[String] match") {
  754. CHECK(String("img1.png").match("*.png"));
  755. CHECK(!String("img1.jpeg").match("*.png"));
  756. CHECK(!String("img1.Png").match("*.png"));
  757. CHECK(String("img1.Png").matchn("*.png"));
  758. }
  759. TEST_CASE("[String] IPVX address to string") {
  760. IPAddress ip0("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  761. IPAddress ip(0x0123, 0x4567, 0x89ab, 0xcdef, true);
  762. IPAddress ip2("fe80::52e5:49ff:fe93:1baf");
  763. IPAddress ip3("::ffff:192.168.0.1");
  764. String ip4 = "192.168.0.1";
  765. CHECK(ip4.is_valid_ip_address());
  766. ip4 = "192.368.0.1";
  767. CHECK(!ip4.is_valid_ip_address());
  768. String ip6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
  769. CHECK(ip6.is_valid_ip_address());
  770. ip6 = "2001:0db8:85j3:0000:0000:8a2e:0370:7334";
  771. CHECK(!ip6.is_valid_ip_address());
  772. ip6 = "2001:0db8:85f345:0000:0000:8a2e:0370:7334";
  773. CHECK(!ip6.is_valid_ip_address());
  774. ip6 = "2001:0db8::0:8a2e:370:7334";
  775. CHECK(ip6.is_valid_ip_address());
  776. ip6 = "::ffff:192.168.0.1";
  777. CHECK(ip6.is_valid_ip_address());
  778. }
  779. TEST_CASE("[String] Capitalize against many strings") {
  780. String input = "bytes2var";
  781. String output = "Bytes 2 Var";
  782. CHECK(input.capitalize() == output);
  783. input = "linear2db";
  784. output = "Linear 2 Db";
  785. CHECK(input.capitalize() == output);
  786. input = "vector3";
  787. output = "Vector 3";
  788. CHECK(input.capitalize() == output);
  789. input = "sha256";
  790. output = "Sha 256";
  791. CHECK(input.capitalize() == output);
  792. input = "2db";
  793. output = "2 Db";
  794. CHECK(input.capitalize() == output);
  795. input = "PascalCase";
  796. output = "Pascal Case";
  797. CHECK(input.capitalize() == output);
  798. input = "PascalPascalCase";
  799. output = "Pascal Pascal Case";
  800. CHECK(input.capitalize() == output);
  801. input = "snake_case";
  802. output = "Snake Case";
  803. CHECK(input.capitalize() == output);
  804. input = "snake_snake_case";
  805. output = "Snake Snake Case";
  806. CHECK(input.capitalize() == output);
  807. input = "sha256sum";
  808. output = "Sha 256 Sum";
  809. CHECK(input.capitalize() == output);
  810. input = "cat2dog";
  811. output = "Cat 2 Dog";
  812. CHECK(input.capitalize() == output);
  813. input = "function(name)";
  814. output = "Function(name)";
  815. CHECK(input.capitalize() == output);
  816. input = "snake_case_function(snake_case_arg)";
  817. output = "Snake Case Function(snake Case Arg)";
  818. CHECK(input.capitalize() == output);
  819. input = "snake_case_function( snake_case_arg )";
  820. output = "Snake Case Function( Snake Case Arg )";
  821. CHECK(input.capitalize() == output);
  822. }
  823. TEST_CASE("[String] Checking string is empty when it should be") {
  824. bool state = true;
  825. bool success;
  826. String a = "";
  827. success = a[0] == 0;
  828. if (!success) {
  829. state = false;
  830. }
  831. String b = "Godot";
  832. success = b[b.size()] == 0;
  833. if (!success) {
  834. state = false;
  835. }
  836. const String c = "";
  837. success = c[0] == 0;
  838. if (!success) {
  839. state = false;
  840. }
  841. const String d = "Godot";
  842. success = d[d.size()] == 0;
  843. if (!success) {
  844. state = false;
  845. }
  846. CHECK(state);
  847. }
  848. TEST_CASE("[String] lstrip and rstrip") {
  849. #define STRIP_TEST(x) \
  850. { \
  851. bool success = x; \
  852. state = state && success; \
  853. }
  854. bool state = true;
  855. // strip none
  856. STRIP_TEST(String("abc").lstrip("") == "abc");
  857. STRIP_TEST(String("abc").rstrip("") == "abc");
  858. // strip one
  859. STRIP_TEST(String("abc").lstrip("a") == "bc");
  860. STRIP_TEST(String("abc").rstrip("c") == "ab");
  861. // strip lots
  862. STRIP_TEST(String("bababbababccc").lstrip("ab") == "ccc");
  863. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("cb") == "aaa");
  864. // strip empty string
  865. STRIP_TEST(String("").lstrip("") == "");
  866. STRIP_TEST(String("").rstrip("") == "");
  867. // strip to empty string
  868. STRIP_TEST(String("abcabcabc").lstrip("bca") == "");
  869. STRIP_TEST(String("abcabcabc").rstrip("bca") == "");
  870. // don't strip wrong end
  871. STRIP_TEST(String("abc").lstrip("c") == "abc");
  872. STRIP_TEST(String("abca").lstrip("a") == "bca");
  873. STRIP_TEST(String("abc").rstrip("a") == "abc");
  874. STRIP_TEST(String("abca").rstrip("a") == "abc");
  875. // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5)
  876. // and the same second as "ÿ" (\u00ff)
  877. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("µÿ")) == String::utf8("¿"));
  878. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("µÿ")) == String::utf8("¿"));
  879. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("µÿ")) == String::utf8("¿ÿ"));
  880. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("µÿ")) == String::utf8("µ¿"));
  881. // the above tests repeated with additional superfluous strip chars
  882. // strip none
  883. STRIP_TEST(String("abc").lstrip("qwjkl") == "abc");
  884. STRIP_TEST(String("abc").rstrip("qwjkl") == "abc");
  885. // strip one
  886. STRIP_TEST(String("abc").lstrip("qwajkl") == "bc");
  887. STRIP_TEST(String("abc").rstrip("qwcjkl") == "ab");
  888. // strip lots
  889. STRIP_TEST(String("bababbababccc").lstrip("qwabjkl") == "ccc");
  890. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("qwcbjkl") == "aaa");
  891. // strip empty string
  892. STRIP_TEST(String("").lstrip("qwjkl") == "");
  893. STRIP_TEST(String("").rstrip("qwjkl") == "");
  894. // strip to empty string
  895. STRIP_TEST(String("abcabcabc").lstrip("qwbcajkl") == "");
  896. STRIP_TEST(String("abcabcabc").rstrip("qwbcajkl") == "");
  897. // don't strip wrong end
  898. STRIP_TEST(String("abc").lstrip("qwcjkl") == "abc");
  899. STRIP_TEST(String("abca").lstrip("qwajkl") == "bca");
  900. STRIP_TEST(String("abc").rstrip("qwajkl") == "abc");
  901. STRIP_TEST(String("abca").rstrip("qwajkl") == "abc");
  902. // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5)
  903. // and the same second as "ÿ" (\u00ff)
  904. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  905. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  906. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿ÿ"));
  907. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("µ¿"));
  908. CHECK(state);
  909. #undef STRIP_TEST
  910. }
  911. TEST_CASE("[String] ensuring empty string into parse_utf8 passes empty string") {
  912. String empty;
  913. CHECK(empty.parse_utf8(nullptr, -1));
  914. }
  915. TEST_CASE("[String] Cyrillic to_lower()") {
  916. String upper = String::utf8("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ");
  917. String lower = String::utf8("абвгдеёжзийклмнопрстуфхцчшщъыьэюя");
  918. String test = upper.to_lower();
  919. bool state = test == lower;
  920. CHECK(state);
  921. }
  922. TEST_CASE("[String] Count and countn functionality") {
  923. #define COUNT_TEST(x) \
  924. { \
  925. bool success = x; \
  926. state = state && success; \
  927. }
  928. bool state = true;
  929. COUNT_TEST(String("").count("Test") == 0);
  930. COUNT_TEST(String("Test").count("") == 0);
  931. COUNT_TEST(String("Test").count("test") == 0);
  932. COUNT_TEST(String("Test").count("TEST") == 0);
  933. COUNT_TEST(String("TEST").count("TEST") == 1);
  934. COUNT_TEST(String("Test").count("Test") == 1);
  935. COUNT_TEST(String("aTest").count("Test") == 1);
  936. COUNT_TEST(String("Testa").count("Test") == 1);
  937. COUNT_TEST(String("TestTestTest").count("Test") == 3);
  938. COUNT_TEST(String("TestTestTest").count("TestTest") == 1);
  939. COUNT_TEST(String("TestGodotTestGodotTestGodot").count("Test") == 3);
  940. COUNT_TEST(String("TestTestTestTest").count("Test", 4, 8) == 1);
  941. COUNT_TEST(String("TestTestTestTest").count("Test", 4, 12) == 2);
  942. COUNT_TEST(String("TestTestTestTest").count("Test", 4, 16) == 3);
  943. COUNT_TEST(String("TestTestTestTest").count("Test", 4) == 3);
  944. COUNT_TEST(String("Test").countn("test") == 1);
  945. COUNT_TEST(String("Test").countn("TEST") == 1);
  946. COUNT_TEST(String("testTest-Testatest").countn("tEst") == 4);
  947. COUNT_TEST(String("testTest-TeStatest").countn("tEsT", 4, 16) == 2);
  948. CHECK(state);
  949. #undef COUNT_TEST
  950. }
  951. TEST_CASE("[String] Bigrams") {
  952. String s = "abcd";
  953. Vector<String> bigr = s.bigrams();
  954. CHECK(bigr.size() == 3);
  955. CHECK(bigr[0] == "ab");
  956. CHECK(bigr[1] == "bc");
  957. CHECK(bigr[2] == "cd");
  958. }
  959. TEST_CASE("[String] c-escape/unescape") {
  960. String s = "\\1\a2\b\f3\n45\r6\t7\v8\'9\?0\"";
  961. CHECK(s.c_escape().c_unescape() == s);
  962. }
  963. TEST_CASE("[String] dedent") {
  964. String s = " aaa\n bbb";
  965. String t = "aaa\nbbb";
  966. CHECK(s.dedent() == t);
  967. }
  968. TEST_CASE("[String] Path functions") {
  969. static const char *path[7] = { "C:\\Godot\\project\\test.tscn", "/Godot/project/test.xscn", "../Godot/project/test.scn", "Godot\\test.doc", "C:\\test.", "res://test", "/.test" };
  970. static const char *base_dir[7] = { "C:\\Godot\\project", "/Godot/project", "../Godot/project", "Godot", "C:\\", "res://", "/" };
  971. static const char *base_name[7] = { "C:\\Godot\\project\\test", "/Godot/project/test", "../Godot/project/test", "Godot\\test", "C:\\test", "res://test", "/" };
  972. static const char *ext[7] = { "tscn", "xscn", "scn", "doc", "", "", "test" };
  973. static const char *file[7] = { "test.tscn", "test.xscn", "test.scn", "test.doc", "test.", "test", ".test" };
  974. static const bool abs[7] = { true, true, false, false, true, true, true };
  975. for (int i = 0; i < 7; i++) {
  976. CHECK(String(path[i]).get_base_dir() == base_dir[i]);
  977. CHECK(String(path[i]).get_basename() == base_name[i]);
  978. CHECK(String(path[i]).get_extension() == ext[i]);
  979. CHECK(String(path[i]).get_file() == file[i]);
  980. CHECK(String(path[i]).is_absolute_path() == abs[i]);
  981. CHECK(String(path[i]).is_relative_path() != abs[i]);
  982. CHECK(String(path[i]).simplify_path().get_base_dir().plus_file(file[i]) == String(path[i]).simplify_path());
  983. }
  984. static const char *file_name[3] = { "test.tscn", "test://.xscn", "?tes*t.scn" };
  985. static const bool valid[3] = { true, false, false };
  986. for (int i = 0; i < 3; i++) {
  987. CHECK(String(file_name[i]).is_valid_filename() == valid[i]);
  988. }
  989. }
  990. TEST_CASE("[String] hash") {
  991. String a = "Test";
  992. String b = "Test";
  993. String c = "West";
  994. CHECK(a.hash() == b.hash());
  995. CHECK(a.hash() != c.hash());
  996. CHECK(a.hash64() == b.hash64());
  997. CHECK(a.hash64() != c.hash64());
  998. }
  999. TEST_CASE("[String] uri_encode/unescape") {
  1000. String s = "Godot Engine:'docs'";
  1001. String t = "Godot%20Engine%3A%27docs%27";
  1002. String x1 = "T%C4%93%C5%A1t";
  1003. static const uint8_t u8str[] = { 0x54, 0xC4, 0x93, 0xC5, 0xA1, 0x74, 0x00 };
  1004. String x2 = String::utf8((const char *)u8str);
  1005. String x3 = U"Tēšt";
  1006. CHECK(x1.uri_decode() == x2);
  1007. CHECK(x1.uri_decode() == x3);
  1008. CHECK((x1 + x3).uri_decode() == (x2 + x3)); // Mixed unicode and URL encoded string, e.g. GTK+ bookmark.
  1009. CHECK(x2.uri_encode() == x1);
  1010. CHECK(x3.uri_encode() == x1);
  1011. CHECK(s.uri_encode() == t);
  1012. CHECK(t.uri_decode() == s);
  1013. }
  1014. TEST_CASE("[String] xml_escape/unescape") {
  1015. String s = "\"Test\" <test@test&'test'>";
  1016. CHECK(s.xml_escape(true).xml_unescape() == s);
  1017. CHECK(s.xml_escape(false).xml_unescape() == s);
  1018. }
  1019. TEST_CASE("[String] xml_unescape") {
  1020. // Named entities
  1021. String input = "&quot;&amp;&apos;&lt;&gt;";
  1022. CHECK(input.xml_unescape() == "\"&\'<>");
  1023. // Numeric entities
  1024. input = "&#x41;&#66;";
  1025. CHECK(input.xml_unescape() == "AB");
  1026. input = "&#0;&x#0;More text";
  1027. String result = input.xml_unescape();
  1028. // Didn't put in a leading NUL and terminate the string
  1029. CHECK(input.length() > 0);
  1030. CHECK(input[0] != '\0');
  1031. // Entity should be left as-is if invalid
  1032. CHECK(input.xml_unescape() == input);
  1033. // Check near char32_t range
  1034. input = "&#xFFFFFFFF;";
  1035. result = input.xml_unescape();
  1036. CHECK(result.length() == 1);
  1037. CHECK(result[0] == 0xFFFFFFFF);
  1038. input = "&#4294967295;";
  1039. result = input.xml_unescape();
  1040. CHECK(result.length() == 1);
  1041. CHECK(result[0] == 0xFFFFFFFF);
  1042. // Check out of range of char32_t
  1043. input = "&#xFFFFFFFFF;";
  1044. CHECK(input.xml_unescape() == input);
  1045. input = "&#4294967296;";
  1046. CHECK(input.xml_unescape() == input);
  1047. // Shouldn't consume without ending in a ';'
  1048. input = "&#66";
  1049. CHECK(input.xml_unescape() == input);
  1050. input = "&#x41";
  1051. CHECK(input.xml_unescape() == input);
  1052. // Invalid characters should make the entity ignored
  1053. input = "&#x41SomeIrrelevantText;";
  1054. CHECK(input.xml_unescape() == input);
  1055. input = "&#66SomeIrrelevantText;";
  1056. CHECK(input.xml_unescape() == input);
  1057. }
  1058. TEST_CASE("[String] Strip escapes") {
  1059. String s = "\t\tTest Test\r\n Test";
  1060. CHECK(s.strip_escapes() == "Test Test Test");
  1061. }
  1062. TEST_CASE("[String] Similarity") {
  1063. String a = "Test";
  1064. String b = "West";
  1065. String c = "Toad";
  1066. CHECK(a.similarity(b) > a.similarity(c));
  1067. }
  1068. TEST_CASE("[String] Strip edges") {
  1069. String s = "\t Test Test ";
  1070. CHECK(s.strip_edges(true, false) == "Test Test ");
  1071. CHECK(s.strip_edges(false, true) == "\t Test Test");
  1072. CHECK(s.strip_edges(true, true) == "Test Test");
  1073. }
  1074. TEST_CASE("[String] Trim") {
  1075. String s = "aaaTestbbb";
  1076. CHECK(s.trim_prefix("aaa") == "Testbbb");
  1077. CHECK(s.trim_suffix("bbb") == "aaaTest");
  1078. CHECK(s.trim_suffix("Test") == s);
  1079. }
  1080. TEST_CASE("[String] Right/Left") {
  1081. String s = "aaaTestbbb";
  1082. // ^
  1083. CHECK(s.right(6) == "estbbb");
  1084. CHECK(s.right(-6) == "tbbb");
  1085. CHECK(s.left(6) == "aaaTes");
  1086. CHECK(s.left(-6) == "aaaT");
  1087. }
  1088. TEST_CASE("[String] Repeat") {
  1089. String s = "abababab";
  1090. String x = "ab";
  1091. String t = x.repeat(4);
  1092. CHECK(t == s);
  1093. }
  1094. TEST_CASE("[String] SHA1/SHA256/MD5") {
  1095. String s = "Godot";
  1096. String sha1 = "a1e91f39b9fce6a9998b14bdbe2aa2b39dc2d201";
  1097. static uint8_t sha1_buf[20] = {
  1098. 0xA1, 0xE9, 0x1F, 0x39, 0xB9, 0xFC, 0xE6, 0xA9, 0x99, 0x8B, 0x14, 0xBD, 0xBE, 0x2A, 0xA2, 0xB3,
  1099. 0x9D, 0xC2, 0xD2, 0x01
  1100. };
  1101. String sha256 = "2a02b2443f7985d89d09001086ae3dcfa6eb0f55c6ef170715d42328e16e6cb8";
  1102. static uint8_t sha256_buf[32] = {
  1103. 0x2A, 0x02, 0xB2, 0x44, 0x3F, 0x79, 0x85, 0xD8, 0x9D, 0x09, 0x00, 0x10, 0x86, 0xAE, 0x3D, 0xCF,
  1104. 0xA6, 0xEB, 0x0F, 0x55, 0xC6, 0xEF, 0x17, 0x07, 0x15, 0xD4, 0x23, 0x28, 0xE1, 0x6E, 0x6C, 0xB8
  1105. };
  1106. String md5 = "4a336d087aeb0390da10ee2ea7cb87f8";
  1107. static uint8_t md5_buf[16] = {
  1108. 0x4A, 0x33, 0x6D, 0x08, 0x7A, 0xEB, 0x03, 0x90, 0xDA, 0x10, 0xEE, 0x2E, 0xA7, 0xCB, 0x87, 0xF8
  1109. };
  1110. PackedByteArray buf = s.sha1_buffer();
  1111. CHECK(memcmp(sha1_buf, buf.ptr(), 20) == 0);
  1112. CHECK(s.sha1_text() == sha1);
  1113. buf = s.sha256_buffer();
  1114. CHECK(memcmp(sha256_buf, buf.ptr(), 32) == 0);
  1115. CHECK(s.sha256_text() == sha256);
  1116. buf = s.md5_buffer();
  1117. CHECK(memcmp(md5_buf, buf.ptr(), 16) == 0);
  1118. CHECK(s.md5_text() == md5);
  1119. }
  1120. TEST_CASE("[String] Join") {
  1121. String s = ", ";
  1122. Vector<String> parts;
  1123. parts.push_back("One");
  1124. parts.push_back("B");
  1125. parts.push_back("C");
  1126. String t = s.join(parts);
  1127. CHECK(t == "One, B, C");
  1128. }
  1129. TEST_CASE("[String] Is_*") {
  1130. static const char *data[12] = { "-30", "100", "10.1", "10,1", "1e2", "1e-2", "1e2e3", "0xAB", "AB", "Test1", "1Test", "Test*1" };
  1131. static bool isnum[12] = { true, true, true, false, false, false, false, false, false, false, false, false };
  1132. static bool isint[12] = { true, true, false, false, false, false, false, false, false, false, false, false };
  1133. static bool ishex[12] = { true, true, false, false, true, false, true, false, true, false, false, false };
  1134. static bool ishex_p[12] = { false, false, false, false, false, false, false, true, false, false, false, false };
  1135. static bool isflt[12] = { true, true, true, false, true, true, false, false, false, false, false, false };
  1136. static bool isid[12] = { false, false, false, false, false, false, false, false, true, true, false, false };
  1137. for (int i = 0; i < 12; i++) {
  1138. String s = String(data[i]);
  1139. CHECK(s.is_numeric() == isnum[i]);
  1140. CHECK(s.is_valid_int() == isint[i]);
  1141. CHECK(s.is_valid_hex_number(false) == ishex[i]);
  1142. CHECK(s.is_valid_hex_number(true) == ishex_p[i]);
  1143. CHECK(s.is_valid_float() == isflt[i]);
  1144. CHECK(s.is_valid_identifier() == isid[i]);
  1145. }
  1146. }
  1147. TEST_CASE("[String] humanize_size") {
  1148. CHECK(String::humanize_size(1000) == "1000 B");
  1149. CHECK(String::humanize_size(1025) == "1.00 KiB");
  1150. CHECK(String::humanize_size(1025300) == "1001.2 KiB");
  1151. CHECK(String::humanize_size(100523550) == "95.86 MiB");
  1152. CHECK(String::humanize_size(5345555000) == "4.97 GiB");
  1153. }
  1154. TEST_CASE("[String] validate_node_name") {
  1155. String numeric_only = "12345";
  1156. CHECK(numeric_only.validate_node_name() == "12345");
  1157. String name_with_spaces = "Name with spaces";
  1158. CHECK(name_with_spaces.validate_node_name() == "Name with spaces");
  1159. String name_with_kana = "Name with kana ゴドツ";
  1160. CHECK(name_with_kana.validate_node_name() == "Name with kana ゴドツ");
  1161. String name_with_invalid_chars = "Name with invalid characters :.@removed!";
  1162. CHECK(name_with_invalid_chars.validate_node_name() == "Name with invalid characters removed!");
  1163. }
  1164. TEST_CASE("[String] Variant indexed get") {
  1165. Variant s = String("abcd");
  1166. bool valid = false;
  1167. bool oob = true;
  1168. String r = s.get_indexed(1, valid, oob);
  1169. CHECK(valid);
  1170. CHECK_FALSE(oob);
  1171. CHECK_EQ(r, String("b"));
  1172. }
  1173. TEST_CASE("[String] Variant validated indexed get") {
  1174. Variant s = String("abcd");
  1175. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(Variant::STRING);
  1176. Variant r;
  1177. bool oob = true;
  1178. getter(&s, 1, &r, &oob);
  1179. CHECK_FALSE(oob);
  1180. CHECK_EQ(r, String("b"));
  1181. }
  1182. TEST_CASE("[String] Variant ptr indexed get") {
  1183. String s("abcd");
  1184. Variant::PTRIndexedGetter getter = Variant::get_member_ptr_indexed_getter(Variant::STRING);
  1185. String r;
  1186. getter(&s, 1, &r);
  1187. CHECK_EQ(r, String("b"));
  1188. }
  1189. TEST_CASE("[String] Variant indexed set") {
  1190. Variant s = String("abcd");
  1191. bool valid = false;
  1192. bool oob = true;
  1193. s.set_indexed(1, String("z"), valid, oob);
  1194. CHECK(valid);
  1195. CHECK_FALSE(oob);
  1196. CHECK_EQ(s, String("azcd"));
  1197. }
  1198. TEST_CASE("[String] Variant validated indexed set") {
  1199. Variant s = String("abcd");
  1200. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(Variant::STRING);
  1201. Variant v = String("z");
  1202. bool oob = true;
  1203. setter(&s, 1, &v, &oob);
  1204. CHECK_FALSE(oob);
  1205. CHECK_EQ(s, String("azcd"));
  1206. }
  1207. TEST_CASE("[String] Variant ptr indexed set") {
  1208. String s("abcd");
  1209. Variant::PTRIndexedSetter setter = Variant::get_member_ptr_indexed_setter(Variant::STRING);
  1210. String v("z");
  1211. setter(&s, 1, &v);
  1212. CHECK_EQ(s, String("azcd"));
  1213. }
  1214. } // namespace TestString
  1215. #endif // TEST_STRING_H