string_test.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/core/lib/gpr/string.h"
  19. #include <limits.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include <grpc/support/string_util.h>
  26. #include "test/core/util/test_config.h"
  27. #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
  28. static void test_strdup(void) {
  29. static const char* src1 = "hello world";
  30. char* dst1;
  31. LOG_TEST_NAME("test_strdup");
  32. dst1 = gpr_strdup(src1);
  33. GPR_ASSERT(0 == strcmp(src1, dst1));
  34. gpr_free(dst1);
  35. GPR_ASSERT(nullptr == gpr_strdup(nullptr));
  36. }
  37. static void expect_dump(const char* buf, size_t len, uint32_t flags,
  38. const char* result) {
  39. char* got = gpr_dump(buf, len, flags);
  40. GPR_ASSERT(0 == strcmp(got, result));
  41. gpr_free(got);
  42. }
  43. static void test_dump(void) {
  44. LOG_TEST_NAME("test_dump");
  45. expect_dump("\x01", 1, GPR_DUMP_HEX, "01");
  46. expect_dump("\x01", 1, GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'");
  47. expect_dump("\x01\x02", 2, GPR_DUMP_HEX, "01 02");
  48. expect_dump("\x01\x23\x45\x67\x89\xab\xcd\xef", 8, GPR_DUMP_HEX,
  49. "01 23 45 67 89 ab cd ef");
  50. expect_dump("ab", 2, GPR_DUMP_HEX | GPR_DUMP_ASCII, "61 62 'ab'");
  51. }
  52. static void test_pu32_fail(const char* s) {
  53. uint32_t out;
  54. GPR_ASSERT(!gpr_parse_bytes_to_uint32(s, strlen(s), &out));
  55. }
  56. static void test_pu32_succeed(const char* s, uint32_t want) {
  57. uint32_t out;
  58. GPR_ASSERT(gpr_parse_bytes_to_uint32(s, strlen(s), &out));
  59. GPR_ASSERT(out == want);
  60. }
  61. static void test_parse_uint32(void) {
  62. LOG_TEST_NAME("test_parse_uint32");
  63. test_pu32_fail("-1");
  64. test_pu32_fail("a");
  65. test_pu32_fail("");
  66. test_pu32_succeed("0", 0);
  67. test_pu32_succeed("1", 1);
  68. test_pu32_succeed("2", 2);
  69. test_pu32_succeed("3", 3);
  70. test_pu32_succeed("4", 4);
  71. test_pu32_succeed("5", 5);
  72. test_pu32_succeed("6", 6);
  73. test_pu32_succeed("7", 7);
  74. test_pu32_succeed("8", 8);
  75. test_pu32_succeed("9", 9);
  76. test_pu32_succeed("10", 10);
  77. test_pu32_succeed("11", 11);
  78. test_pu32_succeed("12", 12);
  79. test_pu32_succeed("13", 13);
  80. test_pu32_succeed("14", 14);
  81. test_pu32_succeed("15", 15);
  82. test_pu32_succeed("16", 16);
  83. test_pu32_succeed("17", 17);
  84. test_pu32_succeed("18", 18);
  85. test_pu32_succeed("19", 19);
  86. test_pu32_succeed("1234567890", 1234567890);
  87. test_pu32_succeed("4294967295", 4294967295u);
  88. test_pu32_fail("4294967296");
  89. test_pu32_fail("4294967297");
  90. test_pu32_fail("4294967298");
  91. test_pu32_fail("4294967299");
  92. }
  93. static void test_asprintf(void) {
  94. char* buf;
  95. int i, j;
  96. LOG_TEST_NAME("test_asprintf");
  97. /* Print an empty string. */
  98. GPR_ASSERT(gpr_asprintf(&buf, "%s", "") == 0);
  99. GPR_ASSERT(buf[0] == '\0');
  100. gpr_free(buf);
  101. /* Print strings of various lengths. */
  102. for (i = 1; i < 100; i++) {
  103. GPR_ASSERT(gpr_asprintf(&buf, "%0*d", i, 1) == i);
  104. /* The buffer should resemble "000001\0". */
  105. for (j = 0; j < i - 2; j++) {
  106. GPR_ASSERT(buf[j] == '0');
  107. }
  108. GPR_ASSERT(buf[i - 1] == '1');
  109. GPR_ASSERT(buf[i] == '\0');
  110. gpr_free(buf);
  111. }
  112. }
  113. static void test_strjoin(void) {
  114. const char* parts[4] = {"one", "two", "three", "four"};
  115. size_t joined_len;
  116. char* joined;
  117. LOG_TEST_NAME("test_strjoin");
  118. joined = gpr_strjoin(parts, 4, &joined_len);
  119. GPR_ASSERT(0 == strcmp("onetwothreefour", joined));
  120. gpr_free(joined);
  121. joined = gpr_strjoin(parts, 0, &joined_len);
  122. GPR_ASSERT(0 == strcmp("", joined));
  123. gpr_free(joined);
  124. joined = gpr_strjoin(parts, 1, &joined_len);
  125. GPR_ASSERT(0 == strcmp("one", joined));
  126. gpr_free(joined);
  127. }
  128. static void test_strjoin_sep(void) {
  129. const char* parts[4] = {"one", "two", "three", "four"};
  130. size_t joined_len;
  131. char* joined;
  132. LOG_TEST_NAME("test_strjoin_sep");
  133. joined = gpr_strjoin_sep(parts, 4, ", ", &joined_len);
  134. GPR_ASSERT(0 == strcmp("one, two, three, four", joined));
  135. gpr_free(joined);
  136. /* empty separator */
  137. joined = gpr_strjoin_sep(parts, 4, "", &joined_len);
  138. GPR_ASSERT(0 == strcmp("onetwothreefour", joined));
  139. gpr_free(joined);
  140. /* degenerated case specifying zero input parts */
  141. joined = gpr_strjoin_sep(parts, 0, ", ", &joined_len);
  142. GPR_ASSERT(0 == strcmp("", joined));
  143. gpr_free(joined);
  144. /* single part should have no separator */
  145. joined = gpr_strjoin_sep(parts, 1, ", ", &joined_len);
  146. GPR_ASSERT(0 == strcmp("one", joined));
  147. gpr_free(joined);
  148. }
  149. static void test_ltoa() {
  150. char* str;
  151. char buf[GPR_LTOA_MIN_BUFSIZE];
  152. LOG_TEST_NAME("test_ltoa");
  153. /* zero */
  154. GPR_ASSERT(1 == gpr_ltoa(0, buf));
  155. GPR_ASSERT(0 == strcmp("0", buf));
  156. /* positive number */
  157. GPR_ASSERT(3 == gpr_ltoa(123, buf));
  158. GPR_ASSERT(0 == strcmp("123", buf));
  159. /* negative number */
  160. GPR_ASSERT(6 == gpr_ltoa(-12345, buf));
  161. GPR_ASSERT(0 == strcmp("-12345", buf));
  162. /* large negative - we don't know the size of long in advance */
  163. GPR_ASSERT(gpr_asprintf(&str, "%lld", (long long)LONG_MIN));
  164. GPR_ASSERT(strlen(str) == (size_t)gpr_ltoa(LONG_MIN, buf));
  165. GPR_ASSERT(0 == strcmp(str, buf));
  166. gpr_free(str);
  167. }
  168. static void test_int64toa() {
  169. char buf[GPR_INT64TOA_MIN_BUFSIZE];
  170. LOG_TEST_NAME("test_int64toa");
  171. /* zero */
  172. GPR_ASSERT(1 == int64_ttoa(0, buf));
  173. GPR_ASSERT(0 == strcmp("0", buf));
  174. /* positive */
  175. GPR_ASSERT(3 == int64_ttoa(123, buf));
  176. GPR_ASSERT(0 == strcmp("123", buf));
  177. /* large positive */
  178. GPR_ASSERT(19 == int64_ttoa(9223372036854775807LL, buf));
  179. GPR_ASSERT(0 == strcmp("9223372036854775807", buf));
  180. /* large negative */
  181. GPR_ASSERT(20 == int64_ttoa(-9223372036854775807LL - 1, buf));
  182. GPR_ASSERT(0 == strcmp("-9223372036854775808", buf));
  183. }
  184. static void test_leftpad() {
  185. char* padded;
  186. LOG_TEST_NAME("test_leftpad");
  187. padded = gpr_leftpad("foo", ' ', 5);
  188. GPR_ASSERT(0 == strcmp(" foo", padded));
  189. gpr_free(padded);
  190. padded = gpr_leftpad("foo", ' ', 4);
  191. GPR_ASSERT(0 == strcmp(" foo", padded));
  192. gpr_free(padded);
  193. padded = gpr_leftpad("foo", ' ', 3);
  194. GPR_ASSERT(0 == strcmp("foo", padded));
  195. gpr_free(padded);
  196. padded = gpr_leftpad("foo", ' ', 2);
  197. GPR_ASSERT(0 == strcmp("foo", padded));
  198. gpr_free(padded);
  199. padded = gpr_leftpad("foo", ' ', 1);
  200. GPR_ASSERT(0 == strcmp("foo", padded));
  201. gpr_free(padded);
  202. padded = gpr_leftpad("foo", ' ', 0);
  203. GPR_ASSERT(0 == strcmp("foo", padded));
  204. gpr_free(padded);
  205. padded = gpr_leftpad("foo", '0', 5);
  206. GPR_ASSERT(0 == strcmp("00foo", padded));
  207. gpr_free(padded);
  208. }
  209. static void test_stricmp(void) {
  210. LOG_TEST_NAME("test_stricmp");
  211. GPR_ASSERT(0 == gpr_stricmp("hello", "hello"));
  212. GPR_ASSERT(0 == gpr_stricmp("HELLO", "hello"));
  213. GPR_ASSERT(gpr_stricmp("a", "b") < 0);
  214. GPR_ASSERT(gpr_stricmp("b", "a") > 0);
  215. }
  216. static void test_memrchr(void) {
  217. LOG_TEST_NAME("test_memrchr");
  218. GPR_ASSERT(nullptr == gpr_memrchr(nullptr, 'a', 0));
  219. GPR_ASSERT(nullptr == gpr_memrchr("", 'a', 0));
  220. GPR_ASSERT(nullptr == gpr_memrchr("hello", 'b', 5));
  221. GPR_ASSERT(0 == strcmp((const char*)gpr_memrchr("hello", 'h', 5), "hello"));
  222. GPR_ASSERT(0 == strcmp((const char*)gpr_memrchr("hello", 'o', 5), "o"));
  223. GPR_ASSERT(0 == strcmp((const char*)gpr_memrchr("hello", 'l', 5), "lo"));
  224. }
  225. static void test_parse_bool_value(void) {
  226. LOG_TEST_NAME("test_parse_bool_value");
  227. bool ret;
  228. GPR_ASSERT(true == gpr_parse_bool_value("truE", &ret) && true == ret);
  229. GPR_ASSERT(true == gpr_parse_bool_value("falsE", &ret) && false == ret);
  230. GPR_ASSERT(true == gpr_parse_bool_value("1", &ret) && true == ret);
  231. GPR_ASSERT(true == gpr_parse_bool_value("0", &ret) && false == ret);
  232. GPR_ASSERT(true == gpr_parse_bool_value("Yes", &ret) && true == ret);
  233. GPR_ASSERT(true == gpr_parse_bool_value("No", &ret) && false == ret);
  234. GPR_ASSERT(true == gpr_parse_bool_value("Y", &ret) && true == ret);
  235. GPR_ASSERT(true == gpr_parse_bool_value("N", &ret) && false == ret);
  236. GPR_ASSERT(false == gpr_parse_bool_value(nullptr, &ret));
  237. GPR_ASSERT(false == gpr_parse_bool_value("", &ret));
  238. }
  239. int main(int argc, char** argv) {
  240. grpc::testing::TestEnvironment env(argc, argv);
  241. test_strdup();
  242. test_dump();
  243. test_parse_uint32();
  244. test_asprintf();
  245. test_strjoin();
  246. test_strjoin_sep();
  247. test_ltoa();
  248. test_int64toa();
  249. test_leftpad();
  250. test_stricmp();
  251. test_memrchr();
  252. test_parse_bool_value();
  253. return 0;
  254. }