string_test.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright 2010-2021 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  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), "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. }
  282. template<typename Ty>
  283. static bool testFromString(Ty _value, const char* _input)
  284. {
  285. char tmp[1024];
  286. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  287. Ty lhs;
  288. bx::fromString(&lhs, tmp);
  289. Ty rhs;
  290. bx::fromString(&rhs, _input);
  291. if (lhs == rhs)
  292. {
  293. return true;
  294. }
  295. printf("result '%f', input '%s'\n", _value, _input);
  296. return false;
  297. }
  298. TEST_CASE("fromString float", "")
  299. {
  300. REQUIRE(testFromString<float>(std::numeric_limits<float>::min(), "1.175494351e-38") );
  301. REQUIRE(testFromString<float>(std::numeric_limits<float>::lowest(), "-3.402823466e+38") );
  302. REQUIRE(testFromString<float>(std::numeric_limits<float>::max(), "3.402823466e+38") );
  303. }
  304. TEST_CASE("fromString double", "")
  305. {
  306. REQUIRE(testFromString<double>(0.0, "0.0") );
  307. REQUIRE(testFromString<double>(-0.0, "-0.0") );
  308. REQUIRE(testFromString<double>(1.0, "1.0") );
  309. REQUIRE(testFromString<double>(-1.0, "-1.0") );
  310. REQUIRE(testFromString<double>(1.2345, "1.2345") );
  311. REQUIRE(testFromString<double>(1.2345678, "1.2345678") );
  312. REQUIRE(testFromString<double>(0.123456789012, "0.123456789012") );
  313. REQUIRE(testFromString<double>(123456.789, "123456.789") );
  314. REQUIRE(testFromString<double>(1234567.8, "1234567.8") );
  315. REQUIRE(testFromString<double>(-79.39773355813419, "-79.39773355813419") );
  316. REQUIRE(testFromString<double>(0.000001, "0.000001") );
  317. REQUIRE(testFromString<double>(0.0000001, "1e-7") );
  318. REQUIRE(testFromString<double>(1e30, "1e30") );
  319. REQUIRE(testFromString<double>(1.234567890123456e30, "1.234567890123456e30") );
  320. REQUIRE(testFromString<double>(-5e-324, "-5e-324") );
  321. REQUIRE(testFromString<double>(2.225073858507201e-308, "2.225073858507201e-308") );
  322. REQUIRE(testFromString<double>(2.2250738585072014e-308, "2.2250738585072014e-308") );
  323. REQUIRE(testFromString<double>(1.7976931348623157e308, "1.7976931348623157e308") );
  324. REQUIRE(testFromString<double>(0.00000123123123, "0.00000123123123") );
  325. REQUIRE(testFromString<double>(0.000000123123123, "1.23123123e-7") );
  326. REQUIRE(testFromString<double>(123123.123, "123123.123") );
  327. REQUIRE(testFromString<double>(1231231.23, "1231231.23") );
  328. REQUIRE(testFromString<double>(0.000000000123123, "1.23123e-10") );
  329. REQUIRE(testFromString<double>(0.0000000001, "1e-10") );
  330. REQUIRE(testFromString<double>(-270.000000, "-270.0") );
  331. 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/
  332. REQUIRE(testFromString<double>(2.2250738585072009e-308, "2.2250738585072009e-308") ); // Max subnormal double
  333. REQUIRE(testFromString<double>(4.9406564584124654e-324, "4.9406564584124654e-324") ); // Min denormal
  334. REQUIRE(testFromString<double>(1.7976931348623157e+308, "1.7976931348623157e+308") ); // Max double
  335. // warning: magnitude of floating-point constant too small for type 'double'; minimum is 4.9406564584124654E-324
  336. // REQUIRE(testFromString<double>(1e-10000, "0.0") ); // Must underflow
  337. // integer literal is too large to be represented in any integer type
  338. // REQUIRE(testFromString<double>(18446744073709551616, "18446744073709551616.0") ); // 2^64 (max of uint64_t + 1, force to use double)
  339. // REQUIRE(testFromString<double>(-9223372036854775809, "-9223372036854775809.0") ); // -2^63 - 1(min of int64_t + 1, force to use double)
  340. REQUIRE(testFromString<double>(0.9868011474609375, "0.9868011474609375") ); // https://github.com/miloyip/rapidjson/issues/120
  341. REQUIRE(testFromString<double>(123e34, "123e34") );
  342. REQUIRE(testFromString<double>(45913141877270640000.0, "45913141877270640000.0") );
  343. REQUIRE(testFromString<double>(std::numeric_limits<double>::min(), "2.2250738585072014e-308") );
  344. REQUIRE(testFromString<double>(std::numeric_limits<double>::lowest(), "-1.7976931348623158e+308") );
  345. REQUIRE(testFromString<double>(std::numeric_limits<double>::max(), "1.7976931348623158e+308") );
  346. }
  347. static bool testFromString(int32_t _value, const char* _input)
  348. {
  349. char tmp[1024];
  350. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  351. double lhs;
  352. bx::fromString(&lhs, tmp);
  353. double rhs;
  354. bx::fromString(&rhs, _input);
  355. if (lhs == rhs)
  356. {
  357. return true;
  358. }
  359. printf("result '%d', input '%s'\n", _value, _input);
  360. return false;
  361. }
  362. TEST_CASE("fromString int32_t", "")
  363. {
  364. REQUIRE(testFromString(1389, "1389") );
  365. REQUIRE(testFromString(1389, " 1389") );
  366. REQUIRE(testFromString(1389, "+1389") );
  367. REQUIRE(testFromString(-1389, "-1389") );
  368. REQUIRE(testFromString(-1389, " -1389") );
  369. REQUIRE(testFromString(555333, "555333") );
  370. REQUIRE(testFromString(-21, "-021") );
  371. }
  372. TEST_CASE("StringView", "")
  373. {
  374. bx::StringView sv("test");
  375. REQUIRE(4 == sv.getLength() );
  376. bx::DefaultAllocator crt;
  377. g_allocator = &crt;
  378. typedef bx::StringT<&g_allocator> String;
  379. String st(sv);
  380. REQUIRE(4 == st.getLength() );
  381. st.append("test");
  382. REQUIRE(8 == st.getLength() );
  383. st.append(bx::StringView("test", 2) );
  384. REQUIRE(10 == st.getLength() );
  385. REQUIRE(0 == bx::strCmp(st.getPtr(), "testtestte") );
  386. st.clear();
  387. REQUIRE(0 == st.getLength() );
  388. REQUIRE(4 == sv.getLength() );
  389. st.append("test");
  390. REQUIRE(4 == st.getLength() );
  391. sv.clear();
  392. REQUIRE(0 == sv.getLength() );
  393. }
  394. TEST_CASE("Trim", "")
  395. {
  396. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "ab"), "vgd") );
  397. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vagbd"), "abvgd") );
  398. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vgd"), "abvgd") );
  399. REQUIRE(0 == bx::strCmp(bx::strLTrim("/555333/podmac/", "/"), "555333/podmac/") );
  400. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "vagbd"), "abvgd") );
  401. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "abv"), "abvgd") );
  402. REQUIRE(0 == bx::strCmp(bx::strRTrim("/555333/podmac/", "/"), "/555333/podmac") );
  403. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", "da"), "bvg") );
  404. REQUIRE(0 == bx::strCmp(bx::strTrim("<1389>", "<>"), "1389") );
  405. REQUIRE(0 == bx::strCmp(bx::strTrim("/555333/podmac/", "/"), "555333/podmac") );
  406. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", ""), "abvgd") );
  407. REQUIRE(0 == bx::strCmp(bx::strTrim(" \t a b\tv g d \t ", " \t"), "a b\tv g d") );
  408. bx::FilePath uri("/555333/podmac/");
  409. REQUIRE(0 == bx::strCmp(bx::strTrim(uri.getPath(), "/"), "555333/podmac") );
  410. }
  411. TEST_CASE("TrimSpace", "")
  412. {
  413. REQUIRE(bx::strLTrimSpace("").isEmpty() );
  414. REQUIRE(bx::strRTrimSpace("").isEmpty() );
  415. REQUIRE(bx::strTrimSpace( "").isEmpty() );
  416. const bx::StringView t0("1389");
  417. const bx::StringView t1(" 1389");
  418. const bx::StringView t2("1389 ");
  419. const bx::StringView t3(" 1389 ");
  420. REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t0), t0) );
  421. REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t1), t0) );
  422. REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t2), t2) );
  423. REQUIRE(0 == bx::strCmp(bx::strLTrimSpace(t3), "1389 ") );
  424. REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t0), t0) );
  425. REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t1), t1) );
  426. REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t2), t0) );
  427. REQUIRE(0 == bx::strCmp(bx::strRTrimSpace(t3), " 1389") );
  428. REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t0), t0) );
  429. REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t1), t0) );
  430. REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t2), t0) );
  431. REQUIRE(0 == bx::strCmp(bx::strTrimSpace(t3), t0) );
  432. }
  433. TEST_CASE("strWord", "")
  434. {
  435. REQUIRE(bx::strWord(" abvgd-1389.0").isEmpty() );
  436. REQUIRE(0 == bx::strCmp(bx::strWord("abvgd-1389.0"), "abvgd") );
  437. }
  438. TEST_CASE("strFindBlock", "")
  439. {
  440. const bx::StringView test0("{ { {} {} abvgd; {} } }");
  441. const bx::StringView test1(test0, 1);
  442. bx::StringView result = bx::strFindBlock(test1, '{', '}');
  443. REQUIRE(19 == result.getLength() );
  444. }
  445. TEST_CASE("hasPrefix", "")
  446. {
  447. REQUIRE( bx::hasPrefix("abvgd-1389.0", "abv") );
  448. REQUIRE(!bx::hasPrefix("abvgd-1389.0", "bvg") );
  449. REQUIRE( bx::hasPrefix("abvgd-1389.0", "") );
  450. }
  451. TEST_CASE("hasSuffix", "")
  452. {
  453. REQUIRE( bx::hasSuffix("abvgd-1389.0", "389.0") );
  454. REQUIRE(!bx::hasSuffix("abvgd-1389.0", "1389") );
  455. REQUIRE( bx::hasSuffix("abvgd-1389.0", "") );
  456. }