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_lower("aAd"));
102 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
103 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
104 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
105 EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
106 EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
107 EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
108 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
109 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\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,StartsWithLower)369 TEST(StringRefTest, StartsWithLower) {
370 StringRef Str("heLLo");
371 EXPECT_TRUE(Str.startswith_lower(""));
372 EXPECT_TRUE(Str.startswith_lower("he"));
373 EXPECT_TRUE(Str.startswith_lower("hell"));
374 EXPECT_TRUE(Str.startswith_lower("HELlo"));
375 EXPECT_FALSE(Str.startswith_lower("helloworld"));
376 EXPECT_FALSE(Str.startswith_lower("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,EndsWith)395 TEST(StringRefTest, EndsWith) {
396 StringRef Str("hello");
397 EXPECT_TRUE(Str.endswith(""));
398 EXPECT_TRUE(Str.endswith("lo"));
399 EXPECT_FALSE(Str.endswith("helloworld"));
400 EXPECT_FALSE(Str.endswith("worldhello"));
401 EXPECT_FALSE(Str.endswith("so"));
402 }
403
TEST(StringRefTest,EndsWithLower)404 TEST(StringRefTest, EndsWithLower) {
405 StringRef Str("heLLo");
406 EXPECT_TRUE(Str.endswith_lower(""));
407 EXPECT_TRUE(Str.endswith_lower("lo"));
408 EXPECT_TRUE(Str.endswith_lower("LO"));
409 EXPECT_TRUE(Str.endswith_lower("ELlo"));
410 EXPECT_FALSE(Str.endswith_lower("helloworld"));
411 EXPECT_FALSE(Str.endswith_lower("hi"));
412 }
413
TEST(StringRefTest,ConsumeBack)414 TEST(StringRefTest, ConsumeBack) {
415 StringRef Str("hello");
416 EXPECT_TRUE(Str.consume_back(""));
417 EXPECT_EQ("hello", Str);
418 EXPECT_TRUE(Str.consume_back("lo"));
419 EXPECT_EQ("hel", Str);
420 EXPECT_FALSE(Str.consume_back("helhel"));
421 EXPECT_EQ("hel", Str);
422 EXPECT_FALSE(Str.consume_back("hle"));
423 EXPECT_EQ("hel", Str);
424 EXPECT_TRUE(Str.consume_back("hel"));
425 EXPECT_EQ("", Str);
426 EXPECT_FALSE(Str.consume_back("h"));
427 EXPECT_TRUE(Str.consume_back(""));
428 }
429
TEST(StringRefTest,Find)430 TEST(StringRefTest, Find) {
431 StringRef Str("helloHELLO");
432 StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
433
434 struct {
435 StringRef Str;
436 char C;
437 std::size_t From;
438 std::size_t Pos;
439 std::size_t LowerPos;
440 } CharExpectations[] = {
441 {Str, 'h', 0U, 0U, 0U},
442 {Str, 'e', 0U, 1U, 1U},
443 {Str, 'l', 0U, 2U, 2U},
444 {Str, 'l', 3U, 3U, 3U},
445 {Str, 'o', 0U, 4U, 4U},
446 {Str, 'L', 0U, 7U, 2U},
447 {Str, 'z', 0U, StringRef::npos, StringRef::npos},
448 };
449
450 struct {
451 StringRef Str;
452 llvm::StringRef S;
453 std::size_t From;
454 std::size_t Pos;
455 std::size_t LowerPos;
456 } StrExpectations[] = {
457 {Str, "helloword", 0, StringRef::npos, StringRef::npos},
458 {Str, "hello", 0, 0U, 0U},
459 {Str, "ello", 0, 1U, 1U},
460 {Str, "zz", 0, StringRef::npos, StringRef::npos},
461 {Str, "ll", 2U, 2U, 2U},
462 {Str, "ll", 3U, StringRef::npos, 7U},
463 {Str, "LL", 2U, 7U, 2U},
464 {Str, "LL", 3U, 7U, 7U},
465 {Str, "", 0U, 0U, 0U},
466 {LongStr, "hello", 0U, 36U, 36U},
467 {LongStr, "foo", 0U, 28U, 28U},
468 {LongStr, "hell", 2U, 12U, 12U},
469 {LongStr, "HELL", 2U, 42U, 12U},
470 {LongStr, "", 0U, 0U, 0U}};
471
472 for (auto &E : CharExpectations) {
473 EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From));
474 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.C, E.From));
475 EXPECT_EQ(E.LowerPos, E.Str.find_lower(toupper(E.C), E.From));
476 }
477
478 for (auto &E : StrExpectations) {
479 EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From));
480 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S, E.From));
481 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S.upper(), E.From));
482 }
483
484 EXPECT_EQ(3U, Str.rfind('l'));
485 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
486 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
487 EXPECT_EQ(0U, Str.rfind("hello"));
488 EXPECT_EQ(1U, Str.rfind("ello"));
489 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
490
491 EXPECT_EQ(8U, Str.rfind_lower('l'));
492 EXPECT_EQ(8U, Str.rfind_lower('L'));
493 EXPECT_EQ(StringRef::npos, Str.rfind_lower('z'));
494 EXPECT_EQ(StringRef::npos, Str.rfind_lower("HELLOWORLD"));
495 EXPECT_EQ(5U, Str.rfind("HELLO"));
496 EXPECT_EQ(6U, Str.rfind("ELLO"));
497 EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));
498
499 EXPECT_EQ(2U, Str.find_first_of('l'));
500 EXPECT_EQ(1U, Str.find_first_of("el"));
501 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
502
503 Str = "hello";
504 EXPECT_EQ(1U, Str.find_first_not_of('h'));
505 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
506 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
507
508 EXPECT_EQ(3U, Str.find_last_not_of('o'));
509 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
510 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
511 }
512
TEST(StringRefTest,Count)513 TEST(StringRefTest, Count) {
514 StringRef Str("hello");
515 EXPECT_EQ(2U, Str.count('l'));
516 EXPECT_EQ(1U, Str.count('o'));
517 EXPECT_EQ(0U, Str.count('z'));
518 EXPECT_EQ(0U, Str.count("helloworld"));
519 EXPECT_EQ(1U, Str.count("hello"));
520 EXPECT_EQ(1U, Str.count("ello"));
521 EXPECT_EQ(0U, Str.count("zz"));
522 EXPECT_EQ(0U, Str.count(""));
523
524 StringRef OverlappingAbba("abbabba");
525 EXPECT_EQ(1U, OverlappingAbba.count("abba"));
526 StringRef NonOverlappingAbba("abbaabba");
527 EXPECT_EQ(2U, NonOverlappingAbba.count("abba"));
528 StringRef ComplexAbba("abbabbaxyzabbaxyz");
529 EXPECT_EQ(2U, ComplexAbba.count("abba"));
530 }
531
TEST(StringRefTest,EditDistance)532 TEST(StringRefTest, EditDistance) {
533 StringRef Hello("hello");
534 EXPECT_EQ(2U, Hello.edit_distance("hill"));
535
536 StringRef Industry("industry");
537 EXPECT_EQ(6U, Industry.edit_distance("interest"));
538
539 StringRef Soylent("soylent green is people");
540 EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green"));
541 EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green",
542 /* allow replacements = */ false));
543 EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green",
544 /* allow replacements = */ true,
545 /* max edit distance = */ 8));
546 EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green "
547 "people soiled our green "
548 "people soiled our green "));
549 }
550
TEST(StringRefTest,Misc)551 TEST(StringRefTest, Misc) {
552 std::string Storage;
553 raw_string_ostream OS(Storage);
554 OS << StringRef("hello");
555 EXPECT_EQ("hello", OS.str());
556 }
557
TEST(StringRefTest,Hashing)558 TEST(StringRefTest, Hashing) {
559 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
560 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
561 std::string S = "hello world";
562 hash_code H = hash_value(S);
563 EXPECT_EQ(H, hash_value(StringRef("hello world")));
564 EXPECT_EQ(H, hash_value(StringRef(S)));
565 EXPECT_NE(H, hash_value(StringRef("hello worl")));
566 EXPECT_EQ(hash_value(std::string("hello worl")),
567 hash_value(StringRef("hello worl")));
568 EXPECT_NE(H, hash_value(StringRef("hello world ")));
569 EXPECT_EQ(hash_value(std::string("hello world ")),
570 hash_value(StringRef("hello world ")));
571 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
572 EXPECT_NE(hash_value(std::string("ello worl")),
573 hash_value(StringRef("hello world").slice(1, -1)));
574 }
575
576 struct UnsignedPair {
577 const char *Str;
578 uint64_t Expected;
579 } Unsigned[] =
580 { {"0", 0}
581 , {"255", 255}
582 , {"256", 256}
583 , {"65535", 65535}
584 , {"65536", 65536}
585 , {"4294967295", 4294967295ULL}
586 , {"4294967296", 4294967296ULL}
587 , {"18446744073709551615", 18446744073709551615ULL}
588 , {"042", 34}
589 , {"0x42", 66}
590 , {"0b101010", 42}
591 };
592
593 struct SignedPair {
594 const char *Str;
595 int64_t Expected;
596 } Signed[] =
597 { {"0", 0}
598 , {"-0", 0}
599 , {"127", 127}
600 , {"128", 128}
601 , {"-128", -128}
602 , {"-129", -129}
603 , {"32767", 32767}
604 , {"32768", 32768}
605 , {"-32768", -32768}
606 , {"-32769", -32769}
607 , {"2147483647", 2147483647LL}
608 , {"2147483648", 2147483648LL}
609 , {"-2147483648", -2147483648LL}
610 , {"-2147483649", -2147483649LL}
611 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
612 , {"042", 34}
613 , {"0x42", 66}
614 , {"0b101010", 42}
615 , {"-042", -34}
616 , {"-0x42", -66}
617 , {"-0b101010", -42}
618 };
619
TEST(StringRefTest,getAsInteger)620 TEST(StringRefTest, getAsInteger) {
621 uint8_t U8;
622 uint16_t U16;
623 uint32_t U32;
624 uint64_t U64;
625
626 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
627 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
628 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
629 ASSERT_FALSE(U8Success);
630 EXPECT_EQ(U8, Unsigned[i].Expected);
631 } else {
632 ASSERT_TRUE(U8Success);
633 }
634 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
635 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
636 ASSERT_FALSE(U16Success);
637 EXPECT_EQ(U16, Unsigned[i].Expected);
638 } else {
639 ASSERT_TRUE(U16Success);
640 }
641 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
642 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
643 ASSERT_FALSE(U32Success);
644 EXPECT_EQ(U32, Unsigned[i].Expected);
645 } else {
646 ASSERT_TRUE(U32Success);
647 }
648 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
649 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
650 ASSERT_FALSE(U64Success);
651 EXPECT_EQ(U64, Unsigned[i].Expected);
652 } else {
653 ASSERT_TRUE(U64Success);
654 }
655 }
656
657 int8_t S8;
658 int16_t S16;
659 int32_t S32;
660 int64_t S64;
661
662 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
663 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
664 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
665 ASSERT_FALSE(S8Success);
666 EXPECT_EQ(S8, Signed[i].Expected);
667 } else {
668 ASSERT_TRUE(S8Success);
669 }
670 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
671 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
672 ASSERT_FALSE(S16Success);
673 EXPECT_EQ(S16, Signed[i].Expected);
674 } else {
675 ASSERT_TRUE(S16Success);
676 }
677 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
678 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
679 ASSERT_FALSE(S32Success);
680 EXPECT_EQ(S32, Signed[i].Expected);
681 } else {
682 ASSERT_TRUE(S32Success);
683 }
684 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
685 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
686 ASSERT_FALSE(S64Success);
687 EXPECT_EQ(S64, Signed[i].Expected);
688 } else {
689 ASSERT_TRUE(S64Success);
690 }
691 }
692 }
693
694
695 static const char* BadStrings[] = {
696 "" // empty string
697 , "18446744073709551617" // value just over max
698 , "123456789012345678901" // value way too large
699 , "4t23v" // illegal decimal characters
700 , "0x123W56" // illegal hex characters
701 , "0b2" // illegal bin characters
702 , "08" // illegal oct characters
703 , "0o8" // illegal oct characters
704 , "-123" // negative unsigned value
705 , "0x"
706 , "0b"
707 };
708
709
TEST(StringRefTest,getAsUnsignedIntegerBadStrings)710 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
711 unsigned long long U64;
712 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
713 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
714 ASSERT_TRUE(IsBadNumber);
715 }
716 }
717
718 struct ConsumeUnsignedPair {
719 const char *Str;
720 uint64_t Expected;
721 const char *Leftover;
722 } ConsumeUnsigned[] = {
723 {"0", 0, ""},
724 {"255", 255, ""},
725 {"256", 256, ""},
726 {"65535", 65535, ""},
727 {"65536", 65536, ""},
728 {"4294967295", 4294967295ULL, ""},
729 {"4294967296", 4294967296ULL, ""},
730 {"255A376", 255, "A376"},
731 {"18446744073709551615", 18446744073709551615ULL, ""},
732 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
733 {"042", 34, ""},
734 {"0x42", 66, ""},
735 {"0x42-0x34", 66, "-0x34"},
736 {"0b101010", 42, ""},
737 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit
738 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit
739 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
740
741 struct ConsumeSignedPair {
742 const char *Str;
743 int64_t Expected;
744 const char *Leftover;
745 } ConsumeSigned[] = {
746 {"0", 0, ""},
747 {"-0", 0, ""},
748 {"0-1", 0, "-1"},
749 {"-0-1", 0, "-1"},
750 {"127", 127, ""},
751 {"128", 128, ""},
752 {"127-1", 127, "-1"},
753 {"128-1", 128, "-1"},
754 {"-128", -128, ""},
755 {"-129", -129, ""},
756 {"-128-1", -128, "-1"},
757 {"-129-1", -129, "-1"},
758 {"32767", 32767, ""},
759 {"32768", 32768, ""},
760 {"32767-1", 32767, "-1"},
761 {"32768-1", 32768, "-1"},
762 {"-32768", -32768, ""},
763 {"-32769", -32769, ""},
764 {"-32768-1", -32768, "-1"},
765 {"-32769-1", -32769, "-1"},
766 {"2147483647", 2147483647LL, ""},
767 {"2147483648", 2147483648LL, ""},
768 {"2147483647-1", 2147483647LL, "-1"},
769 {"2147483648-1", 2147483648LL, "-1"},
770 {"-2147483648", -2147483648LL, ""},
771 {"-2147483649", -2147483649LL, ""},
772 {"-2147483648-1", -2147483648LL, "-1"},
773 {"-2147483649-1", -2147483649LL, "-1"},
774 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
775 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
776 {"042", 34, ""},
777 {"042-1", 34, "-1"},
778 {"0x42", 66, ""},
779 {"0x42-1", 66, "-1"},
780 {"0b101010", 42, ""},
781 {"0b101010-1", 42, "-1"},
782 {"-042", -34, ""},
783 {"-042-1", -34, "-1"},
784 {"-0x42", -66, ""},
785 {"-0x42-1", -66, "-1"},
786 {"-0b101010", -42, ""},
787 {"-0b101010-1", -42, "-1"}};
788
TEST(StringRefTest,consumeIntegerUnsigned)789 TEST(StringRefTest, consumeIntegerUnsigned) {
790 uint8_t U8;
791 uint16_t U16;
792 uint32_t U32;
793 uint64_t U64;
794
795 for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) {
796 StringRef Str = ConsumeUnsigned[i].Str;
797 bool U8Success = Str.consumeInteger(0, U8);
798 if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==
799 ConsumeUnsigned[i].Expected) {
800 ASSERT_FALSE(U8Success);
801 EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);
802 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
803 } else {
804 ASSERT_TRUE(U8Success);
805 }
806
807 Str = ConsumeUnsigned[i].Str;
808 bool U16Success = Str.consumeInteger(0, U16);
809 if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==
810 ConsumeUnsigned[i].Expected) {
811 ASSERT_FALSE(U16Success);
812 EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);
813 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
814 } else {
815 ASSERT_TRUE(U16Success);
816 }
817
818 Str = ConsumeUnsigned[i].Str;
819 bool U32Success = Str.consumeInteger(0, U32);
820 if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==
821 ConsumeUnsigned[i].Expected) {
822 ASSERT_FALSE(U32Success);
823 EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);
824 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
825 } else {
826 ASSERT_TRUE(U32Success);
827 }
828
829 Str = ConsumeUnsigned[i].Str;
830 bool U64Success = Str.consumeInteger(0, U64);
831 if (static_cast<uint64_t>(ConsumeUnsigned[i].Expected) ==
832 ConsumeUnsigned[i].Expected) {
833 ASSERT_FALSE(U64Success);
834 EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);
835 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
836 } else {
837 ASSERT_TRUE(U64Success);
838 }
839 }
840 }
841
TEST(StringRefTest,consumeIntegerSigned)842 TEST(StringRefTest, consumeIntegerSigned) {
843 int8_t S8;
844 int16_t S16;
845 int32_t S32;
846 int64_t S64;
847
848 for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) {
849 StringRef Str = ConsumeSigned[i].Str;
850 bool S8Success = Str.consumeInteger(0, S8);
851 if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
852 ConsumeSigned[i].Expected) {
853 ASSERT_FALSE(S8Success);
854 EXPECT_EQ(S8, ConsumeSigned[i].Expected);
855 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
856 } else {
857 ASSERT_TRUE(S8Success);
858 }
859
860 Str = ConsumeSigned[i].Str;
861 bool S16Success = Str.consumeInteger(0, S16);
862 if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
863 ConsumeSigned[i].Expected) {
864 ASSERT_FALSE(S16Success);
865 EXPECT_EQ(S16, ConsumeSigned[i].Expected);
866 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
867 } else {
868 ASSERT_TRUE(S16Success);
869 }
870
871 Str = ConsumeSigned[i].Str;
872 bool S32Success = Str.consumeInteger(0, S32);
873 if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
874 ConsumeSigned[i].Expected) {
875 ASSERT_FALSE(S32Success);
876 EXPECT_EQ(S32, ConsumeSigned[i].Expected);
877 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
878 } else {
879 ASSERT_TRUE(S32Success);
880 }
881
882 Str = ConsumeSigned[i].Str;
883 bool S64Success = Str.consumeInteger(0, S64);
884 if (static_cast<int64_t>(ConsumeSigned[i].Expected) ==
885 ConsumeSigned[i].Expected) {
886 ASSERT_FALSE(S64Success);
887 EXPECT_EQ(S64, ConsumeSigned[i].Expected);
888 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
889 } else {
890 ASSERT_TRUE(S64Success);
891 }
892 }
893 }
894
895 struct GetDoubleStrings {
896 const char *Str;
897 bool AllowInexact;
898 bool ShouldFail;
899 double D;
900 } DoubleStrings[] = {{"0", false, false, 0.0},
901 {"0.0", false, false, 0.0},
902 {"-0.0", false, false, -0.0},
903 {"123.45", false, true, 123.45},
904 {"123.45", true, false, 123.45},
905 {"1.8e308", true, false, std::numeric_limits<double>::infinity()},
906 {"1.8e308", false, true, std::numeric_limits<double>::infinity()},
907 {"0x0.0000000000001P-1023", false, true, 0.0},
908 {"0x0.0000000000001P-1023", true, false, 0.0},
909 };
910
TEST(StringRefTest,getAsDouble)911 TEST(StringRefTest, getAsDouble) {
912 for (const auto &Entry : DoubleStrings) {
913 double Result;
914 StringRef S(Entry.Str);
915 EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
916 if (!Entry.ShouldFail) {
917 EXPECT_EQ(Result, Entry.D);
918 }
919 }
920 }
921
922 static const char *join_input[] = { "a", "b", "c" };
923 static const char join_result1[] = "a";
924 static const char join_result2[] = "a:b:c";
925 static const char join_result3[] = "a::b::c";
926
TEST(StringRefTest,joinStrings)927 TEST(StringRefTest, joinStrings) {
928 std::vector<StringRef> v1;
929 std::vector<std::string> v2;
930 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
931 v1.push_back(join_input[i]);
932 v2.push_back(join_input[i]);
933 }
934
935 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
936 EXPECT_TRUE(v1_join1);
937 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
938 EXPECT_TRUE(v1_join2);
939 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
940 EXPECT_TRUE(v1_join3);
941
942 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
943 EXPECT_TRUE(v2_join1);
944 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
945 EXPECT_TRUE(v2_join2);
946 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
947 EXPECT_TRUE(v2_join3);
948 v2_join3 = join(v2, "::") == join_result3;
949 EXPECT_TRUE(v2_join3);
950 }
951
952
TEST(StringRefTest,AllocatorCopy)953 TEST(StringRefTest, AllocatorCopy) {
954 BumpPtrAllocator Alloc;
955 // First test empty strings. We don't want these to allocate anything on the
956 // allocator.
957 StringRef StrEmpty = "";
958 StringRef StrEmptyc = StrEmpty.copy(Alloc);
959 EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
960 EXPECT_EQ(StrEmptyc.data(), nullptr);
961 EXPECT_EQ(StrEmptyc.size(), 0u);
962 EXPECT_EQ(Alloc.getTotalMemory(), 0u);
963
964 StringRef Str1 = "hello";
965 StringRef Str2 = "bye";
966 StringRef Str1c = Str1.copy(Alloc);
967 StringRef Str2c = Str2.copy(Alloc);
968 EXPECT_TRUE(Str1.equals(Str1c));
969 EXPECT_NE(Str1.data(), Str1c.data());
970 EXPECT_TRUE(Str2.equals(Str2c));
971 EXPECT_NE(Str2.data(), Str2c.data());
972 }
973
TEST(StringRefTest,Drop)974 TEST(StringRefTest, Drop) {
975 StringRef Test("StringRefTest::Drop");
976
977 StringRef Dropped = Test.drop_front(5);
978 EXPECT_EQ(Dropped, "gRefTest::Drop");
979
980 Dropped = Test.drop_back(5);
981 EXPECT_EQ(Dropped, "StringRefTest:");
982
983 Dropped = Test.drop_front(0);
984 EXPECT_EQ(Dropped, Test);
985
986 Dropped = Test.drop_back(0);
987 EXPECT_EQ(Dropped, Test);
988
989 Dropped = Test.drop_front(Test.size());
990 EXPECT_TRUE(Dropped.empty());
991
992 Dropped = Test.drop_back(Test.size());
993 EXPECT_TRUE(Dropped.empty());
994 }
995
TEST(StringRefTest,Take)996 TEST(StringRefTest, Take) {
997 StringRef Test("StringRefTest::Take");
998
999 StringRef Taken = Test.take_front(5);
1000 EXPECT_EQ(Taken, "Strin");
1001
1002 Taken = Test.take_back(5);
1003 EXPECT_EQ(Taken, ":Take");
1004
1005 Taken = Test.take_front(Test.size());
1006 EXPECT_EQ(Taken, Test);
1007
1008 Taken = Test.take_back(Test.size());
1009 EXPECT_EQ(Taken, Test);
1010
1011 Taken = Test.take_front(0);
1012 EXPECT_TRUE(Taken.empty());
1013
1014 Taken = Test.take_back(0);
1015 EXPECT_TRUE(Taken.empty());
1016 }
1017
TEST(StringRefTest,FindIf)1018 TEST(StringRefTest, FindIf) {
1019 StringRef Punct("Test.String");
1020 StringRef NoPunct("ABCDEFG");
1021 StringRef Empty;
1022
1023 auto IsPunct = [](char c) { return ::ispunct(c); };
1024 auto IsAlpha = [](char c) { return ::isalpha(c); };
1025 EXPECT_EQ(4U, Punct.find_if(IsPunct));
1026 EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
1027 EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
1028
1029 EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
1030 EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
1031 EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
1032 }
1033
TEST(StringRefTest,TakeWhileUntil)1034 TEST(StringRefTest, TakeWhileUntil) {
1035 StringRef Test("String With 1 Number");
1036
1037 StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
1038 EXPECT_EQ("", Taken);
1039
1040 Taken = Test.take_until([](char c) { return ::isdigit(c); });
1041 EXPECT_EQ("String With ", Taken);
1042
1043 Taken = Test.take_while([](char c) { return true; });
1044 EXPECT_EQ(Test, Taken);
1045
1046 Taken = Test.take_until([](char c) { return true; });
1047 EXPECT_EQ("", Taken);
1048
1049 Test = "";
1050 Taken = Test.take_while([](char c) { return true; });
1051 EXPECT_EQ("", Taken);
1052 }
1053
TEST(StringRefTest,DropWhileUntil)1054 TEST(StringRefTest, DropWhileUntil) {
1055 StringRef Test("String With 1 Number");
1056
1057 StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
1058 EXPECT_EQ(Test, Taken);
1059
1060 Taken = Test.drop_until([](char c) { return ::isdigit(c); });
1061 EXPECT_EQ("1 Number", Taken);
1062
1063 Taken = Test.drop_while([](char c) { return true; });
1064 EXPECT_EQ("", Taken);
1065
1066 Taken = Test.drop_until([](char c) { return true; });
1067 EXPECT_EQ(Test, Taken);
1068
1069 StringRef EmptyString = "";
1070 Taken = EmptyString.drop_while([](char c) { return true; });
1071 EXPECT_EQ("", Taken);
1072 }
1073
TEST(StringRefTest,StringLiteral)1074 TEST(StringRefTest, StringLiteral) {
1075 constexpr StringRef StringRefs[] = {"Foo", "Bar"};
1076 EXPECT_EQ(StringRef("Foo"), StringRefs[0]);
1077 EXPECT_EQ(StringRef("Bar"), StringRefs[1]);
1078
1079 constexpr StringLiteral Strings[] = {"Foo", "Bar"};
1080 EXPECT_EQ(StringRef("Foo"), Strings[0]);
1081 EXPECT_EQ(StringRef("Bar"), Strings[1]);
1082 }
1083
1084 // Check gtest prints StringRef as a string instead of a container of chars.
1085 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h
TEST(StringRefTest,GTestPrinter)1086 TEST(StringRefTest, GTestPrinter) {
1087 EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo")));
1088 }
1089
1090 static_assert(is_trivially_copyable<StringRef>::value, "trivially copyable");
1091
1092 } // end anonymous namespace
1093