string_test.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. bx::AllocatorI* g_allocator;
  9. TEST_CASE("chars", "")
  10. {
  11. for (char ch = 'A'; ch <= 'Z'; ++ch)
  12. {
  13. REQUIRE(!bx::isLower(ch) );
  14. REQUIRE(!bx::isNumeric(ch) );
  15. REQUIRE(bx::isUpper(ch) );
  16. REQUIRE(bx::isAlpha(ch) );
  17. REQUIRE(bx::isAlphaNum(ch) );
  18. REQUIRE(bx::isLower(bx::toLower(ch) ) );
  19. }
  20. }
  21. TEST_CASE("strLen", "")
  22. {
  23. const char* test = "test";
  24. REQUIRE(0 == bx::strLen(test, 0) );
  25. REQUIRE(2 == bx::strLen(test, 2) );
  26. REQUIRE(4 == bx::strLen(test, INT32_MAX) );
  27. }
  28. TEST_CASE("strCopy", "")
  29. {
  30. char dst[128];
  31. size_t num;
  32. num = bx::strCopy(dst, 1, "blah");
  33. REQUIRE(num == 0);
  34. num = bx::strCopy(dst, 3, "blah", 3);
  35. REQUIRE(0 == bx::strCmp(dst, "bl") );
  36. REQUIRE(num == 2);
  37. num = bx::strCopy(dst, sizeof(dst), "blah", 3);
  38. REQUIRE(0 == bx::strCmp(dst, "bla") );
  39. REQUIRE(num == 3);
  40. num = bx::strCopy(dst, sizeof(dst), "blah");
  41. REQUIRE(0 == bx::strCmp(dst, "blah") );
  42. REQUIRE(num == 4);
  43. }
  44. TEST_CASE("strCat", "")
  45. {
  46. char dst[128] = { '\0' };
  47. REQUIRE(0 == bx::strCat(dst, 1, "cat") );
  48. REQUIRE(4 == bx::strCopy(dst, 5, "copy") );
  49. REQUIRE(3 == bx::strCat(dst, 8, "cat") );
  50. REQUIRE(0 == bx::strCmp(dst, "copycat") );
  51. REQUIRE(1 == bx::strCat(dst, BX_COUNTOF(dst), "------", 1) );
  52. REQUIRE(3 == bx::strCat(dst, BX_COUNTOF(dst), "cat") );
  53. REQUIRE(0 == bx::strCmp(dst, "copycat-cat") );
  54. }
  55. TEST_CASE("strCmpI", "")
  56. {
  57. REQUIRE(0 == bx::strCmpI("test", "test") );
  58. REQUIRE(0 == bx::strCmpI("test", "testestes", 4) );
  59. REQUIRE(0 == bx::strCmpI("testestes", "test", 4) );
  60. REQUIRE(0 != bx::strCmpI("preprocess", "platform") );
  61. const char* abvgd = "abvgd";
  62. const char* abvgx = "abvgx";
  63. const char* empty = "";
  64. REQUIRE(0 == bx::strCmpI(abvgd, abvgd) );
  65. REQUIRE(0 == bx::strCmpI(abvgd, abvgx, 4) );
  66. REQUIRE(0 > bx::strCmpI(abvgd, abvgx) );
  67. REQUIRE(0 > bx::strCmpI(empty, abvgd) );
  68. REQUIRE(0 < bx::strCmpI(abvgx, abvgd) );
  69. REQUIRE(0 < bx::strCmpI(abvgd, empty) );
  70. }
  71. TEST_CASE("strRFind", "")
  72. {
  73. const char* test = "test";
  74. REQUIRE(NULL == bx::strRFind(test, 's', 0) );
  75. REQUIRE(NULL == bx::strRFind(test, 's', 1) );
  76. REQUIRE(&test[2] == bx::strRFind(test, 's') );
  77. }
  78. TEST_CASE("strFindI", "")
  79. {
  80. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  81. REQUIRE(NULL == bx::strFindI(test, "quick", 8) );
  82. REQUIRE(NULL == bx::strFindI(test, "quick1") );
  83. REQUIRE(&test[4] == bx::strFindI(test, "quick", 9) );
  84. REQUIRE(&test[4] == bx::strFindI(test, "quick") );
  85. }
  86. TEST_CASE("strFind", "")
  87. {
  88. {
  89. const char* test = "test";
  90. REQUIRE(NULL == bx::strFind(test, 's', 0) );
  91. REQUIRE(NULL == bx::strFind(test, 's', 2) );
  92. REQUIRE(&test[2] == bx::strFind(test, 's') );
  93. }
  94. {
  95. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  96. REQUIRE(NULL == bx::strFind(test, "quick", 8) );
  97. REQUIRE(NULL == bx::strFind(test, "quick1") );
  98. REQUIRE(NULL == bx::strFind(test, "quick", 9) );
  99. REQUIRE(NULL == bx::strFind(test, "quick") );
  100. REQUIRE(NULL == bx::strFind(test, "Quick", 8) );
  101. REQUIRE(NULL == bx::strFind(test, "Quick1") );
  102. REQUIRE(&test[4] == bx::strFind(test, "Quick", 9) );
  103. REQUIRE(&test[4] == bx::strFind(test, "Quick") );
  104. }
  105. }
  106. template<typename Ty>
  107. static bool testToString(Ty _value, const char* _expected)
  108. {
  109. char tmp[1024];
  110. int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
  111. int32_t len = (int32_t)bx::strLen(_expected);
  112. if (0 == bx::strCmp(tmp, _expected)
  113. && num == len)
  114. {
  115. return true;
  116. }
  117. printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
  118. return false;
  119. }
  120. TEST_CASE("toString int32_t/uint32_t", "")
  121. {
  122. REQUIRE(testToString(0, "0") );
  123. REQUIRE(testToString(-256, "-256") );
  124. REQUIRE(testToString(INT32_MAX, "2147483647") );
  125. REQUIRE(testToString(UINT32_MAX, "4294967295") );
  126. }
  127. TEST_CASE("toString double", "")
  128. {
  129. REQUIRE(testToString(0.0, "0.0") );
  130. REQUIRE(testToString(-0.0, "-0.0") );
  131. REQUIRE(testToString(1.0, "1.0") );
  132. REQUIRE(testToString(-1.0, "-1.0") );
  133. REQUIRE(testToString(1.2345, "1.2345") );
  134. REQUIRE(testToString(1.2345678, "1.2345678") );
  135. REQUIRE(testToString(0.123456789012, "0.123456789012") );
  136. REQUIRE(testToString(1234567.8, "1234567.8") );
  137. REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
  138. REQUIRE(testToString(0.000001, "0.000001") );
  139. REQUIRE(testToString(0.0000001, "1e-7") );
  140. REQUIRE(testToString(1e30, "1e30") );
  141. REQUIRE(testToString(1.234567890123456e30, "1.234567890123456e30") );
  142. REQUIRE(testToString(-5e-324, "-5e-324") );
  143. REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
  144. REQUIRE(testToString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  145. REQUIRE(testToString(1.7976931348623157e308, "1.7976931348623157e308") );
  146. REQUIRE(testToString(0.00000123123123, "0.00000123123123") );
  147. REQUIRE(testToString(0.000000123123123, "1.23123123e-7") );
  148. REQUIRE(testToString(123123.123, "123123.123") );
  149. REQUIRE(testToString(1231231.23, "1231231.23") );
  150. REQUIRE(testToString(0.000000000123123, "1.23123e-10") );
  151. REQUIRE(testToString(0.0000000001, "1e-10") );
  152. }
  153. static bool testFromString(double _value, const char* _input)
  154. {
  155. char tmp[1024];
  156. bx::toString(tmp, BX_COUNTOF(tmp), _value);
  157. double lhs;
  158. bx::fromString(&lhs, tmp);
  159. double rhs;
  160. bx::fromString(&rhs, _input);
  161. if (lhs == rhs)
  162. {
  163. return true;
  164. }
  165. printf("result '%f', input '%s'\n", _value, _input);
  166. return false;
  167. }
  168. TEST_CASE("fromString double", "")
  169. {
  170. REQUIRE(testFromString(0.0, "0.0") );
  171. REQUIRE(testFromString(-0.0, "-0.0") );
  172. REQUIRE(testFromString(1.0, "1.0") );
  173. REQUIRE(testFromString(-1.0, "-1.0") );
  174. REQUIRE(testFromString(1.2345, "1.2345") );
  175. REQUIRE(testFromString(1.2345678, "1.2345678") );
  176. REQUIRE(testFromString(0.123456789012, "0.123456789012") );
  177. REQUIRE(testFromString(1234567.8, "1234567.8") );
  178. REQUIRE(testFromString(-79.39773355813419, "-79.39773355813419") );
  179. REQUIRE(testFromString(0.000001, "0.000001") );
  180. REQUIRE(testFromString(0.0000001, "1e-7") );
  181. REQUIRE(testFromString(1e30, "1e30") );
  182. REQUIRE(testFromString(1.234567890123456e30, "1.234567890123456e30") );
  183. REQUIRE(testFromString(-5e-324, "-5e-324") );
  184. REQUIRE(testFromString(2.225073858507201e-308, "2.225073858507201e-308") );
  185. REQUIRE(testFromString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  186. REQUIRE(testFromString(1.7976931348623157e308, "1.7976931348623157e308") );
  187. REQUIRE(testFromString(0.00000123123123, "0.00000123123123") );
  188. REQUIRE(testFromString(0.000000123123123, "1.23123123e-7") );
  189. REQUIRE(testFromString(123123.123, "123123.123") );
  190. REQUIRE(testFromString(1231231.23, "1231231.23") );
  191. REQUIRE(testFromString(0.000000000123123, "1.23123e-10") );
  192. REQUIRE(testFromString(0.0000000001, "1e-10") );
  193. }
  194. TEST_CASE("StringView", "")
  195. {
  196. bx::StringView sv("test");
  197. REQUIRE(4 == sv.getLength() );
  198. bx::DefaultAllocator crt;
  199. g_allocator = &crt;
  200. typedef bx::StringT<&g_allocator> String;
  201. String st(sv);
  202. REQUIRE(4 == st.getLength() );
  203. st.append("test");
  204. REQUIRE(8 == st.getLength() );
  205. st.append("test", 2);
  206. REQUIRE(10 == st.getLength() );
  207. REQUIRE(0 == bx::strCmp(st.getPtr(), "testtestte") );
  208. st.clear();
  209. REQUIRE(0 == st.getLength() );
  210. REQUIRE(4 == sv.getLength() );
  211. st.append("test");
  212. REQUIRE(4 == st.getLength() );
  213. sv.clear();
  214. REQUIRE(0 == sv.getLength() );
  215. }