string_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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(NULL == bx::strRFind(bx::StringView(test, 0), 's') );
  152. REQUIRE(NULL == bx::strRFind(bx::StringView(test, 1), 's') );
  153. REQUIRE(&test[2] == bx::strRFind(test, 's') );
  154. }
  155. TEST_CASE("strFindI", "")
  156. {
  157. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  158. REQUIRE(NULL == bx::strFindI(bx::StringView(test, 8), "quick") );
  159. REQUIRE(NULL == bx::strFindI(test, "quick1") );
  160. REQUIRE(&test[4] == bx::strFindI(bx::StringView(test, 9), "quick") );
  161. REQUIRE(&test[4] == bx::strFindI(test, "quick") );
  162. }
  163. TEST_CASE("strFind", "")
  164. {
  165. {
  166. const char* test = "test";
  167. REQUIRE(NULL == bx::strFind(bx::StringView(test, 0), 's') );
  168. REQUIRE(NULL == bx::strFind(bx::StringView(test, 2), 's') );
  169. REQUIRE(&test[2] == bx::strFind(test, 's') );
  170. }
  171. {
  172. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  173. REQUIRE(NULL == bx::strFind(bx::StringView(test, 8), "quick") );
  174. REQUIRE(NULL == bx::strFind(test, "quick1") );
  175. REQUIRE(NULL == bx::strFind(bx::StringView(test, 9), "quick") );
  176. REQUIRE(NULL == bx::strFind(test, "quick") );
  177. REQUIRE(NULL == bx::strFind(bx::StringView(test, 8), "Quick") );
  178. REQUIRE(NULL == bx::strFind(test, "Quick1") );
  179. REQUIRE(&test[4] == bx::strFind(bx::StringView(test, 9), "Quick") );
  180. REQUIRE(&test[4] == bx::strFind(test, "Quick") );
  181. REQUIRE(NULL == bx::strFind("vgd", 'a') );
  182. }
  183. }
  184. template<typename Ty>
  185. static bool testToString(Ty _value, const char* _expected)
  186. {
  187. char tmp[1024];
  188. int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
  189. int32_t len = (int32_t)bx::strLen(_expected);
  190. if (0 == bx::strCmp(tmp, _expected)
  191. && num == len)
  192. {
  193. return true;
  194. }
  195. printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
  196. return false;
  197. }
  198. TEST_CASE("toString int32_t/uint32_t", "")
  199. {
  200. REQUIRE(testToString(0, "0") );
  201. REQUIRE(testToString(-256, "-256") );
  202. REQUIRE(testToString(INT32_MAX, "2147483647") );
  203. REQUIRE(testToString(UINT32_MAX, "4294967295") );
  204. }
  205. TEST_CASE("toString double", "")
  206. {
  207. REQUIRE(testToString(0.0, "0.0") );
  208. REQUIRE(testToString(-0.0, "-0.0") );
  209. REQUIRE(testToString(1.0, "1.0") );
  210. REQUIRE(testToString(-1.0, "-1.0") );
  211. REQUIRE(testToString(1.2345, "1.2345") );
  212. REQUIRE(testToString(1.2345678, "1.2345678") );
  213. REQUIRE(testToString(0.123456789012, "0.123456789012") );
  214. REQUIRE(testToString(1234567.8, "1234567.8") );
  215. REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
  216. REQUIRE(testToString(0.000001, "0.000001") );
  217. REQUIRE(testToString(0.0000001, "1e-7") );
  218. REQUIRE(testToString(1e30, "1e30") );
  219. REQUIRE(testToString(1.234567890123456e30, "1.234567890123456e30") );
  220. REQUIRE(testToString(-5e-324, "-5e-324") );
  221. REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
  222. REQUIRE(testToString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  223. REQUIRE(testToString(1.7976931348623157e308, "1.7976931348623157e308") );
  224. REQUIRE(testToString(0.00000123123123, "0.00000123123123") );
  225. REQUIRE(testToString(0.000000123123123, "1.23123123e-7") );
  226. REQUIRE(testToString(123123.123, "123123.123") );
  227. REQUIRE(testToString(1231231.23, "1231231.23") );
  228. REQUIRE(testToString(0.000000000123123, "1.23123e-10") );
  229. REQUIRE(testToString(0.0000000001, "1e-10") );
  230. REQUIRE(testToString(-270.000000, "-270.0") );
  231. REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
  232. REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
  233. }
  234. static bool testFromString(double _value, const char* _input)
  235. {
  236. char tmp[1024];
  237. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  238. double lhs;
  239. bx::fromString(&lhs, tmp);
  240. double rhs;
  241. bx::fromString(&rhs, _input);
  242. if (lhs == rhs)
  243. {
  244. return true;
  245. }
  246. printf("result '%f', input '%s'\n", _value, _input);
  247. return false;
  248. }
  249. TEST_CASE("fromString double", "")
  250. {
  251. REQUIRE(testFromString(0.0, "0.0") );
  252. REQUIRE(testFromString(-0.0, "-0.0") );
  253. REQUIRE(testFromString(1.0, "1.0") );
  254. REQUIRE(testFromString(-1.0, "-1.0") );
  255. REQUIRE(testFromString(1.2345, "1.2345") );
  256. REQUIRE(testFromString(1.2345678, "1.2345678") );
  257. REQUIRE(testFromString(0.123456789012, "0.123456789012") );
  258. REQUIRE(testFromString(1234567.8, "1234567.8") );
  259. REQUIRE(testFromString(-79.39773355813419, "-79.39773355813419") );
  260. REQUIRE(testFromString(0.000001, "0.000001") );
  261. REQUIRE(testFromString(0.0000001, "1e-7") );
  262. REQUIRE(testFromString(1e30, "1e30") );
  263. REQUIRE(testFromString(1.234567890123456e30, "1.234567890123456e30") );
  264. REQUIRE(testFromString(-5e-324, "-5e-324") );
  265. REQUIRE(testFromString(2.225073858507201e-308, "2.225073858507201e-308") );
  266. REQUIRE(testFromString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  267. REQUIRE(testFromString(1.7976931348623157e308, "1.7976931348623157e308") );
  268. REQUIRE(testFromString(0.00000123123123, "0.00000123123123") );
  269. REQUIRE(testFromString(0.000000123123123, "1.23123123e-7") );
  270. REQUIRE(testFromString(123123.123, "123123.123") );
  271. REQUIRE(testFromString(1231231.23, "1231231.23") );
  272. REQUIRE(testFromString(0.000000000123123, "1.23123e-10") );
  273. REQUIRE(testFromString(0.0000000001, "1e-10") );
  274. REQUIRE(testFromString(-270.000000, "-270.0") );
  275. REQUIRE(testFromString(2.2250738585072011e-308, "2.2250738585072011e-308") );
  276. }
  277. static bool testFromString(int32_t _value, const char* _input)
  278. {
  279. char tmp[1024];
  280. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  281. double lhs;
  282. bx::fromString(&lhs, tmp);
  283. double rhs;
  284. bx::fromString(&rhs, _input);
  285. if (lhs == rhs)
  286. {
  287. return true;
  288. }
  289. printf("result '%d', input '%s'\n", _value, _input);
  290. return false;
  291. }
  292. TEST_CASE("fromString int32_t", "")
  293. {
  294. REQUIRE(testFromString(1389, "1389") );
  295. REQUIRE(testFromString(1389, " 1389") );
  296. REQUIRE(testFromString(1389, "+1389") );
  297. REQUIRE(testFromString(-1389, "-1389") );
  298. REQUIRE(testFromString(-1389, " -1389") );
  299. REQUIRE(testFromString(555333, "555333") );
  300. REQUIRE(testFromString(-21, "-021") );
  301. }
  302. TEST_CASE("StringView", "")
  303. {
  304. bx::StringView sv("test");
  305. REQUIRE(4 == sv.getLength() );
  306. bx::DefaultAllocator crt;
  307. g_allocator = &crt;
  308. typedef bx::StringT<&g_allocator> String;
  309. String st(sv);
  310. REQUIRE(4 == st.getLength() );
  311. st.append("test");
  312. REQUIRE(8 == st.getLength() );
  313. st.append(bx::StringView("test", 2) );
  314. REQUIRE(10 == st.getLength() );
  315. REQUIRE(0 == bx::strCmp(st.getPtr(), "testtestte") );
  316. st.clear();
  317. REQUIRE(0 == st.getLength() );
  318. REQUIRE(4 == sv.getLength() );
  319. st.append("test");
  320. REQUIRE(4 == st.getLength() );
  321. sv.clear();
  322. REQUIRE(0 == sv.getLength() );
  323. }
  324. TEST_CASE("Trim", "")
  325. {
  326. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "ab"), "vgd") );
  327. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vagbd"), "") );
  328. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vgd"), "abvgd") );
  329. REQUIRE(0 == bx::strCmp(bx::strLTrim("/555333/podmac/", "/"), "555333/podmac/") );
  330. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "vagbd"), "") );
  331. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "abv"), "abvgd") );
  332. REQUIRE(0 == bx::strCmp(bx::strRTrim("/555333/podmac/", "/"), "/555333/podmac") );
  333. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", "da"), "bvg") );
  334. REQUIRE(0 == bx::strCmp(bx::strTrim("<1389>", "<>"), "1389") );
  335. REQUIRE(0 == bx::strCmp(bx::strTrim("/555333/podmac/", "/"), "555333/podmac") );
  336. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", ""), "abvgd") );
  337. REQUIRE(0 == bx::strCmp(bx::strTrim(" \t a b\tv g d \t ", " \t"), "a b\tv g d") );
  338. bx::FilePath uri("/555333/podmac/");
  339. REQUIRE(0 == bx::strCmp(bx::strTrim(uri.getPath(), "/"), "555333/podmac") );
  340. }
  341. TEST_CASE("strWord", "")
  342. {
  343. REQUIRE(bx::strWord(" abvgd-1389.0").isEmpty() );
  344. REQUIRE(0 == bx::strCmp(bx::strWord("abvgd-1389.0"), "abvgd") );
  345. }