2
0

string_test.cpp 21 KB

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