test_string.h 54 KB

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