Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / unittests / ADT / StringRefTest.cpp
bloba208527b6c803211122f73f7a49d5cc6d8b00b04
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 //===----------------------------------------------------------------------===//
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;
19 namespace llvm {
21 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
22 OS << S.str();
23 return OS;
26 std::ostream &operator<<(std::ostream &OS,
27 const std::pair<StringRef, StringRef> &P) {
28 OS << "(" << P.first << ", " << P.second << ")";
29 return OS;
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(!std::is_assignable_v<StringRef &, std::string>,
38 "Assigning from prvalue std::string");
39 static_assert(!std::is_assignable_v<StringRef &, std::string &&>,
40 "Assigning from xvalue std::string");
41 static_assert(std::is_assignable_v<StringRef &, std::string &>,
42 "Assigning from lvalue std::string");
43 static_assert(std::is_assignable_v<StringRef &, const char *>,
44 "Assigning from prvalue C string");
45 static_assert(std::is_assignable_v<StringRef &, const char *&&>,
46 "Assigning from xvalue C string");
47 static_assert(std::is_assignable_v<StringRef &, const char *&>,
48 "Assigning from lvalue C string");
50 namespace {
51 TEST(StringRefTest, Construction) {
52 EXPECT_EQ("", StringRef());
53 EXPECT_EQ("hello", StringRef("hello"));
54 EXPECT_EQ("hello", StringRef("hello world", 5));
55 EXPECT_EQ("hello", StringRef(std::string("hello")));
56 EXPECT_EQ("hello", StringRef(std::string_view("hello")));
59 TEST(StringRefTest, Conversion) {
60 EXPECT_EQ("hello", std::string(StringRef("hello")));
61 EXPECT_EQ("hello", std::string_view(StringRef("hello")));
64 TEST(StringRefTest, EmptyInitializerList) {
65 StringRef S = {};
66 EXPECT_TRUE(S.empty());
68 S = {};
69 EXPECT_TRUE(S.empty());
72 TEST(StringRefTest, Iteration) {
73 StringRef S("hello");
74 const char *p = "hello";
75 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
76 EXPECT_EQ(*it, *p);
79 TEST(StringRefTest, StringOps) {
80 const char *p = "hello";
81 EXPECT_EQ(p, StringRef(p, 0).data());
82 EXPECT_TRUE(StringRef().empty());
83 EXPECT_EQ((size_t) 5, StringRef("hello").size());
84 EXPECT_GT( 0, StringRef("aab").compare("aad"));
85 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
86 EXPECT_LT( 0, StringRef("aab").compare("aaa"));
87 EXPECT_GT( 0, StringRef("aab").compare("aabb"));
88 EXPECT_LT( 0, StringRef("aab").compare("aa"));
89 EXPECT_LT( 0, StringRef("\xFF").compare("\1"));
91 EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("aAd"));
92 EXPECT_EQ( 0, StringRef("AaB").compare_insensitive("aab"));
93 EXPECT_EQ( 1, StringRef("AaB").compare_insensitive("AAA"));
94 EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("aaBb"));
95 EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("bb"));
96 EXPECT_EQ( 1, StringRef("aaBb").compare_insensitive("AaB"));
97 EXPECT_EQ( 1, StringRef("bb").compare_insensitive("AaB"));
98 EXPECT_EQ( 1, StringRef("AaB").compare_insensitive("aA"));
99 EXPECT_EQ( 1, StringRef("\xFF").compare_insensitive("\1"));
101 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
102 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
103 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
104 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
105 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
106 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
107 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
108 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
109 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
110 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
111 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
112 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
113 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
114 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
115 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
116 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
117 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
120 TEST(StringRefTest, Operators) {
121 EXPECT_EQ("", StringRef());
122 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
123 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
124 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
125 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
126 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
127 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
128 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
129 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
130 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
131 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
132 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
133 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
134 EXPECT_EQ('a', StringRef("aab")[1]);
137 TEST(StringRefTest, Substr) {
138 StringRef Str("hello");
139 EXPECT_EQ("lo", Str.substr(3));
140 EXPECT_EQ("", Str.substr(100));
141 EXPECT_EQ("hello", Str.substr(0, 100));
142 EXPECT_EQ("o", Str.substr(4, 10));
145 TEST(StringRefTest, Slice) {
146 StringRef Str("hello");
147 EXPECT_EQ("l", Str.slice(2, 3));
148 EXPECT_EQ("ell", Str.slice(1, 4));
149 EXPECT_EQ("llo", Str.slice(2, 100));
150 EXPECT_EQ("", Str.slice(2, 1));
151 EXPECT_EQ("", Str.slice(10, 20));
154 TEST(StringRefTest, Split) {
155 StringRef Str("hello");
156 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
157 Str.split('X'));
158 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
159 Str.split('e'));
160 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
161 Str.split('h'));
162 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
163 Str.split('l'));
164 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
165 Str.split('o'));
167 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
168 Str.rsplit('X'));
169 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
170 Str.rsplit('e'));
171 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
172 Str.rsplit('h'));
173 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
174 Str.rsplit('l'));
175 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
176 Str.rsplit('o'));
178 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("o")),
179 Str.rsplit("ll"));
180 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
181 Str.rsplit("h"));
182 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
183 Str.rsplit("o"));
184 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
185 Str.rsplit("::"));
186 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
187 Str.rsplit("l"));
190 TEST(StringRefTest, Split2) {
191 SmallVector<StringRef, 5> parts;
192 SmallVector<StringRef, 5> expected;
194 expected.push_back("ab"); expected.push_back("c");
195 StringRef(",ab,,c,").split(parts, ",", -1, false);
196 EXPECT_TRUE(parts == expected);
198 expected.clear(); parts.clear();
199 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
200 expected.push_back("c"); expected.push_back("");
201 StringRef(",ab,,c,").split(parts, ",", -1, true);
202 EXPECT_TRUE(parts == expected);
204 expected.clear(); parts.clear();
205 expected.push_back("");
206 StringRef("").split(parts, ",", -1, true);
207 EXPECT_TRUE(parts == expected);
209 expected.clear(); parts.clear();
210 StringRef("").split(parts, ",", -1, false);
211 EXPECT_TRUE(parts == expected);
213 expected.clear(); parts.clear();
214 StringRef(",").split(parts, ",", -1, false);
215 EXPECT_TRUE(parts == expected);
217 expected.clear(); parts.clear();
218 expected.push_back(""); expected.push_back("");
219 StringRef(",").split(parts, ",", -1, true);
220 EXPECT_TRUE(parts == expected);
222 expected.clear(); parts.clear();
223 expected.push_back("a"); expected.push_back("b");
224 StringRef("a,b").split(parts, ",", -1, true);
225 EXPECT_TRUE(parts == expected);
227 // Test MaxSplit
228 expected.clear(); parts.clear();
229 expected.push_back("a,,b,c");
230 StringRef("a,,b,c").split(parts, ",", 0, true);
231 EXPECT_TRUE(parts == expected);
233 expected.clear(); parts.clear();
234 expected.push_back("a,,b,c");
235 StringRef("a,,b,c").split(parts, ",", 0, false);
236 EXPECT_TRUE(parts == expected);
238 expected.clear(); parts.clear();
239 expected.push_back("a"); expected.push_back(",b,c");
240 StringRef("a,,b,c").split(parts, ",", 1, true);
241 EXPECT_TRUE(parts == expected);
243 expected.clear(); parts.clear();
244 expected.push_back("a"); expected.push_back(",b,c");
245 StringRef("a,,b,c").split(parts, ",", 1, false);
246 EXPECT_TRUE(parts == expected);
248 expected.clear(); parts.clear();
249 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
250 StringRef("a,,b,c").split(parts, ",", 2, true);
251 EXPECT_TRUE(parts == expected);
253 expected.clear(); parts.clear();
254 expected.push_back("a"); expected.push_back("b,c");
255 StringRef("a,,b,c").split(parts, ",", 2, false);
256 EXPECT_TRUE(parts == expected);
258 expected.clear(); parts.clear();
259 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
260 expected.push_back("c");
261 StringRef("a,,b,c").split(parts, ",", 3, true);
262 EXPECT_TRUE(parts == expected);
264 expected.clear(); parts.clear();
265 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
266 StringRef("a,,b,c").split(parts, ",", 3, false);
267 EXPECT_TRUE(parts == expected);
269 expected.clear(); parts.clear();
270 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
271 StringRef("a,,b,c").split(parts, ',', 3, false);
272 EXPECT_TRUE(parts == expected);
274 expected.clear(); parts.clear();
275 expected.push_back("");
276 StringRef().split(parts, ",", 0, true);
277 EXPECT_TRUE(parts == expected);
279 expected.clear(); parts.clear();
280 expected.push_back(StringRef());
281 StringRef("").split(parts, ",", 0, true);
282 EXPECT_TRUE(parts == expected);
284 expected.clear(); parts.clear();
285 StringRef("").split(parts, ",", 0, false);
286 EXPECT_TRUE(parts == expected);
287 StringRef().split(parts, ",", 0, false);
288 EXPECT_TRUE(parts == expected);
290 expected.clear(); parts.clear();
291 expected.push_back("a");
292 expected.push_back("");
293 expected.push_back("b");
294 expected.push_back("c,d");
295 StringRef("a,,b,c,d").split(parts, ",", 3, true);
296 EXPECT_TRUE(parts == expected);
298 expected.clear(); parts.clear();
299 expected.push_back("");
300 StringRef().split(parts, ',', 0, true);
301 EXPECT_TRUE(parts == expected);
303 expected.clear(); parts.clear();
304 expected.push_back(StringRef());
305 StringRef("").split(parts, ',', 0, true);
306 EXPECT_TRUE(parts == expected);
308 expected.clear(); parts.clear();
309 StringRef("").split(parts, ',', 0, false);
310 EXPECT_TRUE(parts == expected);
311 StringRef().split(parts, ',', 0, false);
312 EXPECT_TRUE(parts == expected);
314 expected.clear(); parts.clear();
315 expected.push_back("a");
316 expected.push_back("");
317 expected.push_back("b");
318 expected.push_back("c,d");
319 StringRef("a,,b,c,d").split(parts, ',', 3, true);
320 EXPECT_TRUE(parts == expected);
323 TEST(StringRefTest, Trim) {
324 StringRef Str0("hello");
325 StringRef Str1(" hello ");
326 StringRef Str2(" hello ");
327 StringRef Str3("\t\n\v\f\r hello \t\n\v\f\r");
329 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
330 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
331 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
332 EXPECT_EQ(StringRef("\t\n\v\f\r hello"), Str3.rtrim());
333 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
334 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
335 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
336 EXPECT_EQ(StringRef("hello \t\n\v\f\r"), Str3.ltrim());
337 EXPECT_EQ(StringRef("hello"), Str0.trim());
338 EXPECT_EQ(StringRef("hello"), Str1.trim());
339 EXPECT_EQ(StringRef("hello"), Str2.trim());
340 EXPECT_EQ(StringRef("hello"), Str3.trim());
342 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
344 EXPECT_EQ(StringRef(""), StringRef("").trim());
345 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
346 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
347 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
348 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
351 TEST(StringRefTest, StartsWith) {
352 StringRef Str("hello");
353 EXPECT_TRUE(Str.starts_with(""));
354 EXPECT_TRUE(Str.starts_with("he"));
355 EXPECT_FALSE(Str.starts_with("helloworld"));
356 EXPECT_FALSE(Str.starts_with("hi"));
359 TEST(StringRefTest, StartsWithInsensitive) {
360 StringRef Str("heLLo");
361 EXPECT_TRUE(Str.starts_with_insensitive(""));
362 EXPECT_TRUE(Str.starts_with_insensitive("he"));
363 EXPECT_TRUE(Str.starts_with_insensitive("hell"));
364 EXPECT_TRUE(Str.starts_with_insensitive("HELlo"));
365 EXPECT_FALSE(Str.starts_with_insensitive("helloworld"));
366 EXPECT_FALSE(Str.starts_with_insensitive("hi"));
369 TEST(StringRefTest, ConsumeFront) {
370 StringRef Str("hello");
371 EXPECT_TRUE(Str.consume_front(""));
372 EXPECT_EQ("hello", Str);
373 EXPECT_TRUE(Str.consume_front("he"));
374 EXPECT_EQ("llo", Str);
375 EXPECT_FALSE(Str.consume_front("lloworld"));
376 EXPECT_EQ("llo", Str);
377 EXPECT_FALSE(Str.consume_front("lol"));
378 EXPECT_EQ("llo", Str);
379 EXPECT_TRUE(Str.consume_front("llo"));
380 EXPECT_EQ("", Str);
381 EXPECT_FALSE(Str.consume_front("o"));
382 EXPECT_TRUE(Str.consume_front(""));
385 TEST(StringRefTest, ConsumeFrontInsensitive) {
386 StringRef Str("heLLo");
387 EXPECT_TRUE(Str.consume_front_insensitive(""));
388 EXPECT_EQ("heLLo", Str);
389 EXPECT_FALSE(Str.consume_front("HEl"));
390 EXPECT_EQ("heLLo", Str);
391 EXPECT_TRUE(Str.consume_front_insensitive("HEl"));
392 EXPECT_EQ("Lo", Str);
393 EXPECT_FALSE(Str.consume_front_insensitive("loworld"));
394 EXPECT_EQ("Lo", Str);
395 EXPECT_FALSE(Str.consume_front_insensitive("ol"));
396 EXPECT_EQ("Lo", Str);
397 EXPECT_TRUE(Str.consume_front_insensitive("lo"));
398 EXPECT_EQ("", Str);
399 EXPECT_FALSE(Str.consume_front_insensitive("o"));
400 EXPECT_TRUE(Str.consume_front_insensitive(""));
403 TEST(StringRefTest, EndsWith) {
404 StringRef Str("hello");
405 EXPECT_TRUE(Str.ends_with(""));
406 EXPECT_TRUE(Str.ends_with("lo"));
407 EXPECT_FALSE(Str.ends_with("helloworld"));
408 EXPECT_FALSE(Str.ends_with("worldhello"));
409 EXPECT_FALSE(Str.ends_with("so"));
412 TEST(StringRefTest, EndsWithInsensitive) {
413 StringRef Str("heLLo");
414 EXPECT_TRUE(Str.ends_with_insensitive(""));
415 EXPECT_TRUE(Str.ends_with_insensitive("lo"));
416 EXPECT_TRUE(Str.ends_with_insensitive("LO"));
417 EXPECT_TRUE(Str.ends_with_insensitive("ELlo"));
418 EXPECT_FALSE(Str.ends_with_insensitive("helloworld"));
419 EXPECT_FALSE(Str.ends_with_insensitive("hi"));
422 TEST(StringRefTest, ConsumeBack) {
423 StringRef Str("hello");
424 EXPECT_TRUE(Str.consume_back(""));
425 EXPECT_EQ("hello", Str);
426 EXPECT_TRUE(Str.consume_back("lo"));
427 EXPECT_EQ("hel", Str);
428 EXPECT_FALSE(Str.consume_back("helhel"));
429 EXPECT_EQ("hel", Str);
430 EXPECT_FALSE(Str.consume_back("hle"));
431 EXPECT_EQ("hel", Str);
432 EXPECT_TRUE(Str.consume_back("hel"));
433 EXPECT_EQ("", Str);
434 EXPECT_FALSE(Str.consume_back("h"));
435 EXPECT_TRUE(Str.consume_back(""));
438 TEST(StringRefTest, ConsumeBackInsensitive) {
439 StringRef Str("heLLo");
440 EXPECT_TRUE(Str.consume_back_insensitive(""));
441 EXPECT_EQ("heLLo", Str);
442 EXPECT_FALSE(Str.consume_back("lO"));
443 EXPECT_EQ("heLLo", Str);
444 EXPECT_TRUE(Str.consume_back_insensitive("lO"));
445 EXPECT_EQ("heL", Str);
446 EXPECT_FALSE(Str.consume_back_insensitive("helhel"));
447 EXPECT_EQ("heL", Str);
448 EXPECT_FALSE(Str.consume_back_insensitive("hle"));
449 EXPECT_EQ("heL", Str);
450 EXPECT_TRUE(Str.consume_back_insensitive("hEl"));
451 EXPECT_EQ("", Str);
452 EXPECT_FALSE(Str.consume_back_insensitive("h"));
453 EXPECT_TRUE(Str.consume_back_insensitive(""));
456 TEST(StringRefTest, Find) {
457 StringRef Str("helloHELLO");
458 StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
460 struct {
461 StringRef Str;
462 char C;
463 std::size_t From;
464 std::size_t Pos;
465 std::size_t InsensitivePos;
466 } CharExpectations[] = {
467 {Str, 'h', 0U, 0U, 0U},
468 {Str, 'e', 0U, 1U, 1U},
469 {Str, 'l', 0U, 2U, 2U},
470 {Str, 'l', 3U, 3U, 3U},
471 {Str, 'o', 0U, 4U, 4U},
472 {Str, 'L', 0U, 7U, 2U},
473 {Str, 'z', 0U, StringRef::npos, StringRef::npos},
476 struct {
477 StringRef Str;
478 llvm::StringRef S;
479 std::size_t From;
480 std::size_t Pos;
481 std::size_t InsensitivePos;
482 } StrExpectations[] = {
483 {Str, "helloword", 0, StringRef::npos, StringRef::npos},
484 {Str, "hello", 0, 0U, 0U},
485 {Str, "ello", 0, 1U, 1U},
486 {Str, "zz", 0, StringRef::npos, StringRef::npos},
487 {Str, "ll", 2U, 2U, 2U},
488 {Str, "ll", 3U, StringRef::npos, 7U},
489 {Str, "LL", 2U, 7U, 2U},
490 {Str, "LL", 3U, 7U, 7U},
491 {Str, "", 0U, 0U, 0U},
492 {LongStr, "hello", 0U, 36U, 36U},
493 {LongStr, "foo", 0U, 28U, 28U},
494 {LongStr, "hell", 2U, 12U, 12U},
495 {LongStr, "HELL", 2U, 42U, 12U},
496 {LongStr, "", 0U, 0U, 0U}};
498 for (auto &E : CharExpectations) {
499 EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From));
500 EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.C, E.From));
501 EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(toupper(E.C), E.From));
504 for (auto &E : StrExpectations) {
505 EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From));
506 EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.S, E.From));
507 EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.S.upper(), E.From));
510 EXPECT_EQ(3U, Str.rfind('l'));
511 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
512 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
513 EXPECT_EQ(0U, Str.rfind("hello"));
514 EXPECT_EQ(1U, Str.rfind("ello"));
515 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
517 EXPECT_EQ(8U, Str.rfind_insensitive('l'));
518 EXPECT_EQ(8U, Str.rfind_insensitive('L'));
519 EXPECT_EQ(StringRef::npos, Str.rfind_insensitive('z'));
520 EXPECT_EQ(StringRef::npos, Str.rfind_insensitive("HELLOWORLD"));
521 EXPECT_EQ(5U, Str.rfind("HELLO"));
522 EXPECT_EQ(6U, Str.rfind("ELLO"));
523 EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));
525 EXPECT_EQ(2U, Str.find_first_of('l'));
526 EXPECT_EQ(1U, Str.find_first_of("el"));
527 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
529 Str = "hello";
530 EXPECT_EQ(1U, Str.find_first_not_of('h'));
531 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
532 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
534 EXPECT_EQ(3U, Str.find_last_not_of('o'));
535 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
536 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
539 TEST(StringRefTest, Count) {
540 StringRef Str("hello");
541 EXPECT_EQ(2U, Str.count('l'));
542 EXPECT_EQ(1U, Str.count('o'));
543 EXPECT_EQ(0U, Str.count('z'));
544 EXPECT_EQ(0U, Str.count("helloworld"));
545 EXPECT_EQ(1U, Str.count("hello"));
546 EXPECT_EQ(1U, Str.count("ello"));
547 EXPECT_EQ(0U, Str.count("zz"));
548 EXPECT_EQ(0U, Str.count(""));
550 StringRef OverlappingAbba("abbabba");
551 EXPECT_EQ(1U, OverlappingAbba.count("abba"));
552 StringRef NonOverlappingAbba("abbaabba");
553 EXPECT_EQ(2U, NonOverlappingAbba.count("abba"));
554 StringRef ComplexAbba("abbabbaxyzabbaxyz");
555 EXPECT_EQ(2U, ComplexAbba.count("abba"));
558 TEST(StringRefTest, EditDistance) {
559 StringRef Hello("hello");
560 EXPECT_EQ(2U, Hello.edit_distance("hill"));
562 StringRef Industry("industry");
563 EXPECT_EQ(6U, Industry.edit_distance("interest"));
565 StringRef Soylent("soylent green is people");
566 EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green"));
567 EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green",
568 /* allow replacements = */ false));
569 EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green",
570 /* allow replacements = */ true,
571 /* max edit distance = */ 8));
572 EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green "
573 "people soiled our green "
574 "people soiled our green "));
577 TEST(StringRefTest, EditDistanceInsensitive) {
578 StringRef Hello("HELLO");
579 EXPECT_EQ(2U, Hello.edit_distance_insensitive("hill"));
580 EXPECT_EQ(0U, Hello.edit_distance_insensitive("hello"));
582 StringRef Industry("InDuStRy");
583 EXPECT_EQ(6U, Industry.edit_distance_insensitive("iNtErEsT"));
586 TEST(StringRefTest, Misc) {
587 std::string Storage;
588 raw_string_ostream OS(Storage);
589 OS << StringRef("hello");
590 EXPECT_EQ("hello", OS.str());
593 TEST(StringRefTest, Hashing) {
594 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
595 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
596 std::string S = "hello world";
597 hash_code H = hash_value(S);
598 EXPECT_EQ(H, hash_value(StringRef("hello world")));
599 EXPECT_EQ(H, hash_value(StringRef(S)));
600 EXPECT_NE(H, hash_value(StringRef("hello worl")));
601 EXPECT_EQ(hash_value(std::string("hello worl")),
602 hash_value(StringRef("hello worl")));
603 EXPECT_NE(H, hash_value(StringRef("hello world ")));
604 EXPECT_EQ(hash_value(std::string("hello world ")),
605 hash_value(StringRef("hello world ")));
606 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
607 EXPECT_NE(hash_value(std::string("ello worl")),
608 hash_value(StringRef("hello world").slice(1, -1)));
611 struct UnsignedPair {
612 const char *Str;
613 uint64_t Expected;
614 } Unsigned[] =
615 { {"0", 0}
616 , {"255", 255}
617 , {"256", 256}
618 , {"65535", 65535}
619 , {"65536", 65536}
620 , {"4294967295", 4294967295ULL}
621 , {"4294967296", 4294967296ULL}
622 , {"18446744073709551615", 18446744073709551615ULL}
623 , {"042", 34}
624 , {"0x42", 66}
625 , {"0b101010", 42}
628 struct SignedPair {
629 const char *Str;
630 int64_t Expected;
631 } Signed[] =
632 { {"0", 0}
633 , {"-0", 0}
634 , {"127", 127}
635 , {"128", 128}
636 , {"-128", -128}
637 , {"-129", -129}
638 , {"32767", 32767}
639 , {"32768", 32768}
640 , {"-32768", -32768}
641 , {"-32769", -32769}
642 , {"2147483647", 2147483647LL}
643 , {"2147483648", 2147483648LL}
644 , {"-2147483648", -2147483648LL}
645 , {"-2147483649", -2147483649LL}
646 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
647 , {"042", 34}
648 , {"0x42", 66}
649 , {"0b101010", 42}
650 , {"-042", -34}
651 , {"-0x42", -66}
652 , {"-0b101010", -42}
655 TEST(StringRefTest, getAsInteger) {
656 uint8_t U8;
657 uint16_t U16;
658 uint32_t U32;
659 uint64_t U64;
661 for (size_t i = 0; i < std::size(Unsigned); ++i) {
662 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
663 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
664 ASSERT_FALSE(U8Success);
665 EXPECT_EQ(U8, Unsigned[i].Expected);
666 } else {
667 ASSERT_TRUE(U8Success);
669 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
670 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
671 ASSERT_FALSE(U16Success);
672 EXPECT_EQ(U16, Unsigned[i].Expected);
673 } else {
674 ASSERT_TRUE(U16Success);
676 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
677 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
678 ASSERT_FALSE(U32Success);
679 EXPECT_EQ(U32, Unsigned[i].Expected);
680 } else {
681 ASSERT_TRUE(U32Success);
683 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
684 ASSERT_FALSE(U64Success);
685 EXPECT_EQ(U64, Unsigned[i].Expected);
688 int8_t S8;
689 int16_t S16;
690 int32_t S32;
691 int64_t S64;
693 for (size_t i = 0; i < std::size(Signed); ++i) {
694 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
695 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
696 ASSERT_FALSE(S8Success);
697 EXPECT_EQ(S8, Signed[i].Expected);
698 } else {
699 ASSERT_TRUE(S8Success);
701 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
702 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
703 ASSERT_FALSE(S16Success);
704 EXPECT_EQ(S16, Signed[i].Expected);
705 } else {
706 ASSERT_TRUE(S16Success);
708 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
709 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
710 ASSERT_FALSE(S32Success);
711 EXPECT_EQ(S32, Signed[i].Expected);
712 } else {
713 ASSERT_TRUE(S32Success);
715 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
716 ASSERT_FALSE(S64Success);
717 EXPECT_EQ(S64, Signed[i].Expected);
722 static const char* BadStrings[] = {
723 "" // empty string
724 , "18446744073709551617" // value just over max
725 , "123456789012345678901" // value way too large
726 , "4t23v" // illegal decimal characters
727 , "0x123W56" // illegal hex characters
728 , "0b2" // illegal bin characters
729 , "08" // illegal oct characters
730 , "0o8" // illegal oct characters
731 , "-123" // negative unsigned value
732 , "0x"
733 , "0b"
737 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
738 unsigned long long U64;
739 for (size_t i = 0; i < std::size(BadStrings); ++i) {
740 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
741 ASSERT_TRUE(IsBadNumber);
745 struct ConsumeUnsignedPair {
746 const char *Str;
747 uint64_t Expected;
748 const char *Leftover;
749 } ConsumeUnsigned[] = {
750 {"0", 0, ""},
751 {"255", 255, ""},
752 {"256", 256, ""},
753 {"65535", 65535, ""},
754 {"65536", 65536, ""},
755 {"4294967295", 4294967295ULL, ""},
756 {"4294967296", 4294967296ULL, ""},
757 {"255A376", 255, "A376"},
758 {"18446744073709551615", 18446744073709551615ULL, ""},
759 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
760 {"042", 34, ""},
761 {"0x42", 66, ""},
762 {"0x42-0x34", 66, "-0x34"},
763 {"0b101010", 42, ""},
764 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit
765 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit
766 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
768 struct ConsumeSignedPair {
769 const char *Str;
770 int64_t Expected;
771 const char *Leftover;
772 } ConsumeSigned[] = {
773 {"0", 0, ""},
774 {"-0", 0, ""},
775 {"0-1", 0, "-1"},
776 {"-0-1", 0, "-1"},
777 {"127", 127, ""},
778 {"128", 128, ""},
779 {"127-1", 127, "-1"},
780 {"128-1", 128, "-1"},
781 {"-128", -128, ""},
782 {"-129", -129, ""},
783 {"-128-1", -128, "-1"},
784 {"-129-1", -129, "-1"},
785 {"32767", 32767, ""},
786 {"32768", 32768, ""},
787 {"32767-1", 32767, "-1"},
788 {"32768-1", 32768, "-1"},
789 {"-32768", -32768, ""},
790 {"-32769", -32769, ""},
791 {"-32768-1", -32768, "-1"},
792 {"-32769-1", -32769, "-1"},
793 {"2147483647", 2147483647LL, ""},
794 {"2147483648", 2147483648LL, ""},
795 {"2147483647-1", 2147483647LL, "-1"},
796 {"2147483648-1", 2147483648LL, "-1"},
797 {"-2147483648", -2147483648LL, ""},
798 {"-2147483649", -2147483649LL, ""},
799 {"-2147483648-1", -2147483648LL, "-1"},
800 {"-2147483649-1", -2147483649LL, "-1"},
801 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
802 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
803 {"042", 34, ""},
804 {"042-1", 34, "-1"},
805 {"0x42", 66, ""},
806 {"0x42-1", 66, "-1"},
807 {"0b101010", 42, ""},
808 {"0b101010-1", 42, "-1"},
809 {"-042", -34, ""},
810 {"-042-1", -34, "-1"},
811 {"-0x42", -66, ""},
812 {"-0x42-1", -66, "-1"},
813 {"-0b101010", -42, ""},
814 {"-0b101010-1", -42, "-1"}};
816 TEST(StringRefTest, consumeIntegerUnsigned) {
817 uint8_t U8;
818 uint16_t U16;
819 uint32_t U32;
820 uint64_t U64;
821 APInt U;
823 for (size_t i = 0; i < std::size(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);
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);
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);
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);
863 Str = ConsumeUnsigned[i].Str;
864 U64Success = Str.consumeInteger(0, U);
865 ASSERT_FALSE(U64Success);
866 EXPECT_EQ(U.getZExtValue(), ConsumeUnsigned[i].Expected);
867 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
871 TEST(StringRefTest, consumeIntegerSigned) {
872 int8_t S8;
873 int16_t S16;
874 int32_t S32;
875 int64_t S64;
877 for (size_t i = 0; i < std::size(ConsumeSigned); ++i) {
878 StringRef Str = ConsumeSigned[i].Str;
879 bool S8Success = Str.consumeInteger(0, S8);
880 if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
881 ConsumeSigned[i].Expected) {
882 ASSERT_FALSE(S8Success);
883 EXPECT_EQ(S8, ConsumeSigned[i].Expected);
884 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
885 } else {
886 ASSERT_TRUE(S8Success);
889 Str = ConsumeSigned[i].Str;
890 bool S16Success = Str.consumeInteger(0, S16);
891 if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
892 ConsumeSigned[i].Expected) {
893 ASSERT_FALSE(S16Success);
894 EXPECT_EQ(S16, ConsumeSigned[i].Expected);
895 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
896 } else {
897 ASSERT_TRUE(S16Success);
900 Str = ConsumeSigned[i].Str;
901 bool S32Success = Str.consumeInteger(0, S32);
902 if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
903 ConsumeSigned[i].Expected) {
904 ASSERT_FALSE(S32Success);
905 EXPECT_EQ(S32, ConsumeSigned[i].Expected);
906 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
907 } else {
908 ASSERT_TRUE(S32Success);
911 Str = ConsumeSigned[i].Str;
912 bool S64Success = Str.consumeInteger(0, S64);
913 ASSERT_FALSE(S64Success);
914 EXPECT_EQ(S64, ConsumeSigned[i].Expected);
915 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
919 struct GetDoubleStrings {
920 const char *Str;
921 bool AllowInexact;
922 bool ShouldFail;
923 double D;
924 } DoubleStrings[] = {{"0", false, false, 0.0},
925 {"0.0", false, false, 0.0},
926 {"-0.0", false, false, -0.0},
927 {"123.45", false, true, 123.45},
928 {"123.45", true, false, 123.45},
929 {"1.8e308", true, false, std::numeric_limits<double>::infinity()},
930 {"1.8e308", false, true, std::numeric_limits<double>::infinity()},
931 {"0x0.0000000000001P-1023", false, true, 0.0},
932 {"0x0.0000000000001P-1023", true, false, 0.0},
935 TEST(StringRefTest, getAsDouble) {
936 for (const auto &Entry : DoubleStrings) {
937 double Result;
938 StringRef S(Entry.Str);
939 EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
940 if (!Entry.ShouldFail) {
941 EXPECT_EQ(Result, Entry.D);
946 static const char *join_input[] = { "a", "b", "c" };
947 static const char join_result1[] = "a";
948 static const char join_result2[] = "a:b:c";
949 static const char join_result3[] = "a::b::c";
951 TEST(StringRefTest, joinStrings) {
952 std::vector<StringRef> v1;
953 std::vector<std::string> v2;
954 for (size_t i = 0; i < std::size(join_input); ++i) {
955 v1.push_back(join_input[i]);
956 v2.push_back(join_input[i]);
959 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
960 EXPECT_TRUE(v1_join1);
961 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
962 EXPECT_TRUE(v1_join2);
963 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
964 EXPECT_TRUE(v1_join3);
966 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
967 EXPECT_TRUE(v2_join1);
968 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
969 EXPECT_TRUE(v2_join2);
970 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
971 EXPECT_TRUE(v2_join3);
972 v2_join3 = join(v2, "::") == join_result3;
973 EXPECT_TRUE(v2_join3);
977 TEST(StringRefTest, AllocatorCopy) {
978 BumpPtrAllocator Alloc;
979 // First test empty strings. We don't want these to allocate anything on the
980 // allocator.
981 StringRef StrEmpty = "";
982 StringRef StrEmptyc = StrEmpty.copy(Alloc);
983 EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
984 EXPECT_EQ(StrEmptyc.data(), nullptr);
985 EXPECT_EQ(StrEmptyc.size(), 0u);
986 EXPECT_EQ(Alloc.getTotalMemory(), 0u);
988 StringRef Str1 = "hello";
989 StringRef Str2 = "bye";
990 StringRef Str1c = Str1.copy(Alloc);
991 StringRef Str2c = Str2.copy(Alloc);
992 EXPECT_TRUE(Str1.equals(Str1c));
993 EXPECT_NE(Str1.data(), Str1c.data());
994 EXPECT_TRUE(Str2.equals(Str2c));
995 EXPECT_NE(Str2.data(), Str2c.data());
998 TEST(StringRefTest, Drop) {
999 StringRef Test("StringRefTest::Drop");
1001 StringRef Dropped = Test.drop_front(5);
1002 EXPECT_EQ(Dropped, "gRefTest::Drop");
1004 Dropped = Test.drop_back(5);
1005 EXPECT_EQ(Dropped, "StringRefTest:");
1007 Dropped = Test.drop_front(0);
1008 EXPECT_EQ(Dropped, Test);
1010 Dropped = Test.drop_back(0);
1011 EXPECT_EQ(Dropped, Test);
1013 Dropped = Test.drop_front(Test.size());
1014 EXPECT_TRUE(Dropped.empty());
1016 Dropped = Test.drop_back(Test.size());
1017 EXPECT_TRUE(Dropped.empty());
1020 TEST(StringRefTest, Take) {
1021 StringRef Test("StringRefTest::Take");
1023 StringRef Taken = Test.take_front(5);
1024 EXPECT_EQ(Taken, "Strin");
1026 Taken = Test.take_back(5);
1027 EXPECT_EQ(Taken, ":Take");
1029 Taken = Test.take_front(Test.size());
1030 EXPECT_EQ(Taken, Test);
1032 Taken = Test.take_back(Test.size());
1033 EXPECT_EQ(Taken, Test);
1035 Taken = Test.take_front(0);
1036 EXPECT_TRUE(Taken.empty());
1038 Taken = Test.take_back(0);
1039 EXPECT_TRUE(Taken.empty());
1042 TEST(StringRefTest, FindIf) {
1043 StringRef Punct("Test.String");
1044 StringRef NoPunct("ABCDEFG");
1045 StringRef Empty;
1047 auto IsPunct = [](char c) { return ::ispunct(c); };
1048 auto IsAlpha = [](char c) { return ::isalpha(c); };
1049 EXPECT_EQ(4U, Punct.find_if(IsPunct));
1050 EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
1051 EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
1053 EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
1054 EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
1055 EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
1058 TEST(StringRefTest, TakeWhileUntil) {
1059 StringRef Test("String With 1 Number");
1061 StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
1062 EXPECT_EQ("", Taken);
1064 Taken = Test.take_until([](char c) { return ::isdigit(c); });
1065 EXPECT_EQ("String With ", Taken);
1067 Taken = Test.take_while([](char c) { return true; });
1068 EXPECT_EQ(Test, Taken);
1070 Taken = Test.take_until([](char c) { return true; });
1071 EXPECT_EQ("", Taken);
1073 Test = "";
1074 Taken = Test.take_while([](char c) { return true; });
1075 EXPECT_EQ("", Taken);
1078 TEST(StringRefTest, DropWhileUntil) {
1079 StringRef Test("String With 1 Number");
1081 StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
1082 EXPECT_EQ(Test, Taken);
1084 Taken = Test.drop_until([](char c) { return ::isdigit(c); });
1085 EXPECT_EQ("1 Number", Taken);
1087 Taken = Test.drop_while([](char c) { return true; });
1088 EXPECT_EQ("", Taken);
1090 Taken = Test.drop_until([](char c) { return true; });
1091 EXPECT_EQ(Test, Taken);
1093 StringRef EmptyString = "";
1094 Taken = EmptyString.drop_while([](char c) { return true; });
1095 EXPECT_EQ("", Taken);
1098 TEST(StringRefTest, StringLiteral) {
1099 constexpr StringRef StringRefs[] = {"Foo", "Bar"};
1100 EXPECT_EQ(StringRef("Foo"), StringRefs[0]);
1101 EXPECT_EQ(3u, (std::integral_constant<size_t, StringRefs[0].size()>::value));
1102 EXPECT_EQ(false, (std::integral_constant<bool, StringRefs[0].empty()>::value));
1103 EXPECT_EQ(StringRef("Bar"), StringRefs[1]);
1105 constexpr StringLiteral Strings[] = {"Foo", "Bar"};
1106 EXPECT_EQ(StringRef("Foo"), Strings[0]);
1107 EXPECT_EQ(3u, (std::integral_constant<size_t, Strings[0].size()>::value));
1108 EXPECT_EQ(false, (std::integral_constant<bool, Strings[0].empty()>::value));
1109 EXPECT_EQ(StringRef("Bar"), Strings[1]);
1112 // Check gtest prints StringRef as a string instead of a container of chars.
1113 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h
1114 TEST(StringRefTest, GTestPrinter) {
1115 EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo")));
1118 TEST(StringRefTest, LFLineEnding) {
1119 constexpr StringRef Cases[] = {"\nDoggo\nPupper", "Floofer\n", "Woofer"};
1120 EXPECT_EQ(StringRef("\n"), Cases[0].detectEOL());
1121 EXPECT_EQ(StringRef("\n"), Cases[1].detectEOL());
1122 EXPECT_EQ(StringRef("\n"), Cases[2].detectEOL());
1125 TEST(StringRefTest, CRLineEnding) {
1126 constexpr StringRef Cases[] = {"\rDoggo\rPupper", "Floofer\r", "Woo\rfer\n"};
1127 EXPECT_EQ(StringRef("\r"), Cases[0].detectEOL());
1128 EXPECT_EQ(StringRef("\r"), Cases[1].detectEOL());
1129 EXPECT_EQ(StringRef("\r"), Cases[2].detectEOL());
1132 TEST(StringRefTest, CRLFLineEnding) {
1133 constexpr StringRef Cases[] = {"\r\nDoggo\r\nPupper", "Floofer\r\n",
1134 "Woofer\r\nSubWoofer\n"};
1135 EXPECT_EQ(StringRef("\r\n"), Cases[0].detectEOL());
1136 EXPECT_EQ(StringRef("\r\n"), Cases[1].detectEOL());
1137 EXPECT_EQ(StringRef("\r\n"), Cases[2].detectEOL());
1140 TEST(StringRefTest, LFCRLineEnding) {
1141 constexpr StringRef Cases[] = {"\n\rDoggo\n\rPupper", "Floofer\n\r",
1142 "Woofer\n\rSubWoofer\n"};
1143 EXPECT_EQ(StringRef("\n\r"), Cases[0].detectEOL());
1144 EXPECT_EQ(StringRef("\n\r"), Cases[1].detectEOL());
1145 EXPECT_EQ(StringRef("\n\r"), Cases[2].detectEOL());
1148 static_assert(std::is_trivially_copyable_v<StringRef>, "trivially copyable");
1150 } // end anonymous namespace