test_string.h 36 KB

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