test_string.h 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. /**************************************************************************/
  2. /* test_string.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. // Continue.
  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. Error err = s.parse_utf8(s.utf8().get_data());
  79. CHECK(err == OK);
  80. CHECK(s == u32str);
  81. err = s.parse_utf8((const char *)u8str);
  82. CHECK(err == OK);
  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. Error err = s.parse_utf16(s.utf16().get_data());
  93. CHECK(err == OK);
  94. CHECK(s == u32str);
  95. err = s.parse_utf16(u16str);
  96. CHECK(err == OK);
  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. Error err = s.parse_utf8((const char *)u8str);
  107. CHECK(err == OK);
  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. Error err = s.parse_utf16(u16str);
  119. CHECK(err == OK);
  120. CHECK(s == u32str);
  121. err = s.parse_utf16(u16str_swap);
  122. CHECK(err == OK);
  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] UTF8 with CR") {
  130. const String base = U"Hello darkness\r\nMy old friend\nI've come to talk\rWith you again";
  131. String keep_cr;
  132. Error err = keep_cr.parse_utf8(base.utf8().get_data());
  133. CHECK(err == OK);
  134. CHECK(keep_cr == base);
  135. String no_cr;
  136. err = no_cr.parse_utf8(base.utf8().get_data(), -1, true); // Skip CR.
  137. CHECK(err == OK);
  138. CHECK(no_cr == base.replace("\r", ""));
  139. }
  140. TEST_CASE("[String] Invalid UTF8 (non-standard)") {
  141. ERR_PRINT_OFF
  142. static const uint8_t u8str[] = { 0x45, 0xE3, 0x81, 0x8A, 0xE3, 0x82, 0x88, 0xE3, 0x81, 0x86, 0xF0, 0x9F, 0x8E, 0xA4, 0xF0, 0x82, 0x82, 0xAC, 0xED, 0xA0, 0x81, 0 };
  143. // + +2 +2 +2 +3 overlong +3 unpaired +2
  144. static const char32_t u32str[] = { 0x45, 0x304A, 0x3088, 0x3046, 0x1F3A4, 0x20AC, 0xFFFD, 0 };
  145. String s;
  146. Error err = s.parse_utf8((const char *)u8str);
  147. CHECK(err == ERR_INVALID_DATA);
  148. CHECK(s == u32str);
  149. CharString cs = (const char *)u8str;
  150. CHECK(String::utf8(cs) == s);
  151. ERR_PRINT_ON
  152. }
  153. TEST_CASE("[String] Invalid UTF8 (unrecoverable)") {
  154. ERR_PRINT_OFF
  155. static const uint8_t u8str[] = { 0x45, 0xE3, 0x81, 0x8A, 0x8F, 0xE3, 0xE3, 0x98, 0x8F, 0xE3, 0x82, 0x88, 0xE3, 0x81, 0x86, 0xC0, 0x80, 0xF0, 0x9F, 0x8E, 0xA4, 0xF0, 0x82, 0x82, 0xAC, 0xED, 0xA0, 0x81, 0 };
  156. // + +2 inv +2 inv inv inv +2 +2 ovl NUL +1 +3 overlong +3 unpaired +2
  157. static const char32_t u32str[] = { 0x45, 0x304A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x3088, 0x3046, 0xFFFD, 0x1F3A4, 0x20AC, 0xFFFD, 0 };
  158. String s;
  159. Error err = s.parse_utf8((const char *)u8str);
  160. CHECK(err == ERR_INVALID_DATA);
  161. CHECK(s == u32str);
  162. CharString cs = (const char *)u8str;
  163. CHECK(String::utf8(cs) == s);
  164. ERR_PRINT_ON
  165. }
  166. TEST_CASE("[String] Invalid UTF16 (non-standard)") {
  167. ERR_PRINT_OFF
  168. static const char16_t u16str[] = { 0x0045, 0x304A, 0x3088, 0x3046, 0xDFA4, 0 };
  169. // + + + + unpaired
  170. static const char32_t u32str[] = { 0x0045, 0x304A, 0x3088, 0x3046, 0xDFA4, 0 };
  171. String s;
  172. Error err = s.parse_utf16(u16str);
  173. CHECK(err == ERR_PARSE_ERROR);
  174. CHECK(s == u32str);
  175. Char16String cs = u16str;
  176. CHECK(String::utf16(cs) == s);
  177. ERR_PRINT_ON
  178. }
  179. TEST_CASE("[String] ASCII") {
  180. String s = U"Primero Leche";
  181. String t = s.ascii(false).get_data();
  182. CHECK(s == t);
  183. t = s.ascii(true).get_data();
  184. CHECK(s == t);
  185. }
  186. TEST_CASE("[String] Comparisons (equal)") {
  187. String s = "Test Compare";
  188. CHECK(s == "Test Compare");
  189. CHECK(s == U"Test Compare");
  190. CHECK(s == L"Test Compare");
  191. CHECK(s == String("Test Compare"));
  192. CharString empty = "";
  193. CharString cs = "Test Compare";
  194. CHECK(!(empty == cs));
  195. CHECK(!(cs == empty));
  196. CHECK(cs == CharString("Test Compare"));
  197. }
  198. TEST_CASE("[String] Comparisons (not equal)") {
  199. String s = "Test Compare";
  200. CHECK(s != "Peanut");
  201. CHECK(s != U"Coconut");
  202. CHECK(s != L"Coconut");
  203. CHECK(s != String("Butter"));
  204. }
  205. TEST_CASE("[String] Comparisons (operator <)") {
  206. String s = "Bees";
  207. CHECK(s < "Elephant");
  208. CHECK(!(s < U"Amber"));
  209. CHECK(!(s < L"Amber"));
  210. CHECK(!(s < String("Beatrix")));
  211. }
  212. TEST_CASE("[String] Concatenation") {
  213. String s;
  214. s += "Have";
  215. s += ' ';
  216. s += 'a';
  217. s += String(" ");
  218. s = s + U"Nice";
  219. s = s + " ";
  220. s = s + String("Day");
  221. CHECK(s == "Have a Nice Day");
  222. }
  223. TEST_CASE("[String] Testing size and length of string") {
  224. // todo: expand this test to do more tests on size() as it is complicated under the hood.
  225. CHECK(String("Mellon").size() == 7);
  226. CHECK(String("Mellon1").size() == 8);
  227. // length works fine and is easier to test
  228. CHECK(String("Mellon").length() == 6);
  229. CHECK(String("Mellon1").length() == 7);
  230. CHECK(String("Mellon2").length() == 7);
  231. CHECK(String("Mellon3").length() == 7);
  232. }
  233. TEST_CASE("[String] Testing for empty string") {
  234. CHECK(!String("Mellon").is_empty());
  235. // do this more than once, to check for string corruption
  236. CHECK(String("").is_empty());
  237. CHECK(String("").is_empty());
  238. CHECK(String("").is_empty());
  239. }
  240. TEST_CASE("[String] Contains") {
  241. String s = "C:\\Godot\\project\\string_test.tscn";
  242. CHECK(s.contains(":\\"));
  243. CHECK(s.contains("Godot"));
  244. CHECK(s.contains(String("project\\string_test")));
  245. CHECK(s.contains(String("\\string_test.tscn")));
  246. CHECK(!s.contains("://"));
  247. CHECK(!s.contains("Godoh"));
  248. CHECK(!s.contains(String("project\\string test")));
  249. CHECK(!s.contains(String("\\char_test.tscn")));
  250. }
  251. TEST_CASE("[String] Test chr") {
  252. CHECK(String::chr('H') == "H");
  253. CHECK(String::chr(0x3012)[0] == 0x3012);
  254. ERR_PRINT_OFF
  255. CHECK(String::chr(0xd812)[0] == 0xfffd); // Unpaired UTF-16 surrogate
  256. CHECK(String::chr(0x20d812)[0] == 0xfffd); // Outside UTF-32 range
  257. ERR_PRINT_ON
  258. }
  259. TEST_CASE("[String] Operator []") {
  260. String a = "Kugar Sane";
  261. a[0] = 'S';
  262. a[6] = 'C';
  263. CHECK(a == "Sugar Cane");
  264. CHECK(a[1] == 'u');
  265. CHECK(a.unicode_at(1) == 'u');
  266. }
  267. TEST_CASE("[String] Case function test") {
  268. String a = "MoMoNgA";
  269. CHECK(a.to_upper() == "MOMONGA");
  270. CHECK(a.to_lower() == "momonga");
  271. }
  272. TEST_CASE("[String] Case compare function test") {
  273. String a = "MoMoNgA";
  274. CHECK(a.casecmp_to("momonga") != 0);
  275. CHECK(a.nocasecmp_to("momonga") == 0);
  276. }
  277. TEST_CASE("[String] Natural compare function test") {
  278. String a = "img2.png";
  279. CHECK(a.nocasecmp_to("img10.png") > 0);
  280. CHECK(a.naturalnocasecmp_to("img10.png") < 0);
  281. }
  282. TEST_CASE("[String] hex_encode_buffer") {
  283. static const uint8_t u8str[] = { 0x45, 0xE3, 0x81, 0x8A, 0x8F, 0xE3 };
  284. String s = String::hex_encode_buffer(u8str, 6);
  285. CHECK(s == U"45e3818a8fe3");
  286. }
  287. TEST_CASE("[String] Substr") {
  288. String s = "Killer Baby";
  289. CHECK(s.substr(3, 4) == "ler ");
  290. CHECK(s.substr(3) == "ler Baby");
  291. }
  292. TEST_CASE("[String] Find") {
  293. String s = "Pretty Woman Woman";
  294. CHECK(s.find("tty") == 3);
  295. CHECK(s.find("Wo", 9) == 13);
  296. CHECK(s.find("Revenge of the Monster Truck") == -1);
  297. CHECK(s.rfind("man") == 15);
  298. }
  299. TEST_CASE("[String] Find no case") {
  300. String s = "Pretty Whale Whale";
  301. CHECK(s.findn("WHA") == 7);
  302. CHECK(s.findn("WHA", 9) == 13);
  303. CHECK(s.findn("Revenge of the Monster SawFish") == -1);
  304. CHECK(s.rfindn("WHA") == 13);
  305. }
  306. TEST_CASE("[String] Find MK") {
  307. Vector<String> keys;
  308. keys.push_back("sty");
  309. keys.push_back("tty");
  310. keys.push_back("man");
  311. String s = "Pretty Woman";
  312. int key = 0;
  313. CHECK(s.findmk(keys, 0, &key) == 3);
  314. CHECK(key == 1);
  315. CHECK(s.findmk(keys, 5, &key) == 9);
  316. CHECK(key == 2);
  317. }
  318. TEST_CASE("[String] Find and replace") {
  319. String s = "Happy Birthday, Anna!";
  320. s = s.replace("Birthday", "Halloween");
  321. CHECK(s == "Happy Halloween, Anna!");
  322. s = s.replace_first("H", "W");
  323. CHECK(s == "Wappy Halloween, Anna!");
  324. }
  325. TEST_CASE("[String] Insertion") {
  326. String s = "Who is Frederic?";
  327. s = s.insert(s.find("?"), " Chopin");
  328. CHECK(s == "Who is Frederic Chopin?");
  329. }
  330. TEST_CASE("[String] Erasing") {
  331. String s = "Josephine is such a cute girl!";
  332. s = s.erase(s.find("cute "), String("cute ").length());
  333. CHECK(s == "Josephine is such a girl!");
  334. }
  335. TEST_CASE("[String] Number to string") {
  336. CHECK(String::num(0) == "0");
  337. CHECK(String::num(0.0) == "0"); // No trailing zeros.
  338. CHECK(String::num(-0.0) == "-0"); // Includes sign even for zero.
  339. CHECK(String::num(3.141593) == "3.141593");
  340. CHECK(String::num(3.141593, 3) == "3.142");
  341. CHECK(String::num_scientific(30000000) == "3e+07");
  342. CHECK(String::num_int64(3141593) == "3141593");
  343. CHECK(String::num_int64(0xA141593, 16) == "a141593");
  344. CHECK(String::num_int64(0xA141593, 16, true) == "A141593");
  345. CHECK(String::num(42.100023, 4) == "42.1"); // No trailing zeros.
  346. // String::num_real tests.
  347. CHECK(String::num_real(1.0) == "1.0");
  348. CHECK(String::num_real(1.0, false) == "1");
  349. CHECK(String::num_real(9.9) == "9.9");
  350. CHECK(String::num_real(9.99) == "9.99");
  351. CHECK(String::num_real(9.999) == "9.999");
  352. CHECK(String::num_real(9.9999) == "9.9999");
  353. CHECK(String::num_real(3.141593) == "3.141593");
  354. CHECK(String::num_real(3.141) == "3.141"); // No trailing zeros.
  355. #ifdef REAL_T_IS_DOUBLE
  356. CHECK_MESSAGE(String::num_real(123.456789) == "123.456789", "Prints the appropriate amount of digits for real_t = double.");
  357. CHECK_MESSAGE(String::num_real(-123.456789) == "-123.456789", "Prints the appropriate amount of digits for real_t = double.");
  358. CHECK_MESSAGE(String::num_real(Math_PI) == "3.14159265358979", "Prints the appropriate amount of digits for real_t = double.");
  359. 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.");
  360. #else
  361. CHECK_MESSAGE(String::num_real(123.456789) == "123.4568", "Prints the appropriate amount of digits for real_t = float.");
  362. CHECK_MESSAGE(String::num_real(-123.456789) == "-123.4568", "Prints the appropriate amount of digits for real_t = float.");
  363. CHECK_MESSAGE(String::num_real(Math_PI) == "3.141593", "Prints the appropriate amount of digits for real_t = float.");
  364. CHECK_MESSAGE(String::num_real(3.1415f) == "3.1415", "Prints only reliable digits of 32-bit float when real_t = float.");
  365. #endif // REAL_T_IS_DOUBLE
  366. // Checks doubles with many decimal places.
  367. CHECK(String::num(0.0000012345432123454321, -1) == "0.00000123454321"); // -1 uses 14 as sane default.
  368. CHECK(String::num(0.0000012345432123454321) == "0.00000123454321"); // -1 is the default value.
  369. CHECK(String::num(-0.0000012345432123454321) == "-0.00000123454321");
  370. CHECK(String::num(-10000.0000012345432123454321) == "-10000.0000012345");
  371. CHECK(String::num(0.0000000000012345432123454321) == "0.00000000000123");
  372. CHECK(String::num(0.0000000000012345432123454321, 3) == "0");
  373. // Note: When relevant (remainder > 0.5), the last digit gets rounded up,
  374. // which can also lead to not include a trailing zero, e.g. "...89" -> "...9".
  375. CHECK(String::num(0.0000056789876567898765) == "0.00000567898766"); // Should round last digit.
  376. CHECK(String::num(10000.000005678999999999) == "10000.000005679"); // We cut at ...789|99 which is rounded to ...79, so only 13 decimals.
  377. CHECK(String::num(42.12999999, 6) == "42.13"); // Also happens with lower decimals count.
  378. // 32 is MAX_DECIMALS. We can't reliably store that many so we can't compare against a string,
  379. // but we can check that the string length is 34 (32 + 2 for "0.").
  380. CHECK(String::num(0.00000123456789987654321123456789987654321, 32).length() == 34);
  381. CHECK(String::num(0.00000123456789987654321123456789987654321, 42).length() == 34); // Should enforce MAX_DECIMALS.
  382. CHECK(String::num(10000.00000123456789987654321123456789987654321, 42).length() == 38); // 32 decimals + "10000.".
  383. }
  384. TEST_CASE("[String] String to integer") {
  385. static const char *nums[4] = { "1237461283", "- 22", "0", " - 1123412" };
  386. static const int num[4] = { 1237461283, -22, 0, -1123412 };
  387. for (int i = 0; i < 4; i++) {
  388. CHECK(String(nums[i]).to_int() == num[i]);
  389. }
  390. }
  391. TEST_CASE("[String] Hex to integer") {
  392. static const char *nums[4] = { "0xFFAE", "22", "0", "AADDAD" };
  393. static const int64_t num[4] = { 0xFFAE, 0x22, 0, 0xAADDAD };
  394. for (int i = 0; i < 4; i++) {
  395. CHECK(String(nums[i]).hex_to_int() == num[i]);
  396. }
  397. }
  398. TEST_CASE("[String] String to float") {
  399. static const char *nums[4] = { "-12348298412.2", "0.05", "2.0002", " -0.0001" };
  400. static const double num[4] = { -12348298412.2, 0.05, 2.0002, -0.0001 };
  401. for (int i = 0; i < 4; i++) {
  402. CHECK(!(ABS(String(nums[i]).to_float() - num[i]) > 0.00001));
  403. }
  404. }
  405. TEST_CASE("[String] Slicing") {
  406. String s = "Mars,Jupiter,Saturn,Uranus";
  407. const char *slices[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
  408. for (int i = 0; i < s.get_slice_count(","); i++) {
  409. CHECK(s.get_slice(",", i) == slices[i]);
  410. }
  411. }
  412. TEST_CASE("[String] Splitting") {
  413. String s = "Mars,Jupiter,Saturn,Uranus";
  414. Vector<String> l;
  415. const char *slices_l[3] = { "Mars", "Jupiter", "Saturn,Uranus" };
  416. const char *slices_r[3] = { "Mars,Jupiter", "Saturn", "Uranus" };
  417. const char *slices_3[4] = { "t", "e", "s", "t" };
  418. l = s.split(",", true, 2);
  419. CHECK(l.size() == 3);
  420. for (int i = 0; i < l.size(); i++) {
  421. CHECK(l[i] == slices_l[i]);
  422. }
  423. l = s.rsplit(",", true, 2);
  424. CHECK(l.size() == 3);
  425. for (int i = 0; i < l.size(); i++) {
  426. CHECK(l[i] == slices_r[i]);
  427. }
  428. s = "test";
  429. l = s.split();
  430. CHECK(l.size() == 4);
  431. for (int i = 0; i < l.size(); i++) {
  432. CHECK(l[i] == slices_3[i]);
  433. }
  434. s = "";
  435. l = s.split();
  436. CHECK(l.size() == 1);
  437. CHECK(l[0] == "");
  438. l = s.split("", false);
  439. CHECK(l.size() == 0);
  440. s = "Mars Jupiter Saturn Uranus";
  441. const char *slices_s[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
  442. l = s.split_spaces();
  443. for (int i = 0; i < l.size(); i++) {
  444. CHECK(l[i] == slices_s[i]);
  445. }
  446. s = "1.2;2.3 4.5";
  447. const double slices_d[3] = { 1.2, 2.3, 4.5 };
  448. Vector<double> d_arr;
  449. d_arr = s.split_floats(";");
  450. CHECK(d_arr.size() == 2);
  451. for (int i = 0; i < d_arr.size(); i++) {
  452. CHECK(ABS(d_arr[i] - slices_d[i]) <= 0.00001);
  453. }
  454. Vector<String> keys;
  455. keys.push_back(";");
  456. keys.push_back(" ");
  457. Vector<float> f_arr;
  458. f_arr = s.split_floats_mk(keys);
  459. CHECK(f_arr.size() == 3);
  460. for (int i = 0; i < f_arr.size(); i++) {
  461. CHECK(ABS(f_arr[i] - slices_d[i]) <= 0.00001);
  462. }
  463. s = "1;2 4";
  464. const int slices_i[3] = { 1, 2, 4 };
  465. Vector<int> ii;
  466. ii = s.split_ints(";");
  467. CHECK(ii.size() == 2);
  468. for (int i = 0; i < ii.size(); i++) {
  469. CHECK(ii[i] == slices_i[i]);
  470. }
  471. ii = s.split_ints_mk(keys);
  472. CHECK(ii.size() == 3);
  473. for (int i = 0; i < ii.size(); i++) {
  474. CHECK(ii[i] == slices_i[i]);
  475. }
  476. }
  477. struct test_27_data {
  478. char const *data;
  479. char const *part;
  480. bool expected;
  481. };
  482. TEST_CASE("[String] Begins with") {
  483. test_27_data tc[] = {
  484. // Test cases for true:
  485. { "res://foobar", "res://", true },
  486. { "abc", "abc", true },
  487. { "abc", "", true },
  488. { "", "", true },
  489. // Test cases for false:
  490. { "res", "res://", false },
  491. { "abcdef", "foo", false },
  492. { "abc", "ax", false },
  493. { "", "abc", false }
  494. };
  495. size_t count = sizeof(tc) / sizeof(tc[0]);
  496. bool state = true;
  497. for (size_t i = 0; i < count; ++i) {
  498. String s = tc[i].data;
  499. state = s.begins_with(tc[i].part) == tc[i].expected;
  500. CHECK_MESSAGE(state, "first check failed at: ", i);
  501. String sb = tc[i].part;
  502. state = s.begins_with(sb) == tc[i].expected;
  503. CHECK_MESSAGE(state, "second check failed at: ", i);
  504. }
  505. // Test "const char *" version also with nullptr.
  506. String s("foo");
  507. state = s.begins_with(nullptr) == false;
  508. CHECK_MESSAGE(state, "nullptr check failed");
  509. String empty("");
  510. state = empty.begins_with(nullptr) == false;
  511. CHECK_MESSAGE(state, "nullptr check with empty string failed");
  512. }
  513. TEST_CASE("[String] Ends with") {
  514. test_27_data tc[] = {
  515. // test cases for true:
  516. { "res://foobar", "foobar", true },
  517. { "abc", "abc", true },
  518. { "abc", "", true },
  519. { "", "", true },
  520. // test cases for false:
  521. { "res", "res://", false },
  522. { "", "abc", false },
  523. { "abcdef", "foo", false },
  524. { "abc", "xc", false }
  525. };
  526. size_t count = sizeof(tc) / sizeof(tc[0]);
  527. for (size_t i = 0; i < count; ++i) {
  528. String s = tc[i].data;
  529. String sb = tc[i].part;
  530. bool state = s.ends_with(sb) == tc[i].expected;
  531. CHECK_MESSAGE(state, "check failed at: ", i);
  532. }
  533. }
  534. TEST_CASE("[String] format") {
  535. const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";
  536. Dictionary value_dictionary;
  537. value_dictionary["red"] = 10;
  538. value_dictionary["green"] = 20;
  539. value_dictionary["blue"] = "bla";
  540. value_dictionary["alpha"] = 0.4;
  541. String value = value_format.format(value_dictionary, "$_");
  542. CHECK(value == "red=\"10\" green=\"20\" blue=\"bla\" alpha=\"0.4\"");
  543. }
  544. TEST_CASE("[String] sprintf") {
  545. String format, output;
  546. Array args;
  547. bool error;
  548. // %%
  549. format = "fish %% frog";
  550. args.clear();
  551. output = format.sprintf(args, &error);
  552. REQUIRE(error == false);
  553. CHECK(output == String("fish % frog"));
  554. ///// Ints
  555. // Int
  556. format = "fish %d frog";
  557. args.clear();
  558. args.push_back(5);
  559. output = format.sprintf(args, &error);
  560. REQUIRE(error == false);
  561. CHECK(output == String("fish 5 frog"));
  562. // Int left padded with zeroes.
  563. format = "fish %05d frog";
  564. args.clear();
  565. args.push_back(5);
  566. output = format.sprintf(args, &error);
  567. REQUIRE(error == false);
  568. CHECK(output == String("fish 00005 frog"));
  569. // Int left padded with spaces.
  570. format = "fish %5d frog";
  571. args.clear();
  572. args.push_back(5);
  573. output = format.sprintf(args, &error);
  574. REQUIRE(error == false);
  575. CHECK(output == String("fish 5 frog"));
  576. // Int right padded with spaces.
  577. format = "fish %-5d frog";
  578. args.clear();
  579. args.push_back(5);
  580. output = format.sprintf(args, &error);
  581. REQUIRE(error == false);
  582. CHECK(output == String("fish 5 frog"));
  583. // Int with sign (positive).
  584. format = "fish %+d frog";
  585. args.clear();
  586. args.push_back(5);
  587. output = format.sprintf(args, &error);
  588. REQUIRE(error == false);
  589. CHECK(output == String("fish +5 frog"));
  590. // Negative int.
  591. format = "fish %d frog";
  592. args.clear();
  593. args.push_back(-5);
  594. output = format.sprintf(args, &error);
  595. REQUIRE(error == false);
  596. CHECK(output == String("fish -5 frog"));
  597. // Negative int left padded with spaces.
  598. format = "fish %5d frog";
  599. args.clear();
  600. args.push_back(-5);
  601. output = format.sprintf(args, &error);
  602. REQUIRE(error == false);
  603. CHECK(output == String("fish -5 frog"));
  604. // Negative int left padded with zeros.
  605. format = "fish %05d frog";
  606. args.clear();
  607. args.push_back(-5);
  608. output = format.sprintf(args, &error);
  609. REQUIRE(error == false);
  610. CHECK(output == String("fish -0005 frog"));
  611. // Negative int right padded with spaces.
  612. format = "fish %-5d frog";
  613. args.clear();
  614. args.push_back(-5);
  615. output = format.sprintf(args, &error);
  616. REQUIRE(error == false);
  617. CHECK(output == String("fish -5 frog"));
  618. // Negative int right padded with zeros. (0 ignored)
  619. format = "fish %-05d frog";
  620. args.clear();
  621. args.push_back(-5);
  622. ERR_PRINT_OFF; // Silence warning about 0 ignored.
  623. output = format.sprintf(args, &error);
  624. ERR_PRINT_ON;
  625. REQUIRE(error == false);
  626. CHECK(output == String("fish -5 frog"));
  627. // Hex (lower)
  628. format = "fish %x frog";
  629. args.clear();
  630. args.push_back(45);
  631. output = format.sprintf(args, &error);
  632. REQUIRE(error == false);
  633. CHECK(output == String("fish 2d frog"));
  634. // Hex (upper)
  635. format = "fish %X frog";
  636. args.clear();
  637. args.push_back(45);
  638. output = format.sprintf(args, &error);
  639. REQUIRE(error == false);
  640. CHECK(output == String("fish 2D frog"));
  641. // Octal
  642. format = "fish %o frog";
  643. args.clear();
  644. args.push_back(99);
  645. output = format.sprintf(args, &error);
  646. REQUIRE(error == false);
  647. CHECK(output == String("fish 143 frog"));
  648. ///// Reals
  649. // Real
  650. format = "fish %f frog";
  651. args.clear();
  652. args.push_back(99.99);
  653. output = format.sprintf(args, &error);
  654. REQUIRE(error == false);
  655. CHECK(output == String("fish 99.990000 frog"));
  656. // Real left-padded.
  657. format = "fish %11f frog";
  658. args.clear();
  659. args.push_back(99.99);
  660. output = format.sprintf(args, &error);
  661. REQUIRE(error == false);
  662. CHECK(output == String("fish 99.990000 frog"));
  663. // Real (infinity) left-padded
  664. format = "fish %11f frog";
  665. args.clear();
  666. args.push_back(INFINITY);
  667. output = format.sprintf(args, &error);
  668. REQUIRE(error == false);
  669. CHECK(output == String("fish inf frog"));
  670. // Real right-padded.
  671. format = "fish %-11f frog";
  672. args.clear();
  673. args.push_back(99.99);
  674. output = format.sprintf(args, &error);
  675. REQUIRE(error == false);
  676. CHECK(output == String("fish 99.990000 frog"));
  677. // Real given int.
  678. format = "fish %f frog";
  679. args.clear();
  680. args.push_back(99);
  681. output = format.sprintf(args, &error);
  682. REQUIRE(error == false);
  683. CHECK(output == String("fish 99.000000 frog"));
  684. // Real with sign (positive).
  685. format = "fish %+f frog";
  686. args.clear();
  687. args.push_back(99.99);
  688. output = format.sprintf(args, &error);
  689. REQUIRE(error == false);
  690. CHECK(output == String("fish +99.990000 frog"));
  691. // Real with sign (negative zero).
  692. format = "fish %+f frog";
  693. args.clear();
  694. args.push_back(-0.0);
  695. output = format.sprintf(args, &error);
  696. REQUIRE(error == false);
  697. CHECK(output == String("fish -0.000000 frog"));
  698. // Real with sign (positive zero).
  699. format = "fish %+f frog";
  700. args.clear();
  701. args.push_back(0.0);
  702. output = format.sprintf(args, &error);
  703. REQUIRE(error == false);
  704. CHECK(output == String("fish +0.000000 frog"));
  705. // Real with 1 decimal.
  706. format = "fish %.1f frog";
  707. args.clear();
  708. args.push_back(99.99);
  709. output = format.sprintf(args, &error);
  710. REQUIRE(error == false);
  711. CHECK(output == String("fish 100.0 frog"));
  712. // Real with 12 decimals.
  713. format = "fish %.12f frog";
  714. args.clear();
  715. args.push_back(99.99);
  716. output = format.sprintf(args, &error);
  717. REQUIRE(error == false);
  718. CHECK(output == String("fish 99.990000000000 frog"));
  719. // Real with no decimals.
  720. format = "fish %.f frog";
  721. args.clear();
  722. args.push_back(99.99);
  723. output = format.sprintf(args, &error);
  724. REQUIRE(error == false);
  725. CHECK(output == String("fish 100 frog"));
  726. // Negative real right padded with zeros. (0 ignored)
  727. format = "fish %-011f frog";
  728. args.clear();
  729. args.push_back(-99.99);
  730. ERR_PRINT_OFF; // Silence warning about 0 ignored.
  731. output = format.sprintf(args, &error);
  732. ERR_PRINT_ON;
  733. REQUIRE(error == false);
  734. CHECK(output == String("fish -99.990000 frog"));
  735. ///// Vectors
  736. // Vector2
  737. format = "fish %v frog";
  738. args.clear();
  739. args.push_back(Variant(Vector2(19.99, 1.00)));
  740. output = format.sprintf(args, &error);
  741. REQUIRE(error == false);
  742. CHECK(output == String("fish (19.990000, 1.000000) frog"));
  743. // Vector3
  744. format = "fish %v frog";
  745. args.clear();
  746. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  747. output = format.sprintf(args, &error);
  748. REQUIRE(error == false);
  749. CHECK(output == String("fish (19.990000, 1.000000, -2.050000) frog"));
  750. // Vector4
  751. format = "fish %v frog";
  752. args.clear();
  753. args.push_back(Variant(Vector4(19.99, 1.00, -2.05, 5.5)));
  754. output = format.sprintf(args, &error);
  755. REQUIRE(error == false);
  756. CHECK(output == String("fish (19.990000, 1.000000, -2.050000, 5.500000) frog"));
  757. // Vector with negative values.
  758. format = "fish %v frog";
  759. args.clear();
  760. args.push_back(Variant(Vector2(-19.99, -1.00)));
  761. output = format.sprintf(args, &error);
  762. REQUIRE(error == false);
  763. CHECK(output == String("fish (-19.990000, -1.000000) frog"));
  764. // Vector left-padded.
  765. format = "fish %11v frog";
  766. args.clear();
  767. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  768. output = format.sprintf(args, &error);
  769. REQUIRE(error == false);
  770. CHECK(output == String("fish ( 19.990000, 1.000000, -2.050000) frog"));
  771. // Vector left-padded with inf/nan
  772. format = "fish %11v frog";
  773. args.clear();
  774. args.push_back(Variant(Vector2(INFINITY, NAN)));
  775. output = format.sprintf(args, &error);
  776. REQUIRE(error == false);
  777. CHECK(output == String("fish ( inf, nan) frog"));
  778. // Vector right-padded.
  779. format = "fish %-11v frog";
  780. args.clear();
  781. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  782. output = format.sprintf(args, &error);
  783. REQUIRE(error == false);
  784. CHECK(output == String("fish (19.990000 , 1.000000 , -2.050000 ) frog"));
  785. // Vector left-padded with zeros.
  786. format = "fish %011v frog";
  787. args.clear();
  788. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  789. output = format.sprintf(args, &error);
  790. REQUIRE(error == false);
  791. CHECK(output == String("fish (0019.990000, 0001.000000, -002.050000) frog"));
  792. // Vector given Vector3i.
  793. format = "fish %v frog";
  794. args.clear();
  795. args.push_back(Variant(Vector3i(19, 1, -2)));
  796. output = format.sprintf(args, &error);
  797. REQUIRE(error == false);
  798. CHECK(output == String("fish (19.000000, 1.000000, -2.000000) frog"));
  799. // Vector with 1 decimal.
  800. format = "fish %.1v frog";
  801. args.clear();
  802. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  803. output = format.sprintf(args, &error);
  804. REQUIRE(error == false);
  805. CHECK(output == String("fish (20.0, 1.0, -2.0) frog"));
  806. // Vector with 12 decimals.
  807. format = "fish %.12v frog";
  808. args.clear();
  809. args.push_back(Variant(Vector3(19.00, 1.00, -2.00)));
  810. output = format.sprintf(args, &error);
  811. REQUIRE(error == false);
  812. CHECK(output == String("fish (19.000000000000, 1.000000000000, -2.000000000000) frog"));
  813. // Vector with no decimals.
  814. format = "fish %.v frog";
  815. args.clear();
  816. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  817. output = format.sprintf(args, &error);
  818. REQUIRE(error == false);
  819. CHECK(output == String("fish (20, 1, -2) frog"));
  820. ///// Strings
  821. // String
  822. format = "fish %s frog";
  823. args.clear();
  824. args.push_back("cheese");
  825. output = format.sprintf(args, &error);
  826. REQUIRE(error == false);
  827. CHECK(output == String("fish cheese frog"));
  828. // String left-padded.
  829. format = "fish %10s frog";
  830. args.clear();
  831. args.push_back("cheese");
  832. output = format.sprintf(args, &error);
  833. REQUIRE(error == false);
  834. CHECK(output == String("fish cheese frog"));
  835. // String right-padded.
  836. format = "fish %-10s frog";
  837. args.clear();
  838. args.push_back("cheese");
  839. output = format.sprintf(args, &error);
  840. REQUIRE(error == false);
  841. CHECK(output == String("fish cheese frog"));
  842. ///// Characters
  843. // Character as string.
  844. format = "fish %c frog";
  845. args.clear();
  846. args.push_back("A");
  847. output = format.sprintf(args, &error);
  848. REQUIRE(error == false);
  849. CHECK(output == String("fish A frog"));
  850. // Character as int.
  851. format = "fish %c frog";
  852. args.clear();
  853. args.push_back(65);
  854. output = format.sprintf(args, &error);
  855. REQUIRE(error == false);
  856. CHECK(output == String("fish A frog"));
  857. ///// Dynamic width
  858. // String dynamic width.
  859. format = "fish %*s frog";
  860. args.clear();
  861. args.push_back(10);
  862. args.push_back("cheese");
  863. output = format.sprintf(args, &error);
  864. REQUIRE(error == false);
  865. REQUIRE(output == String("fish cheese frog"));
  866. // Int dynamic width.
  867. format = "fish %*d frog";
  868. args.clear();
  869. args.push_back(10);
  870. args.push_back(99);
  871. output = format.sprintf(args, &error);
  872. REQUIRE(error == false);
  873. REQUIRE(output == String("fish 99 frog"));
  874. // Float dynamic width.
  875. format = "fish %*.*f frog";
  876. args.clear();
  877. args.push_back(10);
  878. args.push_back(3);
  879. args.push_back(99.99);
  880. output = format.sprintf(args, &error);
  881. REQUIRE(error == false);
  882. CHECK(output == String("fish 99.990 frog"));
  883. ///// Errors
  884. // More formats than arguments.
  885. format = "fish %s %s frog";
  886. args.clear();
  887. args.push_back("cheese");
  888. output = format.sprintf(args, &error);
  889. REQUIRE(error);
  890. CHECK(output == "not enough arguments for format string");
  891. // More arguments than formats.
  892. format = "fish %s frog";
  893. args.clear();
  894. args.push_back("hello");
  895. args.push_back("cheese");
  896. output = format.sprintf(args, &error);
  897. REQUIRE(error);
  898. CHECK(output == "not all arguments converted during string formatting");
  899. // Incomplete format.
  900. format = "fish %10";
  901. args.clear();
  902. args.push_back("cheese");
  903. output = format.sprintf(args, &error);
  904. REQUIRE(error);
  905. CHECK(output == "incomplete format");
  906. // Bad character in format string.
  907. format = "fish %&f frog";
  908. args.clear();
  909. args.push_back("cheese");
  910. output = format.sprintf(args, &error);
  911. REQUIRE(error);
  912. CHECK(output == "unsupported format character");
  913. // Too many decimals.
  914. format = "fish %2.2.2f frog";
  915. args.clear();
  916. args.push_back(99.99);
  917. output = format.sprintf(args, &error);
  918. REQUIRE(error);
  919. CHECK(output == "too many decimal points in format");
  920. // * not a number or vector.
  921. format = "fish %*f frog";
  922. args.clear();
  923. args.push_back("cheese");
  924. args.push_back(99.99);
  925. output = format.sprintf(args, &error);
  926. REQUIRE(error);
  927. CHECK(output == "* wants number or vector");
  928. // Character too long.
  929. format = "fish %c frog";
  930. args.clear();
  931. args.push_back("sc");
  932. output = format.sprintf(args, &error);
  933. REQUIRE(error);
  934. CHECK(output == "%c requires number or single-character string");
  935. // Character bad type.
  936. format = "fish %c frog";
  937. args.clear();
  938. args.push_back(Array());
  939. output = format.sprintf(args, &error);
  940. REQUIRE(error);
  941. CHECK(output == "%c requires number or single-character string");
  942. }
  943. TEST_CASE("[String] is_numeric") {
  944. CHECK(String("12").is_numeric());
  945. CHECK(String("1.2").is_numeric());
  946. CHECK(!String("AF").is_numeric());
  947. CHECK(String("-12").is_numeric());
  948. CHECK(String("-1.2").is_numeric());
  949. }
  950. TEST_CASE("[String] pad") {
  951. String s = String("test");
  952. CHECK(s.lpad(10, "x") == U"xxxxxxtest");
  953. CHECK(s.rpad(10, "x") == U"testxxxxxx");
  954. s = String("10.10");
  955. CHECK(s.pad_decimals(4) == U"10.1000");
  956. CHECK(s.pad_decimals(1) == U"10.1");
  957. CHECK(s.pad_zeros(4) == U"0010.10");
  958. CHECK(s.pad_zeros(1) == U"10.10");
  959. }
  960. TEST_CASE("[String] is_subsequence_of") {
  961. String a = "is subsequence of";
  962. CHECK(String("sub").is_subsequence_of(a));
  963. CHECK(!String("Sub").is_subsequence_of(a));
  964. CHECK(String("Sub").is_subsequence_ofn(a));
  965. }
  966. TEST_CASE("[String] match") {
  967. CHECK(String("img1.png").match("*.png"));
  968. CHECK(!String("img1.jpeg").match("*.png"));
  969. CHECK(!String("img1.Png").match("*.png"));
  970. CHECK(String("img1.Png").matchn("*.png"));
  971. }
  972. TEST_CASE("[String] IPVX address to string") {
  973. IPAddress ip0("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  974. IPAddress ip(0x0123, 0x4567, 0x89ab, 0xcdef, true);
  975. IPAddress ip2("fe80::52e5:49ff:fe93:1baf");
  976. IPAddress ip3("::ffff:192.168.0.1");
  977. String ip4 = "192.168.0.1";
  978. CHECK(ip4.is_valid_ip_address());
  979. ip4 = "192.368.0.1";
  980. CHECK(!ip4.is_valid_ip_address());
  981. String ip6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
  982. CHECK(ip6.is_valid_ip_address());
  983. ip6 = "2001:0db8:85j3:0000:0000:8a2e:0370:7334";
  984. CHECK(!ip6.is_valid_ip_address());
  985. ip6 = "2001:0db8:85f345:0000:0000:8a2e:0370:7334";
  986. CHECK(!ip6.is_valid_ip_address());
  987. ip6 = "2001:0db8::0:8a2e:370:7334";
  988. CHECK(ip6.is_valid_ip_address());
  989. ip6 = "::ffff:192.168.0.1";
  990. CHECK(ip6.is_valid_ip_address());
  991. }
  992. TEST_CASE("[String] Capitalize against many strings") {
  993. String input = "2D";
  994. String output = "2d";
  995. CHECK(input.capitalize() == output);
  996. input = "2d";
  997. output = "2d";
  998. CHECK(input.capitalize() == output);
  999. input = "2db";
  1000. output = "2 Db";
  1001. CHECK(input.capitalize() == output);
  1002. input = "HTML5 Html5 html5 html_5";
  1003. output = "Html 5 Html 5 Html 5 Html 5";
  1004. CHECK(input.capitalize() == output);
  1005. input = "Node2D Node2d NODE2D NODE_2D node_2d";
  1006. output = "Node 2d Node 2d Node 2d Node 2d Node 2d";
  1007. CHECK(input.capitalize() == output);
  1008. input = "Node2DPosition";
  1009. output = "Node 2d Position";
  1010. CHECK(input.capitalize() == output);
  1011. input = "Number2Digits";
  1012. output = "Number 2 Digits";
  1013. CHECK(input.capitalize() == output);
  1014. input = "bytes2var";
  1015. output = "Bytes 2 Var";
  1016. CHECK(input.capitalize() == output);
  1017. input = "linear2db";
  1018. output = "Linear 2 Db";
  1019. CHECK(input.capitalize() == output);
  1020. input = "vector3";
  1021. output = "Vector 3";
  1022. CHECK(input.capitalize() == output);
  1023. input = "sha256";
  1024. output = "Sha 256";
  1025. CHECK(input.capitalize() == output);
  1026. input = "PascalCase";
  1027. output = "Pascal Case";
  1028. CHECK(input.capitalize() == output);
  1029. input = "PascalPascalCase";
  1030. output = "Pascal Pascal Case";
  1031. CHECK(input.capitalize() == output);
  1032. input = "snake_case";
  1033. output = "Snake Case";
  1034. CHECK(input.capitalize() == output);
  1035. input = "snake_snake_case";
  1036. output = "Snake Snake Case";
  1037. CHECK(input.capitalize() == output);
  1038. input = "sha256sum";
  1039. output = "Sha 256 Sum";
  1040. CHECK(input.capitalize() == output);
  1041. input = "cat2dog";
  1042. output = "Cat 2 Dog";
  1043. CHECK(input.capitalize() == output);
  1044. input = "function(name)";
  1045. output = "Function(name)";
  1046. CHECK(input.capitalize() == output);
  1047. input = "snake_case_function(snake_case_arg)";
  1048. output = "Snake Case Function(snake Case Arg)";
  1049. CHECK(input.capitalize() == output);
  1050. input = "snake_case_function( snake_case_arg )";
  1051. output = "Snake Case Function( Snake Case Arg )";
  1052. CHECK(input.capitalize() == output);
  1053. }
  1054. struct StringCasesTestCase {
  1055. const char *input;
  1056. const char *camel_case;
  1057. const char *pascal_case;
  1058. const char *snake_case;
  1059. };
  1060. TEST_CASE("[String] Checking case conversion methods") {
  1061. StringCasesTestCase test_cases[] = {
  1062. /* clang-format off */
  1063. { "2D", "2d", "2d", "2d" },
  1064. { "2d", "2d", "2d", "2d" },
  1065. { "2db", "2Db", "2Db", "2_db" },
  1066. { "Vector3", "vector3", "Vector3", "vector_3" },
  1067. { "sha256", "sha256", "Sha256", "sha_256" },
  1068. { "Node2D", "node2d", "Node2d", "node_2d" },
  1069. { "RichTextLabel", "richTextLabel", "RichTextLabel", "rich_text_label" },
  1070. { "HTML5", "html5", "Html5", "html_5" },
  1071. { "Node2DPosition", "node2dPosition", "Node2dPosition", "node_2d_position" },
  1072. { "Number2Digits", "number2Digits", "Number2Digits", "number_2_digits" },
  1073. { "get_property_list", "getPropertyList", "GetPropertyList", "get_property_list" },
  1074. { "get_camera_2d", "getCamera2d", "GetCamera2d", "get_camera_2d" },
  1075. { "_physics_process", "physicsProcess", "PhysicsProcess", "_physics_process" },
  1076. { "bytes2var", "bytes2Var", "Bytes2Var", "bytes_2_var" },
  1077. { "linear2db", "linear2Db", "Linear2Db", "linear_2_db" },
  1078. { "sha256sum", "sha256Sum", "Sha256Sum", "sha_256_sum" },
  1079. { "camelCase", "camelCase", "CamelCase", "camel_case" },
  1080. { "PascalCase", "pascalCase", "PascalCase", "pascal_case" },
  1081. { "snake_case", "snakeCase", "SnakeCase", "snake_case" },
  1082. { "Test TEST test", "testTestTest", "TestTestTest", "test_test_test" },
  1083. { nullptr, nullptr, nullptr, nullptr },
  1084. /* clang-format on */
  1085. };
  1086. int idx = 0;
  1087. while (test_cases[idx].input != nullptr) {
  1088. String input = test_cases[idx].input;
  1089. CHECK(input.to_camel_case() == test_cases[idx].camel_case);
  1090. CHECK(input.to_pascal_case() == test_cases[idx].pascal_case);
  1091. CHECK(input.to_snake_case() == test_cases[idx].snake_case);
  1092. idx++;
  1093. }
  1094. }
  1095. TEST_CASE("[String] Checking string is empty when it should be") {
  1096. bool state = true;
  1097. bool success;
  1098. String a = "";
  1099. success = a[0] == 0;
  1100. if (!success) {
  1101. state = false;
  1102. }
  1103. String b = "Godot";
  1104. success = b[b.size()] == 0;
  1105. if (!success) {
  1106. state = false;
  1107. }
  1108. const String c = "";
  1109. success = c[0] == 0;
  1110. if (!success) {
  1111. state = false;
  1112. }
  1113. const String d = "Godot";
  1114. success = d[d.size()] == 0;
  1115. if (!success) {
  1116. state = false;
  1117. }
  1118. CHECK(state);
  1119. }
  1120. TEST_CASE("[String] lstrip and rstrip") {
  1121. #define STRIP_TEST(x) \
  1122. { \
  1123. bool success = x; \
  1124. state = state && success; \
  1125. }
  1126. bool state = true;
  1127. // strip none
  1128. STRIP_TEST(String("abc").lstrip("") == "abc");
  1129. STRIP_TEST(String("abc").rstrip("") == "abc");
  1130. // strip one
  1131. STRIP_TEST(String("abc").lstrip("a") == "bc");
  1132. STRIP_TEST(String("abc").rstrip("c") == "ab");
  1133. // strip lots
  1134. STRIP_TEST(String("bababbababccc").lstrip("ab") == "ccc");
  1135. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("cb") == "aaa");
  1136. // strip empty string
  1137. STRIP_TEST(String("").lstrip("") == "");
  1138. STRIP_TEST(String("").rstrip("") == "");
  1139. // strip to empty string
  1140. STRIP_TEST(String("abcabcabc").lstrip("bca") == "");
  1141. STRIP_TEST(String("abcabcabc").rstrip("bca") == "");
  1142. // don't strip wrong end
  1143. STRIP_TEST(String("abc").lstrip("c") == "abc");
  1144. STRIP_TEST(String("abca").lstrip("a") == "bca");
  1145. STRIP_TEST(String("abc").rstrip("a") == "abc");
  1146. STRIP_TEST(String("abca").rstrip("a") == "abc");
  1147. // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5)
  1148. // and the same second as "ÿ" (\u00ff)
  1149. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("µÿ")) == String::utf8("¿"));
  1150. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("µÿ")) == String::utf8("¿"));
  1151. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("µÿ")) == String::utf8("¿ÿ"));
  1152. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("µÿ")) == String::utf8("µ¿"));
  1153. // the above tests repeated with additional superfluous strip chars
  1154. // strip none
  1155. STRIP_TEST(String("abc").lstrip("qwjkl") == "abc");
  1156. STRIP_TEST(String("abc").rstrip("qwjkl") == "abc");
  1157. // strip one
  1158. STRIP_TEST(String("abc").lstrip("qwajkl") == "bc");
  1159. STRIP_TEST(String("abc").rstrip("qwcjkl") == "ab");
  1160. // strip lots
  1161. STRIP_TEST(String("bababbababccc").lstrip("qwabjkl") == "ccc");
  1162. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("qwcbjkl") == "aaa");
  1163. // strip empty string
  1164. STRIP_TEST(String("").lstrip("qwjkl") == "");
  1165. STRIP_TEST(String("").rstrip("qwjkl") == "");
  1166. // strip to empty string
  1167. STRIP_TEST(String("abcabcabc").lstrip("qwbcajkl") == "");
  1168. STRIP_TEST(String("abcabcabc").rstrip("qwbcajkl") == "");
  1169. // don't strip wrong end
  1170. STRIP_TEST(String("abc").lstrip("qwcjkl") == "abc");
  1171. STRIP_TEST(String("abca").lstrip("qwajkl") == "bca");
  1172. STRIP_TEST(String("abc").rstrip("qwajkl") == "abc");
  1173. STRIP_TEST(String("abca").rstrip("qwajkl") == "abc");
  1174. // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5)
  1175. // and the same second as "ÿ" (\u00ff)
  1176. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  1177. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  1178. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿ÿ"));
  1179. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("µ¿"));
  1180. CHECK(state);
  1181. #undef STRIP_TEST
  1182. }
  1183. TEST_CASE("[String] Ensuring empty string into parse_utf8 passes empty string") {
  1184. String empty;
  1185. CHECK(empty.parse_utf8(nullptr, -1) == ERR_INVALID_DATA);
  1186. }
  1187. TEST_CASE("[String] Cyrillic to_lower()") {
  1188. String upper = U"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
  1189. String lower = U"абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
  1190. String test = upper.to_lower();
  1191. bool state = test == lower;
  1192. CHECK(state);
  1193. }
  1194. TEST_CASE("[String] Count and countn functionality") {
  1195. #define COUNT_TEST(x) \
  1196. { \
  1197. bool success = x; \
  1198. state = state && success; \
  1199. }
  1200. bool state = true;
  1201. COUNT_TEST(String("").count("Test") == 0);
  1202. COUNT_TEST(String("Test").count("") == 0);
  1203. COUNT_TEST(String("Test").count("test") == 0);
  1204. COUNT_TEST(String("Test").count("TEST") == 0);
  1205. COUNT_TEST(String("TEST").count("TEST") == 1);
  1206. COUNT_TEST(String("Test").count("Test") == 1);
  1207. COUNT_TEST(String("aTest").count("Test") == 1);
  1208. COUNT_TEST(String("Testa").count("Test") == 1);
  1209. COUNT_TEST(String("TestTestTest").count("Test") == 3);
  1210. COUNT_TEST(String("TestTestTest").count("TestTest") == 1);
  1211. COUNT_TEST(String("TestGodotTestGodotTestGodot").count("Test") == 3);
  1212. COUNT_TEST(String("TestTestTestTest").count("Test", 4, 8) == 1);
  1213. COUNT_TEST(String("TestTestTestTest").count("Test", 4, 12) == 2);
  1214. COUNT_TEST(String("TestTestTestTest").count("Test", 4, 16) == 3);
  1215. COUNT_TEST(String("TestTestTestTest").count("Test", 4) == 3);
  1216. COUNT_TEST(String("Test").countn("test") == 1);
  1217. COUNT_TEST(String("Test").countn("TEST") == 1);
  1218. COUNT_TEST(String("testTest-Testatest").countn("tEst") == 4);
  1219. COUNT_TEST(String("testTest-TeStatest").countn("tEsT", 4, 16) == 2);
  1220. CHECK(state);
  1221. #undef COUNT_TEST
  1222. }
  1223. TEST_CASE("[String] Bigrams") {
  1224. String s = "abcd";
  1225. Vector<String> bigr = s.bigrams();
  1226. CHECK(bigr.size() == 3);
  1227. CHECK(bigr[0] == "ab");
  1228. CHECK(bigr[1] == "bc");
  1229. CHECK(bigr[2] == "cd");
  1230. }
  1231. TEST_CASE("[String] c-escape/unescape") {
  1232. String s = "\\1\a2\b\f3\n45\r6\t7\v8\'9\?0\"";
  1233. CHECK(s.c_escape().c_unescape() == s);
  1234. }
  1235. TEST_CASE("[String] indent") {
  1236. static const char *input[] = {
  1237. "",
  1238. "aaa\nbbb",
  1239. "\tcontains\n\tindent",
  1240. "empty\n\nline",
  1241. };
  1242. static const char *expected[] = {
  1243. "",
  1244. "\taaa\n\tbbb",
  1245. "\t\tcontains\n\t\tindent",
  1246. "\tempty\n\n\tline",
  1247. };
  1248. for (int i = 0; i < 3; i++) {
  1249. CHECK(String(input[i]).indent("\t") == expected[i]);
  1250. }
  1251. }
  1252. TEST_CASE("[String] dedent") {
  1253. String s = " aaa\n bbb";
  1254. String t = "aaa\nbbb";
  1255. CHECK(s.dedent() == t);
  1256. }
  1257. TEST_CASE("[String] Path functions") {
  1258. static const char *path[8] = { "C:\\Godot\\project\\test.tscn", "/Godot/project/test.xscn", "../Godot/project/test.scn", "Godot\\test.doc", "C:\\test.", "res://test", "user://test", "/.test" };
  1259. static const char *base_dir[8] = { "C:\\Godot\\project", "/Godot/project", "../Godot/project", "Godot", "C:\\", "res://", "user://", "/" };
  1260. static const char *base_name[8] = { "C:\\Godot\\project\\test", "/Godot/project/test", "../Godot/project/test", "Godot\\test", "C:\\test", "res://test", "user://test", "/" };
  1261. static const char *ext[8] = { "tscn", "xscn", "scn", "doc", "", "", "", "test" };
  1262. static const char *file[8] = { "test.tscn", "test.xscn", "test.scn", "test.doc", "test.", "test", "test", ".test" };
  1263. static const char *simplified[8] = { "C:/Godot/project/test.tscn", "/Godot/project/test.xscn", "Godot/project/test.scn", "Godot/test.doc", "C:/test.", "res://test", "user://test", "/.test" };
  1264. static const bool abs[8] = { true, true, false, false, true, true, true, true };
  1265. for (int i = 0; i < 8; i++) {
  1266. CHECK(String(path[i]).get_base_dir() == base_dir[i]);
  1267. CHECK(String(path[i]).get_basename() == base_name[i]);
  1268. CHECK(String(path[i]).get_extension() == ext[i]);
  1269. CHECK(String(path[i]).get_file() == file[i]);
  1270. CHECK(String(path[i]).is_absolute_path() == abs[i]);
  1271. CHECK(String(path[i]).is_relative_path() != abs[i]);
  1272. CHECK(String(path[i]).simplify_path() == String(simplified[i]));
  1273. CHECK(String(path[i]).simplify_path().get_base_dir().path_join(file[i]) == String(path[i]).simplify_path());
  1274. }
  1275. static const char *file_name[3] = { "test.tscn", "test://.xscn", "?tes*t.scn" };
  1276. static const bool valid[3] = { true, false, false };
  1277. for (int i = 0; i < 3; i++) {
  1278. CHECK(String(file_name[i]).is_valid_filename() == valid[i]);
  1279. }
  1280. }
  1281. TEST_CASE("[String] hash") {
  1282. String a = "Test";
  1283. String b = "Test";
  1284. String c = "West";
  1285. CHECK(a.hash() == b.hash());
  1286. CHECK(a.hash() != c.hash());
  1287. CHECK(a.hash64() == b.hash64());
  1288. CHECK(a.hash64() != c.hash64());
  1289. }
  1290. TEST_CASE("[String] uri_encode/unescape") {
  1291. String s = "Godot Engine:'docs'";
  1292. String t = "Godot%20Engine%3A%27docs%27";
  1293. String x1 = "T%C4%93%C5%A1t";
  1294. static const uint8_t u8str[] = { 0x54, 0xC4, 0x93, 0xC5, 0xA1, 0x74, 0x00 };
  1295. String x2 = String::utf8((const char *)u8str);
  1296. String x3 = U"Tēšt";
  1297. CHECK(x1.uri_decode() == x2);
  1298. CHECK(x1.uri_decode() == x3);
  1299. CHECK((x1 + x3).uri_decode() == (x2 + x3)); // Mixed unicode and URL encoded string, e.g. GTK+ bookmark.
  1300. CHECK(x2.uri_encode() == x1);
  1301. CHECK(x3.uri_encode() == x1);
  1302. CHECK(s.uri_encode() == t);
  1303. CHECK(t.uri_decode() == s);
  1304. }
  1305. TEST_CASE("[String] xml_escape/unescape") {
  1306. String s = "\"Test\" <test@test&'test'>";
  1307. CHECK(s.xml_escape(true).xml_unescape() == s);
  1308. CHECK(s.xml_escape(false).xml_unescape() == s);
  1309. }
  1310. TEST_CASE("[String] xml_unescape") {
  1311. // Named entities
  1312. String input = "&quot;&amp;&apos;&lt;&gt;";
  1313. CHECK(input.xml_unescape() == "\"&\'<>");
  1314. // Numeric entities
  1315. input = "&#x41;&#66;";
  1316. CHECK(input.xml_unescape() == "AB");
  1317. input = "&#0;&x#0;More text";
  1318. String result = input.xml_unescape();
  1319. // Didn't put in a leading NUL and terminate the string
  1320. CHECK(input.length() > 0);
  1321. CHECK(input[0] != '\0');
  1322. // Entity should be left as-is if invalid
  1323. CHECK(input.xml_unescape() == input);
  1324. // Check near char32_t range
  1325. input = "&#xFFFFFFFF;";
  1326. result = input.xml_unescape();
  1327. CHECK(result.length() == 1);
  1328. CHECK(result[0] == 0xFFFFFFFF);
  1329. input = "&#4294967295;";
  1330. result = input.xml_unescape();
  1331. CHECK(result.length() == 1);
  1332. CHECK(result[0] == 0xFFFFFFFF);
  1333. // Check out of range of char32_t
  1334. input = "&#xFFFFFFFFF;";
  1335. CHECK(input.xml_unescape() == input);
  1336. input = "&#4294967296;";
  1337. CHECK(input.xml_unescape() == input);
  1338. // Shouldn't consume without ending in a ';'
  1339. input = "&#66";
  1340. CHECK(input.xml_unescape() == input);
  1341. input = "&#x41";
  1342. CHECK(input.xml_unescape() == input);
  1343. // Invalid characters should make the entity ignored
  1344. input = "&#x41SomeIrrelevantText;";
  1345. CHECK(input.xml_unescape() == input);
  1346. input = "&#66SomeIrrelevantText;";
  1347. CHECK(input.xml_unescape() == input);
  1348. }
  1349. TEST_CASE("[String] Strip escapes") {
  1350. String s = "\t\tTest Test\r\n Test";
  1351. CHECK(s.strip_escapes() == "Test Test Test");
  1352. }
  1353. TEST_CASE("[String] Similarity") {
  1354. String a = "Test";
  1355. String b = "West";
  1356. String c = "Toad";
  1357. CHECK(a.similarity(b) > a.similarity(c));
  1358. }
  1359. TEST_CASE("[String] Strip edges") {
  1360. String s = "\t Test Test ";
  1361. CHECK(s.strip_edges(true, false) == "Test Test ");
  1362. CHECK(s.strip_edges(false, true) == "\t Test Test");
  1363. CHECK(s.strip_edges(true, true) == "Test Test");
  1364. }
  1365. TEST_CASE("[String] Trim") {
  1366. String s = "aaaTestbbb";
  1367. CHECK(s.trim_prefix("aaa") == "Testbbb");
  1368. CHECK(s.trim_suffix("bbb") == "aaaTest");
  1369. CHECK(s.trim_suffix("Test") == s);
  1370. }
  1371. TEST_CASE("[String] Right/Left") {
  1372. String s = "aaaTestbbb";
  1373. // ^
  1374. CHECK(s.right(6) == "estbbb");
  1375. CHECK(s.right(-6) == "tbbb");
  1376. CHECK(s.left(6) == "aaaTes");
  1377. CHECK(s.left(-6) == "aaaT");
  1378. }
  1379. TEST_CASE("[String] Repeat") {
  1380. String s = "abababab";
  1381. String x = "ab";
  1382. String t = x.repeat(4);
  1383. CHECK(t == s);
  1384. }
  1385. TEST_CASE("[String] Reverse") {
  1386. String s = "Abcd";
  1387. CHECK(s.reverse() == "dcbA");
  1388. }
  1389. TEST_CASE("[String] SHA1/SHA256/MD5") {
  1390. String s = "Godot";
  1391. String sha1 = "a1e91f39b9fce6a9998b14bdbe2aa2b39dc2d201";
  1392. static uint8_t sha1_buf[20] = {
  1393. 0xA1, 0xE9, 0x1F, 0x39, 0xB9, 0xFC, 0xE6, 0xA9, 0x99, 0x8B, 0x14, 0xBD, 0xBE, 0x2A, 0xA2, 0xB3,
  1394. 0x9D, 0xC2, 0xD2, 0x01
  1395. };
  1396. String sha256 = "2a02b2443f7985d89d09001086ae3dcfa6eb0f55c6ef170715d42328e16e6cb8";
  1397. static uint8_t sha256_buf[32] = {
  1398. 0x2A, 0x02, 0xB2, 0x44, 0x3F, 0x79, 0x85, 0xD8, 0x9D, 0x09, 0x00, 0x10, 0x86, 0xAE, 0x3D, 0xCF,
  1399. 0xA6, 0xEB, 0x0F, 0x55, 0xC6, 0xEF, 0x17, 0x07, 0x15, 0xD4, 0x23, 0x28, 0xE1, 0x6E, 0x6C, 0xB8
  1400. };
  1401. String md5 = "4a336d087aeb0390da10ee2ea7cb87f8";
  1402. static uint8_t md5_buf[16] = {
  1403. 0x4A, 0x33, 0x6D, 0x08, 0x7A, 0xEB, 0x03, 0x90, 0xDA, 0x10, 0xEE, 0x2E, 0xA7, 0xCB, 0x87, 0xF8
  1404. };
  1405. PackedByteArray buf = s.sha1_buffer();
  1406. CHECK(memcmp(sha1_buf, buf.ptr(), 20) == 0);
  1407. CHECK(s.sha1_text() == sha1);
  1408. buf = s.sha256_buffer();
  1409. CHECK(memcmp(sha256_buf, buf.ptr(), 32) == 0);
  1410. CHECK(s.sha256_text() == sha256);
  1411. buf = s.md5_buffer();
  1412. CHECK(memcmp(md5_buf, buf.ptr(), 16) == 0);
  1413. CHECK(s.md5_text() == md5);
  1414. }
  1415. TEST_CASE("[String] Join") {
  1416. String s = ", ";
  1417. Vector<String> parts;
  1418. parts.push_back("One");
  1419. parts.push_back("B");
  1420. parts.push_back("C");
  1421. String t = s.join(parts);
  1422. CHECK(t == "One, B, C");
  1423. }
  1424. TEST_CASE("[String] Is_*") {
  1425. static const char *data[12] = { "-30", "100", "10.1", "10,1", "1e2", "1e-2", "1e2e3", "0xAB", "AB", "Test1", "1Test", "Test*1" };
  1426. static bool isnum[12] = { true, true, true, false, false, false, false, false, false, false, false, false };
  1427. static bool isint[12] = { true, true, false, false, false, false, false, false, false, false, false, false };
  1428. static bool ishex[12] = { true, true, false, false, true, false, true, false, true, false, false, false };
  1429. static bool ishex_p[12] = { false, false, false, false, false, false, false, true, false, false, false, false };
  1430. static bool isflt[12] = { true, true, true, false, true, true, false, false, false, false, false, false };
  1431. static bool isid[12] = { false, false, false, false, false, false, false, false, true, true, false, false };
  1432. for (int i = 0; i < 12; i++) {
  1433. String s = String(data[i]);
  1434. CHECK(s.is_numeric() == isnum[i]);
  1435. CHECK(s.is_valid_int() == isint[i]);
  1436. CHECK(s.is_valid_hex_number(false) == ishex[i]);
  1437. CHECK(s.is_valid_hex_number(true) == ishex_p[i]);
  1438. CHECK(s.is_valid_float() == isflt[i]);
  1439. CHECK(s.is_valid_identifier() == isid[i]);
  1440. }
  1441. }
  1442. TEST_CASE("[String] humanize_size") {
  1443. CHECK(String::humanize_size(1000) == "1000 B");
  1444. CHECK(String::humanize_size(1025) == "1.00 KiB");
  1445. CHECK(String::humanize_size(1025300) == "1001.2 KiB");
  1446. CHECK(String::humanize_size(100523550) == "95.86 MiB");
  1447. CHECK(String::humanize_size(5345555000) == "4.97 GiB");
  1448. }
  1449. TEST_CASE("[String] validate_node_name") {
  1450. String numeric_only = "12345";
  1451. CHECK(numeric_only.validate_node_name() == "12345");
  1452. String name_with_spaces = "Name with spaces";
  1453. CHECK(name_with_spaces.validate_node_name() == "Name with spaces");
  1454. String name_with_kana = U"Name with kana ゴドツ";
  1455. CHECK(name_with_kana.validate_node_name() == U"Name with kana ゴドツ");
  1456. String name_with_invalid_chars = "Name with invalid characters :.@%removed!";
  1457. CHECK(name_with_invalid_chars.validate_node_name() == "Name with invalid characters ____removed!");
  1458. }
  1459. TEST_CASE("[String] validate_identifier") {
  1460. String empty_string;
  1461. CHECK(empty_string.validate_identifier() == "_");
  1462. String numeric_only = "12345";
  1463. CHECK(numeric_only.validate_identifier() == "_12345");
  1464. String name_with_spaces = "Name with spaces";
  1465. CHECK(name_with_spaces.validate_identifier() == "Name_with_spaces");
  1466. String name_with_invalid_chars = U"Invalid characters:@*#&世界";
  1467. CHECK(name_with_invalid_chars.validate_identifier() == "Invalid_characters_______");
  1468. }
  1469. TEST_CASE("[String] Variant indexed get") {
  1470. Variant s = String("abcd");
  1471. bool valid = false;
  1472. bool oob = true;
  1473. String r = s.get_indexed(1, valid, oob);
  1474. CHECK(valid);
  1475. CHECK_FALSE(oob);
  1476. CHECK_EQ(r, String("b"));
  1477. }
  1478. TEST_CASE("[String] Variant validated indexed get") {
  1479. Variant s = String("abcd");
  1480. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(Variant::STRING);
  1481. Variant r;
  1482. bool oob = true;
  1483. getter(&s, 1, &r, &oob);
  1484. CHECK_FALSE(oob);
  1485. CHECK_EQ(r, String("b"));
  1486. }
  1487. TEST_CASE("[String] Variant ptr indexed get") {
  1488. String s("abcd");
  1489. Variant::PTRIndexedGetter getter = Variant::get_member_ptr_indexed_getter(Variant::STRING);
  1490. String r;
  1491. getter(&s, 1, &r);
  1492. CHECK_EQ(r, String("b"));
  1493. }
  1494. TEST_CASE("[String] Variant indexed set") {
  1495. Variant s = String("abcd");
  1496. bool valid = false;
  1497. bool oob = true;
  1498. s.set_indexed(1, String("z"), valid, oob);
  1499. CHECK(valid);
  1500. CHECK_FALSE(oob);
  1501. CHECK_EQ(s, String("azcd"));
  1502. }
  1503. TEST_CASE("[String] Variant validated indexed set") {
  1504. Variant s = String("abcd");
  1505. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(Variant::STRING);
  1506. Variant v = String("z");
  1507. bool oob = true;
  1508. setter(&s, 1, &v, &oob);
  1509. CHECK_FALSE(oob);
  1510. CHECK_EQ(s, String("azcd"));
  1511. }
  1512. TEST_CASE("[String] Variant ptr indexed set") {
  1513. String s("abcd");
  1514. Variant::PTRIndexedSetter setter = Variant::get_member_ptr_indexed_setter(Variant::STRING);
  1515. String v("z");
  1516. setter(&s, 1, &v);
  1517. CHECK_EQ(s, String("azcd"));
  1518. }
  1519. TEST_CASE("[Stress][String] Empty via ' == String()'") {
  1520. for (int i = 0; i < 100000; ++i) {
  1521. String str = "Hello World!";
  1522. if (str == String()) {
  1523. continue;
  1524. }
  1525. }
  1526. }
  1527. TEST_CASE("[Stress][String] Empty via `is_empty()`") {
  1528. for (int i = 0; i < 100000; ++i) {
  1529. String str = "Hello World!";
  1530. if (str.is_empty()) {
  1531. continue;
  1532. }
  1533. }
  1534. }
  1535. } // namespace TestString
  1536. #endif // TEST_STRING_H