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
19 #include "src/core/lib/gpr/string.h"
20
21 #include <limits.h>
22 #include <stddef.h>
23 #include <string.h>
24
25 #include "gtest/gtest.h"
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/string_util.h>
29
30 #include "test/core/util/test_config.h"
31
TEST(StringTest,Strdup)32 TEST(StringTest, Strdup) {
33 static const char* src1 = "hello world";
34 char* dst1;
35
36 dst1 = gpr_strdup(src1);
37 ASSERT_STREQ(src1, dst1);
38 gpr_free(dst1);
39
40 ASSERT_EQ(nullptr, gpr_strdup(nullptr));
41 }
42
expect_dump(const char * buf,size_t len,uint32_t flags,const char * result)43 static void expect_dump(const char* buf, size_t len, uint32_t flags,
44 const char* result) {
45 char* got = gpr_dump(buf, len, flags);
46 ASSERT_STREQ(got, result);
47 gpr_free(got);
48 }
49
TEST(StringTest,Dump)50 TEST(StringTest, Dump) {
51 expect_dump("\x01", 1, GPR_DUMP_HEX, "01");
52 expect_dump("\x01", 1, GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'");
53 expect_dump("\x01\x02", 2, GPR_DUMP_HEX, "01 02");
54 expect_dump("\x01\x23\x45\x67\x89\xab\xcd\xef", 8, GPR_DUMP_HEX,
55 "01 23 45 67 89 ab cd ef");
56 expect_dump("ab", 2, GPR_DUMP_HEX | GPR_DUMP_ASCII, "61 62 'ab'");
57 }
58
test_pu32_fail(const char * s)59 static void test_pu32_fail(const char* s) {
60 uint32_t out;
61 ASSERT_FALSE(gpr_parse_bytes_to_uint32(s, strlen(s), &out));
62 }
63
test_pu32_succeed(const char * s,uint32_t want)64 static void test_pu32_succeed(const char* s, uint32_t want) {
65 uint32_t out;
66 ASSERT_TRUE(gpr_parse_bytes_to_uint32(s, strlen(s), &out));
67 ASSERT_EQ(out, want);
68 }
69
TEST(StringTest,ParseUint32)70 TEST(StringTest, ParseUint32) {
71 test_pu32_fail("-1");
72 test_pu32_fail("a");
73 test_pu32_fail("");
74 test_pu32_succeed("0", 0);
75 test_pu32_succeed("1", 1);
76 test_pu32_succeed("2", 2);
77 test_pu32_succeed("3", 3);
78 test_pu32_succeed("4", 4);
79 test_pu32_succeed("5", 5);
80 test_pu32_succeed("6", 6);
81 test_pu32_succeed("7", 7);
82 test_pu32_succeed("8", 8);
83 test_pu32_succeed("9", 9);
84 test_pu32_succeed("10", 10);
85 test_pu32_succeed("11", 11);
86 test_pu32_succeed("12", 12);
87 test_pu32_succeed("13", 13);
88 test_pu32_succeed("14", 14);
89 test_pu32_succeed("15", 15);
90 test_pu32_succeed("16", 16);
91 test_pu32_succeed("17", 17);
92 test_pu32_succeed("18", 18);
93 test_pu32_succeed("19", 19);
94 test_pu32_succeed("1234567890", 1234567890);
95 test_pu32_succeed("4294967295", 4294967295u);
96 test_pu32_fail("4294967296");
97 test_pu32_fail("4294967297");
98 test_pu32_fail("4294967298");
99 test_pu32_fail("4294967299");
100 }
101
TEST(StringTest,Asprintf)102 TEST(StringTest, Asprintf) {
103 char* buf;
104 int i, j;
105
106 // Print an empty string.
107 ASSERT_EQ(gpr_asprintf(&buf, "%s", ""), 0);
108 ASSERT_EQ(buf[0], '\0');
109 gpr_free(buf);
110
111 // Print strings of various lengths.
112 for (i = 1; i < 100; i++) {
113 ASSERT_EQ(gpr_asprintf(&buf, "%0*d", i, 1), i);
114
115 // The buffer should resemble "000001\0".
116 for (j = 0; j < i - 2; j++) {
117 ASSERT_EQ(buf[j], '0');
118 }
119 ASSERT_EQ(buf[i - 1], '1');
120 ASSERT_EQ(buf[i], '\0');
121 gpr_free(buf);
122 }
123 }
124
TEST(StringTest,Strjoin)125 TEST(StringTest, Strjoin) {
126 const char* parts[4] = {"one", "two", "three", "four"};
127 size_t joined_len;
128 char* joined;
129
130 joined = gpr_strjoin(parts, 4, &joined_len);
131 ASSERT_STREQ("onetwothreefour", joined);
132 gpr_free(joined);
133
134 joined = gpr_strjoin(parts, 0, &joined_len);
135 ASSERT_STREQ("", joined);
136 gpr_free(joined);
137
138 joined = gpr_strjoin(parts, 1, &joined_len);
139 ASSERT_STREQ("one", joined);
140 gpr_free(joined);
141 }
142
TEST(StringTest,StrjoinSep)143 TEST(StringTest, StrjoinSep) {
144 const char* parts[4] = {"one", "two", "three", "four"};
145 size_t joined_len;
146 char* joined;
147
148 joined = gpr_strjoin_sep(parts, 4, ", ", &joined_len);
149 ASSERT_STREQ("one, two, three, four", joined);
150 gpr_free(joined);
151
152 // empty separator
153 joined = gpr_strjoin_sep(parts, 4, "", &joined_len);
154 ASSERT_STREQ("onetwothreefour", joined);
155 gpr_free(joined);
156
157 // degenerated case specifying zero input parts
158 joined = gpr_strjoin_sep(parts, 0, ", ", &joined_len);
159 ASSERT_STREQ("", joined);
160 gpr_free(joined);
161
162 // single part should have no separator
163 joined = gpr_strjoin_sep(parts, 1, ", ", &joined_len);
164 ASSERT_STREQ("one", joined);
165 gpr_free(joined);
166 }
167
TEST(StringTest,Ltoa)168 TEST(StringTest, Ltoa) {
169 char* str;
170 char buf[GPR_LTOA_MIN_BUFSIZE];
171
172 // zero
173 ASSERT_EQ(1, gpr_ltoa(0, buf));
174 ASSERT_STREQ("0", buf);
175
176 // positive number
177 ASSERT_EQ(3, gpr_ltoa(123, buf));
178 ASSERT_STREQ("123", buf);
179
180 // negative number
181 ASSERT_EQ(6, gpr_ltoa(-12345, buf));
182 ASSERT_STREQ("-12345", buf);
183
184 // large negative - we don't know the size of long in advance
185 ASSERT_TRUE(gpr_asprintf(&str, "%lld", (long long)LONG_MIN));
186 ASSERT_EQ(strlen(str), (size_t)gpr_ltoa(LONG_MIN, buf));
187 ASSERT_STREQ(str, buf);
188 gpr_free(str);
189 }
190
TEST(StringTest,Int64Toa)191 TEST(StringTest, Int64Toa) {
192 char buf[GPR_INT64TOA_MIN_BUFSIZE];
193
194 // zero
195 ASSERT_EQ(1, int64_ttoa(0, buf));
196 ASSERT_STREQ("0", buf);
197
198 // positive
199 ASSERT_EQ(3, int64_ttoa(123, buf));
200 ASSERT_STREQ("123", buf);
201
202 // large positive
203 ASSERT_EQ(19, int64_ttoa(9223372036854775807LL, buf));
204 ASSERT_STREQ("9223372036854775807", buf);
205
206 // large negative
207 ASSERT_EQ(20, int64_ttoa(-9223372036854775807LL - 1, buf));
208 ASSERT_STREQ("-9223372036854775808", buf);
209 }
210
TEST(StringTest,Leftpad)211 TEST(StringTest, Leftpad) {
212 char* padded;
213
214 padded = gpr_leftpad("foo", ' ', 5);
215 ASSERT_STREQ(" foo", padded);
216 gpr_free(padded);
217
218 padded = gpr_leftpad("foo", ' ', 4);
219 ASSERT_STREQ(" foo", padded);
220 gpr_free(padded);
221
222 padded = gpr_leftpad("foo", ' ', 3);
223 ASSERT_STREQ("foo", padded);
224 gpr_free(padded);
225
226 padded = gpr_leftpad("foo", ' ', 2);
227 ASSERT_STREQ("foo", padded);
228 gpr_free(padded);
229
230 padded = gpr_leftpad("foo", ' ', 1);
231 ASSERT_STREQ("foo", padded);
232 gpr_free(padded);
233
234 padded = gpr_leftpad("foo", ' ', 0);
235 ASSERT_STREQ("foo", padded);
236 gpr_free(padded);
237
238 padded = gpr_leftpad("foo", '0', 5);
239 ASSERT_STREQ("00foo", padded);
240 gpr_free(padded);
241 }
242
TEST(StringTest,Stricmp)243 TEST(StringTest, Stricmp) {
244 ASSERT_EQ(0, gpr_stricmp("hello", "hello"));
245 ASSERT_EQ(0, gpr_stricmp("HELLO", "hello"));
246 ASSERT_LT(gpr_stricmp("a", "b"), 0);
247 ASSERT_GT(gpr_stricmp("b", "a"), 0);
248 }
249
TEST(StringTest,Memrchr)250 TEST(StringTest, Memrchr) {
251 ASSERT_EQ(nullptr, gpr_memrchr(nullptr, 'a', 0));
252 ASSERT_EQ(nullptr, gpr_memrchr("", 'a', 0));
253 ASSERT_EQ(nullptr, gpr_memrchr("hello", 'b', 5));
254 ASSERT_STREQ((const char*)gpr_memrchr("hello", 'h', 5), "hello");
255 ASSERT_STREQ((const char*)gpr_memrchr("hello", 'o', 5), "o");
256 ASSERT_STREQ((const char*)gpr_memrchr("hello", 'l', 5), "lo");
257 }
258
TEST(StringTest,ParseBoolValue)259 TEST(StringTest, ParseBoolValue) {
260 bool ret;
261 ASSERT_TRUE(true == gpr_parse_bool_value("truE", &ret) && true == ret);
262 ASSERT_TRUE(true == gpr_parse_bool_value("falsE", &ret) && false == ret);
263 ASSERT_TRUE(true == gpr_parse_bool_value("1", &ret) && true == ret);
264 ASSERT_TRUE(true == gpr_parse_bool_value("0", &ret) && false == ret);
265 ASSERT_TRUE(true == gpr_parse_bool_value("Yes", &ret) && true == ret);
266 ASSERT_TRUE(true == gpr_parse_bool_value("No", &ret) && false == ret);
267 ASSERT_TRUE(true == gpr_parse_bool_value("Y", &ret) && true == ret);
268 ASSERT_TRUE(true == gpr_parse_bool_value("N", &ret) && false == ret);
269 ASSERT_EQ(false, gpr_parse_bool_value(nullptr, &ret));
270 ASSERT_EQ(false, gpr_parse_bool_value("", &ret));
271 }
272
main(int argc,char ** argv)273 int main(int argc, char** argv) {
274 grpc::testing::TestEnvironment env(&argc, argv);
275 ::testing::InitGoogleTest(&argc, argv);
276 return RUN_ALL_TESTS();
277 }
278