string_test.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/crtimpl.h>
  8. #include <bx/handlealloc.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("strRFind", "")
  73. {
  74. const char* test = "test";
  75. REQUIRE(NULL == bx::strRFind(test, 's', 0) );
  76. REQUIRE(NULL == bx::strRFind(test, 's', 1) );
  77. REQUIRE(&test[2] == bx::strRFind(test, 's') );
  78. }
  79. TEST_CASE("strFindI", "")
  80. {
  81. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  82. REQUIRE(NULL == bx::strFindI(test, "quick", 8) );
  83. REQUIRE(NULL == bx::strFindI(test, "quick1") );
  84. REQUIRE(&test[4] == bx::strFindI(test, "quick", 9) );
  85. REQUIRE(&test[4] == bx::strFindI(test, "quick") );
  86. }
  87. TEST_CASE("strFind", "")
  88. {
  89. {
  90. const char* test = "test";
  91. REQUIRE(NULL == bx::strFind(test, 's', 0) );
  92. REQUIRE(NULL == bx::strFind(test, 's', 2) );
  93. REQUIRE(&test[2] == bx::strFind(test, 's') );
  94. }
  95. {
  96. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  97. REQUIRE(NULL == bx::strFind(test, "quick", 8) );
  98. REQUIRE(NULL == bx::strFind(test, "quick1") );
  99. REQUIRE(NULL == bx::strFind(test, "quick", 9) );
  100. REQUIRE(NULL == bx::strFind(test, "quick") );
  101. REQUIRE(NULL == bx::strFind(test, "Quick", 8) );
  102. REQUIRE(NULL == bx::strFind(test, "Quick1") );
  103. REQUIRE(&test[4] == bx::strFind(test, "Quick", 9) );
  104. REQUIRE(&test[4] == bx::strFind(test, "Quick") );
  105. }
  106. }
  107. template<typename Ty>
  108. static bool testToString(Ty _value, const char* _expected)
  109. {
  110. char tmp[1024];
  111. int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
  112. int32_t len = (int32_t)bx::strLen(_expected);
  113. if (0 == bx::strCmp(tmp, _expected)
  114. && num == len)
  115. {
  116. return true;
  117. }
  118. printf("result '%s' (%d), expected '%s' (%d)\n", tmp, num, _expected, len);
  119. return false;
  120. }
  121. TEST_CASE("toString int32_t/uint32_t", "")
  122. {
  123. REQUIRE(testToString(0, "0") );
  124. REQUIRE(testToString(-256, "-256") );
  125. REQUIRE(testToString(INT32_MAX, "2147483647") );
  126. REQUIRE(testToString(UINT32_MAX, "4294967295") );
  127. }
  128. TEST_CASE("toString double", "")
  129. {
  130. REQUIRE(testToString(0.0, "0.0") );
  131. REQUIRE(testToString(-0.0, "-0.0") );
  132. REQUIRE(testToString(1.0, "1.0") );
  133. REQUIRE(testToString(-1.0, "-1.0") );
  134. REQUIRE(testToString(1.2345, "1.2345") );
  135. REQUIRE(testToString(1.2345678, "1.2345678") );
  136. REQUIRE(testToString(0.123456789012, "0.123456789012") );
  137. REQUIRE(testToString(1234567.8, "1234567.8") );
  138. REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") );
  139. REQUIRE(testToString(0.000001, "0.000001") );
  140. REQUIRE(testToString(0.0000001, "1e-7") );
  141. REQUIRE(testToString(1e30, "1e30") );
  142. REQUIRE(testToString(1.234567890123456e30, "1.234567890123456e30") );
  143. REQUIRE(testToString(-5e-324, "-5e-324") );
  144. REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") );
  145. REQUIRE(testToString(2.2250738585072014e-308, "2.2250738585072014e-308") );
  146. REQUIRE(testToString(1.7976931348623157e308, "1.7976931348623157e308") );
  147. REQUIRE(testToString(0.00000123123123, "0.00000123123123") );
  148. REQUIRE(testToString(0.000000123123123, "1.23123123e-7") );
  149. REQUIRE(testToString(123123.123, "123123.123") );
  150. REQUIRE(testToString(1231231.23, "1231231.23") );
  151. REQUIRE(testToString(0.000000000123123, "1.23123e-10") );
  152. REQUIRE(testToString(0.0000000001, "1e-10") );
  153. }
  154. TEST_CASE("StringView", "")
  155. {
  156. bx::StringView sv("test");
  157. REQUIRE(4 == sv.getLength() );
  158. bx::DefaultAllocator crt;
  159. g_allocator = &crt;
  160. typedef bx::StringT<&g_allocator> String;
  161. String st(sv);
  162. REQUIRE(4 == st.getLength() );
  163. st.append("test");
  164. REQUIRE(8 == st.getLength() );
  165. st.append("test", 2);
  166. REQUIRE(10 == st.getLength() );
  167. REQUIRE(0 == bx::strCmp(st.getPtr(), "testtestte") );
  168. st.clear();
  169. REQUIRE(0 == st.getLength() );
  170. REQUIRE(4 == sv.getLength() );
  171. st.append("test");
  172. REQUIRE(4 == st.getLength() );
  173. sv.clear();
  174. REQUIRE(0 == sv.getLength() );
  175. }