test_string.h 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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] Contains case insensitive") {
  252. String s = "C:\\Godot\\project\\string_test.tscn";
  253. CHECK(s.containsn("Godot"));
  254. CHECK(s.containsn("godot"));
  255. CHECK(s.containsn(String("Project\\string_test")));
  256. CHECK(s.containsn(String("\\string_Test.tscn")));
  257. CHECK(!s.containsn("Godoh"));
  258. CHECK(!s.containsn("godoh"));
  259. CHECK(!s.containsn(String("project\\string test")));
  260. CHECK(!s.containsn(String("\\char_test.tscn")));
  261. }
  262. TEST_CASE("[String] Test chr") {
  263. CHECK(String::chr('H') == "H");
  264. CHECK(String::chr(0x3012)[0] == 0x3012);
  265. ERR_PRINT_OFF
  266. CHECK(String::chr(0xd812)[0] == 0xfffd); // Unpaired UTF-16 surrogate
  267. CHECK(String::chr(0x20d812)[0] == 0xfffd); // Outside UTF-32 range
  268. ERR_PRINT_ON
  269. }
  270. TEST_CASE("[String] Operator []") {
  271. String a = "Kugar Sane";
  272. a[0] = 'S';
  273. a[6] = 'C';
  274. CHECK(a == "Sugar Cane");
  275. CHECK(a[1] == 'u');
  276. CHECK(a.unicode_at(1) == 'u');
  277. }
  278. TEST_CASE("[String] Case function test") {
  279. String a = "MoMoNgA";
  280. CHECK(a.to_upper() == "MOMONGA");
  281. CHECK(a.to_lower() == "momonga");
  282. }
  283. TEST_CASE("[String] Case compare function test") {
  284. String a = "MoMoNgA";
  285. CHECK(a.casecmp_to("momonga") != 0);
  286. CHECK(a.nocasecmp_to("momonga") == 0);
  287. }
  288. TEST_CASE("[String] Natural compare function test") {
  289. String a = "img2.png";
  290. CHECK(a.nocasecmp_to("img10.png") > 0);
  291. CHECK(a.naturalnocasecmp_to("img10.png") < 0);
  292. }
  293. TEST_CASE("[String] File compare function test") {
  294. String a = "_img2.png";
  295. String b = "img2.png";
  296. CHECK(a.nocasecmp_to("img10.png") > 0);
  297. CHECK_MESSAGE(a.filenocasecmp_to("img10.png") < 0, "Should sort before letters.");
  298. CHECK_MESSAGE(a.filenocasecmp_to(".img10.png") > 0, "Should sort after period.");
  299. CHECK(b.filenocasecmp_to("img3.png") < 0);
  300. }
  301. TEST_CASE("[String] hex_encode_buffer") {
  302. static const uint8_t u8str[] = { 0x45, 0xE3, 0x81, 0x8A, 0x8F, 0xE3 };
  303. String s = String::hex_encode_buffer(u8str, 6);
  304. CHECK(s == U"45e3818a8fe3");
  305. }
  306. TEST_CASE("[String] Substr") {
  307. String s = "Killer Baby";
  308. CHECK(s.substr(3, 4) == "ler ");
  309. CHECK(s.substr(3) == "ler Baby");
  310. }
  311. TEST_CASE("[String] Find") {
  312. String s = "Pretty Woman Woman";
  313. MULTICHECK_STRING_EQ(s, find, "tty", 3);
  314. MULTICHECK_STRING_EQ(s, find, "Revenge of the Monster Truck", -1);
  315. MULTICHECK_STRING_INT_EQ(s, find, "Wo", 9, 13);
  316. MULTICHECK_STRING_EQ(s, find, "", -1);
  317. MULTICHECK_STRING_EQ(s, find, "Pretty Woman Woman", 0);
  318. MULTICHECK_STRING_EQ(s, find, "WOMAN", -1);
  319. MULTICHECK_STRING_INT_EQ(s, find, "", 9, -1);
  320. MULTICHECK_STRING_EQ(s, rfind, "", -1);
  321. MULTICHECK_STRING_EQ(s, rfind, "foo", -1);
  322. MULTICHECK_STRING_EQ(s, rfind, "Pretty Woman Woman", 0);
  323. MULTICHECK_STRING_EQ(s, rfind, "man", 15);
  324. MULTICHECK_STRING_EQ(s, rfind, "WOMAN", -1);
  325. MULTICHECK_STRING_INT_EQ(s, rfind, "", 15, -1);
  326. }
  327. TEST_CASE("[String] Find case insensitive") {
  328. String s = "Pretty Whale Whale";
  329. MULTICHECK_STRING_EQ(s, findn, "WHA", 7);
  330. MULTICHECK_STRING_INT_EQ(s, findn, "WHA", 9, 13);
  331. MULTICHECK_STRING_EQ(s, findn, "Revenge of the Monster SawFish", -1);
  332. MULTICHECK_STRING_EQ(s, findn, "", -1);
  333. MULTICHECK_STRING_EQ(s, findn, "wha", 7);
  334. MULTICHECK_STRING_EQ(s, findn, "Wha", 7);
  335. MULTICHECK_STRING_INT_EQ(s, findn, "", 3, -1);
  336. MULTICHECK_STRING_EQ(s, rfindn, "WHA", 13);
  337. MULTICHECK_STRING_EQ(s, rfindn, "", -1);
  338. MULTICHECK_STRING_EQ(s, rfindn, "wha", 13);
  339. MULTICHECK_STRING_EQ(s, rfindn, "Wha", 13);
  340. MULTICHECK_STRING_INT_EQ(s, rfindn, "", 13, -1);
  341. }
  342. TEST_CASE("[String] Find MK") {
  343. Vector<String> keys;
  344. keys.push_back("sty");
  345. keys.push_back("tty");
  346. keys.push_back("man");
  347. String s = "Pretty Woman";
  348. int key = 0;
  349. CHECK(s.findmk(keys, 0, &key) == 3);
  350. CHECK(key == 1);
  351. CHECK(s.findmk(keys, 5, &key) == 9);
  352. CHECK(key == 2);
  353. }
  354. TEST_CASE("[String] Find and replace") {
  355. String s = "Happy Birthday, Anna!";
  356. MULTICHECK_STRING_STRING_EQ(s, replace, "Birthday", "Halloween", "Happy Halloween, Anna!");
  357. MULTICHECK_STRING_STRING_EQ(s, replace_first, "y", "Y", "HappY Birthday, Anna!");
  358. MULTICHECK_STRING_STRING_EQ(s, replacen, "Y", "Y", "HappY BirthdaY, Anna!");
  359. }
  360. TEST_CASE("[String] Insertion") {
  361. String s = "Who is Frederic?";
  362. s = s.insert(s.find("?"), " Chopin");
  363. CHECK(s == "Who is Frederic Chopin?");
  364. s = "foobar";
  365. CHECK(s.insert(0, "X") == "Xfoobar");
  366. CHECK(s.insert(-100, "X") == "foobar");
  367. CHECK(s.insert(6, "X") == "foobarX");
  368. CHECK(s.insert(100, "X") == "foobarX");
  369. CHECK(s.insert(2, "") == "foobar");
  370. s = "";
  371. CHECK(s.insert(0, "abc") == "abc");
  372. CHECK(s.insert(100, "abc") == "abc");
  373. CHECK(s.insert(-100, "abc") == "");
  374. CHECK(s.insert(0, "") == "");
  375. }
  376. TEST_CASE("[String] Erasing") {
  377. String s = "Josephine is such a cute girl!";
  378. s = s.erase(s.find("cute "), String("cute ").length());
  379. CHECK(s == "Josephine is such a girl!");
  380. }
  381. TEST_CASE("[String] Number to string") {
  382. CHECK(String::num(0) == "0");
  383. CHECK(String::num(0.0) == "0"); // No trailing zeros.
  384. CHECK(String::num(-0.0) == "-0"); // Includes sign even for zero.
  385. CHECK(String::num(3.141593) == "3.141593");
  386. CHECK(String::num(3.141593, 3) == "3.142");
  387. CHECK(String::num_scientific(30000000) == "3e+07");
  388. CHECK(String::num_int64(3141593) == "3141593");
  389. CHECK(String::num_int64(0xA141593, 16) == "a141593");
  390. CHECK(String::num_int64(0xA141593, 16, true) == "A141593");
  391. CHECK(String::num(42.100023, 4) == "42.1"); // No trailing zeros.
  392. // String::num_real tests.
  393. CHECK(String::num_real(1.0) == "1.0");
  394. CHECK(String::num_real(1.0, false) == "1");
  395. CHECK(String::num_real(9.9) == "9.9");
  396. CHECK(String::num_real(9.99) == "9.99");
  397. CHECK(String::num_real(9.999) == "9.999");
  398. CHECK(String::num_real(9.9999) == "9.9999");
  399. CHECK(String::num_real(3.141593) == "3.141593");
  400. CHECK(String::num_real(3.141) == "3.141"); // No trailing zeros.
  401. #ifdef REAL_T_IS_DOUBLE
  402. CHECK_MESSAGE(String::num_real(123.456789) == "123.456789", "Prints the appropriate amount of digits for real_t = double.");
  403. CHECK_MESSAGE(String::num_real(-123.456789) == "-123.456789", "Prints the appropriate amount of digits for real_t = double.");
  404. CHECK_MESSAGE(String::num_real(Math_PI) == "3.14159265358979", "Prints the appropriate amount of digits for real_t = double.");
  405. 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.");
  406. #else
  407. CHECK_MESSAGE(String::num_real(123.456789) == "123.4568", "Prints the appropriate amount of digits for real_t = float.");
  408. CHECK_MESSAGE(String::num_real(-123.456789) == "-123.4568", "Prints the appropriate amount of digits for real_t = float.");
  409. CHECK_MESSAGE(String::num_real(Math_PI) == "3.141593", "Prints the appropriate amount of digits for real_t = float.");
  410. CHECK_MESSAGE(String::num_real(3.1415f) == "3.1415", "Prints only reliable digits of 32-bit float when real_t = float.");
  411. #endif // REAL_T_IS_DOUBLE
  412. // Checks doubles with many decimal places.
  413. CHECK(String::num(0.0000012345432123454321, -1) == "0.00000123454321"); // -1 uses 14 as sane default.
  414. CHECK(String::num(0.0000012345432123454321) == "0.00000123454321"); // -1 is the default value.
  415. CHECK(String::num(-0.0000012345432123454321) == "-0.00000123454321");
  416. CHECK(String::num(-10000.0000012345432123454321) == "-10000.0000012345");
  417. CHECK(String::num(0.0000000000012345432123454321) == "0.00000000000123");
  418. CHECK(String::num(0.0000000000012345432123454321, 3) == "0");
  419. // Note: When relevant (remainder > 0.5), the last digit gets rounded up,
  420. // which can also lead to not include a trailing zero, e.g. "...89" -> "...9".
  421. CHECK(String::num(0.0000056789876567898765) == "0.00000567898766"); // Should round last digit.
  422. CHECK(String::num(10000.000005678999999999) == "10000.000005679"); // We cut at ...789|99 which is rounded to ...79, so only 13 decimals.
  423. CHECK(String::num(42.12999999, 6) == "42.13"); // Also happens with lower decimals count.
  424. // 32 is MAX_DECIMALS. We can't reliably store that many so we can't compare against a string,
  425. // but we can check that the string length is 34 (32 + 2 for "0.").
  426. CHECK(String::num(0.00000123456789987654321123456789987654321, 32).length() == 34);
  427. CHECK(String::num(0.00000123456789987654321123456789987654321, 42).length() == 34); // Should enforce MAX_DECIMALS.
  428. CHECK(String::num(10000.00000123456789987654321123456789987654321, 42).length() == 38); // 32 decimals + "10000.".
  429. }
  430. TEST_CASE("[String] String to integer") {
  431. static const char *nums[14] = { "1237461283", "- 22", "0", " - 1123412", "", "10_000_000", "-1_2_3_4", "10__000", " 1 2 34 ", "-0", "007", "--45", "---46", "-7-2" };
  432. static const int num[14] = { 1237461283, -22, 0, -1123412, 0, 10000000, -1234, 10000, 1234, 0, 7, 45, -46, -72 };
  433. for (int i = 0; i < 14; i++) {
  434. CHECK(String(nums[i]).to_int() == num[i]);
  435. }
  436. CHECK(String("0b1011").to_int() == 1011); // Looks like a binary number, but to_int() handles this as a base-10 number, "b" is just ignored.
  437. CHECK(String("0x1012").to_int() == 1012); // Looks like a hexadecimal number, but to_int() handles this as a base-10 number, "x" is just ignored.
  438. ERR_PRINT_OFF
  439. CHECK(String("999999999999999999999999999999999999999999999999999999999").to_int() == INT64_MAX); // Too large, largest possible is returned.
  440. CHECK(String("-999999999999999999999999999999999999999999999999999999999").to_int() == INT64_MIN); // Too small, smallest possible is returned.
  441. ERR_PRINT_ON
  442. }
  443. TEST_CASE("[String] Hex to integer") {
  444. static const char *nums[12] = { "0xFFAE", "22", "0", "AADDAD", "0x7FFFFFFFFFFFFFFF", "-0xf", "", "000", "000f", "0xaA", "-ff", "-" };
  445. static const int64_t num[12] = { 0xFFAE, 0x22, 0, 0xAADDAD, 0x7FFFFFFFFFFFFFFF, -0xf, 0, 0, 0xf, 0xaa, -0xff, 0x0 };
  446. for (int i = 0; i < 12; i++) {
  447. CHECK(String(nums[i]).hex_to_int() == num[i]);
  448. }
  449. // Invalid hex strings should return 0.
  450. static const char *invalid_nums[15] = { "qwerty", "QWERTY", "0xqwerty", "0x00qwerty", "qwerty00", "0x", "0x__", "__", "x12", "+", " ff", "ff ", "f f", "+ff", "--0x78" };
  451. ERR_PRINT_OFF
  452. for (int i = 0; i < 15; i++) {
  453. CHECK(String(invalid_nums[i]).hex_to_int() == 0);
  454. }
  455. CHECK(String("0xFFFFFFFFFFFFFFFFFFFFFFF").hex_to_int() == INT64_MAX); // Too large, largest possible is returned.
  456. CHECK(String("-0xFFFFFFFFFFFFFFFFFFFFFFF").hex_to_int() == INT64_MIN); // Too small, smallest possible is returned.
  457. ERR_PRINT_ON
  458. }
  459. TEST_CASE("[String] Bin to integer") {
  460. static const char *nums[10] = { "", "0", "0b0", "0b1", "0b", "1", "0b1010", "-0b11", "-1010", "0b0111111111111111111111111111111111111111111111111111111111111111" };
  461. static const int64_t num[10] = { 0, 0, 0, 1, 0, 1, 10, -3, -10, 0x7FFFFFFFFFFFFFFF };
  462. for (int i = 0; i < 10; i++) {
  463. CHECK(String(nums[i]).bin_to_int() == num[i]);
  464. }
  465. // Invalid bin strings should return 0. The long "0x11...11" is just too long for a 64 bit int.
  466. static const char *invalid_nums[16] = { "qwerty", "QWERTY", "0bqwerty", "0b00qwerty", "qwerty00", "0x__", "0b__", "__", "b12", "+", "-", "0x12ab", " 11", "11 ", "1 1", "--0b11" };
  467. for (int i = 0; i < 16; i++) {
  468. CHECK(String(invalid_nums[i]).bin_to_int() == 0);
  469. }
  470. ERR_PRINT_OFF
  471. CHECK(String("0b111111111111111111111111111111111111111111111111111111111111111111111111111111111").bin_to_int() == INT64_MAX); // Too large, largest possible is returned.
  472. CHECK(String("-0b111111111111111111111111111111111111111111111111111111111111111111111111111111111").bin_to_int() == INT64_MIN); // Too small, smallest possible is returned.
  473. ERR_PRINT_ON
  474. }
  475. TEST_CASE("[String] String to float") {
  476. static const char *nums[12] = { "-12348298412.2", "0.05", "2.0002", " -0.0001", "0", "000", "123", "0.0", "000.000", "000.007", "234__", "3..14" };
  477. static const double num[12] = { -12348298412.2, 0.05, 2.0002, -0.0001, 0.0, 0.0, 123.0, 0.0, 0.0, 0.007, 234.0, 3.0 };
  478. for (int i = 0; i < 12; i++) {
  479. CHECK(!(ABS(String(nums[i]).to_float() - num[i]) > 0.00001));
  480. }
  481. // Invalid float strings should return 0.
  482. static const char *invalid_nums[6] = { "qwerty", "qwerty123", "0xffff", "0b1010", "--3.13", "__345" };
  483. for (int i = 0; i < 6; i++) {
  484. CHECK(String(invalid_nums[i]).to_float() == 0);
  485. }
  486. // Very large exponents.
  487. CHECK(String("1e308").to_float() == 1e308);
  488. CHECK(String("-1e308").to_float() == -1e308);
  489. // Exponent is so high that value is INFINITY/-INFINITY.
  490. CHECK(String("1e309").to_float() == INFINITY);
  491. CHECK(String("1e511").to_float() == INFINITY);
  492. CHECK(String("-1e309").to_float() == -INFINITY);
  493. CHECK(String("-1e511").to_float() == -INFINITY);
  494. // Exponent is so high that a warning message is printed. Value is INFINITY/-INFINITY.
  495. ERR_PRINT_OFF
  496. CHECK(String("1e512").to_float() == INFINITY);
  497. CHECK(String("-1e512").to_float() == -INFINITY);
  498. ERR_PRINT_ON
  499. }
  500. TEST_CASE("[String] Slicing") {
  501. String s = "Mars,Jupiter,Saturn,Uranus";
  502. const char *slices[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
  503. MULTICHECK_GET_SLICE(s, ",", slices);
  504. }
  505. TEST_CASE("[String] Begins with") {
  506. // Test cases for true:
  507. MULTICHECK_STRING_EQ(String("res://foobar"), begins_with, "res://", true);
  508. MULTICHECK_STRING_EQ(String("abc"), begins_with, "abc", true);
  509. MULTICHECK_STRING_EQ(String("abc"), begins_with, "", true);
  510. MULTICHECK_STRING_EQ(String(""), begins_with, "", true);
  511. // Test cases for false:
  512. MULTICHECK_STRING_EQ(String("res"), begins_with, "res://", false);
  513. MULTICHECK_STRING_EQ(String("abcdef"), begins_with, "foo", false);
  514. MULTICHECK_STRING_EQ(String("abc"), begins_with, "ax", false);
  515. MULTICHECK_STRING_EQ(String(""), begins_with, "abc", false);
  516. // Test "const char *" version also with nullptr.
  517. String s("foo");
  518. bool state = s.begins_with(nullptr) == false;
  519. CHECK_MESSAGE(state, "nullptr check failed");
  520. String empty("");
  521. state = empty.begins_with(nullptr) == false;
  522. CHECK_MESSAGE(state, "nullptr check with empty string failed");
  523. }
  524. TEST_CASE("[String] Ends with") {
  525. // Test cases for true:
  526. MULTICHECK_STRING_EQ(String("res://foobar"), ends_with, "foobar", true);
  527. MULTICHECK_STRING_EQ(String("abc"), ends_with, "abc", true);
  528. MULTICHECK_STRING_EQ(String("abc"), ends_with, "", true);
  529. MULTICHECK_STRING_EQ(String(""), ends_with, "", true);
  530. // Test cases for false:
  531. MULTICHECK_STRING_EQ(String("res"), ends_with, "res://", false);
  532. MULTICHECK_STRING_EQ(String("abcdef"), ends_with, "foo", false);
  533. MULTICHECK_STRING_EQ(String("abc"), ends_with, "ax", false);
  534. MULTICHECK_STRING_EQ(String(""), ends_with, "abc", false);
  535. // Test "const char *" version also with nullptr.
  536. String s("foo");
  537. bool state = s.ends_with(nullptr) == false;
  538. CHECK_MESSAGE(state, "nullptr check failed");
  539. String empty("");
  540. state = empty.ends_with(nullptr) == false;
  541. CHECK_MESSAGE(state, "nullptr check with empty string failed");
  542. }
  543. TEST_CASE("[String] Splitting") {
  544. {
  545. const String s = "Mars,Jupiter,Saturn,Uranus";
  546. const char *slices_l[3] = { "Mars", "Jupiter", "Saturn,Uranus" };
  547. MULTICHECK_SPLIT(s, split, ",", true, 2, slices_l, 3);
  548. const char *slices_r[3] = { "Mars,Jupiter", "Saturn", "Uranus" };
  549. MULTICHECK_SPLIT(s, rsplit, ",", true, 2, slices_r, 3);
  550. }
  551. {
  552. const String s = "test";
  553. const char *slices[4] = { "t", "e", "s", "t" };
  554. MULTICHECK_SPLIT(s, split, "", true, 0, slices, 4);
  555. }
  556. {
  557. const String s = "";
  558. const char *slices[1] = { "" };
  559. MULTICHECK_SPLIT(s, split, "", true, 0, slices, 1);
  560. MULTICHECK_SPLIT(s, split, "", false, 0, slices, 0);
  561. }
  562. {
  563. const String s = "Mars Jupiter Saturn Uranus";
  564. const char *slices[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
  565. Vector<String> l = s.split_spaces();
  566. for (int i = 0; i < l.size(); i++) {
  567. CHECK(l[i] == slices[i]);
  568. }
  569. }
  570. {
  571. const String s = "1.2;2.3 4.5";
  572. const double slices[3] = { 1.2, 2.3, 4.5 };
  573. const Vector<double> d_arr = s.split_floats(";");
  574. CHECK(d_arr.size() == 2);
  575. for (int i = 0; i < d_arr.size(); i++) {
  576. CHECK(ABS(d_arr[i] - slices[i]) <= 0.00001);
  577. }
  578. const Vector<String> keys = { ";", " " };
  579. const Vector<float> f_arr = s.split_floats_mk(keys);
  580. CHECK(f_arr.size() == 3);
  581. for (int i = 0; i < f_arr.size(); i++) {
  582. CHECK(ABS(f_arr[i] - slices[i]) <= 0.00001);
  583. }
  584. }
  585. {
  586. const String s = " -2.0 5";
  587. const double slices[10] = { 0, -2, 0, 0, 0, 0, 0, 0, 0, 5 };
  588. const Vector<double> arr = s.split_floats(" ");
  589. CHECK(arr.size() == 10);
  590. for (int i = 0; i < arr.size(); i++) {
  591. CHECK(ABS(arr[i] - slices[i]) <= 0.00001);
  592. }
  593. const Vector<String> keys = { ";", " " };
  594. const Vector<float> mk = s.split_floats_mk(keys);
  595. CHECK(mk.size() == 10);
  596. for (int i = 0; i < mk.size(); i++) {
  597. CHECK(mk[i] == slices[i]);
  598. }
  599. }
  600. {
  601. const String s = "1;2 4";
  602. const int slices[3] = { 1, 2, 4 };
  603. const Vector<int> arr = s.split_ints(";");
  604. CHECK(arr.size() == 2);
  605. for (int i = 0; i < arr.size(); i++) {
  606. CHECK(arr[i] == slices[i]);
  607. }
  608. const Vector<String> keys = { ";", " " };
  609. const Vector<int> mk = s.split_ints_mk(keys);
  610. CHECK(mk.size() == 3);
  611. for (int i = 0; i < mk.size(); i++) {
  612. CHECK(mk[i] == slices[i]);
  613. }
  614. }
  615. }
  616. TEST_CASE("[String] format") {
  617. const String value_format = "red=\"$red\" green=\"$green\" blue=\"$blue\" alpha=\"$alpha\"";
  618. Dictionary value_dictionary;
  619. value_dictionary["red"] = 10;
  620. value_dictionary["green"] = 20;
  621. value_dictionary["blue"] = "bla";
  622. value_dictionary["alpha"] = 0.4;
  623. String value = value_format.format(value_dictionary, "$_");
  624. CHECK(value == "red=\"10\" green=\"20\" blue=\"bla\" alpha=\"0.4\"");
  625. }
  626. TEST_CASE("[String] sprintf") {
  627. String format, output;
  628. Array args;
  629. bool error;
  630. // %%
  631. format = "fish %% frog";
  632. args.clear();
  633. output = format.sprintf(args, &error);
  634. REQUIRE(error == false);
  635. CHECK(output == String("fish % frog"));
  636. ///// Ints
  637. // Int
  638. format = "fish %d frog";
  639. args.clear();
  640. args.push_back(5);
  641. output = format.sprintf(args, &error);
  642. REQUIRE(error == false);
  643. CHECK(output == String("fish 5 frog"));
  644. // Int left padded with zeroes.
  645. format = "fish %05d frog";
  646. args.clear();
  647. args.push_back(5);
  648. output = format.sprintf(args, &error);
  649. REQUIRE(error == false);
  650. CHECK(output == String("fish 00005 frog"));
  651. // Int left padded with spaces.
  652. format = "fish %5d frog";
  653. args.clear();
  654. args.push_back(5);
  655. output = format.sprintf(args, &error);
  656. REQUIRE(error == false);
  657. CHECK(output == String("fish 5 frog"));
  658. // Int right padded with spaces.
  659. format = "fish %-5d frog";
  660. args.clear();
  661. args.push_back(5);
  662. output = format.sprintf(args, &error);
  663. REQUIRE(error == false);
  664. CHECK(output == String("fish 5 frog"));
  665. // Int with sign (positive).
  666. format = "fish %+d frog";
  667. args.clear();
  668. args.push_back(5);
  669. output = format.sprintf(args, &error);
  670. REQUIRE(error == false);
  671. CHECK(output == String("fish +5 frog"));
  672. // Negative int.
  673. format = "fish %d frog";
  674. args.clear();
  675. args.push_back(-5);
  676. output = format.sprintf(args, &error);
  677. REQUIRE(error == false);
  678. CHECK(output == String("fish -5 frog"));
  679. // Negative int left padded with spaces.
  680. format = "fish %5d frog";
  681. args.clear();
  682. args.push_back(-5);
  683. output = format.sprintf(args, &error);
  684. REQUIRE(error == false);
  685. CHECK(output == String("fish -5 frog"));
  686. // Negative int left padded with zeros.
  687. format = "fish %05d frog";
  688. args.clear();
  689. args.push_back(-5);
  690. output = format.sprintf(args, &error);
  691. REQUIRE(error == false);
  692. CHECK(output == String("fish -0005 frog"));
  693. // Negative int right padded with spaces.
  694. format = "fish %-5d frog";
  695. args.clear();
  696. args.push_back(-5);
  697. output = format.sprintf(args, &error);
  698. REQUIRE(error == false);
  699. CHECK(output == String("fish -5 frog"));
  700. // Negative int right padded with zeros. (0 ignored)
  701. format = "fish %-05d frog";
  702. args.clear();
  703. args.push_back(-5);
  704. ERR_PRINT_OFF; // Silence warning about 0 ignored.
  705. output = format.sprintf(args, &error);
  706. ERR_PRINT_ON;
  707. REQUIRE(error == false);
  708. CHECK(output == String("fish -5 frog"));
  709. // Hex (lower)
  710. format = "fish %x frog";
  711. args.clear();
  712. args.push_back(45);
  713. output = format.sprintf(args, &error);
  714. REQUIRE(error == false);
  715. CHECK(output == String("fish 2d frog"));
  716. // Hex (upper)
  717. format = "fish %X frog";
  718. args.clear();
  719. args.push_back(45);
  720. output = format.sprintf(args, &error);
  721. REQUIRE(error == false);
  722. CHECK(output == String("fish 2D frog"));
  723. // Octal
  724. format = "fish %o frog";
  725. args.clear();
  726. args.push_back(99);
  727. output = format.sprintf(args, &error);
  728. REQUIRE(error == false);
  729. CHECK(output == String("fish 143 frog"));
  730. ///// Reals
  731. // Real
  732. format = "fish %f frog";
  733. args.clear();
  734. args.push_back(99.99);
  735. output = format.sprintf(args, &error);
  736. REQUIRE(error == false);
  737. CHECK(output == String("fish 99.990000 frog"));
  738. // Real left-padded.
  739. format = "fish %11f frog";
  740. args.clear();
  741. args.push_back(99.99);
  742. output = format.sprintf(args, &error);
  743. REQUIRE(error == false);
  744. CHECK(output == String("fish 99.990000 frog"));
  745. // Real (infinity) left-padded
  746. format = "fish %11f frog";
  747. args.clear();
  748. args.push_back(INFINITY);
  749. output = format.sprintf(args, &error);
  750. REQUIRE(error == false);
  751. CHECK(output == String("fish inf frog"));
  752. // Real right-padded.
  753. format = "fish %-11f frog";
  754. args.clear();
  755. args.push_back(99.99);
  756. output = format.sprintf(args, &error);
  757. REQUIRE(error == false);
  758. CHECK(output == String("fish 99.990000 frog"));
  759. // Real given int.
  760. format = "fish %f frog";
  761. args.clear();
  762. args.push_back(99);
  763. output = format.sprintf(args, &error);
  764. REQUIRE(error == false);
  765. CHECK(output == String("fish 99.000000 frog"));
  766. // Real with sign (positive).
  767. format = "fish %+f frog";
  768. args.clear();
  769. args.push_back(99.99);
  770. output = format.sprintf(args, &error);
  771. REQUIRE(error == false);
  772. CHECK(output == String("fish +99.990000 frog"));
  773. // Real with sign (negative zero).
  774. format = "fish %+f frog";
  775. args.clear();
  776. args.push_back(-0.0);
  777. output = format.sprintf(args, &error);
  778. REQUIRE(error == false);
  779. CHECK(output == String("fish -0.000000 frog"));
  780. // Real with sign (positive zero).
  781. format = "fish %+f frog";
  782. args.clear();
  783. args.push_back(0.0);
  784. output = format.sprintf(args, &error);
  785. REQUIRE(error == false);
  786. CHECK(output == String("fish +0.000000 frog"));
  787. // Real with 1 decimal.
  788. format = "fish %.1f frog";
  789. args.clear();
  790. args.push_back(99.99);
  791. output = format.sprintf(args, &error);
  792. REQUIRE(error == false);
  793. CHECK(output == String("fish 100.0 frog"));
  794. // Real with 12 decimals.
  795. format = "fish %.12f frog";
  796. args.clear();
  797. args.push_back(99.99);
  798. output = format.sprintf(args, &error);
  799. REQUIRE(error == false);
  800. CHECK(output == String("fish 99.990000000000 frog"));
  801. // Real with no decimals.
  802. format = "fish %.f frog";
  803. args.clear();
  804. args.push_back(99.99);
  805. output = format.sprintf(args, &error);
  806. REQUIRE(error == false);
  807. CHECK(output == String("fish 100 frog"));
  808. // Negative real right padded with zeros. (0 ignored)
  809. format = "fish %-011f frog";
  810. args.clear();
  811. args.push_back(-99.99);
  812. ERR_PRINT_OFF; // Silence warning about 0 ignored.
  813. output = format.sprintf(args, &error);
  814. ERR_PRINT_ON;
  815. REQUIRE(error == false);
  816. CHECK(output == String("fish -99.990000 frog"));
  817. ///// Vectors
  818. // Vector2
  819. format = "fish %v frog";
  820. args.clear();
  821. args.push_back(Variant(Vector2(19.99, 1.00)));
  822. output = format.sprintf(args, &error);
  823. REQUIRE(error == false);
  824. CHECK(output == String("fish (19.990000, 1.000000) frog"));
  825. // Vector3
  826. format = "fish %v frog";
  827. args.clear();
  828. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  829. output = format.sprintf(args, &error);
  830. REQUIRE(error == false);
  831. CHECK(output == String("fish (19.990000, 1.000000, -2.050000) frog"));
  832. // Vector4
  833. format = "fish %v frog";
  834. args.clear();
  835. args.push_back(Variant(Vector4(19.99, 1.00, -2.05, 5.5)));
  836. output = format.sprintf(args, &error);
  837. REQUIRE(error == false);
  838. CHECK(output == String("fish (19.990000, 1.000000, -2.050000, 5.500000) frog"));
  839. // Vector with negative values.
  840. format = "fish %v frog";
  841. args.clear();
  842. args.push_back(Variant(Vector2(-19.99, -1.00)));
  843. output = format.sprintf(args, &error);
  844. REQUIRE(error == false);
  845. CHECK(output == String("fish (-19.990000, -1.000000) frog"));
  846. // Vector left-padded.
  847. format = "fish %11v frog";
  848. args.clear();
  849. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  850. output = format.sprintf(args, &error);
  851. REQUIRE(error == false);
  852. CHECK(output == String("fish ( 19.990000, 1.000000, -2.050000) frog"));
  853. // Vector left-padded with inf/nan
  854. format = "fish %11v frog";
  855. args.clear();
  856. args.push_back(Variant(Vector2(INFINITY, NAN)));
  857. output = format.sprintf(args, &error);
  858. REQUIRE(error == false);
  859. CHECK(output == String("fish ( inf, nan) frog"));
  860. // Vector right-padded.
  861. format = "fish %-11v frog";
  862. args.clear();
  863. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  864. output = format.sprintf(args, &error);
  865. REQUIRE(error == false);
  866. CHECK(output == String("fish (19.990000 , 1.000000 , -2.050000 ) frog"));
  867. // Vector left-padded with zeros.
  868. format = "fish %011v frog";
  869. args.clear();
  870. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  871. output = format.sprintf(args, &error);
  872. REQUIRE(error == false);
  873. CHECK(output == String("fish (0019.990000, 0001.000000, -002.050000) frog"));
  874. // Vector given Vector3i.
  875. format = "fish %v frog";
  876. args.clear();
  877. args.push_back(Variant(Vector3i(19, 1, -2)));
  878. output = format.sprintf(args, &error);
  879. REQUIRE(error == false);
  880. CHECK(output == String("fish (19.000000, 1.000000, -2.000000) frog"));
  881. // Vector with 1 decimal.
  882. format = "fish %.1v frog";
  883. args.clear();
  884. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  885. output = format.sprintf(args, &error);
  886. REQUIRE(error == false);
  887. CHECK(output == String("fish (20.0, 1.0, -2.0) frog"));
  888. // Vector with 12 decimals.
  889. format = "fish %.12v frog";
  890. args.clear();
  891. args.push_back(Variant(Vector3(19.00, 1.00, -2.00)));
  892. output = format.sprintf(args, &error);
  893. REQUIRE(error == false);
  894. CHECK(output == String("fish (19.000000000000, 1.000000000000, -2.000000000000) frog"));
  895. // Vector with no decimals.
  896. format = "fish %.v frog";
  897. args.clear();
  898. args.push_back(Variant(Vector3(19.99, 1.00, -2.05)));
  899. output = format.sprintf(args, &error);
  900. REQUIRE(error == false);
  901. CHECK(output == String("fish (20, 1, -2) frog"));
  902. ///// Strings
  903. // String
  904. format = "fish %s frog";
  905. args.clear();
  906. args.push_back("cheese");
  907. output = format.sprintf(args, &error);
  908. REQUIRE(error == false);
  909. CHECK(output == String("fish cheese frog"));
  910. // String left-padded.
  911. format = "fish %10s frog";
  912. args.clear();
  913. args.push_back("cheese");
  914. output = format.sprintf(args, &error);
  915. REQUIRE(error == false);
  916. CHECK(output == String("fish cheese frog"));
  917. // String right-padded.
  918. format = "fish %-10s frog";
  919. args.clear();
  920. args.push_back("cheese");
  921. output = format.sprintf(args, &error);
  922. REQUIRE(error == false);
  923. CHECK(output == String("fish cheese frog"));
  924. ///// Characters
  925. // Character as string.
  926. format = "fish %c frog";
  927. args.clear();
  928. args.push_back("A");
  929. output = format.sprintf(args, &error);
  930. REQUIRE(error == false);
  931. CHECK(output == String("fish A frog"));
  932. // Character as int.
  933. format = "fish %c frog";
  934. args.clear();
  935. args.push_back(65);
  936. output = format.sprintf(args, &error);
  937. REQUIRE(error == false);
  938. CHECK(output == String("fish A frog"));
  939. ///// Dynamic width
  940. // String dynamic width.
  941. format = "fish %*s frog";
  942. args.clear();
  943. args.push_back(10);
  944. args.push_back("cheese");
  945. output = format.sprintf(args, &error);
  946. REQUIRE(error == false);
  947. REQUIRE(output == String("fish cheese frog"));
  948. // Int dynamic width.
  949. format = "fish %*d frog";
  950. args.clear();
  951. args.push_back(10);
  952. args.push_back(99);
  953. output = format.sprintf(args, &error);
  954. REQUIRE(error == false);
  955. REQUIRE(output == String("fish 99 frog"));
  956. // Float dynamic width.
  957. format = "fish %*.*f frog";
  958. args.clear();
  959. args.push_back(10);
  960. args.push_back(3);
  961. args.push_back(99.99);
  962. output = format.sprintf(args, &error);
  963. REQUIRE(error == false);
  964. CHECK(output == String("fish 99.990 frog"));
  965. ///// Errors
  966. // More formats than arguments.
  967. format = "fish %s %s frog";
  968. args.clear();
  969. args.push_back("cheese");
  970. output = format.sprintf(args, &error);
  971. REQUIRE(error);
  972. CHECK(output == "not enough arguments for format string");
  973. // More arguments than formats.
  974. format = "fish %s frog";
  975. args.clear();
  976. args.push_back("hello");
  977. args.push_back("cheese");
  978. output = format.sprintf(args, &error);
  979. REQUIRE(error);
  980. CHECK(output == "not all arguments converted during string formatting");
  981. // Incomplete format.
  982. format = "fish %10";
  983. args.clear();
  984. args.push_back("cheese");
  985. output = format.sprintf(args, &error);
  986. REQUIRE(error);
  987. CHECK(output == "incomplete format");
  988. // Bad character in format string.
  989. format = "fish %&f frog";
  990. args.clear();
  991. args.push_back("cheese");
  992. output = format.sprintf(args, &error);
  993. REQUIRE(error);
  994. CHECK(output == "unsupported format character");
  995. // Too many decimals.
  996. format = "fish %2.2.2f frog";
  997. args.clear();
  998. args.push_back(99.99);
  999. output = format.sprintf(args, &error);
  1000. REQUIRE(error);
  1001. CHECK(output == "too many decimal points in format");
  1002. // * not a number or vector.
  1003. format = "fish %*f frog";
  1004. args.clear();
  1005. args.push_back("cheese");
  1006. args.push_back(99.99);
  1007. output = format.sprintf(args, &error);
  1008. REQUIRE(error);
  1009. CHECK(output == "* wants number or vector");
  1010. // Character too long.
  1011. format = "fish %c frog";
  1012. args.clear();
  1013. args.push_back("sc");
  1014. output = format.sprintf(args, &error);
  1015. REQUIRE(error);
  1016. CHECK(output == "%c requires number or single-character string");
  1017. // Character bad type.
  1018. format = "fish %c frog";
  1019. args.clear();
  1020. args.push_back(Array());
  1021. output = format.sprintf(args, &error);
  1022. REQUIRE(error);
  1023. CHECK(output == "%c requires number or single-character string");
  1024. }
  1025. TEST_CASE("[String] is_numeric") {
  1026. CHECK(String("12").is_numeric());
  1027. CHECK(String("1.2").is_numeric());
  1028. CHECK(!String("AF").is_numeric());
  1029. CHECK(String("-12").is_numeric());
  1030. CHECK(String("-1.2").is_numeric());
  1031. }
  1032. TEST_CASE("[String] pad") {
  1033. String s = String("test");
  1034. CHECK(s.lpad(10, "x") == U"xxxxxxtest");
  1035. CHECK(s.rpad(10, "x") == U"testxxxxxx");
  1036. s = String("10.10");
  1037. CHECK(s.pad_decimals(4) == U"10.1000");
  1038. CHECK(s.pad_decimals(1) == U"10.1");
  1039. CHECK(s.pad_zeros(4) == U"0010.10");
  1040. CHECK(s.pad_zeros(1) == U"10.10");
  1041. }
  1042. TEST_CASE("[String] is_subsequence_of") {
  1043. String a = "is subsequence of";
  1044. CHECK(String("sub").is_subsequence_of(a));
  1045. CHECK(!String("Sub").is_subsequence_of(a));
  1046. CHECK(String("Sub").is_subsequence_ofn(a));
  1047. }
  1048. TEST_CASE("[String] match") {
  1049. CHECK(String("img1.png").match("*.png"));
  1050. CHECK(!String("img1.jpeg").match("*.png"));
  1051. CHECK(!String("img1.Png").match("*.png"));
  1052. CHECK(String("img1.Png").matchn("*.png"));
  1053. }
  1054. TEST_CASE("[String] IPVX address to string") {
  1055. IPAddress ip0("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  1056. IPAddress ip(0x0123, 0x4567, 0x89ab, 0xcdef, true);
  1057. IPAddress ip2("fe80::52e5:49ff:fe93:1baf");
  1058. IPAddress ip3("::ffff:192.168.0.1");
  1059. String ip4 = "192.168.0.1";
  1060. CHECK(ip4.is_valid_ip_address());
  1061. ip4 = "192.368.0.1";
  1062. CHECK(!ip4.is_valid_ip_address());
  1063. String ip6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
  1064. CHECK(ip6.is_valid_ip_address());
  1065. ip6 = "2001:0db8:85j3:0000:0000:8a2e:0370:7334";
  1066. CHECK(!ip6.is_valid_ip_address());
  1067. ip6 = "2001:0db8:85f345:0000:0000:8a2e:0370:7334";
  1068. CHECK(!ip6.is_valid_ip_address());
  1069. ip6 = "2001:0db8::0:8a2e:370:7334";
  1070. CHECK(ip6.is_valid_ip_address());
  1071. ip6 = "::ffff:192.168.0.1";
  1072. CHECK(ip6.is_valid_ip_address());
  1073. }
  1074. TEST_CASE("[String] Capitalize against many strings") {
  1075. String input = "2D";
  1076. String output = "2d";
  1077. CHECK(input.capitalize() == output);
  1078. input = "2d";
  1079. output = "2d";
  1080. CHECK(input.capitalize() == output);
  1081. input = "2db";
  1082. output = "2 Db";
  1083. CHECK(input.capitalize() == output);
  1084. input = "HTML5 Html5 html5 html_5";
  1085. output = "Html 5 Html 5 Html 5 Html 5";
  1086. CHECK(input.capitalize() == output);
  1087. input = "Node2D Node2d NODE2D NODE_2D node_2d";
  1088. output = "Node 2d Node 2d Node 2d Node 2d Node 2d";
  1089. CHECK(input.capitalize() == output);
  1090. input = "Node2DPosition";
  1091. output = "Node 2d Position";
  1092. CHECK(input.capitalize() == output);
  1093. input = "Number2Digits";
  1094. output = "Number 2 Digits";
  1095. CHECK(input.capitalize() == output);
  1096. input = "bytes2var";
  1097. output = "Bytes 2 Var";
  1098. CHECK(input.capitalize() == output);
  1099. input = "linear2db";
  1100. output = "Linear 2 Db";
  1101. CHECK(input.capitalize() == output);
  1102. input = "vector3";
  1103. output = "Vector 3";
  1104. CHECK(input.capitalize() == output);
  1105. input = "sha256";
  1106. output = "Sha 256";
  1107. CHECK(input.capitalize() == output);
  1108. input = "PascalCase";
  1109. output = "Pascal Case";
  1110. CHECK(input.capitalize() == output);
  1111. input = "PascalPascalCase";
  1112. output = "Pascal Pascal Case";
  1113. CHECK(input.capitalize() == output);
  1114. input = "snake_case";
  1115. output = "Snake Case";
  1116. CHECK(input.capitalize() == output);
  1117. input = "snake_snake_case";
  1118. output = "Snake Snake Case";
  1119. CHECK(input.capitalize() == output);
  1120. input = "sha256sum";
  1121. output = "Sha 256 Sum";
  1122. CHECK(input.capitalize() == output);
  1123. input = "cat2dog";
  1124. output = "Cat 2 Dog";
  1125. CHECK(input.capitalize() == output);
  1126. input = "function(name)";
  1127. output = "Function(name)";
  1128. CHECK(input.capitalize() == output);
  1129. input = "snake_case_function(snake_case_arg)";
  1130. output = "Snake Case Function(snake Case Arg)";
  1131. CHECK(input.capitalize() == output);
  1132. input = "snake_case_function( snake_case_arg )";
  1133. output = "Snake Case Function( Snake Case Arg )";
  1134. CHECK(input.capitalize() == output);
  1135. input = U"словоСлово_слово слово";
  1136. output = U"Слово Слово Слово Слово";
  1137. CHECK(input.capitalize() == output);
  1138. input = U"λέξηΛέξη_λέξη λέξη";
  1139. output = U"Λέξη Λέξη Λέξη Λέξη";
  1140. CHECK(input.capitalize() == output);
  1141. input = U"բառԲառ_բառ բառ";
  1142. output = U"Բառ Բառ Բառ Բառ";
  1143. CHECK(input.capitalize() == output);
  1144. }
  1145. struct StringCasesTestCase {
  1146. const char32_t *input;
  1147. const char32_t *camel_case;
  1148. const char32_t *pascal_case;
  1149. const char32_t *snake_case;
  1150. };
  1151. TEST_CASE("[String] Checking case conversion methods") {
  1152. StringCasesTestCase test_cases[] = {
  1153. /* clang-format off */
  1154. { U"2D", U"2d", U"2d", U"2d" },
  1155. { U"2d", U"2d", U"2d", U"2d" },
  1156. { U"2db", U"2Db", U"2Db", U"2_db" },
  1157. { U"Vector3", U"vector3", U"Vector3", U"vector_3" },
  1158. { U"sha256", U"sha256", U"Sha256", U"sha_256" },
  1159. { U"Node2D", U"node2d", U"Node2d", U"node_2d" },
  1160. { U"RichTextLabel", U"richTextLabel", U"RichTextLabel", U"rich_text_label" },
  1161. { U"HTML5", U"html5", U"Html5", U"html_5" },
  1162. { U"Node2DPosition", U"node2dPosition", U"Node2dPosition", U"node_2d_position" },
  1163. { U"Number2Digits", U"number2Digits", U"Number2Digits", U"number_2_digits" },
  1164. { U"get_property_list", U"getPropertyList", U"GetPropertyList", U"get_property_list" },
  1165. { U"get_camera_2d", U"getCamera2d", U"GetCamera2d", U"get_camera_2d" },
  1166. { U"_physics_process", U"physicsProcess", U"PhysicsProcess", U"_physics_process" },
  1167. { U"bytes2var", U"bytes2Var", U"Bytes2Var", U"bytes_2_var" },
  1168. { U"linear2db", U"linear2Db", U"Linear2Db", U"linear_2_db" },
  1169. { U"sha256sum", U"sha256Sum", U"Sha256Sum", U"sha_256_sum" },
  1170. { U"camelCase", U"camelCase", U"CamelCase", U"camel_case" },
  1171. { U"PascalCase", U"pascalCase", U"PascalCase", U"pascal_case" },
  1172. { U"snake_case", U"snakeCase", U"SnakeCase", U"snake_case" },
  1173. { U"Test TEST test", U"testTestTest", U"TestTestTest", U"test_test_test" },
  1174. { U"словоСлово_слово слово", U"словоСловоСловоСлово", U"СловоСловоСловоСлово", U"слово_слово_слово_слово" },
  1175. { U"λέξηΛέξη_λέξη λέξη", U"λέξηΛέξηΛέξηΛέξη", U"ΛέξηΛέξηΛέξηΛέξη", U"λέξη_λέξη_λέξη_λέξη" },
  1176. { U"բառԲառ_բառ բառ", U"բառԲառԲառԲառ", U"ԲառԲառԲառԲառ", U"բառ_բառ_բառ_բառ" },
  1177. { nullptr, nullptr, nullptr, nullptr },
  1178. /* clang-format on */
  1179. };
  1180. int idx = 0;
  1181. while (test_cases[idx].input != nullptr) {
  1182. String input = test_cases[idx].input;
  1183. CHECK(input.to_camel_case() == test_cases[idx].camel_case);
  1184. CHECK(input.to_pascal_case() == test_cases[idx].pascal_case);
  1185. CHECK(input.to_snake_case() == test_cases[idx].snake_case);
  1186. idx++;
  1187. }
  1188. }
  1189. TEST_CASE("[String] Checking string is empty when it should be") {
  1190. bool state = true;
  1191. bool success;
  1192. String a = "";
  1193. success = a[0] == 0;
  1194. if (!success) {
  1195. state = false;
  1196. }
  1197. String b = "Godot";
  1198. success = b[b.size()] == 0;
  1199. if (!success) {
  1200. state = false;
  1201. }
  1202. const String c = "";
  1203. success = c[0] == 0;
  1204. if (!success) {
  1205. state = false;
  1206. }
  1207. const String d = "Godot";
  1208. success = d[d.size()] == 0;
  1209. if (!success) {
  1210. state = false;
  1211. }
  1212. CHECK(state);
  1213. }
  1214. TEST_CASE("[String] lstrip and rstrip") {
  1215. #define STRIP_TEST(x) \
  1216. { \
  1217. bool success = x; \
  1218. state = state && success; \
  1219. }
  1220. bool state = true;
  1221. // strip none
  1222. STRIP_TEST(String("abc").lstrip("") == "abc");
  1223. STRIP_TEST(String("abc").rstrip("") == "abc");
  1224. // strip one
  1225. STRIP_TEST(String("abc").lstrip("a") == "bc");
  1226. STRIP_TEST(String("abc").rstrip("c") == "ab");
  1227. // strip lots
  1228. STRIP_TEST(String("bababbababccc").lstrip("ab") == "ccc");
  1229. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("cb") == "aaa");
  1230. // strip empty string
  1231. STRIP_TEST(String("").lstrip("") == "");
  1232. STRIP_TEST(String("").rstrip("") == "");
  1233. // strip to empty string
  1234. STRIP_TEST(String("abcabcabc").lstrip("bca") == "");
  1235. STRIP_TEST(String("abcabcabc").rstrip("bca") == "");
  1236. // don't strip wrong end
  1237. STRIP_TEST(String("abc").lstrip("c") == "abc");
  1238. STRIP_TEST(String("abca").lstrip("a") == "bca");
  1239. STRIP_TEST(String("abc").rstrip("a") == "abc");
  1240. STRIP_TEST(String("abca").rstrip("a") == "abc");
  1241. // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5)
  1242. // and the same second as "ÿ" (\u00ff)
  1243. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("µÿ")) == String::utf8("¿"));
  1244. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("µÿ")) == String::utf8("¿"));
  1245. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("µÿ")) == String::utf8("¿ÿ"));
  1246. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("µÿ")) == String::utf8("µ¿"));
  1247. // the above tests repeated with additional superfluous strip chars
  1248. // strip none
  1249. STRIP_TEST(String("abc").lstrip("qwjkl") == "abc");
  1250. STRIP_TEST(String("abc").rstrip("qwjkl") == "abc");
  1251. // strip one
  1252. STRIP_TEST(String("abc").lstrip("qwajkl") == "bc");
  1253. STRIP_TEST(String("abc").rstrip("qwcjkl") == "ab");
  1254. // strip lots
  1255. STRIP_TEST(String("bababbababccc").lstrip("qwabjkl") == "ccc");
  1256. STRIP_TEST(String("aaabcbcbcbbcbbc").rstrip("qwcbjkl") == "aaa");
  1257. // strip empty string
  1258. STRIP_TEST(String("").lstrip("qwjkl") == "");
  1259. STRIP_TEST(String("").rstrip("qwjkl") == "");
  1260. // strip to empty string
  1261. STRIP_TEST(String("abcabcabc").lstrip("qwbcajkl") == "");
  1262. STRIP_TEST(String("abcabcabc").rstrip("qwbcajkl") == "");
  1263. // don't strip wrong end
  1264. STRIP_TEST(String("abc").lstrip("qwcjkl") == "abc");
  1265. STRIP_TEST(String("abca").lstrip("qwajkl") == "bca");
  1266. STRIP_TEST(String("abc").rstrip("qwajkl") == "abc");
  1267. STRIP_TEST(String("abca").rstrip("qwajkl") == "abc");
  1268. // in utf-8 "¿" (\u00bf) has the same first byte as "µ" (\u00b5)
  1269. // and the same second as "ÿ" (\u00ff)
  1270. STRIP_TEST(String::utf8("¿").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  1271. STRIP_TEST(String::utf8("¿").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿"));
  1272. STRIP_TEST(String::utf8("µ¿ÿ").lstrip(String::utf8("qwaµÿjkl")) == String::utf8("¿ÿ"));
  1273. STRIP_TEST(String::utf8("µ¿ÿ").rstrip(String::utf8("qwaµÿjkl")) == String::utf8("µ¿"));
  1274. CHECK(state);
  1275. #undef STRIP_TEST
  1276. }
  1277. TEST_CASE("[String] Ensuring empty string into parse_utf8 passes empty string") {
  1278. String empty;
  1279. CHECK(empty.parse_utf8(nullptr, -1) == ERR_INVALID_DATA);
  1280. }
  1281. TEST_CASE("[String] Cyrillic to_lower()") {
  1282. String upper = U"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
  1283. String lower = U"абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
  1284. String test = upper.to_lower();
  1285. bool state = test == lower;
  1286. CHECK(state);
  1287. }
  1288. TEST_CASE("[String] Count and countn functionality") {
  1289. String s = String("");
  1290. MULTICHECK_STRING_EQ(s, count, "Test", 0);
  1291. s = "Test";
  1292. MULTICHECK_STRING_EQ(s, count, "", 0);
  1293. s = "Test";
  1294. MULTICHECK_STRING_EQ(s, count, "test", 0);
  1295. s = "Test";
  1296. MULTICHECK_STRING_EQ(s, count, "TEST", 0);
  1297. s = "TEST";
  1298. MULTICHECK_STRING_EQ(s, count, "TEST", 1);
  1299. s = "Test";
  1300. MULTICHECK_STRING_EQ(s, count, "Test", 1);
  1301. s = "aTest";
  1302. MULTICHECK_STRING_EQ(s, count, "Test", 1);
  1303. s = "Testa";
  1304. MULTICHECK_STRING_EQ(s, count, "Test", 1);
  1305. s = "TestTestTest";
  1306. MULTICHECK_STRING_EQ(s, count, "Test", 3);
  1307. s = "TestTestTest";
  1308. MULTICHECK_STRING_EQ(s, count, "TestTest", 1);
  1309. s = "TestGodotTestGodotTestGodot";
  1310. MULTICHECK_STRING_EQ(s, count, "Test", 3);
  1311. s = "TestTestTestTest";
  1312. MULTICHECK_STRING_INT_INT_EQ(s, count, "Test", 4, 8, 1);
  1313. s = "TestTestTestTest";
  1314. MULTICHECK_STRING_INT_INT_EQ(s, count, "Test", 4, 12, 2);
  1315. s = "TestTestTestTest";
  1316. MULTICHECK_STRING_INT_INT_EQ(s, count, "Test", 4, 16, 3);
  1317. s = "TestTestTestTest";
  1318. MULTICHECK_STRING_INT_EQ(s, count, "Test", 4, 3);
  1319. s = "Test";
  1320. MULTICHECK_STRING_EQ(s, countn, "test", 1);
  1321. s = "Test";
  1322. MULTICHECK_STRING_EQ(s, countn, "TEST", 1);
  1323. s = "testTest-Testatest";
  1324. MULTICHECK_STRING_EQ(s, countn, "tEst", 4);
  1325. s = "testTest-TeStatest";
  1326. MULTICHECK_STRING_INT_INT_EQ(s, countn, "tEsT", 4, 16, 2);
  1327. }
  1328. TEST_CASE("[String] Bigrams") {
  1329. String s = "abcd";
  1330. Vector<String> bigr = s.bigrams();
  1331. CHECK(bigr.size() == 3);
  1332. CHECK(bigr[0] == "ab");
  1333. CHECK(bigr[1] == "bc");
  1334. CHECK(bigr[2] == "cd");
  1335. }
  1336. TEST_CASE("[String] c-escape/unescape") {
  1337. String s = "\\1\a2\b\f3\n45\r6\t7\v8\'9\?0\"";
  1338. CHECK(s.c_escape().c_unescape() == s);
  1339. }
  1340. TEST_CASE("[String] indent") {
  1341. static const char *input[] = {
  1342. "",
  1343. "aaa\nbbb",
  1344. "\tcontains\n\tindent",
  1345. "empty\n\nline",
  1346. };
  1347. static const char *expected[] = {
  1348. "",
  1349. "\taaa\n\tbbb",
  1350. "\t\tcontains\n\t\tindent",
  1351. "\tempty\n\n\tline",
  1352. };
  1353. for (int i = 0; i < 3; i++) {
  1354. CHECK(String(input[i]).indent("\t") == expected[i]);
  1355. }
  1356. }
  1357. TEST_CASE("[String] dedent") {
  1358. String s = " aaa\n bbb";
  1359. String t = "aaa\nbbb";
  1360. CHECK(s.dedent() == t);
  1361. }
  1362. TEST_CASE("[String] Path functions") {
  1363. 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" };
  1364. static const char *base_dir[8] = { "C:\\Godot\\project", "/Godot/project", "../Godot/project", "Godot", "C:\\", "res://", "user://", "/" };
  1365. static const char *base_name[8] = { "C:\\Godot\\project\\test", "/Godot/project/test", "../Godot/project/test", "Godot\\test", "C:\\test", "res://test", "user://test", "/" };
  1366. static const char *ext[8] = { "tscn", "xscn", "scn", "doc", "", "", "", "test" };
  1367. static const char *file[8] = { "test.tscn", "test.xscn", "test.scn", "test.doc", "test.", "test", "test", ".test" };
  1368. 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" };
  1369. static const bool abs[8] = { true, true, false, false, true, true, true, true };
  1370. for (int i = 0; i < 8; i++) {
  1371. CHECK(String(path[i]).get_base_dir() == base_dir[i]);
  1372. CHECK(String(path[i]).get_basename() == base_name[i]);
  1373. CHECK(String(path[i]).get_extension() == ext[i]);
  1374. CHECK(String(path[i]).get_file() == file[i]);
  1375. CHECK(String(path[i]).is_absolute_path() == abs[i]);
  1376. CHECK(String(path[i]).is_relative_path() != abs[i]);
  1377. CHECK(String(path[i]).simplify_path() == String(simplified[i]));
  1378. CHECK(String(path[i]).simplify_path().get_base_dir().path_join(file[i]) == String(path[i]).simplify_path());
  1379. }
  1380. static const char *file_name[3] = { "test.tscn", "test://.xscn", "?tes*t.scn" };
  1381. static const bool valid[3] = { true, false, false };
  1382. for (int i = 0; i < 3; i++) {
  1383. CHECK(String(file_name[i]).is_valid_filename() == valid[i]);
  1384. }
  1385. }
  1386. TEST_CASE("[String] hash") {
  1387. String a = "Test";
  1388. String b = "Test";
  1389. String c = "West";
  1390. CHECK(a.hash() == b.hash());
  1391. CHECK(a.hash() != c.hash());
  1392. CHECK(a.hash64() == b.hash64());
  1393. CHECK(a.hash64() != c.hash64());
  1394. }
  1395. TEST_CASE("[String] uri_encode/unescape") {
  1396. String s = "Godot Engine:'docs'";
  1397. String t = "Godot%20Engine%3A%27docs%27";
  1398. String x1 = "T%C4%93%C5%A1t";
  1399. static const uint8_t u8str[] = { 0x54, 0xC4, 0x93, 0xC5, 0xA1, 0x74, 0x00 };
  1400. String x2 = String::utf8((const char *)u8str);
  1401. String x3 = U"Tēšt";
  1402. CHECK(x1.uri_decode() == x2);
  1403. CHECK(x1.uri_decode() == x3);
  1404. CHECK((x1 + x3).uri_decode() == (x2 + x3)); // Mixed unicode and URL encoded string, e.g. GTK+ bookmark.
  1405. CHECK(x2.uri_encode() == x1);
  1406. CHECK(x3.uri_encode() == x1);
  1407. CHECK(s.uri_encode() == t);
  1408. CHECK(t.uri_decode() == s);
  1409. }
  1410. TEST_CASE("[String] xml_escape/unescape") {
  1411. String s = "\"Test\" <test@test&'test'>";
  1412. CHECK(s.xml_escape(true).xml_unescape() == s);
  1413. CHECK(s.xml_escape(false).xml_unescape() == s);
  1414. }
  1415. TEST_CASE("[String] xml_unescape") {
  1416. // Named entities
  1417. String input = "&quot;&amp;&apos;&lt;&gt;";
  1418. CHECK(input.xml_unescape() == "\"&\'<>");
  1419. // Numeric entities
  1420. input = "&#x41;&#66;";
  1421. CHECK(input.xml_unescape() == "AB");
  1422. input = "&#0;&x#0;More text";
  1423. String result = input.xml_unescape();
  1424. // Didn't put in a leading NUL and terminate the string
  1425. CHECK(input.length() > 0);
  1426. CHECK(input[0] != '\0');
  1427. // Entity should be left as-is if invalid
  1428. CHECK(input.xml_unescape() == input);
  1429. // Check near char32_t range
  1430. input = "&#xFFFFFFFF;";
  1431. result = input.xml_unescape();
  1432. CHECK(result.length() == 1);
  1433. CHECK(result[0] == 0xFFFFFFFF);
  1434. input = "&#4294967295;";
  1435. result = input.xml_unescape();
  1436. CHECK(result.length() == 1);
  1437. CHECK(result[0] == 0xFFFFFFFF);
  1438. // Check out of range of char32_t
  1439. input = "&#xFFFFFFFFF;";
  1440. CHECK(input.xml_unescape() == input);
  1441. input = "&#4294967296;";
  1442. CHECK(input.xml_unescape() == input);
  1443. // Shouldn't consume without ending in a ';'
  1444. input = "&#66";
  1445. CHECK(input.xml_unescape() == input);
  1446. input = "&#x41";
  1447. CHECK(input.xml_unescape() == input);
  1448. // Invalid characters should make the entity ignored
  1449. input = "&#x41SomeIrrelevantText;";
  1450. CHECK(input.xml_unescape() == input);
  1451. input = "&#66SomeIrrelevantText;";
  1452. CHECK(input.xml_unescape() == input);
  1453. }
  1454. TEST_CASE("[String] Strip escapes") {
  1455. String s = "\t\tTest Test\r\n Test";
  1456. CHECK(s.strip_escapes() == "Test Test Test");
  1457. }
  1458. TEST_CASE("[String] Similarity") {
  1459. String a = "Test";
  1460. String b = "West";
  1461. String c = "Toad";
  1462. CHECK(a.similarity(b) > a.similarity(c));
  1463. }
  1464. TEST_CASE("[String] Strip edges") {
  1465. String s = "\t Test Test ";
  1466. CHECK(s.strip_edges(true, false) == "Test Test ");
  1467. CHECK(s.strip_edges(false, true) == "\t Test Test");
  1468. CHECK(s.strip_edges(true, true) == "Test Test");
  1469. }
  1470. TEST_CASE("[String] Trim") {
  1471. String s = "aaaTestbbb";
  1472. MULTICHECK_STRING_EQ(s, trim_prefix, "aaa", "Testbbb");
  1473. MULTICHECK_STRING_EQ(s, trim_prefix, "Test", s);
  1474. MULTICHECK_STRING_EQ(s, trim_prefix, "", s);
  1475. MULTICHECK_STRING_EQ(s, trim_prefix, "aaaTestbbb", "");
  1476. MULTICHECK_STRING_EQ(s, trim_prefix, "bbb", s);
  1477. MULTICHECK_STRING_EQ(s, trim_prefix, "AAA", s);
  1478. MULTICHECK_STRING_EQ(s, trim_suffix, "bbb", "aaaTest");
  1479. MULTICHECK_STRING_EQ(s, trim_suffix, "Test", s);
  1480. MULTICHECK_STRING_EQ(s, trim_suffix, "", s);
  1481. MULTICHECK_STRING_EQ(s, trim_suffix, "aaaTestbbb", "");
  1482. MULTICHECK_STRING_EQ(s, trim_suffix, "aaa", s);
  1483. MULTICHECK_STRING_EQ(s, trim_suffix, "BBB", s);
  1484. }
  1485. TEST_CASE("[String] Right/Left") {
  1486. String s = "aaaTestbbb";
  1487. // ^
  1488. CHECK(s.right(6) == "estbbb");
  1489. CHECK(s.right(-6) == "tbbb");
  1490. CHECK(s.left(6) == "aaaTes");
  1491. CHECK(s.left(-6) == "aaaT");
  1492. }
  1493. TEST_CASE("[String] Repeat") {
  1494. String s = "abababab";
  1495. String x = "ab";
  1496. String t = x.repeat(4);
  1497. CHECK(t == s);
  1498. }
  1499. TEST_CASE("[String] Reverse") {
  1500. String s = "Abcd";
  1501. CHECK(s.reverse() == "dcbA");
  1502. }
  1503. TEST_CASE("[String] SHA1/SHA256/MD5") {
  1504. String s = "Godot";
  1505. String sha1 = "a1e91f39b9fce6a9998b14bdbe2aa2b39dc2d201";
  1506. static uint8_t sha1_buf[20] = {
  1507. 0xA1, 0xE9, 0x1F, 0x39, 0xB9, 0xFC, 0xE6, 0xA9, 0x99, 0x8B, 0x14, 0xBD, 0xBE, 0x2A, 0xA2, 0xB3,
  1508. 0x9D, 0xC2, 0xD2, 0x01
  1509. };
  1510. String sha256 = "2a02b2443f7985d89d09001086ae3dcfa6eb0f55c6ef170715d42328e16e6cb8";
  1511. static uint8_t sha256_buf[32] = {
  1512. 0x2A, 0x02, 0xB2, 0x44, 0x3F, 0x79, 0x85, 0xD8, 0x9D, 0x09, 0x00, 0x10, 0x86, 0xAE, 0x3D, 0xCF,
  1513. 0xA6, 0xEB, 0x0F, 0x55, 0xC6, 0xEF, 0x17, 0x07, 0x15, 0xD4, 0x23, 0x28, 0xE1, 0x6E, 0x6C, 0xB8
  1514. };
  1515. String md5 = "4a336d087aeb0390da10ee2ea7cb87f8";
  1516. static uint8_t md5_buf[16] = {
  1517. 0x4A, 0x33, 0x6D, 0x08, 0x7A, 0xEB, 0x03, 0x90, 0xDA, 0x10, 0xEE, 0x2E, 0xA7, 0xCB, 0x87, 0xF8
  1518. };
  1519. PackedByteArray buf = s.sha1_buffer();
  1520. CHECK(memcmp(sha1_buf, buf.ptr(), 20) == 0);
  1521. CHECK(s.sha1_text() == sha1);
  1522. buf = s.sha256_buffer();
  1523. CHECK(memcmp(sha256_buf, buf.ptr(), 32) == 0);
  1524. CHECK(s.sha256_text() == sha256);
  1525. buf = s.md5_buffer();
  1526. CHECK(memcmp(md5_buf, buf.ptr(), 16) == 0);
  1527. CHECK(s.md5_text() == md5);
  1528. }
  1529. TEST_CASE("[String] Join") {
  1530. String comma = ", ";
  1531. String empty = "";
  1532. Vector<String> parts;
  1533. CHECK(comma.join(parts) == "");
  1534. CHECK(empty.join(parts) == "");
  1535. parts.push_back("One");
  1536. CHECK(comma.join(parts) == "One");
  1537. CHECK(empty.join(parts) == "One");
  1538. parts.push_back("B");
  1539. parts.push_back("C");
  1540. CHECK(comma.join(parts) == "One, B, C");
  1541. CHECK(empty.join(parts) == "OneBC");
  1542. parts.push_back("");
  1543. CHECK(comma.join(parts) == "One, B, C, ");
  1544. CHECK(empty.join(parts) == "OneBC");
  1545. }
  1546. TEST_CASE("[String] Is_*") {
  1547. static const char *data[13] = { "-30", "100", "10.1", "10,1", "1e2", "1e-2", "1e2e3", "0xAB", "AB", "Test1", "1Test", "Test*1", "文字" };
  1548. static bool isnum[13] = { true, true, true, false, false, false, false, false, false, false, false, false, false };
  1549. static bool isint[13] = { true, true, false, false, false, false, false, false, false, false, false, false, false };
  1550. static bool ishex[13] = { true, true, false, false, true, false, true, false, true, false, false, false, false };
  1551. static bool ishex_p[13] = { false, false, false, false, false, false, false, true, false, false, false, false, false };
  1552. static bool isflt[13] = { true, true, true, false, true, true, false, false, false, false, false, false, false };
  1553. static bool isaid[13] = { false, false, false, false, false, false, false, false, true, true, false, false, false };
  1554. static bool isuid[13] = { false, false, false, false, false, false, false, false, true, true, false, false, true };
  1555. for (int i = 0; i < 12; i++) {
  1556. String s = String::utf8(data[i]);
  1557. CHECK(s.is_numeric() == isnum[i]);
  1558. CHECK(s.is_valid_int() == isint[i]);
  1559. CHECK(s.is_valid_hex_number(false) == ishex[i]);
  1560. CHECK(s.is_valid_hex_number(true) == ishex_p[i]);
  1561. CHECK(s.is_valid_float() == isflt[i]);
  1562. CHECK(s.is_valid_ascii_identifier() == isaid[i]);
  1563. CHECK(s.is_valid_unicode_identifier() == isuid[i]);
  1564. }
  1565. }
  1566. TEST_CASE("[String] humanize_size") {
  1567. CHECK(String::humanize_size(1000) == "1000 B");
  1568. CHECK(String::humanize_size(1025) == "1.00 KiB");
  1569. CHECK(String::humanize_size(1025300) == "1001.2 KiB");
  1570. CHECK(String::humanize_size(100523550) == "95.86 MiB");
  1571. CHECK(String::humanize_size(5345555000) == "4.97 GiB");
  1572. }
  1573. TEST_CASE("[String] validate_node_name") {
  1574. String numeric_only = "12345";
  1575. CHECK(numeric_only.validate_node_name() == "12345");
  1576. String name_with_spaces = "Name with spaces";
  1577. CHECK(name_with_spaces.validate_node_name() == "Name with spaces");
  1578. String name_with_kana = U"Name with kana ゴドツ";
  1579. CHECK(name_with_kana.validate_node_name() == U"Name with kana ゴドツ");
  1580. String name_with_invalid_chars = "Name with invalid characters :.@%removed!";
  1581. CHECK(name_with_invalid_chars.validate_node_name() == "Name with invalid characters ____removed!");
  1582. }
  1583. TEST_CASE("[String] validate_ascii_identifier") {
  1584. String empty_string;
  1585. CHECK(empty_string.validate_ascii_identifier() == "_");
  1586. String numeric_only = "12345";
  1587. CHECK(numeric_only.validate_ascii_identifier() == "_12345");
  1588. String name_with_spaces = "Name with spaces";
  1589. CHECK(name_with_spaces.validate_ascii_identifier() == "Name_with_spaces");
  1590. String name_with_invalid_chars = U"Invalid characters:@*#&世界";
  1591. CHECK(name_with_invalid_chars.validate_ascii_identifier() == "Invalid_characters_______");
  1592. }
  1593. TEST_CASE("[String] validate_unicode_identifier") {
  1594. String empty_string;
  1595. CHECK(empty_string.validate_unicode_identifier() == "_");
  1596. String numeric_only = "12345";
  1597. CHECK(numeric_only.validate_unicode_identifier() == "_12345");
  1598. String name_with_spaces = "Name with spaces";
  1599. CHECK(name_with_spaces.validate_unicode_identifier() == "Name_with_spaces");
  1600. String name_with_invalid_chars = U"Invalid characters:@*#&世界";
  1601. CHECK(name_with_invalid_chars.validate_unicode_identifier() == U"Invalid_characters_____世界");
  1602. }
  1603. TEST_CASE("[String] Variant indexed get") {
  1604. Variant s = String("abcd");
  1605. bool valid = false;
  1606. bool oob = true;
  1607. String r = s.get_indexed(1, valid, oob);
  1608. CHECK(valid);
  1609. CHECK_FALSE(oob);
  1610. CHECK_EQ(r, String("b"));
  1611. }
  1612. TEST_CASE("[String] Variant validated indexed get") {
  1613. Variant s = String("abcd");
  1614. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(Variant::STRING);
  1615. Variant r;
  1616. bool oob = true;
  1617. getter(&s, 1, &r, &oob);
  1618. CHECK_FALSE(oob);
  1619. CHECK_EQ(r, String("b"));
  1620. }
  1621. TEST_CASE("[String] Variant ptr indexed get") {
  1622. String s("abcd");
  1623. Variant::PTRIndexedGetter getter = Variant::get_member_ptr_indexed_getter(Variant::STRING);
  1624. String r;
  1625. getter(&s, 1, &r);
  1626. CHECK_EQ(r, String("b"));
  1627. }
  1628. TEST_CASE("[String] Variant indexed set") {
  1629. Variant s = String("abcd");
  1630. bool valid = false;
  1631. bool oob = true;
  1632. s.set_indexed(1, String("z"), valid, oob);
  1633. CHECK(valid);
  1634. CHECK_FALSE(oob);
  1635. CHECK_EQ(s, String("azcd"));
  1636. }
  1637. TEST_CASE("[String] Variant validated indexed set") {
  1638. Variant s = String("abcd");
  1639. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(Variant::STRING);
  1640. Variant v = String("z");
  1641. bool oob = true;
  1642. setter(&s, 1, &v, &oob);
  1643. CHECK_FALSE(oob);
  1644. CHECK_EQ(s, String("azcd"));
  1645. }
  1646. TEST_CASE("[String] Variant ptr indexed set") {
  1647. String s("abcd");
  1648. Variant::PTRIndexedSetter setter = Variant::get_member_ptr_indexed_setter(Variant::STRING);
  1649. String v("z");
  1650. setter(&s, 1, &v);
  1651. CHECK_EQ(s, String("azcd"));
  1652. }
  1653. TEST_CASE("[String][URL] Parse URL") {
  1654. #define CHECK_URL(m_url_to_parse, m_expected_schema, m_expected_host, m_expected_port, m_expected_path, m_expected_fragment, m_expected_error) \
  1655. if (true) { \
  1656. int port; \
  1657. String url(m_url_to_parse), schema, host, path, fragment; \
  1658. \
  1659. CHECK_EQ(url.parse_url(schema, host, port, path, fragment), m_expected_error); \
  1660. CHECK_EQ(schema, m_expected_schema); \
  1661. CHECK_EQ(host, m_expected_host); \
  1662. CHECK_EQ(path, m_expected_path); \
  1663. CHECK_EQ(fragment, m_expected_fragment); \
  1664. CHECK_EQ(port, m_expected_port); \
  1665. } else \
  1666. ((void)0)
  1667. // All elements.
  1668. CHECK_URL("https://www.example.com:8080/path/to/file.html#fragment", "https://", "www.example.com", 8080, "/path/to/file.html", "fragment", Error::OK);
  1669. // Valid URLs.
  1670. CHECK_URL("https://godotengine.org", "https://", "godotengine.org", 0, "", "", Error::OK);
  1671. CHECK_URL("https://godotengine.org/", "https://", "godotengine.org", 0, "/", "", Error::OK);
  1672. CHECK_URL("godotengine.org/", "", "godotengine.org", 0, "/", "", Error::OK);
  1673. CHECK_URL("HTTPS://godotengine.org/", "https://", "godotengine.org", 0, "/", "", Error::OK);
  1674. CHECK_URL("https://GODOTENGINE.ORG/", "https://", "godotengine.org", 0, "/", "", Error::OK);
  1675. CHECK_URL("http://godotengine.org", "http://", "godotengine.org", 0, "", "", Error::OK);
  1676. CHECK_URL("https://godotengine.org:8080", "https://", "godotengine.org", 8080, "", "", Error::OK);
  1677. CHECK_URL("https://godotengine.org/blog", "https://", "godotengine.org", 0, "/blog", "", Error::OK);
  1678. CHECK_URL("https://godotengine.org/blog/", "https://", "godotengine.org", 0, "/blog/", "", Error::OK);
  1679. CHECK_URL("https://docs.godotengine.org/en/stable", "https://", "docs.godotengine.org", 0, "/en/stable", "", Error::OK);
  1680. CHECK_URL("https://docs.godotengine.org/en/stable/", "https://", "docs.godotengine.org", 0, "/en/stable/", "", Error::OK);
  1681. CHECK_URL("https://me:[email protected]", "https://", "godotengine.org", 0, "", "", Error::OK);
  1682. CHECK_URL("https://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]/ipv6", "https://", "fedc:ba98:7654:3210:fedc:ba98:7654:3210", 0, "/ipv6", "", Error::OK);
  1683. // Scheme vs Fragment.
  1684. CHECK_URL("google.com/#goto=http://redirect_url/", "", "google.com", 0, "/", "goto=http://redirect_url/", Error::OK);
  1685. // Invalid URLs.
  1686. // Invalid Scheme.
  1687. CHECK_URL("https_://godotengine.org", "", "https_", 0, "//godotengine.org", "", Error::ERR_INVALID_PARAMETER);
  1688. // Multiple ports.
  1689. CHECK_URL("https://godotengine.org:8080:433", "https://", "", 0, "", "", Error::ERR_INVALID_PARAMETER);
  1690. // Missing ] on literal IPv6.
  1691. CHECK_URL("https://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210/ipv6", "https://", "", 0, "/ipv6", "", Error::ERR_INVALID_PARAMETER);
  1692. // Missing host.
  1693. CHECK_URL("https:///blog", "https://", "", 0, "/blog", "", Error::ERR_INVALID_PARAMETER);
  1694. // Invalid ports.
  1695. CHECK_URL("https://godotengine.org:notaport", "https://", "godotengine.org", 0, "", "", Error::ERR_INVALID_PARAMETER);
  1696. CHECK_URL("https://godotengine.org:-8080", "https://", "godotengine.org", -8080, "", "", Error::ERR_INVALID_PARAMETER);
  1697. CHECK_URL("https://godotengine.org:88888", "https://", "godotengine.org", 88888, "", "", Error::ERR_INVALID_PARAMETER);
  1698. #undef CHECK_URL
  1699. }
  1700. TEST_CASE("[Stress][String] Empty via ' == String()'") {
  1701. for (int i = 0; i < 100000; ++i) {
  1702. String str = "Hello World!";
  1703. if (str == String()) {
  1704. continue;
  1705. }
  1706. }
  1707. }
  1708. TEST_CASE("[Stress][String] Empty via `is_empty()`") {
  1709. for (int i = 0; i < 100000; ++i) {
  1710. String str = "Hello World!";
  1711. if (str.is_empty()) {
  1712. continue;
  1713. }
  1714. }
  1715. }
  1716. } // namespace TestString
  1717. #endif // TEST_STRING_H