test_string.h 55 KB

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