test_string.h 70 KB

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