2
0

string_test.cpp 19 KB

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