string_test.cc
Go to the documentation of this file.
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 
20 
21 #include <limits.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
29 
31 
32 #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
33 
34 static void test_strdup(void) {
35  static const char* src1 = "hello world";
36  char* dst1;
37 
38  LOG_TEST_NAME("test_strdup");
39 
40  dst1 = gpr_strdup(src1);
41  GPR_ASSERT(0 == strcmp(src1, dst1));
42  gpr_free(dst1);
43 
44  GPR_ASSERT(nullptr == gpr_strdup(nullptr));
45 }
46 
47 static void expect_dump(const char* buf, size_t len, uint32_t flags,
48  const char* result) {
49  char* got = gpr_dump(buf, len, flags);
50  GPR_ASSERT(0 == strcmp(got, result));
51  gpr_free(got);
52 }
53 
54 static void test_dump(void) {
55  LOG_TEST_NAME("test_dump");
56  expect_dump("\x01", 1, GPR_DUMP_HEX, "01");
57  expect_dump("\x01", 1, GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'");
58  expect_dump("\x01\x02", 2, GPR_DUMP_HEX, "01 02");
59  expect_dump("\x01\x23\x45\x67\x89\xab\xcd\xef", 8, GPR_DUMP_HEX,
60  "01 23 45 67 89 ab cd ef");
61  expect_dump("ab", 2, GPR_DUMP_HEX | GPR_DUMP_ASCII, "61 62 'ab'");
62 }
63 
64 static void test_pu32_fail(const char* s) {
65  uint32_t out;
66  GPR_ASSERT(!gpr_parse_bytes_to_uint32(s, strlen(s), &out));
67 }
68 
69 static void test_pu32_succeed(const char* s, uint32_t want) {
70  uint32_t out;
71  GPR_ASSERT(gpr_parse_bytes_to_uint32(s, strlen(s), &out));
72  GPR_ASSERT(out == want);
73 }
74 
75 static void test_parse_uint32(void) {
76  LOG_TEST_NAME("test_parse_uint32");
77 
78  test_pu32_fail("-1");
79  test_pu32_fail("a");
80  test_pu32_fail("");
81  test_pu32_succeed("0", 0);
82  test_pu32_succeed("1", 1);
83  test_pu32_succeed("2", 2);
84  test_pu32_succeed("3", 3);
85  test_pu32_succeed("4", 4);
86  test_pu32_succeed("5", 5);
87  test_pu32_succeed("6", 6);
88  test_pu32_succeed("7", 7);
89  test_pu32_succeed("8", 8);
90  test_pu32_succeed("9", 9);
91  test_pu32_succeed("10", 10);
92  test_pu32_succeed("11", 11);
93  test_pu32_succeed("12", 12);
94  test_pu32_succeed("13", 13);
95  test_pu32_succeed("14", 14);
96  test_pu32_succeed("15", 15);
97  test_pu32_succeed("16", 16);
98  test_pu32_succeed("17", 17);
99  test_pu32_succeed("18", 18);
100  test_pu32_succeed("19", 19);
101  test_pu32_succeed("1234567890", 1234567890);
102  test_pu32_succeed("4294967295", 4294967295u);
103  test_pu32_fail("4294967296");
104  test_pu32_fail("4294967297");
105  test_pu32_fail("4294967298");
106  test_pu32_fail("4294967299");
107 }
108 
109 static void test_asprintf(void) {
110  char* buf;
111  int i, j;
112 
113  LOG_TEST_NAME("test_asprintf");
114 
115  /* Print an empty string. */
116  GPR_ASSERT(gpr_asprintf(&buf, "%s", "") == 0);
117  GPR_ASSERT(buf[0] == '\0');
118  gpr_free(buf);
119 
120  /* Print strings of various lengths. */
121  for (i = 1; i < 100; i++) {
122  GPR_ASSERT(gpr_asprintf(&buf, "%0*d", i, 1) == i);
123 
124  /* The buffer should resemble "000001\0". */
125  for (j = 0; j < i - 2; j++) {
126  GPR_ASSERT(buf[j] == '0');
127  }
128  GPR_ASSERT(buf[i - 1] == '1');
129  GPR_ASSERT(buf[i] == '\0');
130  gpr_free(buf);
131  }
132 }
133 
134 static void test_strjoin(void) {
135  const char* parts[4] = {"one", "two", "three", "four"};
136  size_t joined_len;
137  char* joined;
138 
139  LOG_TEST_NAME("test_strjoin");
140 
141  joined = gpr_strjoin(parts, 4, &joined_len);
142  GPR_ASSERT(0 == strcmp("onetwothreefour", joined));
143  gpr_free(joined);
144 
145  joined = gpr_strjoin(parts, 0, &joined_len);
146  GPR_ASSERT(0 == strcmp("", joined));
147  gpr_free(joined);
148 
149  joined = gpr_strjoin(parts, 1, &joined_len);
150  GPR_ASSERT(0 == strcmp("one", joined));
151  gpr_free(joined);
152 }
153 
154 static void test_strjoin_sep(void) {
155  const char* parts[4] = {"one", "two", "three", "four"};
156  size_t joined_len;
157  char* joined;
158 
159  LOG_TEST_NAME("test_strjoin_sep");
160 
161  joined = gpr_strjoin_sep(parts, 4, ", ", &joined_len);
162  GPR_ASSERT(0 == strcmp("one, two, three, four", joined));
163  gpr_free(joined);
164 
165  /* empty separator */
166  joined = gpr_strjoin_sep(parts, 4, "", &joined_len);
167  GPR_ASSERT(0 == strcmp("onetwothreefour", joined));
168  gpr_free(joined);
169 
170  /* degenerated case specifying zero input parts */
171  joined = gpr_strjoin_sep(parts, 0, ", ", &joined_len);
172  GPR_ASSERT(0 == strcmp("", joined));
173  gpr_free(joined);
174 
175  /* single part should have no separator */
176  joined = gpr_strjoin_sep(parts, 1, ", ", &joined_len);
177  GPR_ASSERT(0 == strcmp("one", joined));
178  gpr_free(joined);
179 }
180 
181 static void test_ltoa() {
182  char* str;
184 
185  LOG_TEST_NAME("test_ltoa");
186 
187  /* zero */
188  GPR_ASSERT(1 == gpr_ltoa(0, buf));
189  GPR_ASSERT(0 == strcmp("0", buf));
190 
191  /* positive number */
192  GPR_ASSERT(3 == gpr_ltoa(123, buf));
193  GPR_ASSERT(0 == strcmp("123", buf));
194 
195  /* negative number */
196  GPR_ASSERT(6 == gpr_ltoa(-12345, buf));
197  GPR_ASSERT(0 == strcmp("-12345", buf));
198 
199  /* large negative - we don't know the size of long in advance */
200  GPR_ASSERT(gpr_asprintf(&str, "%lld", (long long)LONG_MIN));
201  GPR_ASSERT(strlen(str) == (size_t)gpr_ltoa(LONG_MIN, buf));
202  GPR_ASSERT(0 == strcmp(str, buf));
203  gpr_free(str);
204 }
205 
206 static void test_int64toa() {
208 
209  LOG_TEST_NAME("test_int64toa");
210 
211  /* zero */
212  GPR_ASSERT(1 == int64_ttoa(0, buf));
213  GPR_ASSERT(0 == strcmp("0", buf));
214 
215  /* positive */
216  GPR_ASSERT(3 == int64_ttoa(123, buf));
217  GPR_ASSERT(0 == strcmp("123", buf));
218 
219  /* large positive */
220  GPR_ASSERT(19 == int64_ttoa(9223372036854775807LL, buf));
221  GPR_ASSERT(0 == strcmp("9223372036854775807", buf));
222 
223  /* large negative */
224  GPR_ASSERT(20 == int64_ttoa(-9223372036854775807LL - 1, buf));
225  GPR_ASSERT(0 == strcmp("-9223372036854775808", buf));
226 }
227 
228 static void test_leftpad() {
229  char* padded;
230 
231  LOG_TEST_NAME("test_leftpad");
232 
233  padded = gpr_leftpad("foo", ' ', 5);
234  GPR_ASSERT(0 == strcmp(" foo", padded));
235  gpr_free(padded);
236 
237  padded = gpr_leftpad("foo", ' ', 4);
238  GPR_ASSERT(0 == strcmp(" foo", padded));
239  gpr_free(padded);
240 
241  padded = gpr_leftpad("foo", ' ', 3);
242  GPR_ASSERT(0 == strcmp("foo", padded));
243  gpr_free(padded);
244 
245  padded = gpr_leftpad("foo", ' ', 2);
246  GPR_ASSERT(0 == strcmp("foo", padded));
247  gpr_free(padded);
248 
249  padded = gpr_leftpad("foo", ' ', 1);
250  GPR_ASSERT(0 == strcmp("foo", padded));
251  gpr_free(padded);
252 
253  padded = gpr_leftpad("foo", ' ', 0);
254  GPR_ASSERT(0 == strcmp("foo", padded));
255  gpr_free(padded);
256 
257  padded = gpr_leftpad("foo", '0', 5);
258  GPR_ASSERT(0 == strcmp("00foo", padded));
259  gpr_free(padded);
260 }
261 
262 static void test_stricmp(void) {
263  LOG_TEST_NAME("test_stricmp");
264 
265  GPR_ASSERT(0 == gpr_stricmp("hello", "hello"));
266  GPR_ASSERT(0 == gpr_stricmp("HELLO", "hello"));
267  GPR_ASSERT(gpr_stricmp("a", "b") < 0);
268  GPR_ASSERT(gpr_stricmp("b", "a") > 0);
269 }
270 
271 static void test_memrchr(void) {
272  LOG_TEST_NAME("test_memrchr");
273 
274  GPR_ASSERT(nullptr == gpr_memrchr(nullptr, 'a', 0));
275  GPR_ASSERT(nullptr == gpr_memrchr("", 'a', 0));
276  GPR_ASSERT(nullptr == gpr_memrchr("hello", 'b', 5));
277  GPR_ASSERT(0 == strcmp((const char*)gpr_memrchr("hello", 'h', 5), "hello"));
278  GPR_ASSERT(0 == strcmp((const char*)gpr_memrchr("hello", 'o', 5), "o"));
279  GPR_ASSERT(0 == strcmp((const char*)gpr_memrchr("hello", 'l', 5), "lo"));
280 }
281 
282 static void test_parse_bool_value(void) {
283  LOG_TEST_NAME("test_parse_bool_value");
284 
285  bool ret;
286  GPR_ASSERT(true == gpr_parse_bool_value("truE", &ret) && true == ret);
287  GPR_ASSERT(true == gpr_parse_bool_value("falsE", &ret) && false == ret);
288  GPR_ASSERT(true == gpr_parse_bool_value("1", &ret) && true == ret);
289  GPR_ASSERT(true == gpr_parse_bool_value("0", &ret) && false == ret);
290  GPR_ASSERT(true == gpr_parse_bool_value("Yes", &ret) && true == ret);
291  GPR_ASSERT(true == gpr_parse_bool_value("No", &ret) && false == ret);
292  GPR_ASSERT(true == gpr_parse_bool_value("Y", &ret) && true == ret);
293  GPR_ASSERT(true == gpr_parse_bool_value("N", &ret) && false == ret);
294  GPR_ASSERT(false == gpr_parse_bool_value(nullptr, &ret));
295  GPR_ASSERT(false == gpr_parse_bool_value("", &ret));
296 }
297 
298 int main(int argc, char** argv) {
299  grpc::testing::TestEnvironment env(&argc, argv);
300  test_strdup();
301  test_dump();
303  test_asprintf();
304  test_strjoin();
306  test_ltoa();
307  test_int64toa();
308  test_leftpad();
309  test_stricmp();
310  test_memrchr();
312  return 0;
313 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
test_dump
static void test_dump(void)
Definition: string_test.cc:54
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
main
int main(int argc, char **argv)
Definition: string_test.cc:298
log.h
LOG_TEST_NAME
#define LOG_TEST_NAME(x)
Definition: string_test.cc:32
generate.env
env
Definition: generate.py:37
test_stricmp
static void test_stricmp(void)
Definition: string_test.cc:262
test_pu32_fail
static void test_pu32_fail(const char *s)
Definition: string_test.cc:64
test_asprintf
static void test_asprintf(void)
Definition: string_test.cc:109
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
expect_dump
static void expect_dump(const char *buf, size_t len, uint32_t flags, const char *result)
Definition: string_test.cc:47
test_strjoin_sep
static void test_strjoin_sep(void)
Definition: string_test.cc:154
test_strdup
static void test_strdup(void)
Definition: string_test.cc:34
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
GPR_DUMP_HEX
#define GPR_DUMP_HEX
Definition: string.h:34
GPR_DUMP_ASCII
#define GPR_DUMP_ASCII
Definition: string.h:35
gpr_parse_bool_value
bool gpr_parse_bool_value(const char *value, bool *dst)
Definition: string.cc:325
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
test_int64toa
static void test_int64toa()
Definition: string_test.cc:206
string_util.h
test_memrchr
static void test_memrchr(void)
Definition: string_test.cc:271
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_strjoin
char * gpr_strjoin(const char **strs, size_t nstrs, size_t *final_length)
Definition: string.cc:235
GPR_LTOA_MIN_BUFSIZE
#define GPR_LTOA_MIN_BUFSIZE
Definition: string.h:51
gpr_leftpad
char * gpr_leftpad(const char *str, char flag, size_t length)
Definition: string.cc:225
gpr_strjoin_sep
char * gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep, size_t *final_length)
Definition: string.cc:239
gpr_memrchr
void * gpr_memrchr(const void *s, int c, size_t n)
Definition: string.cc:313
gpr_stricmp
int gpr_stricmp(const char *a, const char *b)
Definition: string.cc:282
gpr_asprintf
GPRAPI int gpr_asprintf(char **strp, const char *format,...) GPR_PRINT_FORMAT_CHECK(2
GPR_INT64TOA_MIN_BUFSIZE
#define GPR_INT64TOA_MIN_BUFSIZE
Definition: string.h:59
test_parse_uint32
static void test_parse_uint32(void)
Definition: string_test.cc:75
test_config.h
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
gpr_parse_bytes_to_uint32
int gpr_parse_bytes_to_uint32(const char *buf, size_t len, uint32_t *result)
Definition: string.cc:149
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
test_pu32_succeed
static void test_pu32_succeed(const char *s, uint32_t want)
Definition: string_test.cc:69
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
test_parse_bool_value
static void test_parse_bool_value(void)
Definition: string_test.cc:282
int64_ttoa
int int64_ttoa(int64_t value, char *output)
Definition: string.cc:197
test_strjoin
static void test_strjoin(void)
Definition: string_test.cc:134
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
gpr_dump
char * gpr_dump(const char *buf, size_t len, uint32_t flags)
Definition: string.cc:144
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
test_leftpad
static void test_leftpad()
Definition: string_test.cc:228
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
LL
#define LL(x)
gpr_ltoa
int gpr_ltoa(long value, char *output)
Definition: string.cc:176
test_ltoa
static void test_ltoa()
Definition: string_test.cc:181


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:25