string_test.cpp 23 KB

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