string_test.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * Copyright 2010-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/filepath.h>
  7. #include <bx/string.h>
  8. #include <bx/handlealloc.h>
  9. #include <bx/sort.h>
  10. #include <string>
  11. #include <tinystl/string.h>
  12. bx::AllocatorI* g_allocator;
  13. TEST_CASE("stringPrintfTy", "[string]")
  14. {
  15. std::string test;
  16. bx::stringPrintf(test, "printf into std::string.");
  17. REQUIRE(0 == bx::strCmp(bx::StringView(test.data(), int32_t(test.length() ) ), "printf into std::string.") );
  18. }
  19. TEST_CASE("prettify", "[string]")
  20. {
  21. char tmp[1024];
  22. prettify(tmp, BX_COUNTOF(tmp), 4000, bx::Units::Kilo);
  23. REQUIRE(0 == bx::strCmp(tmp, "4.00 kB") );
  24. prettify(tmp, BX_COUNTOF(tmp), 4096, bx::Units::Kibi);
  25. REQUIRE(0 == bx::strCmp(tmp, "4.00 KiB") );
  26. }
  27. TEST_CASE("chars", "[string]")
  28. {
  29. for (char ch = 'A'; ch <= 'Z'; ++ch)
  30. {
  31. REQUIRE(!bx::isLower(ch) );
  32. REQUIRE(!bx::isNumeric(ch) );
  33. REQUIRE(bx::isUpper(ch) );
  34. REQUIRE(bx::isAlpha(ch) );
  35. REQUIRE(bx::isAlphaNum(ch) );
  36. REQUIRE(bx::isLower(bx::toLower(ch) ) );
  37. }
  38. }
  39. TEST_CASE("strLen", "[string]")
  40. {
  41. const char* test = "test";
  42. REQUIRE(0 == bx::strLen(test, 0) );
  43. REQUIRE(2 == bx::strLen(test, 2) );
  44. REQUIRE(4 == bx::strLen(test, INT32_MAX) );
  45. }
  46. TEST_CASE("strCopy", "[string]")
  47. {
  48. char dst[128];
  49. size_t num;
  50. num = bx::strCopy(dst, 1, "blah");
  51. REQUIRE(num == 0);
  52. num = bx::strCopy(dst, 3, "blah", 3);
  53. REQUIRE(0 == bx::strCmp(dst, "bl") );
  54. REQUIRE(num == 2);
  55. num = bx::strCopy(dst, sizeof(dst), "blah", 3);
  56. REQUIRE(0 == bx::strCmp(dst, "bla") );
  57. REQUIRE(num == 3);
  58. num = bx::strCopy(dst, sizeof(dst), "blah");
  59. REQUIRE(0 == bx::strCmp(dst, "bl", 2) );
  60. REQUIRE(0 == bx::strCmp(dst, "blah") );
  61. REQUIRE(num == 4);
  62. }
  63. TEST_CASE("strCat", "[string]")
  64. {
  65. char dst[128] = { '\0' };
  66. REQUIRE(0 == bx::strCat(dst, 1, "cat") );
  67. REQUIRE(4 == bx::strCopy(dst, 5, "copy") );
  68. REQUIRE(3 == bx::strCat(dst, 8, "cat") );
  69. REQUIRE(0 == bx::strCmp(dst, "copycat") );
  70. REQUIRE(0 == bx::strCmp(dst, "copy", 4) );
  71. REQUIRE(1 == bx::strCat(dst, BX_COUNTOF(dst), "------", 1) );
  72. REQUIRE(3 == bx::strCat(dst, BX_COUNTOF(dst), "cat") );
  73. REQUIRE(0 == bx::strCmp(dst, "copycat-cat") );
  74. }
  75. TEST_CASE("strCmp", "[string]")
  76. {
  77. REQUIRE(0 < bx::strCmp("abvgd", "abv") );
  78. REQUIRE(0 < bx::strCmp("abvgd", "") );
  79. REQUIRE(0 > bx::strCmp("", "abvgd") );
  80. REQUIRE(0 != bx::strCmp(".tar.gz", ".") );
  81. REQUIRE(0 != bx::strCmp("meh", "meh/") );
  82. }
  83. TEST_CASE("strCmpI", "[string]")
  84. {
  85. REQUIRE(0 == bx::strCmpI("test", "test") );
  86. REQUIRE(0 == bx::strCmpI("test", "testestes", 4) );
  87. REQUIRE(0 == bx::strCmpI("testestes", "test", 4) );
  88. REQUIRE(0 != bx::strCmpI("preprocess", "platform") );
  89. const char* abvgd = "abvgd";
  90. const char* abvgx = "abvgx";
  91. const char* empty = "";
  92. REQUIRE(0 == bx::strCmpI(abvgd, abvgd) );
  93. REQUIRE(0 == bx::strCmpI(abvgd, abvgx, 4) );
  94. REQUIRE(0 == bx::strCmpI(empty, empty) );
  95. REQUIRE(0 > bx::strCmpI(abvgd, abvgx) );
  96. REQUIRE(0 > bx::strCmpI(empty, abvgd) );
  97. REQUIRE(0 < bx::strCmpI(abvgx, abvgd) );
  98. REQUIRE(0 < bx::strCmpI(abvgd, empty) );
  99. }
  100. TEST_CASE("strCmpV", "[string]")
  101. {
  102. REQUIRE(0 == bx::strCmpV("test", "test") );
  103. REQUIRE(0 == bx::strCmpV("test", "testestes", 4) );
  104. REQUIRE(0 == bx::strCmpV("testestes", "test", 4) );
  105. REQUIRE(0 != bx::strCmpV("preprocess", "platform") );
  106. const char* abvgd = "abvgd";
  107. const char* abvgx = "abvgx";
  108. const char* empty = "";
  109. REQUIRE(0 == bx::strCmpV(abvgd, abvgd) );
  110. REQUIRE(0 == bx::strCmpV(abvgd, abvgx, 4) );
  111. REQUIRE(0 == bx::strCmpV(empty, empty) );
  112. REQUIRE(0 > bx::strCmpV(abvgd, abvgx) );
  113. REQUIRE(0 > bx::strCmpV(empty, abvgd) );
  114. REQUIRE(0 < bx::strCmpV(abvgx, abvgd) );
  115. REQUIRE(0 < bx::strCmpV(abvgd, empty) );
  116. }
  117. static int32_t strCmpV(const void* _lhs, const void* _rhs)
  118. {
  119. const char* lhs = *(const char**)_lhs;
  120. const char* rhs = *(const char**)_rhs;
  121. int32_t result = bx::strCmpV(lhs, rhs);
  122. return result;
  123. }
  124. TEST_CASE("strCmpV sort", "[string][sort]")
  125. {
  126. const char* test[] =
  127. {
  128. "test_1.txt",
  129. "test_10.txt",
  130. "test_100.txt",
  131. "test_15.txt",
  132. "test_11.txt",
  133. "test_23.txt",
  134. "test_3.txt",
  135. };
  136. const char* expected[] =
  137. {
  138. "test_1.txt",
  139. "test_3.txt",
  140. "test_10.txt",
  141. "test_11.txt",
  142. "test_15.txt",
  143. "test_23.txt",
  144. "test_100.txt",
  145. };
  146. static_assert(BX_COUNTOF(test) == BX_COUNTOF(expected) );
  147. bx::quickSort(test, BX_COUNTOF(test), sizeof(const char*), strCmpV);
  148. for (uint32_t ii = 0; ii < BX_COUNTOF(test); ++ii)
  149. {
  150. REQUIRE(0 == bx::strCmp(test[ii], expected[ii]) );
  151. }
  152. }
  153. TEST_CASE("overlap", "[string]")
  154. {
  155. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  156. const bx::StringView quick = bx::strFind(test, "Quick");
  157. REQUIRE(bx::overlap(quick, test) );
  158. const bx::StringView empty;
  159. REQUIRE(!bx::overlap(quick, empty) );
  160. REQUIRE(!bx::overlap(test, empty) );
  161. }
  162. TEST_CASE("strRFind", "[string]")
  163. {
  164. const char* test = "test";
  165. REQUIRE(bx::strRFind(bx::StringView(test, 0), 's').isEmpty() );
  166. REQUIRE(bx::strRFind(bx::StringView(test, 1), 's').isEmpty() );
  167. REQUIRE(&test[2] == bx::strRFind(test, 's').getPtr() );
  168. REQUIRE(&test[3] == bx::strRFind(test, 't').getPtr() );
  169. }
  170. TEST_CASE("strFindI", "[string]")
  171. {
  172. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  173. REQUIRE(bx::strFindI(bx::StringView(test, 8), "quick").isEmpty() );
  174. REQUIRE(bx::strFindI(test, "quick1").isEmpty() );
  175. REQUIRE(&test[4] == bx::strFindI(bx::StringView(test, 9), "quick").getPtr() );
  176. REQUIRE(&test[4] == bx::strFindI(test, "quick").getPtr() );
  177. }
  178. TEST_CASE("strFind", "[string]")
  179. {
  180. {
  181. const char* test = "test";
  182. REQUIRE(bx::strFind(bx::StringView(test, 0), 's').isEmpty() );
  183. REQUIRE(bx::strFind(bx::StringView(test, 2), 's').isEmpty() );
  184. REQUIRE(&test[2] == bx::strFind(test, 's').getPtr() );
  185. }
  186. {
  187. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  188. REQUIRE(bx::strFind(bx::StringView(test, 8), "quick").isEmpty() );
  189. REQUIRE(bx::strFind(test, "quick1").isEmpty() );
  190. REQUIRE(bx::strFind(bx::StringView(test, 9), "quick").isEmpty() );
  191. REQUIRE(bx::strFind(test, "quick").isEmpty() );
  192. REQUIRE(bx::strFind(bx::StringView(test, 8), "Quick").isEmpty() );
  193. REQUIRE(bx::strFind(test, "Quick1").isEmpty() );
  194. REQUIRE(&test[4] == bx::strFind(bx::StringView(test, 9), "Quick").getPtr() );
  195. REQUIRE(&test[4] == bx::strFind(test, "Quick").getPtr() );
  196. REQUIRE(bx::strFind("vgd", 'a').isEmpty() );
  197. }
  198. {
  199. bx::StringView test = bx::strFind("a", "a");
  200. REQUIRE(test.getLength() == 1);
  201. REQUIRE(*test.getPtr() == 'a');
  202. }
  203. {
  204. bx::StringView test = bx::strFind("a", bx::StringView("a ", 1) );
  205. REQUIRE(test.getLength() == 1);
  206. REQUIRE(*test.getPtr() == 'a');
  207. }
  208. }
  209. TEST_CASE("strSkip", "[string]")
  210. {
  211. const bx::StringView t0(" test X");
  212. const bx::StringView t1 = bx::strLTrimSpace(t0);
  213. REQUIRE(0 == bx::strCmp(t1, "test", 4) );
  214. const bx::StringView t2 = bx::strLTrimNonSpace(t1);
  215. REQUIRE(0 == bx::strCmp(t2, " X", 2) );
  216. const bx::StringView t3("test");
  217. const bx::StringView t4 = bx::strLTrimNonSpace(t3);
  218. REQUIRE(t4.getTerm() == t4.getPtr() );
  219. }
  220. template<typename Ty>
  221. static bool testToStringS(Ty _value, const char* _expected, char _separator = '\0')
  222. {
  223. char tmp[1024];
  224. int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value, 10, _separator);
  225. int32_t len = (int32_t)bx::strLen(_expected);
  226. if (0 == bx::strCmp(tmp, _expected)
  227. && num == len)
  228. {
  229. return true;
  230. }
  231. printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
  232. return false;
  233. }
  234. TEST_CASE("toString intXX_t/uintXX_t", "[string]")
  235. {
  236. REQUIRE(testToStringS(0, "0") );
  237. REQUIRE(testToStringS(-256, "-256") );
  238. REQUIRE(testToStringS(INT32_MIN, "-2147483648") );
  239. REQUIRE(testToStringS(INT32_MAX, "2147483647") );
  240. REQUIRE(testToStringS(UINT32_MAX, "4294967295") );
  241. REQUIRE(testToStringS(INT64_MIN, "-9223372036854775808") );
  242. REQUIRE(testToStringS(INT64_MAX, "9223372036854775807") );
  243. REQUIRE(testToStringS(UINT64_MAX, "18446744073709551615") );
  244. REQUIRE(testToStringS(0, "0", ',') );
  245. REQUIRE(testToStringS(-256, "-256", ',') );
  246. REQUIRE(testToStringS(INT32_MAX, "2,147,483,647", ',') );
  247. REQUIRE(testToStringS(UINT32_MAX, "4,294,967,295", ',') );
  248. REQUIRE(testToStringS(INT64_MAX, "9,223,372,036,854,775,807", ',') );
  249. REQUIRE(testToStringS(UINT64_MAX, "18,446,744,073,709,551,615", ',') );
  250. }
  251. template<typename Ty>
  252. static bool testToString(Ty _value, const char* _expected)
  253. {
  254. char tmp[1024];
  255. int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
  256. int32_t len = (int32_t)bx::strLen(_expected);
  257. if (0 == bx::strCmp(tmp, _expected)
  258. && num == len)
  259. {
  260. return true;
  261. }
  262. printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
  263. return false;
  264. }
  265. TEST_CASE("toString double", "[string]")
  266. {
  267. REQUIRE(testToString(0.0, "0.0") );
  268. REQUIRE(testToString(-0.0, "-0.0") );
  269. REQUIRE(testToString(1.0, "1.0") );
  270. REQUIRE(testToString(-1.0, "-1.0") );
  271. REQUIRE(testToString(1.2345, "1.2345") );
  272. REQUIRE(testToString(1.2345678, "1.2345678") );
  273. REQUIRE(testToString(0.123456789012, "0.123456789012") );
  274. REQUIRE(testToString(1234567.8, "1234567.8") );
  275. REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
  276. REQUIRE(testToString(0.000001, "0.000001") );
  277. REQUIRE(testToString(0.0000001, "1e-7") );
  278. REQUIRE(testToString(1e30, "1e30") );
  279. REQUIRE(testToString(1.234567890123456e30, "1.234567890123456e30") );
  280. REQUIRE(testToString(-5e-324, "-5e-324") );
  281. REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
  282. REQUIRE(testToString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  283. REQUIRE(testToString(1.7976931348623157e308, "1.7976931348623157e308") );
  284. REQUIRE(testToString(0.00000123123123, "0.00000123123123") );
  285. REQUIRE(testToString(0.000000123123123, "1.23123123e-7") );
  286. REQUIRE(testToString(123123.123, "123123.123") );
  287. REQUIRE(testToString(1231231.23, "1231231.23") );
  288. REQUIRE(testToString(0.000000000123123, "1.23123e-10") );
  289. REQUIRE(testToString(0.0000000001, "1e-10") );
  290. REQUIRE(testToString(-270.000000, "-270.0") );
  291. REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
  292. REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
  293. REQUIRE(testToString(-1.234567e-9, "-1.234567e-9") );
  294. }
  295. template<typename Ty>
  296. static bool testFromString(Ty _value, const char* _input)
  297. {
  298. char tmp[1024];
  299. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  300. Ty lhs;
  301. bx::fromString(&lhs, tmp);
  302. Ty rhs;
  303. bx::fromString(&rhs, _input);
  304. if (lhs == rhs)
  305. {
  306. return true;
  307. }
  308. printf("result '%f', input '%s'\n", _value, _input);
  309. return false;
  310. }
  311. TEST_CASE("fromString float", "[string]")
  312. {
  313. REQUIRE(testFromString<float>(std::numeric_limits<float>::min(), "1.175494351e-38") );
  314. REQUIRE(testFromString<float>(std::numeric_limits<float>::lowest(), "-3.402823466e+38") );
  315. REQUIRE(testFromString<float>(std::numeric_limits<float>::max(), "3.402823466e+38") );
  316. }
  317. TEST_CASE("fromString double", "[string]")
  318. {
  319. REQUIRE(testFromString<double>(0.0, "0.0") );
  320. REQUIRE(testFromString<double>(-0.0, "-0.0") );
  321. REQUIRE(testFromString<double>(1.0, "1.0") );
  322. REQUIRE(testFromString<double>(-1.0, "-1.0") );
  323. REQUIRE(testFromString<double>(1.2345, "1.2345") );
  324. REQUIRE(testFromString<double>(1.2345678, "1.2345678") );
  325. REQUIRE(testFromString<double>(0.123456789012, "0.123456789012") );
  326. REQUIRE(testFromString<double>(123456.789, "123456.789") );
  327. REQUIRE(testFromString<double>(1234567.8, "1234567.8") );
  328. REQUIRE(testFromString<double>(-79.39773355813419, "-79.39773355813419") );
  329. REQUIRE(testFromString<double>(0.000001, "0.000001") );
  330. REQUIRE(testFromString<double>(0.0000001, "1e-7") );
  331. REQUIRE(testFromString<double>(1e30, "1e30") );
  332. REQUIRE(testFromString<double>(1.234567890123456e30, "1.234567890123456e30") );
  333. REQUIRE(testFromString<double>(-5e-324, "-5e-324") );
  334. REQUIRE(testFromString<double>(2.225073858507201e-308, "2.225073858507201e-308") );
  335. REQUIRE(testFromString<double>(2.2250738585072014e-308, "2.2250738585072014e-308") );
  336. REQUIRE(testFromString<double>(1.7976931348623157e308, "1.7976931348623157e308") );
  337. REQUIRE(testFromString<double>(0.00000123123123, "0.00000123123123") );
  338. REQUIRE(testFromString<double>(0.000000123123123, "1.23123123e-7") );
  339. REQUIRE(testFromString<double>(123123.123, "123123.123") );
  340. REQUIRE(testFromString<double>(1231231.23, "1231231.23") );
  341. REQUIRE(testFromString<double>(0.000000000123123, "1.23123e-10") );
  342. REQUIRE(testFromString<double>(0.0000000001, "1e-10") );
  343. REQUIRE(testFromString<double>(-270.000000, "-270.0") );
  344. REQUIRE(testFromString<double>(2.2250738585072011e-308, "2.2250738585072011e-308") ); // https://web.archive.org/web/20181112222123/https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
  345. REQUIRE(testFromString<double>(2.2250738585072009e-308, "2.2250738585072009e-308") ); // Max subnormal double
  346. REQUIRE(testFromString<double>(4.9406564584124654e-324, "4.9406564584124654e-324") ); // Min denormal
  347. REQUIRE(testFromString<double>(1.7976931348623157e+308, "1.7976931348623157e+308") ); // Max double
  348. // warning: magnitude of floating-point constant too small for type 'double'; minimum is 4.9406564584124654E-324
  349. // REQUIRE(testFromString<double>(1e-10000, "0.0") ); // Must underflow
  350. // integer literal is too large to be represented in any integer type
  351. // REQUIRE(testFromString<double>(18446744073709551616, "18446744073709551616.0") ); // 2^64 (max of uint64_t + 1, force to use double)
  352. // REQUIRE(testFromString<double>(-9223372036854775809, "-9223372036854775809.0") ); // -2^63 - 1(min of int64_t + 1, force to use double)
  353. REQUIRE(testFromString<double>(0.9868011474609375, "0.9868011474609375") ); // https://github.com/miloyip/rapidjson/issues/120
  354. REQUIRE(testFromString<double>(123e34, "123e34") );
  355. REQUIRE(testFromString<double>(45913141877270640000.0, "45913141877270640000.0") );
  356. REQUIRE(testFromString<double>(std::numeric_limits<double>::min(), "2.2250738585072014e-308") );
  357. REQUIRE(testFromString<double>(std::numeric_limits<double>::lowest(), "-1.7976931348623158e+308") );
  358. REQUIRE(testFromString<double>(std::numeric_limits<double>::max(), "1.7976931348623158e+308") );
  359. }
  360. static bool testFromString(int32_t _value, const char* _input)
  361. {
  362. char tmp[1024];
  363. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  364. double lhs;
  365. bx::fromString(&lhs, tmp);
  366. double rhs;
  367. bx::fromString(&rhs, _input);
  368. if (lhs == rhs)
  369. {
  370. return true;
  371. }
  372. printf("result '%d', input '%s'\n", _value, _input);
  373. return false;
  374. }
  375. TEST_CASE("fromString int32_t", "[string]")
  376. {
  377. REQUIRE(testFromString(1389, "1389") );
  378. REQUIRE(testFromString(1389, " 1389") );
  379. REQUIRE(testFromString(1389, "+1389") );
  380. REQUIRE(testFromString(-1389, "-1389") );
  381. REQUIRE(testFromString(-1389, " -1389") );
  382. REQUIRE(testFromString(555333, "555333") );
  383. REQUIRE(testFromString(-21, "-021") );
  384. }
  385. TEST_CASE("StringLiteral", "[string]")
  386. {
  387. constexpr bx::StringLiteral tmp[] = { "1389", "abvgd", "mac", "pod" };
  388. REQUIRE(bx::isSorted(tmp, BX_COUNTOF(tmp) ) );
  389. STATIC_REQUIRE(4 == tmp[0].getLength() );
  390. STATIC_REQUIRE(4 == bx::strLen(tmp[0]) );
  391. REQUIRE(0 == bx::strCmp("1389", tmp[0]) );
  392. STATIC_REQUIRE(5 == tmp[1].getLength() );
  393. STATIC_REQUIRE(5 == bx::strLen(tmp[1]) );
  394. REQUIRE(0 == bx::strCmp("abvgd", tmp[1]) );
  395. STATIC_REQUIRE(3 == tmp[2].getLength() );
  396. STATIC_REQUIRE(3 == bx::strLen(tmp[2]) );
  397. REQUIRE(0 == bx::strCmp("mac", tmp[2]) );
  398. STATIC_REQUIRE(3 == tmp[3].getLength() );
  399. STATIC_REQUIRE(3 == bx::strLen(tmp[3]) );
  400. REQUIRE(0 == bx::strCmp("pod", tmp[3]) );
  401. constexpr bx::StringLiteral copy(tmp[0]);
  402. STATIC_REQUIRE(4 == copy.getLength() );
  403. REQUIRE(4 == bx::strLen(copy) );
  404. REQUIRE(0 == bx::strCmp("1389", copy) );
  405. constexpr bx::StringView sv(tmp[1]);
  406. STATIC_REQUIRE(5 == sv.getLength() );
  407. STATIC_REQUIRE(5 == bx::strLen(sv) );
  408. STATIC_REQUIRE("abvgd" == sv);
  409. }
  410. TEST_CASE("StringView constexpr", "[string]")
  411. {
  412. constexpr bx::StringView sv("1389");
  413. STATIC_REQUIRE(sv == "1389");
  414. STATIC_REQUIRE(4 == bx::strLen(sv) );
  415. }
  416. TEST_CASE("StringView", "[string]")
  417. {
  418. bx::StringView sv("test");
  419. REQUIRE(4 == sv.getLength() );
  420. bx::DefaultAllocator crt;
  421. g_allocator = &crt;
  422. typedef bx::StringT<&g_allocator> String;
  423. String st(sv);
  424. REQUIRE(4 == st.getLength() );
  425. st.append("test");
  426. REQUIRE(8 == st.getLength() );
  427. st.append(bx::StringView("test", 2) );
  428. REQUIRE(10 == st.getLength() );
  429. REQUIRE(0 == bx::strCmp(st.getCPtr(), "testtestte") );
  430. st.clear();
  431. REQUIRE(0 == st.getLength() );
  432. REQUIRE(4 == sv.getLength() );
  433. st.append("test");
  434. REQUIRE(4 == st.getLength() );
  435. sv.clear();
  436. REQUIRE(0 == sv.getLength() );
  437. }
  438. TEST_CASE("Trim", "[string]")
  439. {
  440. REQUIRE(bx::strLTrim("a", "a").isEmpty() );
  441. REQUIRE(0 == bx::strCmp(bx::strLTrim("aba", "a"), "ba") );
  442. REQUIRE(bx::strRTrim("a", "a").isEmpty() );
  443. REQUIRE(0 == bx::strCmp(bx::strRTrim("aba", "a"), "ab") );
  444. REQUIRE(bx::strTrim("a", "a").isEmpty() );
  445. REQUIRE(0 == bx::strCmp(bx::strTrim("aba", "a"), "b") );
  446. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "ab"), "vgd") );
  447. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vagbd"), "") );
  448. REQUIRE(0 == bx::strCmp(bx::strTrimPrefix("abvgd", "vagbd"), "abvgd") );
  449. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vgd"), "abvgd") );
  450. REQUIRE(0 == bx::strCmp(bx::strLTrim("/555333/podmac/", "/"), "555333/podmac/") );
  451. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "vagbd"), "") );
  452. REQUIRE(0 == bx::strCmp(bx::strTrimSuffix("abvgd", "vagbd"), "abvgd") );
  453. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "abv"), "abvgd") );
  454. REQUIRE(0 == bx::strCmp(bx::strRTrim("/555333/podmac/", "/"), "/555333/podmac") );
  455. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", "da"), "bvg") );
  456. REQUIRE(0 == bx::strCmp(bx::strTrim("<1389>", "<>"), "1389") );
  457. REQUIRE(0 == bx::strCmp(bx::strTrim("/555333/podmac/", "/"), "555333/podmac") );
  458. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", ""), "abvgd") );
  459. REQUIRE(0 == bx::strCmp(bx::strTrim(" \t a b\tv g d \t ", " \t"), "a b\tv g d") );
  460. bx::FilePath uri("/555333/podmac/");
  461. REQUIRE(0 == bx::strCmp(bx::strTrim(uri.getPath(), "/"), "555333/podmac") );
  462. }
  463. TEST_CASE("TrimSpace", "[string]")
  464. {
  465. REQUIRE(bx::strLTrimSpace("").isEmpty() );
  466. REQUIRE(bx::strRTrimSpace("").isEmpty() );
  467. REQUIRE(bx::strTrimSpace( "").isEmpty() );
  468. REQUIRE(bx::strLTrimSpace("\n").isEmpty() );
  469. REQUIRE(bx::strRTrimSpace("\n").isEmpty() );
  470. REQUIRE(bx::strTrimSpace( "\n").isEmpty() );
  471. const bx::StringView t0("1389");
  472. const bx::StringView t1(" 1389");
  473. const bx::StringView t2("1389 ");
  474. const bx::StringView t3(" 1389 ");
  475. REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t0), t0) );
  476. REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t1), t0) );
  477. REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t2), t2) );
  478. REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t3), "1389 ") );
  479. REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t0), t0) );
  480. REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t1), t1) );
  481. REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t2), t0) );
  482. REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t3), " 1389") );
  483. REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t0), t0) );
  484. REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t1), t0) );
  485. REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t2), t0) );
  486. REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t3), t0) );
  487. }
  488. TEST_CASE("strWord", "[string]")
  489. {
  490. REQUIRE(bx::strWord(" abvgd-1389.0").isEmpty() );
  491. REQUIRE(0 == bx::strCmp(bx::strWord("abvgd-1389.0"), "abvgd") );
  492. }
  493. TEST_CASE("strFindBlock", "[string]")
  494. {
  495. const bx::StringView test0("{ { {} {} abvgd; {} } }");
  496. const bx::StringView test1(test0, 1, INT32_MAX);
  497. bx::StringView result = bx::strFindBlock(test1, '{', '}');
  498. REQUIRE(19 == result.getLength() );
  499. }
  500. TEST_CASE("prefix", "[string]")
  501. {
  502. REQUIRE( bx::hasPrefix("abvgd-1389.0", "abv") );
  503. REQUIRE(!bx::hasPrefix("abvgd-1389.0", "bvg") );
  504. REQUIRE( bx::hasPrefix("abvgd-1389.0", "") );
  505. REQUIRE(0 == bx::strCmp(bx::strTrimPrefix("abvgd-1389.0", "abv"), "gd-1389.0") );
  506. REQUIRE(0 == bx::strCmp(bx::strTrimPrefix("abvgd-1389.0", "xyz"), "abvgd-1389.0") );
  507. }
  508. TEST_CASE("suffix", "[string]")
  509. {
  510. REQUIRE( bx::hasSuffix("abvgd-1389.0", "389.0") );
  511. REQUIRE(!bx::hasSuffix("abvgd-1389.0", "1389") );
  512. REQUIRE( bx::hasSuffix("abvgd-1389.0", "") );
  513. REQUIRE(0 == bx::strCmp(bx::strTrimSuffix("abvgd-1389.0", "389.0"), "abvgd-1") );
  514. REQUIRE(0 == bx::strCmp(bx::strTrimSuffix("abvgd-1389.0", "xyz"), "abvgd-1389.0") );
  515. }
  516. TEST_CASE("0terminated", "[string]")
  517. {
  518. const bx::StringView t0("1389");
  519. REQUIRE(t0.is0Terminated() );
  520. const bx::StringView t1(strTrimPrefix(t0, "13") );
  521. REQUIRE(!t1.is0Terminated() );
  522. const bx::StringView t2(strTrimSuffix(t0, "89") );
  523. REQUIRE(!t2.is0Terminated() );
  524. bx::DefaultAllocator crt;
  525. g_allocator = &crt;
  526. typedef bx::StringT<&g_allocator> String;
  527. String st;
  528. st = strTrimPrefix(t0, "13");
  529. REQUIRE(2 == st.getLength() );
  530. st = strTrimSuffix(t0, "89");
  531. REQUIRE(2 == st.getLength() );
  532. }
  533. TEST_CASE("FixedStringT", "[string]")
  534. {
  535. bx::FixedString64 fs64("1389");
  536. bx::FixedString256 fs256(fs64);
  537. REQUIRE(0 == strCmp(fs64, fs256) );
  538. REQUIRE(0 == strCmp(fs64, "1389") );
  539. REQUIRE(0 == strCmp(fs256, "1389") );
  540. fs64.append("9831");
  541. REQUIRE(8 == fs64.getLength() );
  542. REQUIRE(0 != strCmp(fs64, fs256) );
  543. REQUIRE(0 == strCmp(fs64, "13899831") );
  544. }
  545. TEST(tinystl_string_constructor)
  546. {
  547. using tinystl::string;
  548. {
  549. string s;
  550. CHECK( s.size() == 0 );
  551. }
  552. {
  553. string s("hello");
  554. CHECK( s.size() == 5 );
  555. CHECK( 0 == strcmp(s.c_str(), "hello") );
  556. }
  557. {
  558. string s("hello world", 5);
  559. CHECK( s.size() == 5 );
  560. CHECK( 0 == strcmp(s.c_str(), "hello") );
  561. }
  562. {
  563. const string other("hello");
  564. string s = other;
  565. CHECK( s.size() == 5 );
  566. CHECK( 0 == strcmp(s.c_str(), "hello") );
  567. }
  568. {
  569. string other("hello");
  570. string s = std::move(other);
  571. CHECK( s.size() == 5 );
  572. CHECK( 0 == strcmp(s.c_str(), "hello") );
  573. CHECK( other.size() == 0 );
  574. }
  575. }
  576. TEST(tinystl_string_assign)
  577. {
  578. using tinystl::string;
  579. {
  580. const string other("hello");
  581. string s("new");
  582. s = other;
  583. CHECK( s.size() == 5 );
  584. CHECK( 0 == strcmp(s.c_str(), "hello") );
  585. }
  586. {
  587. string other("hello");
  588. string s("new");
  589. s = std::move(other);
  590. CHECK( s.size() == 5 );
  591. CHECK( 0 == strcmp(s.c_str(), "hello") );
  592. CHECK( other.size() == 0 );
  593. }
  594. {
  595. const string other("hello longer string here");
  596. string s("short");
  597. s = other;
  598. CHECK( s.size() == 24 );
  599. CHECK( 0 == strcmp(s.c_str(), "hello longer string here") );
  600. }
  601. {
  602. string other("hello longer string here");
  603. string s("short");
  604. s = std::move(other);
  605. CHECK( s.size() == 24 );
  606. CHECK( 0 == strcmp(s.c_str(), "hello longer string here") );
  607. CHECK( other.size() == 0 );
  608. }
  609. {
  610. const string other("short");
  611. string s("hello longer string here");
  612. s = other;
  613. CHECK( s.size() == 5 );
  614. CHECK( 0 == strcmp(s.c_str(), "short") );
  615. }
  616. {
  617. string other("short");
  618. string s("hello longer string here");
  619. s = std::move(other);
  620. CHECK( s.size() == 5 );
  621. CHECK( 0 == strcmp(s.c_str(), "short") );
  622. CHECK( other.size() == 0 );
  623. }
  624. }