test_string.h 43 KB

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