string_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright 2010-2017 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. bx::AllocatorI* g_allocator;
  11. TEST_CASE("chars", "")
  12. {
  13. for (char ch = 'A'; ch <= 'Z'; ++ch)
  14. {
  15. REQUIRE(!bx::isLower(ch) );
  16. REQUIRE(!bx::isNumeric(ch) );
  17. REQUIRE(bx::isUpper(ch) );
  18. REQUIRE(bx::isAlpha(ch) );
  19. REQUIRE(bx::isAlphaNum(ch) );
  20. REQUIRE(bx::isLower(bx::toLower(ch) ) );
  21. }
  22. }
  23. TEST_CASE("strLen", "")
  24. {
  25. const char* test = "test";
  26. REQUIRE(0 == bx::strLen(test, 0) );
  27. REQUIRE(2 == bx::strLen(test, 2) );
  28. REQUIRE(4 == bx::strLen(test, INT32_MAX) );
  29. }
  30. TEST_CASE("strCopy", "")
  31. {
  32. char dst[128];
  33. size_t num;
  34. num = bx::strCopy(dst, 1, "blah");
  35. REQUIRE(num == 0);
  36. num = bx::strCopy(dst, 3, "blah", 3);
  37. REQUIRE(0 == bx::strCmp(dst, "bl") );
  38. REQUIRE(num == 2);
  39. num = bx::strCopy(dst, sizeof(dst), "blah", 3);
  40. REQUIRE(0 == bx::strCmp(dst, "bla") );
  41. REQUIRE(num == 3);
  42. num = bx::strCopy(dst, sizeof(dst), "blah");
  43. REQUIRE(0 == bx::strCmp(dst, "bl", 2) );
  44. REQUIRE(0 == bx::strCmp(dst, "blah") );
  45. REQUIRE(num == 4);
  46. }
  47. TEST_CASE("strCat", "")
  48. {
  49. char dst[128] = { '\0' };
  50. REQUIRE(0 == bx::strCat(dst, 1, "cat") );
  51. REQUIRE(4 == bx::strCopy(dst, 5, "copy") );
  52. REQUIRE(3 == bx::strCat(dst, 8, "cat") );
  53. REQUIRE(0 == bx::strCmp(dst, "copycat") );
  54. REQUIRE(0 == bx::strCmp(dst, "copy", 4) );
  55. REQUIRE(1 == bx::strCat(dst, BX_COUNTOF(dst), "------", 1) );
  56. REQUIRE(3 == bx::strCat(dst, BX_COUNTOF(dst), "cat") );
  57. REQUIRE(0 == bx::strCmp(dst, "copycat-cat") );
  58. }
  59. TEST_CASE("strCmp", "")
  60. {
  61. REQUIRE(0 != bx::strCmp("meh", "meh/") );
  62. }
  63. TEST_CASE("strCmpI", "")
  64. {
  65. REQUIRE(0 == bx::strCmpI("test", "test") );
  66. REQUIRE(0 == bx::strCmpI("test", "testestes", 4) );
  67. REQUIRE(0 == bx::strCmpI("testestes", "test", 4) );
  68. REQUIRE(0 != bx::strCmpI("preprocess", "platform") );
  69. const char* abvgd = "abvgd";
  70. const char* abvgx = "abvgx";
  71. const char* empty = "";
  72. REQUIRE(0 == bx::strCmpI(abvgd, abvgd) );
  73. REQUIRE(0 == bx::strCmpI(abvgd, abvgx, 4) );
  74. REQUIRE(0 == bx::strCmpI(empty, empty) );
  75. REQUIRE(0 > bx::strCmpI(abvgd, abvgx) );
  76. REQUIRE(0 > bx::strCmpI(empty, abvgd) );
  77. REQUIRE(0 < bx::strCmpI(abvgx, abvgd) );
  78. REQUIRE(0 < bx::strCmpI(abvgd, empty) );
  79. }
  80. TEST_CASE("strCmpV", "")
  81. {
  82. REQUIRE(0 == bx::strCmpV("test", "test") );
  83. REQUIRE(0 == bx::strCmpV("test", "testestes", 4) );
  84. REQUIRE(0 == bx::strCmpV("testestes", "test", 4) );
  85. REQUIRE(0 != bx::strCmpV("preprocess", "platform") );
  86. const char* abvgd = "abvgd";
  87. const char* abvgx = "abvgx";
  88. const char* empty = "";
  89. REQUIRE(0 == bx::strCmpV(abvgd, abvgd) );
  90. REQUIRE(0 == bx::strCmpV(abvgd, abvgx, 4) );
  91. REQUIRE(0 == bx::strCmpV(empty, empty) );
  92. REQUIRE(0 > bx::strCmpV(abvgd, abvgx) );
  93. REQUIRE(0 > bx::strCmpV(empty, abvgd) );
  94. REQUIRE(0 < bx::strCmpV(abvgx, abvgd) );
  95. REQUIRE(0 < bx::strCmpV(abvgd, empty) );
  96. }
  97. static int32_t strCmpV(const void* _lhs, const void* _rhs)
  98. {
  99. const char* lhs = *(const char**)_lhs;
  100. const char* rhs = *(const char**)_rhs;
  101. int32_t result = bx::strCmpV(lhs, rhs);
  102. return result;
  103. }
  104. TEST_CASE("strCmpV sort", "")
  105. {
  106. const char* test[] =
  107. {
  108. "test_1.txt",
  109. "test_10.txt",
  110. "test_100.txt",
  111. "test_15.txt",
  112. "test_11.txt",
  113. "test_23.txt",
  114. "test_3.txt",
  115. };
  116. const char* expected[] =
  117. {
  118. "test_1.txt",
  119. "test_3.txt",
  120. "test_10.txt",
  121. "test_11.txt",
  122. "test_15.txt",
  123. "test_23.txt",
  124. "test_100.txt",
  125. };
  126. BX_STATIC_ASSERT(BX_COUNTOF(test) == BX_COUNTOF(expected) );
  127. bx::quickSort(test, BX_COUNTOF(test), sizeof(const char*), strCmpV);
  128. for (uint32_t ii = 0; ii < BX_COUNTOF(test); ++ii)
  129. {
  130. REQUIRE(0 == bx::strCmp(test[ii], expected[ii]) );
  131. }
  132. }
  133. TEST_CASE("strRFind", "")
  134. {
  135. const char* test = "test";
  136. REQUIRE(NULL == bx::strRFind(bx::StringView(test, 0), 's') );
  137. REQUIRE(NULL == bx::strRFind(bx::StringView(test, 1), 's') );
  138. REQUIRE(&test[2] == bx::strRFind(test, 's') );
  139. }
  140. TEST_CASE("strFindI", "")
  141. {
  142. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  143. REQUIRE(NULL == bx::strFindI(bx::StringView(test, 8), "quick") );
  144. REQUIRE(NULL == bx::strFindI(test, "quick1") );
  145. REQUIRE(&test[4] == bx::strFindI(bx::StringView(test, 9), "quick") );
  146. REQUIRE(&test[4] == bx::strFindI(test, "quick") );
  147. }
  148. TEST_CASE("strFind", "")
  149. {
  150. {
  151. const char* test = "test";
  152. REQUIRE(NULL == bx::strFind(bx::StringView(test, 0), 's') );
  153. REQUIRE(NULL == bx::strFind(bx::StringView(test, 2), 's') );
  154. REQUIRE(&test[2] == bx::strFind(test, 's') );
  155. }
  156. {
  157. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  158. REQUIRE(NULL == bx::strFind(bx::StringView(test, 8), "quick") );
  159. REQUIRE(NULL == bx::strFind(test, "quick1") );
  160. REQUIRE(NULL == bx::strFind(bx::StringView(test, 9), "quick") );
  161. REQUIRE(NULL == bx::strFind(test, "quick") );
  162. REQUIRE(NULL == bx::strFind(bx::StringView(test, 8), "Quick") );
  163. REQUIRE(NULL == bx::strFind(test, "Quick1") );
  164. REQUIRE(&test[4] == bx::strFind(bx::StringView(test, 9), "Quick") );
  165. REQUIRE(&test[4] == bx::strFind(test, "Quick") );
  166. REQUIRE(NULL == bx::strFind("vgd", 'a') );
  167. }
  168. }
  169. template<typename Ty>
  170. static bool testToString(Ty _value, const char* _expected)
  171. {
  172. char tmp[1024];
  173. int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
  174. int32_t len = (int32_t)bx::strLen(_expected);
  175. if (0 == bx::strCmp(tmp, _expected)
  176. && num == len)
  177. {
  178. return true;
  179. }
  180. printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
  181. return false;
  182. }
  183. TEST_CASE("toString int32_t/uint32_t", "")
  184. {
  185. REQUIRE(testToString(0, "0") );
  186. REQUIRE(testToString(-256, "-256") );
  187. REQUIRE(testToString(INT32_MAX, "2147483647") );
  188. REQUIRE(testToString(UINT32_MAX, "4294967295") );
  189. }
  190. TEST_CASE("toString double", "")
  191. {
  192. REQUIRE(testToString(0.0, "0.0") );
  193. REQUIRE(testToString(-0.0, "-0.0") );
  194. REQUIRE(testToString(1.0, "1.0") );
  195. REQUIRE(testToString(-1.0, "-1.0") );
  196. REQUIRE(testToString(1.2345, "1.2345") );
  197. REQUIRE(testToString(1.2345678, "1.2345678") );
  198. REQUIRE(testToString(0.123456789012, "0.123456789012") );
  199. REQUIRE(testToString(1234567.8, "1234567.8") );
  200. REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
  201. REQUIRE(testToString(0.000001, "0.000001") );
  202. REQUIRE(testToString(0.0000001, "1e-7") );
  203. REQUIRE(testToString(1e30, "1e30") );
  204. REQUIRE(testToString(1.234567890123456e30, "1.234567890123456e30") );
  205. REQUIRE(testToString(-5e-324, "-5e-324") );
  206. REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
  207. REQUIRE(testToString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  208. REQUIRE(testToString(1.7976931348623157e308, "1.7976931348623157e308") );
  209. REQUIRE(testToString(0.00000123123123, "0.00000123123123") );
  210. REQUIRE(testToString(0.000000123123123, "1.23123123e-7") );
  211. REQUIRE(testToString(123123.123, "123123.123") );
  212. REQUIRE(testToString(1231231.23, "1231231.23") );
  213. REQUIRE(testToString(0.000000000123123, "1.23123e-10") );
  214. REQUIRE(testToString(0.0000000001, "1e-10") );
  215. REQUIRE(testToString(-270.000000, "-270.0") );
  216. }
  217. static bool testFromString(double _value, const char* _input)
  218. {
  219. char tmp[1024];
  220. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  221. double lhs;
  222. bx::fromString(&lhs, tmp);
  223. double rhs;
  224. bx::fromString(&rhs, _input);
  225. if (lhs == rhs)
  226. {
  227. return true;
  228. }
  229. printf("result '%f', input '%s'\n", _value, _input);
  230. return false;
  231. }
  232. TEST_CASE("fromString double", "")
  233. {
  234. REQUIRE(testFromString(0.0, "0.0") );
  235. REQUIRE(testFromString(-0.0, "-0.0") );
  236. REQUIRE(testFromString(1.0, "1.0") );
  237. REQUIRE(testFromString(-1.0, "-1.0") );
  238. REQUIRE(testFromString(1.2345, "1.2345") );
  239. REQUIRE(testFromString(1.2345678, "1.2345678") );
  240. REQUIRE(testFromString(0.123456789012, "0.123456789012") );
  241. REQUIRE(testFromString(1234567.8, "1234567.8") );
  242. REQUIRE(testFromString(-79.39773355813419, "-79.39773355813419") );
  243. REQUIRE(testFromString(0.000001, "0.000001") );
  244. REQUIRE(testFromString(0.0000001, "1e-7") );
  245. REQUIRE(testFromString(1e30, "1e30") );
  246. REQUIRE(testFromString(1.234567890123456e30, "1.234567890123456e30") );
  247. REQUIRE(testFromString(-5e-324, "-5e-324") );
  248. REQUIRE(testFromString(2.225073858507201e-308, "2.225073858507201e-308") );
  249. REQUIRE(testFromString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  250. REQUIRE(testFromString(1.7976931348623157e308, "1.7976931348623157e308") );
  251. REQUIRE(testFromString(0.00000123123123, "0.00000123123123") );
  252. REQUIRE(testFromString(0.000000123123123, "1.23123123e-7") );
  253. REQUIRE(testFromString(123123.123, "123123.123") );
  254. REQUIRE(testFromString(1231231.23, "1231231.23") );
  255. REQUIRE(testFromString(0.000000000123123, "1.23123e-10") );
  256. REQUIRE(testFromString(0.0000000001, "1e-10") );
  257. REQUIRE(testFromString(-270.000000, "-270.0") );
  258. }
  259. static bool testFromString(int32_t _value, const char* _input)
  260. {
  261. char tmp[1024];
  262. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  263. double lhs;
  264. bx::fromString(&lhs, tmp);
  265. double rhs;
  266. bx::fromString(&rhs, _input);
  267. if (lhs == rhs)
  268. {
  269. return true;
  270. }
  271. printf("result '%d', input '%s'\n", _value, _input);
  272. return false;
  273. }
  274. TEST_CASE("fromString int32_t", "")
  275. {
  276. REQUIRE(testFromString(1389, "1389") );
  277. REQUIRE(testFromString(1389, " 1389") );
  278. REQUIRE(testFromString(1389, "+1389") );
  279. REQUIRE(testFromString(-1389, "-1389") );
  280. REQUIRE(testFromString(-1389, " -1389") );
  281. REQUIRE(testFromString(555333, "555333") );
  282. REQUIRE(testFromString(-21, "-021") );
  283. }
  284. TEST_CASE("StringView", "")
  285. {
  286. bx::StringView sv("test");
  287. REQUIRE(4 == sv.getLength() );
  288. bx::DefaultAllocator crt;
  289. g_allocator = &crt;
  290. typedef bx::StringT<&g_allocator> String;
  291. String st(sv);
  292. REQUIRE(4 == st.getLength() );
  293. st.append("test");
  294. REQUIRE(8 == st.getLength() );
  295. st.append(bx::StringView("test", 2) );
  296. REQUIRE(10 == st.getLength() );
  297. REQUIRE(0 == bx::strCmp(st.getPtr(), "testtestte") );
  298. st.clear();
  299. REQUIRE(0 == st.getLength() );
  300. REQUIRE(4 == sv.getLength() );
  301. st.append("test");
  302. REQUIRE(4 == st.getLength() );
  303. sv.clear();
  304. REQUIRE(0 == sv.getLength() );
  305. }
  306. TEST_CASE("Trim", "")
  307. {
  308. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "ab"), "vgd") );
  309. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vagbd"), "") );
  310. REQUIRE(0 == bx::strCmp(bx::strLTrim("abvgd", "vgd"), "abvgd") );
  311. REQUIRE(0 == bx::strCmp(bx::strLTrim("/555333/podmac/", "/"), "555333/podmac/") );
  312. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "vagbd"), "") );
  313. REQUIRE(0 == bx::strCmp(bx::strRTrim("abvgd", "abv"), "abvgd") );
  314. REQUIRE(0 == bx::strCmp(bx::strRTrim("/555333/podmac/", "/"), "/555333/podmac") );
  315. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", "da"), "bvg") );
  316. REQUIRE(0 == bx::strCmp(bx::strTrim("<1389>", "<>"), "1389") );
  317. REQUIRE(0 == bx::strCmp(bx::strTrim("/555333/podmac/", "/"), "555333/podmac") );
  318. REQUIRE(0 == bx::strCmp(bx::strTrim("abvgd", ""), "abvgd") );
  319. REQUIRE(0 == bx::strCmp(bx::strTrim(" \t a b\tv g d \t ", " \t"), "a b\tv g d") );
  320. bx::FilePath uri("/555333/podmac/");
  321. REQUIRE(0 == bx::strCmp(bx::strTrim(uri.getPath(), "/"), "555333/podmac") );
  322. }
  323. TEST_CASE("strWord", "")
  324. {
  325. REQUIRE(bx::strWord(" abvgd-1389.0").isEmpty() );
  326. REQUIRE(0 == bx::strCmp(bx::strWord("abvgd-1389.0"), "abvgd") );
  327. }