string_test.cpp 9.7 KB

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