1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/StringRef.h"
10 #include "llvm/ADT/Hashing.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "gtest/gtest.h"
17 using namespace llvm;
18
19 namespace llvm {
20
operator <<(std::ostream & OS,const StringRef & S)21 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
22 OS << S.str();
23 return OS;
24 }
25
operator <<(std::ostream & OS,const std::pair<StringRef,StringRef> & P)26 std::ostream &operator<<(std::ostream &OS,
27 const std::pair<StringRef, StringRef> &P) {
28 OS << "(" << P.first << ", " << P.second << ")";
29 return OS;
30 }
31
32 }
33
34 // Check that we can't accidentally assign a temporary std::string to a
35 // StringRef. (Unfortunately we can't make use of the same thing with
36 // constructors.)
37 static_assert(
38 !std::is_assignable<StringRef&, std::string>::value,
39 "Assigning from prvalue std::string");
40 static_assert(
41 !std::is_assignable<StringRef&, std::string &&>::value,
42 "Assigning from xvalue std::string");
43 static_assert(
44 std::is_assignable<StringRef&, std::string &>::value,
45 "Assigning from lvalue std::string");
46 static_assert(
47 std::is_assignable<StringRef&, const char *>::value,
48 "Assigning from prvalue C string");
49 static_assert(
50 std::is_assignable<StringRef&, const char * &&>::value,
51 "Assigning from xvalue C string");
52 static_assert(
53 std::is_assignable<StringRef&, const char * &>::value,
54 "Assigning from lvalue C string");
55
56 namespace {
TEST(StringRefTest,Construction)57 TEST(StringRefTest, Construction) {
58 EXPECT_EQ("", StringRef());
59 EXPECT_EQ("hello", StringRef("hello"));
60 EXPECT_EQ("hello", StringRef("hello world", 5));
61 EXPECT_EQ("hello", StringRef(std::string("hello")));
62 #if __cplusplus > 201402L
63 EXPECT_EQ("hello", StringRef(std::string_view("hello")));
64 #endif
65 }
66
TEST(StringRefTest,Conversion)67 TEST(StringRefTest, Conversion) {
68 EXPECT_EQ("hello", std::string(StringRef("hello")));
69 #if __cplusplus > 201402L
70 EXPECT_EQ("hello", std::string_view(StringRef("hello")));
71 #endif
72 }
73
TEST(StringRefTest,EmptyInitializerList)74 TEST(StringRefTest, EmptyInitializerList) {
75 StringRef S = {};
76 EXPECT_TRUE(S.empty());
77
78 S = {};
79 EXPECT_TRUE(S.empty());
80 }
81
TEST(StringRefTest,Iteration)82 TEST(StringRefTest, Iteration) {
83 StringRef S("hello");
84 const char *p = "hello";
85 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
86 EXPECT_EQ(*it, *p);
87 }
88
TEST(StringRefTest,StringOps)89 TEST(StringRefTest, StringOps) {
90 const char *p = "hello";
91 EXPECT_EQ(p, StringRef(p, 0).data());
92 EXPECT_TRUE(StringRef().empty());
93 EXPECT_EQ((size_t) 5, StringRef("hello").size());
94 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
95 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
96 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
97 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
98 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
99 EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
100
101 EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("aAd"));
102 EXPECT_EQ( 0, StringRef("AaB").compare_insensitive("aab"));
103 EXPECT_EQ( 1, StringRef("AaB").compare_insensitive("AAA"));
104 EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("aaBb"));
105 EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("bb"));
106 EXPECT_EQ( 1, StringRef("aaBb").compare_insensitive("AaB"));
107 EXPECT_EQ( 1, StringRef("bb").compare_insensitive("AaB"));
108 EXPECT_EQ( 1, StringRef("AaB").compare_insensitive("aA"));
109 EXPECT_EQ( 1, StringRef("\xFF").compare_insensitive("\1"));
110
111 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
112 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
113 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
114 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
115 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
116 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
117 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
118 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
119 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
120 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
121 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
122 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
123 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
124 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
125 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
126 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
127 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
128 }
129
TEST(StringRefTest,Operators)130 TEST(StringRefTest, Operators) {
131 EXPECT_EQ("", StringRef());
132 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
133 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
134 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
135 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
136 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
137 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
138 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
139 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
140 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
141 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
142 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
143 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
144 EXPECT_EQ('a', StringRef("aab")[1]);
145 }
146
TEST(StringRefTest,Substr)147 TEST(StringRefTest, Substr) {
148 StringRef Str("hello");
149 EXPECT_EQ("lo", Str.substr(3));
150 EXPECT_EQ("", Str.substr(100));
151 EXPECT_EQ("hello", Str.substr(0, 100));
152 EXPECT_EQ("o", Str.substr(4, 10));
153 }
154
TEST(StringRefTest,Slice)155 TEST(StringRefTest, Slice) {
156 StringRef Str("hello");
157 EXPECT_EQ("l", Str.slice(2, 3));
158 EXPECT_EQ("ell", Str.slice(1, 4));
159 EXPECT_EQ("llo", Str.slice(2, 100));
160 EXPECT_EQ("", Str.slice(2, 1));
161 EXPECT_EQ("", Str.slice(10, 20));
162 }
163
TEST(StringRefTest,Split)164 TEST(StringRefTest, Split) {
165 StringRef Str("hello");
166 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
167 Str.split('X'));
168 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
169 Str.split('e'));
170 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
171 Str.split('h'));
172 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
173 Str.split('l'));
174 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
175 Str.split('o'));
176
177 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
178 Str.rsplit('X'));
179 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
180 Str.rsplit('e'));
181 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
182 Str.rsplit('h'));
183 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
184 Str.rsplit('l'));
185 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
186 Str.rsplit('o'));
187
188 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("o")),
189 Str.rsplit("ll"));
190 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
191 Str.rsplit("h"));
192 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
193 Str.rsplit("o"));
194 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
195 Str.rsplit("::"));
196 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
197 Str.rsplit("l"));
198 }
199
TEST(StringRefTest,Split2)200 TEST(StringRefTest, Split2) {
201 SmallVector<StringRef, 5> parts;
202 SmallVector<StringRef, 5> expected;
203
204 expected.push_back("ab"); expected.push_back("c");
205 StringRef(",ab,,c,").split(parts, ",", -1, false);
206 EXPECT_TRUE(parts == expected);
207
208 expected.clear(); parts.clear();
209 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
210 expected.push_back("c"); expected.push_back("");
211 StringRef(",ab,,c,").split(parts, ",", -1, true);
212 EXPECT_TRUE(parts == expected);
213
214 expected.clear(); parts.clear();
215 expected.push_back("");
216 StringRef("").split(parts, ",", -1, true);
217 EXPECT_TRUE(parts == expected);
218
219 expected.clear(); parts.clear();
220 StringRef("").split(parts, ",", -1, false);
221 EXPECT_TRUE(parts == expected);
222
223 expected.clear(); parts.clear();
224 StringRef(",").split(parts, ",", -1, false);
225 EXPECT_TRUE(parts == expected);
226
227 expected.clear(); parts.clear();
228 expected.push_back(""); expected.push_back("");
229 StringRef(",").split(parts, ",", -1, true);
230 EXPECT_TRUE(parts == expected);
231
232 expected.clear(); parts.clear();
233 expected.push_back("a"); expected.push_back("b");
234 StringRef("a,b").split(parts, ",", -1, true);
235 EXPECT_TRUE(parts == expected);
236
237 // Test MaxSplit
238 expected.clear(); parts.clear();
239 expected.push_back("a,,b,c");
240 StringRef("a,,b,c").split(parts, ",", 0, true);
241 EXPECT_TRUE(parts == expected);
242
243 expected.clear(); parts.clear();
244 expected.push_back("a,,b,c");
245 StringRef("a,,b,c").split(parts, ",", 0, false);
246 EXPECT_TRUE(parts == expected);
247
248 expected.clear(); parts.clear();
249 expected.push_back("a"); expected.push_back(",b,c");
250 StringRef("a,,b,c").split(parts, ",", 1, true);
251 EXPECT_TRUE(parts == expected);
252
253 expected.clear(); parts.clear();
254 expected.push_back("a"); expected.push_back(",b,c");
255 StringRef("a,,b,c").split(parts, ",", 1, false);
256 EXPECT_TRUE(parts == expected);
257
258 expected.clear(); parts.clear();
259 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
260 StringRef("a,,b,c").split(parts, ",", 2, true);
261 EXPECT_TRUE(parts == expected);
262
263 expected.clear(); parts.clear();
264 expected.push_back("a"); expected.push_back("b,c");
265 StringRef("a,,b,c").split(parts, ",", 2, false);
266 EXPECT_TRUE(parts == expected);
267
268 expected.clear(); parts.clear();
269 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
270 expected.push_back("c");
271 StringRef("a,,b,c").split(parts, ",", 3, true);
272 EXPECT_TRUE(parts == expected);
273
274 expected.clear(); parts.clear();
275 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
276 StringRef("a,,b,c").split(parts, ",", 3, false);
277 EXPECT_TRUE(parts == expected);
278
279 expected.clear(); parts.clear();
280 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
281 StringRef("a,,b,c").split(parts, ',', 3, false);
282 EXPECT_TRUE(parts == expected);
283
284 expected.clear(); parts.clear();
285 expected.push_back("");
286 StringRef().split(parts, ",", 0, true);
287 EXPECT_TRUE(parts == expected);
288
289 expected.clear(); parts.clear();
290 expected.push_back(StringRef());
291 StringRef("").split(parts, ",", 0, true);
292 EXPECT_TRUE(parts == expected);
293
294 expected.clear(); parts.clear();
295 StringRef("").split(parts, ",", 0, false);
296 EXPECT_TRUE(parts == expected);
297 StringRef().split(parts, ",", 0, false);
298 EXPECT_TRUE(parts == expected);
299
300 expected.clear(); parts.clear();
301 expected.push_back("a");
302 expected.push_back("");
303 expected.push_back("b");
304 expected.push_back("c,d");
305 StringRef("a,,b,c,d").split(parts, ",", 3, true);
306 EXPECT_TRUE(parts == expected);
307
308 expected.clear(); parts.clear();
309 expected.push_back("");
310 StringRef().split(parts, ',', 0, true);
311 EXPECT_TRUE(parts == expected);
312
313 expected.clear(); parts.clear();
314 expected.push_back(StringRef());
315 StringRef("").split(parts, ',', 0, true);
316 EXPECT_TRUE(parts == expected);
317
318 expected.clear(); parts.clear();
319 StringRef("").split(parts, ',', 0, false);
320 EXPECT_TRUE(parts == expected);
321 StringRef().split(parts, ',', 0, false);
322 EXPECT_TRUE(parts == expected);
323
324 expected.clear(); parts.clear();
325 expected.push_back("a");
326 expected.push_back("");
327 expected.push_back("b");
328 expected.push_back("c,d");
329 StringRef("a,,b,c,d").split(parts, ',', 3, true);
330 EXPECT_TRUE(parts == expected);
331 }
332
TEST(StringRefTest,Trim)333 TEST(StringRefTest, Trim) {
334 StringRef Str0("hello");
335 StringRef Str1(" hello ");
336 StringRef Str2(" hello ");
337 StringRef Str3("\t\n\v\f\r hello \t\n\v\f\r");
338
339 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
340 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
341 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
342 EXPECT_EQ(StringRef("\t\n\v\f\r hello"), Str3.rtrim());
343 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
344 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
345 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
346 EXPECT_EQ(StringRef("hello \t\n\v\f\r"), Str3.ltrim());
347 EXPECT_EQ(StringRef("hello"), Str0.trim());
348 EXPECT_EQ(StringRef("hello"), Str1.trim());
349 EXPECT_EQ(StringRef("hello"), Str2.trim());
350 EXPECT_EQ(StringRef("hello"), Str3.trim());
351
352 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
353
354 EXPECT_EQ(StringRef(""), StringRef("").trim());
355 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
356 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
357 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
358 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
359 }
360
TEST(StringRefTest,StartsWith)361 TEST(StringRefTest, StartsWith) {
362 StringRef Str("hello");
363 EXPECT_TRUE(Str.startswith(""));
364 EXPECT_TRUE(Str.startswith("he"));
365 EXPECT_FALSE(Str.startswith("helloworld"));
366 EXPECT_FALSE(Str.startswith("hi"));
367 }
368
TEST(StringRefTest,StartsWithInsensitive)369 TEST(StringRefTest, StartsWithInsensitive) {
370 StringRef Str("heLLo");
371 EXPECT_TRUE(Str.startswith_insensitive(""));
372 EXPECT_TRUE(Str.startswith_insensitive("he"));
373 EXPECT_TRUE(Str.startswith_insensitive("hell"));
374 EXPECT_TRUE(Str.startswith_insensitive("HELlo"));
375 EXPECT_FALSE(Str.startswith_insensitive("helloworld"));
376 EXPECT_FALSE(Str.startswith_insensitive("hi"));
377 }
378
TEST(StringRefTest,ConsumeFront)379 TEST(StringRefTest, ConsumeFront) {
380 StringRef Str("hello");
381 EXPECT_TRUE(Str.consume_front(""));
382 EXPECT_EQ("hello", Str);
383 EXPECT_TRUE(Str.consume_front("he"));
384 EXPECT_EQ("llo", Str);
385 EXPECT_FALSE(Str.consume_front("lloworld"));
386 EXPECT_EQ("llo", Str);
387 EXPECT_FALSE(Str.consume_front("lol"));
388 EXPECT_EQ("llo", Str);
389 EXPECT_TRUE(Str.consume_front("llo"));
390 EXPECT_EQ("", Str);
391 EXPECT_FALSE(Str.consume_front("o"));
392 EXPECT_TRUE(Str.consume_front(""));
393 }
394
TEST(StringRefTest,ConsumeFrontInsensitive)395 TEST(StringRefTest, ConsumeFrontInsensitive) {
396 StringRef Str("heLLo");
397 EXPECT_TRUE(Str.consume_front_insensitive(""));
398 EXPECT_EQ("heLLo", Str);
399 EXPECT_FALSE(Str.consume_front("HEl"));
400 EXPECT_EQ("heLLo", Str);
401 EXPECT_TRUE(Str.consume_front_insensitive("HEl"));
402 EXPECT_EQ("Lo", Str);
403 EXPECT_FALSE(Str.consume_front_insensitive("loworld"));
404 EXPECT_EQ("Lo", Str);
405 EXPECT_FALSE(Str.consume_front_insensitive("ol"));
406 EXPECT_EQ("Lo", Str);
407 EXPECT_TRUE(Str.consume_front_insensitive("lo"));
408 EXPECT_EQ("", Str);
409 EXPECT_FALSE(Str.consume_front_insensitive("o"));
410 EXPECT_TRUE(Str.consume_front_insensitive(""));
411 }
412
TEST(StringRefTest,EndsWith)413 TEST(StringRefTest, EndsWith) {
414 StringRef Str("hello");
415 EXPECT_TRUE(Str.endswith(""));
416 EXPECT_TRUE(Str.endswith("lo"));
417 EXPECT_FALSE(Str.endswith("helloworld"));
418 EXPECT_FALSE(Str.endswith("worldhello"));
419 EXPECT_FALSE(Str.endswith("so"));
420 }
421
TEST(StringRefTest,EndsWithInsensitive)422 TEST(StringRefTest, EndsWithInsensitive) {
423 StringRef Str("heLLo");
424 EXPECT_TRUE(Str.endswith_insensitive(""));
425 EXPECT_TRUE(Str.endswith_insensitive("lo"));
426 EXPECT_TRUE(Str.endswith_insensitive("LO"));
427 EXPECT_TRUE(Str.endswith_insensitive("ELlo"));
428 EXPECT_FALSE(Str.endswith_insensitive("helloworld"));
429 EXPECT_FALSE(Str.endswith_insensitive("hi"));
430 }
431
TEST(StringRefTest,ConsumeBack)432 TEST(StringRefTest, ConsumeBack) {
433 StringRef Str("hello");
434 EXPECT_TRUE(Str.consume_back(""));
435 EXPECT_EQ("hello", Str);
436 EXPECT_TRUE(Str.consume_back("lo"));
437 EXPECT_EQ("hel", Str);
438 EXPECT_FALSE(Str.consume_back("helhel"));
439 EXPECT_EQ("hel", Str);
440 EXPECT_FALSE(Str.consume_back("hle"));
441 EXPECT_EQ("hel", Str);
442 EXPECT_TRUE(Str.consume_back("hel"));
443 EXPECT_EQ("", Str);
444 EXPECT_FALSE(Str.consume_back("h"));
445 EXPECT_TRUE(Str.consume_back(""));
446 }
447
TEST(StringRefTest,ConsumeBackInsensitive)448 TEST(StringRefTest, ConsumeBackInsensitive) {
449 StringRef Str("heLLo");
450 EXPECT_TRUE(Str.consume_back_insensitive(""));
451 EXPECT_EQ("heLLo", Str);
452 EXPECT_FALSE(Str.consume_back("lO"));
453 EXPECT_EQ("heLLo", Str);
454 EXPECT_TRUE(Str.consume_back_insensitive("lO"));
455 EXPECT_EQ("heL", Str);
456 EXPECT_FALSE(Str.consume_back_insensitive("helhel"));
457 EXPECT_EQ("heL", Str);
458 EXPECT_FALSE(Str.consume_back_insensitive("hle"));
459 EXPECT_EQ("heL", Str);
460 EXPECT_TRUE(Str.consume_back_insensitive("hEl"));
461 EXPECT_EQ("", Str);
462 EXPECT_FALSE(Str.consume_back_insensitive("h"));
463 EXPECT_TRUE(Str.consume_back_insensitive(""));
464 }
465
TEST(StringRefTest,Find)466 TEST(StringRefTest, Find) {
467 StringRef Str("helloHELLO");
468 StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
469
470 struct {
471 StringRef Str;
472 char C;
473 std::size_t From;
474 std::size_t Pos;
475 std::size_t InsensitivePos;
476 } CharExpectations[] = {
477 {Str, 'h', 0U, 0U, 0U},
478 {Str, 'e', 0U, 1U, 1U},
479 {Str, 'l', 0U, 2U, 2U},
480 {Str, 'l', 3U, 3U, 3U},
481 {Str, 'o', 0U, 4U, 4U},
482 {Str, 'L', 0U, 7U, 2U},
483 {Str, 'z', 0U, StringRef::npos, StringRef::npos},
484 };
485
486 struct {
487 StringRef Str;
488 llvm::StringRef S;
489 std::size_t From;
490 std::size_t Pos;
491 std::size_t InsensitivePos;
492 } StrExpectations[] = {
493 {Str, "helloword", 0, StringRef::npos, StringRef::npos},
494 {Str, "hello", 0, 0U, 0U},
495 {Str, "ello", 0, 1U, 1U},
496 {Str, "zz", 0, StringRef::npos, StringRef::npos},
497 {Str, "ll", 2U, 2U, 2U},
498 {Str, "ll", 3U, StringRef::npos, 7U},
499 {Str, "LL", 2U, 7U, 2U},
500 {Str, "LL", 3U, 7U, 7U},
501 {Str, "", 0U, 0U, 0U},
502 {LongStr, "hello", 0U, 36U, 36U},
503 {LongStr, "foo", 0U, 28U, 28U},
504 {LongStr, "hell", 2U, 12U, 12U},
505 {LongStr, "HELL", 2U, 42U, 12U},
506 {LongStr, "", 0U, 0U, 0U}};
507
508 for (auto &E : CharExpectations) {
509 EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From));
510 EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.C, E.From));
511 EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(toupper(E.C), E.From));
512 }
513
514 for (auto &E : StrExpectations) {
515 EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From));
516 EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.S, E.From));
517 EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.S.upper(), E.From));
518 }
519
520 EXPECT_EQ(3U, Str.rfind('l'));
521 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
522 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
523 EXPECT_EQ(0U, Str.rfind("hello"));
524 EXPECT_EQ(1U, Str.rfind("ello"));
525 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
526
527 EXPECT_EQ(8U, Str.rfind_insensitive('l'));
528 EXPECT_EQ(8U, Str.rfind_insensitive('L'));
529 EXPECT_EQ(StringRef::npos, Str.rfind_insensitive('z'));
530 EXPECT_EQ(StringRef::npos, Str.rfind_insensitive("HELLOWORLD"));
531 EXPECT_EQ(5U, Str.rfind("HELLO"));
532 EXPECT_EQ(6U, Str.rfind("ELLO"));
533 EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));
534
535 EXPECT_EQ(2U, Str.find_first_of('l'));
536 EXPECT_EQ(1U, Str.find_first_of("el"));
537 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
538
539 Str = "hello";
540 EXPECT_EQ(1U, Str.find_first_not_of('h'));
541 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
542 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
543
544 EXPECT_EQ(3U, Str.find_last_not_of('o'));
545 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
546 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
547 }
548
TEST(StringRefTest,Count)549 TEST(StringRefTest, Count) {
550 StringRef Str("hello");
551 EXPECT_EQ(2U, Str.count('l'));
552 EXPECT_EQ(1U, Str.count('o'));
553 EXPECT_EQ(0U, Str.count('z'));
554 EXPECT_EQ(0U, Str.count("helloworld"));
555 EXPECT_EQ(1U, Str.count("hello"));
556 EXPECT_EQ(1U, Str.count("ello"));
557 EXPECT_EQ(0U, Str.count("zz"));
558 EXPECT_EQ(0U, Str.count(""));
559
560 StringRef OverlappingAbba("abbabba");
561 EXPECT_EQ(1U, OverlappingAbba.count("abba"));
562 StringRef NonOverlappingAbba("abbaabba");
563 EXPECT_EQ(2U, NonOverlappingAbba.count("abba"));
564 StringRef ComplexAbba("abbabbaxyzabbaxyz");
565 EXPECT_EQ(2U, ComplexAbba.count("abba"));
566 }
567
TEST(StringRefTest,EditDistance)568 TEST(StringRefTest, EditDistance) {
569 StringRef Hello("hello");
570 EXPECT_EQ(2U, Hello.edit_distance("hill"));
571
572 StringRef Industry("industry");
573 EXPECT_EQ(6U, Industry.edit_distance("interest"));
574
575 StringRef Soylent("soylent green is people");
576 EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green"));
577 EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green",
578 /* allow replacements = */ false));
579 EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green",
580 /* allow replacements = */ true,
581 /* max edit distance = */ 8));
582 EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green "
583 "people soiled our green "
584 "people soiled our green "));
585 }
586
TEST(StringRefTest,Misc)587 TEST(StringRefTest, Misc) {
588 std::string Storage;
589 raw_string_ostream OS(Storage);
590 OS << StringRef("hello");
591 EXPECT_EQ("hello", OS.str());
592 }
593
TEST(StringRefTest,Hashing)594 TEST(StringRefTest, Hashing) {
595 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
596 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
597 std::string S = "hello world";
598 hash_code H = hash_value(S);
599 EXPECT_EQ(H, hash_value(StringRef("hello world")));
600 EXPECT_EQ(H, hash_value(StringRef(S)));
601 EXPECT_NE(H, hash_value(StringRef("hello worl")));
602 EXPECT_EQ(hash_value(std::string("hello worl")),
603 hash_value(StringRef("hello worl")));
604 EXPECT_NE(H, hash_value(StringRef("hello world ")));
605 EXPECT_EQ(hash_value(std::string("hello world ")),
606 hash_value(StringRef("hello world ")));
607 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
608 EXPECT_NE(hash_value(std::string("ello worl")),
609 hash_value(StringRef("hello world").slice(1, -1)));
610 }
611
612 struct UnsignedPair {
613 const char *Str;
614 uint64_t Expected;
615 } Unsigned[] =
616 { {"0", 0}
617 , {"255", 255}
618 , {"256", 256}
619 , {"65535", 65535}
620 , {"65536", 65536}
621 , {"4294967295", 4294967295ULL}
622 , {"4294967296", 4294967296ULL}
623 , {"18446744073709551615", 18446744073709551615ULL}
624 , {"042", 34}
625 , {"0x42", 66}
626 , {"0b101010", 42}
627 };
628
629 struct SignedPair {
630 const char *Str;
631 int64_t Expected;
632 } Signed[] =
633 { {"0", 0}
634 , {"-0", 0}
635 , {"127", 127}
636 , {"128", 128}
637 , {"-128", -128}
638 , {"-129", -129}
639 , {"32767", 32767}
640 , {"32768", 32768}
641 , {"-32768", -32768}
642 , {"-32769", -32769}
643 , {"2147483647", 2147483647LL}
644 , {"2147483648", 2147483648LL}
645 , {"-2147483648", -2147483648LL}
646 , {"-2147483649", -2147483649LL}
647 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
648 , {"042", 34}
649 , {"0x42", 66}
650 , {"0b101010", 42}
651 , {"-042", -34}
652 , {"-0x42", -66}
653 , {"-0b101010", -42}
654 };
655
TEST(StringRefTest,getAsInteger)656 TEST(StringRefTest, getAsInteger) {
657 uint8_t U8;
658 uint16_t U16;
659 uint32_t U32;
660 uint64_t U64;
661
662 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
663 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
664 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
665 ASSERT_FALSE(U8Success);
666 EXPECT_EQ(U8, Unsigned[i].Expected);
667 } else {
668 ASSERT_TRUE(U8Success);
669 }
670 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
671 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
672 ASSERT_FALSE(U16Success);
673 EXPECT_EQ(U16, Unsigned[i].Expected);
674 } else {
675 ASSERT_TRUE(U16Success);
676 }
677 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
678 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
679 ASSERT_FALSE(U32Success);
680 EXPECT_EQ(U32, Unsigned[i].Expected);
681 } else {
682 ASSERT_TRUE(U32Success);
683 }
684 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
685 ASSERT_FALSE(U64Success);
686 EXPECT_EQ(U64, Unsigned[i].Expected);
687 }
688
689 int8_t S8;
690 int16_t S16;
691 int32_t S32;
692 int64_t S64;
693
694 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
695 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
696 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
697 ASSERT_FALSE(S8Success);
698 EXPECT_EQ(S8, Signed[i].Expected);
699 } else {
700 ASSERT_TRUE(S8Success);
701 }
702 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
703 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
704 ASSERT_FALSE(S16Success);
705 EXPECT_EQ(S16, Signed[i].Expected);
706 } else {
707 ASSERT_TRUE(S16Success);
708 }
709 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
710 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
711 ASSERT_FALSE(S32Success);
712 EXPECT_EQ(S32, Signed[i].Expected);
713 } else {
714 ASSERT_TRUE(S32Success);
715 }
716 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
717 ASSERT_FALSE(S64Success);
718 EXPECT_EQ(S64, Signed[i].Expected);
719 }
720 }
721
722
723 static const char* BadStrings[] = {
724 "" // empty string
725 , "18446744073709551617" // value just over max
726 , "123456789012345678901" // value way too large
727 , "4t23v" // illegal decimal characters
728 , "0x123W56" // illegal hex characters
729 , "0b2" // illegal bin characters
730 , "08" // illegal oct characters
731 , "0o8" // illegal oct characters
732 , "-123" // negative unsigned value
733 , "0x"
734 , "0b"
735 };
736
737
TEST(StringRefTest,getAsUnsignedIntegerBadStrings)738 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
739 unsigned long long U64;
740 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
741 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
742 ASSERT_TRUE(IsBadNumber);
743 }
744 }
745
746 struct ConsumeUnsignedPair {
747 const char *Str;
748 uint64_t Expected;
749 const char *Leftover;
750 } ConsumeUnsigned[] = {
751 {"0", 0, ""},
752 {"255", 255, ""},
753 {"256", 256, ""},
754 {"65535", 65535, ""},
755 {"65536", 65536, ""},
756 {"4294967295", 4294967295ULL, ""},
757 {"4294967296", 4294967296ULL, ""},
758 {"255A376", 255, "A376"},
759 {"18446744073709551615", 18446744073709551615ULL, ""},
760 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
761 {"042", 34, ""},
762 {"0x42", 66, ""},
763 {"0x42-0x34", 66, "-0x34"},
764 {"0b101010", 42, ""},
765 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit
766 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit
767 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
768
769 struct ConsumeSignedPair {
770 const char *Str;
771 int64_t Expected;
772 const char *Leftover;
773 } ConsumeSigned[] = {
774 {"0", 0, ""},
775 {"-0", 0, ""},
776 {"0-1", 0, "-1"},
777 {"-0-1", 0, "-1"},
778 {"127", 127, ""},
779 {"128", 128, ""},
780 {"127-1", 127, "-1"},
781 {"128-1", 128, "-1"},
782 {"-128", -128, ""},
783 {"-129", -129, ""},
784 {"-128-1", -128, "-1"},
785 {"-129-1", -129, "-1"},
786 {"32767", 32767, ""},
787 {"32768", 32768, ""},
788 {"32767-1", 32767, "-1"},
789 {"32768-1", 32768, "-1"},
790 {"-32768", -32768, ""},
791 {"-32769", -32769, ""},
792 {"-32768-1", -32768, "-1"},
793 {"-32769-1", -32769, "-1"},
794 {"2147483647", 2147483647LL, ""},
795 {"2147483648", 2147483648LL, ""},
796 {"2147483647-1", 2147483647LL, "-1"},
797 {"2147483648-1", 2147483648LL, "-1"},
798 {"-2147483648", -2147483648LL, ""},
799 {"-2147483649", -2147483649LL, ""},
800 {"-2147483648-1", -2147483648LL, "-1"},
801 {"-2147483649-1", -2147483649LL, "-1"},
802 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
803 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
804 {"042", 34, ""},
805 {"042-1", 34, "-1"},
806 {"0x42", 66, ""},
807 {"0x42-1", 66, "-1"},
808 {"0b101010", 42, ""},
809 {"0b101010-1", 42, "-1"},
810 {"-042", -34, ""},
811 {"-042-1", -34, "-1"},
812 {"-0x42", -66, ""},
813 {"-0x42-1", -66, "-1"},
814 {"-0b101010", -42, ""},
815 {"-0b101010-1", -42, "-1"}};
816
TEST(StringRefTest,consumeIntegerUnsigned)817 TEST(StringRefTest, consumeIntegerUnsigned) {
818 uint8_t U8;
819 uint16_t U16;
820 uint32_t U32;
821 uint64_t U64;
822
823 for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) {
824 StringRef Str = ConsumeUnsigned[i].Str;
825 bool U8Success = Str.consumeInteger(0, U8);
826 if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==
827 ConsumeUnsigned[i].Expected) {
828 ASSERT_FALSE(U8Success);
829 EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);
830 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
831 } else {
832 ASSERT_TRUE(U8Success);
833 }
834
835 Str = ConsumeUnsigned[i].Str;
836 bool U16Success = Str.consumeInteger(0, U16);
837 if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==
838 ConsumeUnsigned[i].Expected) {
839 ASSERT_FALSE(U16Success);
840 EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);
841 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
842 } else {
843 ASSERT_TRUE(U16Success);
844 }
845
846 Str = ConsumeUnsigned[i].Str;
847 bool U32Success = Str.consumeInteger(0, U32);
848 if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==
849 ConsumeUnsigned[i].Expected) {
850 ASSERT_FALSE(U32Success);
851 EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);
852 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
853 } else {
854 ASSERT_TRUE(U32Success);
855 }
856
857 Str = ConsumeUnsigned[i].Str;
858 bool U64Success = Str.consumeInteger(0, U64);
859 ASSERT_FALSE(U64Success);
860 EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);
861 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
862 }
863 }
864
TEST(StringRefTest,consumeIntegerSigned)865 TEST(StringRefTest, consumeIntegerSigned) {
866 int8_t S8;
867 int16_t S16;
868 int32_t S32;
869 int64_t S64;
870
871 for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) {
872 StringRef Str = ConsumeSigned[i].Str;
873 bool S8Success = Str.consumeInteger(0, S8);
874 if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
875 ConsumeSigned[i].Expected) {
876 ASSERT_FALSE(S8Success);
877 EXPECT_EQ(S8, ConsumeSigned[i].Expected);
878 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
879 } else {
880 ASSERT_TRUE(S8Success);
881 }
882
883 Str = ConsumeSigned[i].Str;
884 bool S16Success = Str.consumeInteger(0, S16);
885 if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
886 ConsumeSigned[i].Expected) {
887 ASSERT_FALSE(S16Success);
888 EXPECT_EQ(S16, ConsumeSigned[i].Expected);
889 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
890 } else {
891 ASSERT_TRUE(S16Success);
892 }
893
894 Str = ConsumeSigned[i].Str;
895 bool S32Success = Str.consumeInteger(0, S32);
896 if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
897 ConsumeSigned[i].Expected) {
898 ASSERT_FALSE(S32Success);
899 EXPECT_EQ(S32, ConsumeSigned[i].Expected);
900 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
901 } else {
902 ASSERT_TRUE(S32Success);
903 }
904
905 Str = ConsumeSigned[i].Str;
906 bool S64Success = Str.consumeInteger(0, S64);
907 ASSERT_FALSE(S64Success);
908 EXPECT_EQ(S64, ConsumeSigned[i].Expected);
909 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
910 }
911 }
912
913 struct GetDoubleStrings {
914 const char *Str;
915 bool AllowInexact;
916 bool ShouldFail;
917 double D;
918 } DoubleStrings[] = {{"0", false, false, 0.0},
919 {"0.0", false, false, 0.0},
920 {"-0.0", false, false, -0.0},
921 {"123.45", false, true, 123.45},
922 {"123.45", true, false, 123.45},
923 {"1.8e308", true, false, std::numeric_limits<double>::infinity()},
924 {"1.8e308", false, true, std::numeric_limits<double>::infinity()},
925 {"0x0.0000000000001P-1023", false, true, 0.0},
926 {"0x0.0000000000001P-1023", true, false, 0.0},
927 };
928
TEST(StringRefTest,getAsDouble)929 TEST(StringRefTest, getAsDouble) {
930 for (const auto &Entry : DoubleStrings) {
931 double Result;
932 StringRef S(Entry.Str);
933 EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
934 if (!Entry.ShouldFail) {
935 EXPECT_EQ(Result, Entry.D);
936 }
937 }
938 }
939
940 static const char *join_input[] = { "a", "b", "c" };
941 static const char join_result1[] = "a";
942 static const char join_result2[] = "a:b:c";
943 static const char join_result3[] = "a::b::c";
944
TEST(StringRefTest,joinStrings)945 TEST(StringRefTest, joinStrings) {
946 std::vector<StringRef> v1;
947 std::vector<std::string> v2;
948 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
949 v1.push_back(join_input[i]);
950 v2.push_back(join_input[i]);
951 }
952
953 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
954 EXPECT_TRUE(v1_join1);
955 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
956 EXPECT_TRUE(v1_join2);
957 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
958 EXPECT_TRUE(v1_join3);
959
960 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
961 EXPECT_TRUE(v2_join1);
962 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
963 EXPECT_TRUE(v2_join2);
964 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
965 EXPECT_TRUE(v2_join3);
966 v2_join3 = join(v2, "::") == join_result3;
967 EXPECT_TRUE(v2_join3);
968 }
969
970
TEST(StringRefTest,AllocatorCopy)971 TEST(StringRefTest, AllocatorCopy) {
972 BumpPtrAllocator Alloc;
973 // First test empty strings. We don't want these to allocate anything on the
974 // allocator.
975 StringRef StrEmpty = "";
976 StringRef StrEmptyc = StrEmpty.copy(Alloc);
977 EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
978 EXPECT_EQ(StrEmptyc.data(), nullptr);
979 EXPECT_EQ(StrEmptyc.size(), 0u);
980 EXPECT_EQ(Alloc.getTotalMemory(), 0u);
981
982 StringRef Str1 = "hello";
983 StringRef Str2 = "bye";
984 StringRef Str1c = Str1.copy(Alloc);
985 StringRef Str2c = Str2.copy(Alloc);
986 EXPECT_TRUE(Str1.equals(Str1c));
987 EXPECT_NE(Str1.data(), Str1c.data());
988 EXPECT_TRUE(Str2.equals(Str2c));
989 EXPECT_NE(Str2.data(), Str2c.data());
990 }
991
TEST(StringRefTest,Drop)992 TEST(StringRefTest, Drop) {
993 StringRef Test("StringRefTest::Drop");
994
995 StringRef Dropped = Test.drop_front(5);
996 EXPECT_EQ(Dropped, "gRefTest::Drop");
997
998 Dropped = Test.drop_back(5);
999 EXPECT_EQ(Dropped, "StringRefTest:");
1000
1001 Dropped = Test.drop_front(0);
1002 EXPECT_EQ(Dropped, Test);
1003
1004 Dropped = Test.drop_back(0);
1005 EXPECT_EQ(Dropped, Test);
1006
1007 Dropped = Test.drop_front(Test.size());
1008 EXPECT_TRUE(Dropped.empty());
1009
1010 Dropped = Test.drop_back(Test.size());
1011 EXPECT_TRUE(Dropped.empty());
1012 }
1013
TEST(StringRefTest,Take)1014 TEST(StringRefTest, Take) {
1015 StringRef Test("StringRefTest::Take");
1016
1017 StringRef Taken = Test.take_front(5);
1018 EXPECT_EQ(Taken, "Strin");
1019
1020 Taken = Test.take_back(5);
1021 EXPECT_EQ(Taken, ":Take");
1022
1023 Taken = Test.take_front(Test.size());
1024 EXPECT_EQ(Taken, Test);
1025
1026 Taken = Test.take_back(Test.size());
1027 EXPECT_EQ(Taken, Test);
1028
1029 Taken = Test.take_front(0);
1030 EXPECT_TRUE(Taken.empty());
1031
1032 Taken = Test.take_back(0);
1033 EXPECT_TRUE(Taken.empty());
1034 }
1035
TEST(StringRefTest,FindIf)1036 TEST(StringRefTest, FindIf) {
1037 StringRef Punct("Test.String");
1038 StringRef NoPunct("ABCDEFG");
1039 StringRef Empty;
1040
1041 auto IsPunct = [](char c) { return ::ispunct(c); };
1042 auto IsAlpha = [](char c) { return ::isalpha(c); };
1043 EXPECT_EQ(4U, Punct.find_if(IsPunct));
1044 EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
1045 EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
1046
1047 EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
1048 EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
1049 EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
1050 }
1051
TEST(StringRefTest,TakeWhileUntil)1052 TEST(StringRefTest, TakeWhileUntil) {
1053 StringRef Test("String With 1 Number");
1054
1055 StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
1056 EXPECT_EQ("", Taken);
1057
1058 Taken = Test.take_until([](char c) { return ::isdigit(c); });
1059 EXPECT_EQ("String With ", Taken);
1060
1061 Taken = Test.take_while([](char c) { return true; });
1062 EXPECT_EQ(Test, Taken);
1063
1064 Taken = Test.take_until([](char c) { return true; });
1065 EXPECT_EQ("", Taken);
1066
1067 Test = "";
1068 Taken = Test.take_while([](char c) { return true; });
1069 EXPECT_EQ("", Taken);
1070 }
1071
TEST(StringRefTest,DropWhileUntil)1072 TEST(StringRefTest, DropWhileUntil) {
1073 StringRef Test("String With 1 Number");
1074
1075 StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
1076 EXPECT_EQ(Test, Taken);
1077
1078 Taken = Test.drop_until([](char c) { return ::isdigit(c); });
1079 EXPECT_EQ("1 Number", Taken);
1080
1081 Taken = Test.drop_while([](char c) { return true; });
1082 EXPECT_EQ("", Taken);
1083
1084 Taken = Test.drop_until([](char c) { return true; });
1085 EXPECT_EQ(Test, Taken);
1086
1087 StringRef EmptyString = "";
1088 Taken = EmptyString.drop_while([](char c) { return true; });
1089 EXPECT_EQ("", Taken);
1090 }
1091
TEST(StringRefTest,StringLiteral)1092 TEST(StringRefTest, StringLiteral) {
1093 constexpr StringRef StringRefs[] = {"Foo", "Bar"};
1094 EXPECT_EQ(StringRef("Foo"), StringRefs[0]);
1095 EXPECT_EQ(StringRef("Bar"), StringRefs[1]);
1096
1097 constexpr StringLiteral Strings[] = {"Foo", "Bar"};
1098 EXPECT_EQ(StringRef("Foo"), Strings[0]);
1099 EXPECT_EQ(StringRef("Bar"), Strings[1]);
1100 }
1101
1102 // Check gtest prints StringRef as a string instead of a container of chars.
1103 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h
TEST(StringRefTest,GTestPrinter)1104 TEST(StringRefTest, GTestPrinter) {
1105 EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo")));
1106 }
1107
1108 static_assert(std::is_trivially_copyable<StringRef>::value,
1109 "trivially copyable");
1110
1111 } // end anonymous namespace
1112