test_string.h 43 KB

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