string_test.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright 2010-2018 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("meh", "meh/") );
  77. }
  78. TEST_CASE("strCmpI", "")
  79. {
  80. REQUIRE(0 == bx::strCmpI("test", "test") );
  81. REQUIRE(0 == bx::strCmpI("test", "testestes", 4) );
  82. REQUIRE(0 == bx::strCmpI("testestes", "test", 4) );
  83. REQUIRE(0 != bx::strCmpI("preprocess", "platform") );
  84. const char* abvgd = "abvgd";
  85. const char* abvgx = "abvgx";
  86. const char* empty = "";
  87. REQUIRE(0 == bx::strCmpI(abvgd, abvgd) );
  88. REQUIRE(0 == bx::strCmpI(abvgd, abvgx, 4) );
  89. REQUIRE(0 == bx::strCmpI(empty, empty) );
  90. REQUIRE(0 > bx::strCmpI(abvgd, abvgx) );
  91. REQUIRE(0 > bx::strCmpI(empty, abvgd) );
  92. REQUIRE(0 < bx::strCmpI(abvgx, abvgd) );
  93. REQUIRE(0 < bx::strCmpI(abvgd, empty) );
  94. }
  95. TEST_CASE("strCmpV", "")
  96. {
  97. REQUIRE(0 == bx::strCmpV("test", "test") );
  98. REQUIRE(0 == bx::strCmpV("test", "testestes", 4) );
  99. REQUIRE(0 == bx::strCmpV("testestes", "test", 4) );
  100. REQUIRE(0 != bx::strCmpV("preprocess", "platform") );
  101. const char* abvgd = "abvgd";
  102. const char* abvgx = "abvgx";
  103. const char* empty = "";
  104. REQUIRE(0 == bx::strCmpV(abvgd, abvgd) );
  105. REQUIRE(0 == bx::strCmpV(abvgd, abvgx, 4) );
  106. REQUIRE(0 == bx::strCmpV(empty, empty) );
  107. REQUIRE(0 > bx::strCmpV(abvgd, abvgx) );
  108. REQUIRE(0 > bx::strCmpV(empty, abvgd) );
  109. REQUIRE(0 < bx::strCmpV(abvgx, abvgd) );
  110. REQUIRE(0 < bx::strCmpV(abvgd, empty) );
  111. }
  112. static int32_t strCmpV(const void* _lhs, const void* _rhs)
  113. {
  114. const char* lhs = *(const char**)_lhs;
  115. const char* rhs = *(const char**)_rhs;
  116. int32_t result = bx::strCmpV(lhs, rhs);
  117. return result;
  118. }
  119. TEST_CASE("strCmpV sort", "")
  120. {
  121. const char* test[] =
  122. {
  123. "test_1.txt",
  124. "test_10.txt",
  125. "test_100.txt",
  126. "test_15.txt",
  127. "test_11.txt",
  128. "test_23.txt",
  129. "test_3.txt",
  130. };
  131. const char* expected[] =
  132. {
  133. "test_1.txt",
  134. "test_3.txt",
  135. "test_10.txt",
  136. "test_11.txt",
  137. "test_15.txt",
  138. "test_23.txt",
  139. "test_100.txt",
  140. };
  141. BX_STATIC_ASSERT(BX_COUNTOF(test) == BX_COUNTOF(expected) );
  142. bx::quickSort(test, BX_COUNTOF(test), sizeof(const char*), strCmpV);
  143. for (uint32_t ii = 0; ii < BX_COUNTOF(test); ++ii)
  144. {
  145. REQUIRE(0 == bx::strCmp(test[ii], expected[ii]) );
  146. }
  147. }
  148. TEST_CASE("strRFind", "")
  149. {
  150. const char* test = "test";
  151. REQUIRE(bx::strRFind(bx::StringView(test, 0), 's').isEmpty() );
  152. REQUIRE(bx::strRFind(bx::StringView(test, 1), 's').isEmpty() );
  153. REQUIRE(&test[2] == bx::strRFind(test, 's').getPtr() );
  154. }
  155. TEST_CASE("strFindI", "")
  156. {
  157. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  158. REQUIRE(bx::strFindI(bx::StringView(test, 8), "quick").isEmpty() );
  159. REQUIRE(bx::strFindI(test, "quick1").isEmpty() );
  160. REQUIRE(&test[4] == bx::strFindI(bx::StringView(test, 9), "quick").getPtr() );
  161. REQUIRE(&test[4] == bx::strFindI(test, "quick").getPtr() );
  162. }
  163. TEST_CASE("strFind", "")
  164. {
  165. {
  166. const char* test = "test";
  167. REQUIRE(bx::strFind(bx::StringView(test, 0), 's').isEmpty() );
  168. REQUIRE(bx::strFind(bx::StringView(test, 2), 's').isEmpty() );
  169. REQUIRE(&test[2] == bx::strFind(test, 's').getPtr() );
  170. }
  171. {
  172. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  173. REQUIRE(bx::strFind(bx::StringView(test, 8), "quick").isEmpty() );
  174. REQUIRE(bx::strFind(test, "quick1").isEmpty() );
  175. REQUIRE(bx::strFind(bx::StringView(test, 9), "quick").isEmpty() );
  176. REQUIRE(bx::strFind(test, "quick").isEmpty() );
  177. REQUIRE(bx::strFind(bx::StringView(test, 8), "Quick").isEmpty() );
  178. REQUIRE(bx::strFind(test, "Quick1").isEmpty() );
  179. REQUIRE(&test[4] == bx::strFind(bx::StringView(test, 9), "Quick").getPtr() );
  180. REQUIRE(&test[4] == bx::strFind(test, "Quick").getPtr() );
  181. REQUIRE(bx::strFind("vgd", 'a').isEmpty() );
  182. }
  183. }
  184. TEST_CASE("strSkip", "")
  185. {
  186. const bx::StringView t0(" test X");
  187. const bx::StringView t1 = bx::strLTrimSpace(t0);
  188. REQUIRE(0 == bx::strCmp(t1, "test", 4) );
  189. const bx::StringView t2 = bx::strLTrimNonSpace(t1);
  190. REQUIRE(0 == bx::strCmp(t2, " X", 2) );
  191. const bx::StringView t3("test");
  192. const bx::StringView t4 = bx::strLTrimNonSpace(t3);
  193. REQUIRE(t4.getTerm() == t4.getPtr() );
  194. }
  195. template<typename Ty>
  196. static bool testToStringS(Ty _value, const char* _expected, char _separator = '\0')
  197. {
  198. char tmp[1024];
  199. int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value, 10, _separator);
  200. int32_t len = (int32_t)bx::strLen(_expected);
  201. if (0 == bx::strCmp(tmp, _expected)
  202. && num == len)
  203. {
  204. return true;
  205. }
  206. printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
  207. return false;
  208. }
  209. TEST_CASE("toString intXX_t/uintXX_t", "")
  210. {
  211. REQUIRE(testToStringS(0, "0") );
  212. REQUIRE(testToStringS(-256, "-256") );
  213. REQUIRE(testToStringS(INT32_MAX, "2147483647") );
  214. REQUIRE(testToStringS(UINT32_MAX, "4294967295") );
  215. REQUIRE(testToStringS(INT64_MAX, "9223372036854775807") );
  216. REQUIRE(testToStringS(UINT64_MAX, "18446744073709551615") );
  217. REQUIRE(testToStringS(0, "0", ',') );
  218. REQUIRE(testToStringS(-256, "-256", ',') );
  219. REQUIRE(testToStringS(INT32_MAX, "2,147,483,647", ',') );
  220. REQUIRE(testToStringS(UINT32_MAX, "4,294,967,295", ',') );
  221. REQUIRE(testToStringS(INT64_MAX, "9,223,372,036,854,775,807", ',') );
  222. REQUIRE(testToStringS(UINT64_MAX, "18,446,744,073,709,551,615", ',') );
  223. }
  224. template<typename Ty>
  225. static bool testToString(Ty _value, const char* _expected)
  226. {
  227. char tmp[1024];
  228. int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
  229. int32_t len = (int32_t)bx::strLen(_expected);
  230. if (0 == bx::strCmp(tmp, _expected)
  231. && num == len)
  232. {
  233. return true;
  234. }
  235. printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
  236. return false;
  237. }
  238. TEST_CASE("toString double", "")
  239. {
  240. REQUIRE(testToString(0.0, "0.0") );
  241. REQUIRE(testToString(-0.0, "-0.0") );
  242. REQUIRE(testToString(1.0, "1.0") );
  243. REQUIRE(testToString(-1.0, "-1.0") );
  244. REQUIRE(testToString(1.2345, "1.2345") );
  245. REQUIRE(testToString(1.2345678, "1.2345678") );
  246. REQUIRE(testToString(0.123456789012, "0.123456789012") );
  247. REQUIRE(testToString(1234567.8, "1234567.8") );
  248. REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
  249. REQUIRE(testToString(0.000001, "0.000001") );
  250. REQUIRE(testToString(0.0000001, "1e-7") );
  251. REQUIRE(testToString(1e30, "1e30") );
  252. REQUIRE(testToString(1.234567890123456e30, "1.234567890123456e30") );
  253. REQUIRE(testToString(-5e-324, "-5e-324") );
  254. REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
  255. REQUIRE(testToString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  256. REQUIRE(testToString(1.7976931348623157e308, "1.7976931348623157e308") );
  257. REQUIRE(testToString(0.00000123123123, "0.00000123123123") );
  258. REQUIRE(testToString(0.000000123123123, "1.23123123e-7") );
  259. REQUIRE(testToString(123123.123, "123123.123") );
  260. REQUIRE(testToString(1231231.23, "1231231.23") );
  261. REQUIRE(testToString(0.000000000123123, "1.23123e-10") );
  262. REQUIRE(testToString(0.0000000001, "1e-10") );
  263. REQUIRE(testToString(-270.000000, "-270.0") );
  264. REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
  265. REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
  266. }
  267. static bool testFromString(double _value, const char* _input)
  268. {
  269. char tmp[1024];
  270. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  271. double lhs;
  272. bx::fromString(&lhs, tmp);
  273. double rhs;
  274. bx::fromString(&rhs, _input);
  275. if (lhs == rhs)
  276. {
  277. return true;
  278. }
  279. printf("result '%f', input '%s'\n", _value, _input);
  280. return false;
  281. }
  282. TEST_CASE("fromString double", "")
  283. {
  284. REQUIRE(testFromString(0.0, "0.0") );
  285. REQUIRE(testFromString(-0.0, "-0.0") );
  286. REQUIRE(testFromString(1.0, "1.0") );
  287. REQUIRE(testFromString(-1.0, "-1.0") );
  288. REQUIRE(testFromString(1.2345, "1.2345") );
  289. REQUIRE(testFromString(1.2345678, "1.2345678") );
  290. REQUIRE(testFromString(0.123456789012, "0.123456789012") );
  291. REQUIRE(testFromString(1234567.8, "1234567.8") );
  292. REQUIRE(testFromString(-79.39773355813419, "-79.39773355813419") );
  293. REQUIRE(testFromString(0.000001, "0.000001") );
  294. REQUIRE(testFromString(0.0000001, "1e-7") );
  295. REQUIRE(testFromString(1e30, "1e30") );
  296. REQUIRE(testFromString(1.234567890123456e30, "1.234567890123456e30") );
  297. REQUIRE(testFromString(-5e-324, "-5e-324") );
  298. REQUIRE(testFromString(2.225073858507201e-308, "2.225073858507201e-308") );
  299. REQUIRE(testFromString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  300. REQUIRE(testFromString(1.7976931348623157e308, "1.7976931348623157e308") );
  301. REQUIRE(testFromString(0.00000123123123, "0.00000123123123") );
  302. REQUIRE(testFromString(0.000000123123123, "1.23123123e-7") );
  303. REQUIRE(testFromString(123123.123, "123123.123") );
  304. REQUIRE(testFromString(1231231.23, "1231231.23") );
  305. REQUIRE(testFromString(0.000000000123123, "1.23123e-10") );
  306. REQUIRE(testFromString(0.0000000001, "1e-10") );
  307. REQUIRE(testFromString(-270.000000, "-270.0") );
  308. REQUIRE(testFromString(2.2250738585072011e-308, "2.2250738585072011e-308") );
  309. }
  310. static bool testFromString(int32_t _value, const char* _input)
  311. {
  312. char tmp[1024];
  313. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  314. double lhs;
  315. bx::fromString(&lhs, tmp);
  316. double rhs;
  317. bx::fromString(&rhs, _input);
  318. if (lhs == rhs)
  319. {
  320. return true;
  321. }
  322. printf("result '%d', input '%s'\n", _value, _input);
  323. return false;
  324. }
  325. TEST_CASE("fromString int32_t", "")
  326. {
  327. REQUIRE(testFromString(1389, "1389") );
  328. REQUIRE(testFromString(1389, " 1389") );
  329. REQUIRE(testFromString(1389, "+1389") );
  330. REQUIRE(testFromString(-1389, "-1389") );
  331. REQUIRE(testFromString(-1389, " -1389") );
  332. REQUIRE(testFromString(555333, "555333") );
  333. REQUIRE(testFromString(-21, "-021") );
  334. }
  335. TEST_CASE("StringView", "")
  336. {
  337. bx::StringView sv("test");
  338. REQUIRE(4 == sv.getLength() );
  339. bx::DefaultAllocator crt;
  340. g_allocator = &crt;
  341. typedef bx::StringT<&g_allocator> String;
  342. String st(sv);
  343. REQUIRE(4 == st.getLength() );
  344. st.append("test");
  345. REQUIRE(8 == st.getLength() );
  346. st.append(bx::StringView("test", 2) );
  347. REQUIRE(10 == st.getLength() );
  348. REQUIRE(0 == bx::strCmp(st.getPtr(), "testtestte") );
  349. st.clear();
  350. REQUIRE(0 == st.getLength() );
  351. REQUIRE(4 == sv.getLength() );
  352. st.append("test");
  353. REQUIRE(4 == st.getLength() );
  354. sv.clear();
  355. REQUIRE(0 == sv.getLength() );
  356. }
  357. TEST_CASE("Trim", "")
  358. {
  359. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "ab"), "vgd") );
  360. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vagbd"), "abvgd") );
  361. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vgd"), "abvgd") );
  362. REQUIRE(0 == bx::strCmp(bx::strLTrim("/555333/podmac/", "/"), "555333/podmac/") );
  363. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "vagbd"), "abvgd") );
  364. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "abv"), "abvgd") );
  365. REQUIRE(0 == bx::strCmp(bx::strRTrim("/555333/podmac/", "/"), "/555333/podmac") );
  366. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", "da"), "bvg") );
  367. REQUIRE(0 == bx::strCmp(bx::strTrim("<1389>", "<>"), "1389") );
  368. REQUIRE(0 == bx::strCmp(bx::strTrim("/555333/podmac/", "/"), "555333/podmac") );
  369. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", ""), "abvgd") );
  370. REQUIRE(0 == bx::strCmp(bx::strTrim(" \t a b\tv g d \t ", " \t"), "a b\tv g d") );
  371. bx::FilePath uri("/555333/podmac/");
  372. REQUIRE(0 == bx::strCmp(bx::strTrim(uri.getPath(), "/"), "555333/podmac") );
  373. }
  374. TEST_CASE("strWord", "")
  375. {
  376. REQUIRE(bx::strWord(" abvgd-1389.0").isEmpty() );
  377. REQUIRE(0 == bx::strCmp(bx::strWord("abvgd-1389.0"), "abvgd") );
  378. }
  379. TEST_CASE("strFindBlock", "")
  380. {
  381. const bx::StringView test0("{ { {} {} abvgd; {} } }");
  382. const bx::StringView test1(test0, 1);
  383. bx::StringView result = bx::strFindBlock(test1, '{', '}');
  384. REQUIRE(19 == result.getLength() );
  385. }