string_test.cpp 9.9 KB

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