2
0

string_test.cpp 10 KB

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