1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
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
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"
21 std::ostream
&operator<<(std::ostream
&OS
, const StringRef
&S
) {
26 std::ostream
&operator<<(std::ostream
&OS
,
27 const std::pair
<StringRef
, StringRef
> &P
) {
28 OS
<< "(" << P
.first
<< ", " << P
.second
<< ")";
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
38 // Disable this check under MSVC; even MSVC 2015 isn't consistent between
39 // std::is_assignable and actually writing such an assignment.
40 #if !defined(_MSC_VER)
42 !std::is_assignable
<StringRef
&, std::string
>::value
,
43 "Assigning from prvalue std::string");
45 !std::is_assignable
<StringRef
&, std::string
&&>::value
,
46 "Assigning from xvalue std::string");
48 std::is_assignable
<StringRef
&, std::string
&>::value
,
49 "Assigning from lvalue std::string");
51 std::is_assignable
<StringRef
&, const char *>::value
,
52 "Assigning from prvalue C string");
54 std::is_assignable
<StringRef
&, const char * &&>::value
,
55 "Assigning from xvalue C string");
57 std::is_assignable
<StringRef
&, const char * &>::value
,
58 "Assigning from lvalue C string");
63 TEST(StringRefTest
, Construction
) {
64 EXPECT_EQ("", StringRef());
65 EXPECT_EQ("hello", StringRef("hello"));
66 EXPECT_EQ("hello", StringRef("hello world", 5));
67 EXPECT_EQ("hello", StringRef(std::string("hello")));
70 TEST(StringRefTest
, EmptyInitializerList
) {
72 EXPECT_TRUE(S
.empty());
75 EXPECT_TRUE(S
.empty());
78 TEST(StringRefTest
, Iteration
) {
80 const char *p
= "hello";
81 for (const char *it
= S
.begin(), *ie
= S
.end(); it
!= ie
; ++it
, ++p
)
85 TEST(StringRefTest
, StringOps
) {
86 const char *p
= "hello";
87 EXPECT_EQ(p
, StringRef(p
, 0).data());
88 EXPECT_TRUE(StringRef().empty());
89 EXPECT_EQ((size_t) 5, StringRef("hello").size());
90 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
91 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
92 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
93 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
94 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
95 EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
97 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
98 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
99 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
100 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
101 EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
102 EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
103 EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
104 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
105 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
107 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
108 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
109 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
110 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
111 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
112 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
113 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
114 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
115 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
116 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
117 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
118 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
119 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
120 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
121 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
122 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
123 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
126 TEST(StringRefTest
, Operators
) {
127 EXPECT_EQ("", StringRef());
128 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
129 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
130 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
131 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
132 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
133 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
134 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
135 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
136 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
137 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
138 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
139 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
140 EXPECT_EQ('a', StringRef("aab")[1]);
143 TEST(StringRefTest
, Substr
) {
144 StringRef
Str("hello");
145 EXPECT_EQ("lo", Str
.substr(3));
146 EXPECT_EQ("", Str
.substr(100));
147 EXPECT_EQ("hello", Str
.substr(0, 100));
148 EXPECT_EQ("o", Str
.substr(4, 10));
151 TEST(StringRefTest
, Slice
) {
152 StringRef
Str("hello");
153 EXPECT_EQ("l", Str
.slice(2, 3));
154 EXPECT_EQ("ell", Str
.slice(1, 4));
155 EXPECT_EQ("llo", Str
.slice(2, 100));
156 EXPECT_EQ("", Str
.slice(2, 1));
157 EXPECT_EQ("", Str
.slice(10, 20));
160 TEST(StringRefTest
, Split
) {
161 StringRef
Str("hello");
162 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
164 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
166 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
168 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
170 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
173 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
175 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
177 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
179 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
181 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
184 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("o")),
186 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
188 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
190 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
192 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
196 TEST(StringRefTest
, Split2
) {
197 SmallVector
<StringRef
, 5> parts
;
198 SmallVector
<StringRef
, 5> expected
;
200 expected
.push_back("ab"); expected
.push_back("c");
201 StringRef(",ab,,c,").split(parts
, ",", -1, false);
202 EXPECT_TRUE(parts
== expected
);
204 expected
.clear(); parts
.clear();
205 expected
.push_back(""); expected
.push_back("ab"); expected
.push_back("");
206 expected
.push_back("c"); expected
.push_back("");
207 StringRef(",ab,,c,").split(parts
, ",", -1, true);
208 EXPECT_TRUE(parts
== expected
);
210 expected
.clear(); parts
.clear();
211 expected
.push_back("");
212 StringRef("").split(parts
, ",", -1, true);
213 EXPECT_TRUE(parts
== expected
);
215 expected
.clear(); parts
.clear();
216 StringRef("").split(parts
, ",", -1, false);
217 EXPECT_TRUE(parts
== expected
);
219 expected
.clear(); parts
.clear();
220 StringRef(",").split(parts
, ",", -1, false);
221 EXPECT_TRUE(parts
== expected
);
223 expected
.clear(); parts
.clear();
224 expected
.push_back(""); expected
.push_back("");
225 StringRef(",").split(parts
, ",", -1, true);
226 EXPECT_TRUE(parts
== expected
);
228 expected
.clear(); parts
.clear();
229 expected
.push_back("a"); expected
.push_back("b");
230 StringRef("a,b").split(parts
, ",", -1, true);
231 EXPECT_TRUE(parts
== expected
);
234 expected
.clear(); parts
.clear();
235 expected
.push_back("a,,b,c");
236 StringRef("a,,b,c").split(parts
, ",", 0, true);
237 EXPECT_TRUE(parts
== expected
);
239 expected
.clear(); parts
.clear();
240 expected
.push_back("a,,b,c");
241 StringRef("a,,b,c").split(parts
, ",", 0, false);
242 EXPECT_TRUE(parts
== expected
);
244 expected
.clear(); parts
.clear();
245 expected
.push_back("a"); expected
.push_back(",b,c");
246 StringRef("a,,b,c").split(parts
, ",", 1, true);
247 EXPECT_TRUE(parts
== expected
);
249 expected
.clear(); parts
.clear();
250 expected
.push_back("a"); expected
.push_back(",b,c");
251 StringRef("a,,b,c").split(parts
, ",", 1, false);
252 EXPECT_TRUE(parts
== expected
);
254 expected
.clear(); parts
.clear();
255 expected
.push_back("a"); expected
.push_back(""); expected
.push_back("b,c");
256 StringRef("a,,b,c").split(parts
, ",", 2, true);
257 EXPECT_TRUE(parts
== expected
);
259 expected
.clear(); parts
.clear();
260 expected
.push_back("a"); expected
.push_back("b,c");
261 StringRef("a,,b,c").split(parts
, ",", 2, false);
262 EXPECT_TRUE(parts
== expected
);
264 expected
.clear(); parts
.clear();
265 expected
.push_back("a"); expected
.push_back(""); expected
.push_back("b");
266 expected
.push_back("c");
267 StringRef("a,,b,c").split(parts
, ",", 3, true);
268 EXPECT_TRUE(parts
== expected
);
270 expected
.clear(); parts
.clear();
271 expected
.push_back("a"); expected
.push_back("b"); expected
.push_back("c");
272 StringRef("a,,b,c").split(parts
, ",", 3, false);
273 EXPECT_TRUE(parts
== expected
);
275 expected
.clear(); parts
.clear();
276 expected
.push_back("a"); expected
.push_back("b"); expected
.push_back("c");
277 StringRef("a,,b,c").split(parts
, ',', 3, false);
278 EXPECT_TRUE(parts
== expected
);
280 expected
.clear(); parts
.clear();
281 expected
.push_back("");
282 StringRef().split(parts
, ",", 0, true);
283 EXPECT_TRUE(parts
== expected
);
285 expected
.clear(); parts
.clear();
286 expected
.push_back(StringRef());
287 StringRef("").split(parts
, ",", 0, true);
288 EXPECT_TRUE(parts
== expected
);
290 expected
.clear(); parts
.clear();
291 StringRef("").split(parts
, ",", 0, false);
292 EXPECT_TRUE(parts
== expected
);
293 StringRef().split(parts
, ",", 0, false);
294 EXPECT_TRUE(parts
== expected
);
296 expected
.clear(); parts
.clear();
297 expected
.push_back("a");
298 expected
.push_back("");
299 expected
.push_back("b");
300 expected
.push_back("c,d");
301 StringRef("a,,b,c,d").split(parts
, ",", 3, true);
302 EXPECT_TRUE(parts
== expected
);
304 expected
.clear(); parts
.clear();
305 expected
.push_back("");
306 StringRef().split(parts
, ',', 0, true);
307 EXPECT_TRUE(parts
== expected
);
309 expected
.clear(); parts
.clear();
310 expected
.push_back(StringRef());
311 StringRef("").split(parts
, ',', 0, true);
312 EXPECT_TRUE(parts
== expected
);
314 expected
.clear(); parts
.clear();
315 StringRef("").split(parts
, ',', 0, false);
316 EXPECT_TRUE(parts
== expected
);
317 StringRef().split(parts
, ',', 0, false);
318 EXPECT_TRUE(parts
== expected
);
320 expected
.clear(); parts
.clear();
321 expected
.push_back("a");
322 expected
.push_back("");
323 expected
.push_back("b");
324 expected
.push_back("c,d");
325 StringRef("a,,b,c,d").split(parts
, ',', 3, true);
326 EXPECT_TRUE(parts
== expected
);
329 TEST(StringRefTest
, Trim
) {
330 StringRef
Str0("hello");
331 StringRef
Str1(" hello ");
332 StringRef
Str2(" hello ");
334 EXPECT_EQ(StringRef("hello"), Str0
.rtrim());
335 EXPECT_EQ(StringRef(" hello"), Str1
.rtrim());
336 EXPECT_EQ(StringRef(" hello"), Str2
.rtrim());
337 EXPECT_EQ(StringRef("hello"), Str0
.ltrim());
338 EXPECT_EQ(StringRef("hello "), Str1
.ltrim());
339 EXPECT_EQ(StringRef("hello "), Str2
.ltrim());
340 EXPECT_EQ(StringRef("hello"), Str0
.trim());
341 EXPECT_EQ(StringRef("hello"), Str1
.trim());
342 EXPECT_EQ(StringRef("hello"), Str2
.trim());
344 EXPECT_EQ(StringRef("ello"), Str0
.trim("hhhhhhhhhhh"));
346 EXPECT_EQ(StringRef(""), StringRef("").trim());
347 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
348 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
349 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
350 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
353 TEST(StringRefTest
, StartsWith
) {
354 StringRef
Str("hello");
355 EXPECT_TRUE(Str
.startswith(""));
356 EXPECT_TRUE(Str
.startswith("he"));
357 EXPECT_FALSE(Str
.startswith("helloworld"));
358 EXPECT_FALSE(Str
.startswith("hi"));
361 TEST(StringRefTest
, StartsWithLower
) {
362 StringRef
Str("heLLo");
363 EXPECT_TRUE(Str
.startswith_lower(""));
364 EXPECT_TRUE(Str
.startswith_lower("he"));
365 EXPECT_TRUE(Str
.startswith_lower("hell"));
366 EXPECT_TRUE(Str
.startswith_lower("HELlo"));
367 EXPECT_FALSE(Str
.startswith_lower("helloworld"));
368 EXPECT_FALSE(Str
.startswith_lower("hi"));
371 TEST(StringRefTest
, ConsumeFront
) {
372 StringRef
Str("hello");
373 EXPECT_TRUE(Str
.consume_front(""));
374 EXPECT_EQ("hello", Str
);
375 EXPECT_TRUE(Str
.consume_front("he"));
376 EXPECT_EQ("llo", Str
);
377 EXPECT_FALSE(Str
.consume_front("lloworld"));
378 EXPECT_EQ("llo", Str
);
379 EXPECT_FALSE(Str
.consume_front("lol"));
380 EXPECT_EQ("llo", Str
);
381 EXPECT_TRUE(Str
.consume_front("llo"));
383 EXPECT_FALSE(Str
.consume_front("o"));
384 EXPECT_TRUE(Str
.consume_front(""));
387 TEST(StringRefTest
, EndsWith
) {
388 StringRef
Str("hello");
389 EXPECT_TRUE(Str
.endswith(""));
390 EXPECT_TRUE(Str
.endswith("lo"));
391 EXPECT_FALSE(Str
.endswith("helloworld"));
392 EXPECT_FALSE(Str
.endswith("worldhello"));
393 EXPECT_FALSE(Str
.endswith("so"));
396 TEST(StringRefTest
, EndsWithLower
) {
397 StringRef
Str("heLLo");
398 EXPECT_TRUE(Str
.endswith_lower(""));
399 EXPECT_TRUE(Str
.endswith_lower("lo"));
400 EXPECT_TRUE(Str
.endswith_lower("LO"));
401 EXPECT_TRUE(Str
.endswith_lower("ELlo"));
402 EXPECT_FALSE(Str
.endswith_lower("helloworld"));
403 EXPECT_FALSE(Str
.endswith_lower("hi"));
406 TEST(StringRefTest
, ConsumeBack
) {
407 StringRef
Str("hello");
408 EXPECT_TRUE(Str
.consume_back(""));
409 EXPECT_EQ("hello", Str
);
410 EXPECT_TRUE(Str
.consume_back("lo"));
411 EXPECT_EQ("hel", Str
);
412 EXPECT_FALSE(Str
.consume_back("helhel"));
413 EXPECT_EQ("hel", Str
);
414 EXPECT_FALSE(Str
.consume_back("hle"));
415 EXPECT_EQ("hel", Str
);
416 EXPECT_TRUE(Str
.consume_back("hel"));
418 EXPECT_FALSE(Str
.consume_back("h"));
419 EXPECT_TRUE(Str
.consume_back(""));
422 TEST(StringRefTest
, Find
) {
423 StringRef
Str("helloHELLO");
424 StringRef
LongStr("hellx xello hell ello world foo bar hello HELLO");
431 std::size_t LowerPos
;
432 } CharExpectations
[] = {
433 {Str
, 'h', 0U, 0U, 0U},
434 {Str
, 'e', 0U, 1U, 1U},
435 {Str
, 'l', 0U, 2U, 2U},
436 {Str
, 'l', 3U, 3U, 3U},
437 {Str
, 'o', 0U, 4U, 4U},
438 {Str
, 'L', 0U, 7U, 2U},
439 {Str
, 'z', 0U, StringRef::npos
, StringRef::npos
},
447 std::size_t LowerPos
;
448 } StrExpectations
[] = {
449 {Str
, "helloword", 0, StringRef::npos
, StringRef::npos
},
450 {Str
, "hello", 0, 0U, 0U},
451 {Str
, "ello", 0, 1U, 1U},
452 {Str
, "zz", 0, StringRef::npos
, StringRef::npos
},
453 {Str
, "ll", 2U, 2U, 2U},
454 {Str
, "ll", 3U, StringRef::npos
, 7U},
455 {Str
, "LL", 2U, 7U, 2U},
456 {Str
, "LL", 3U, 7U, 7U},
457 {Str
, "", 0U, 0U, 0U},
458 {LongStr
, "hello", 0U, 36U, 36U},
459 {LongStr
, "foo", 0U, 28U, 28U},
460 {LongStr
, "hell", 2U, 12U, 12U},
461 {LongStr
, "HELL", 2U, 42U, 12U},
462 {LongStr
, "", 0U, 0U, 0U}};
464 for (auto &E
: CharExpectations
) {
465 EXPECT_EQ(E
.Pos
, E
.Str
.find(E
.C
, E
.From
));
466 EXPECT_EQ(E
.LowerPos
, E
.Str
.find_lower(E
.C
, E
.From
));
467 EXPECT_EQ(E
.LowerPos
, E
.Str
.find_lower(toupper(E
.C
), E
.From
));
470 for (auto &E
: StrExpectations
) {
471 EXPECT_EQ(E
.Pos
, E
.Str
.find(E
.S
, E
.From
));
472 EXPECT_EQ(E
.LowerPos
, E
.Str
.find_lower(E
.S
, E
.From
));
473 EXPECT_EQ(E
.LowerPos
, E
.Str
.find_lower(E
.S
.upper(), E
.From
));
476 EXPECT_EQ(3U, Str
.rfind('l'));
477 EXPECT_EQ(StringRef::npos
, Str
.rfind('z'));
478 EXPECT_EQ(StringRef::npos
, Str
.rfind("helloworld"));
479 EXPECT_EQ(0U, Str
.rfind("hello"));
480 EXPECT_EQ(1U, Str
.rfind("ello"));
481 EXPECT_EQ(StringRef::npos
, Str
.rfind("zz"));
483 EXPECT_EQ(8U, Str
.rfind_lower('l'));
484 EXPECT_EQ(8U, Str
.rfind_lower('L'));
485 EXPECT_EQ(StringRef::npos
, Str
.rfind_lower('z'));
486 EXPECT_EQ(StringRef::npos
, Str
.rfind_lower("HELLOWORLD"));
487 EXPECT_EQ(5U, Str
.rfind("HELLO"));
488 EXPECT_EQ(6U, Str
.rfind("ELLO"));
489 EXPECT_EQ(StringRef::npos
, Str
.rfind("ZZ"));
491 EXPECT_EQ(2U, Str
.find_first_of('l'));
492 EXPECT_EQ(1U, Str
.find_first_of("el"));
493 EXPECT_EQ(StringRef::npos
, Str
.find_first_of("xyz"));
496 EXPECT_EQ(1U, Str
.find_first_not_of('h'));
497 EXPECT_EQ(4U, Str
.find_first_not_of("hel"));
498 EXPECT_EQ(StringRef::npos
, Str
.find_first_not_of("hello"));
500 EXPECT_EQ(3U, Str
.find_last_not_of('o'));
501 EXPECT_EQ(1U, Str
.find_last_not_of("lo"));
502 EXPECT_EQ(StringRef::npos
, Str
.find_last_not_of("helo"));
505 TEST(StringRefTest
, Count
) {
506 StringRef
Str("hello");
507 EXPECT_EQ(2U, Str
.count('l'));
508 EXPECT_EQ(1U, Str
.count('o'));
509 EXPECT_EQ(0U, Str
.count('z'));
510 EXPECT_EQ(0U, Str
.count("helloworld"));
511 EXPECT_EQ(1U, Str
.count("hello"));
512 EXPECT_EQ(1U, Str
.count("ello"));
513 EXPECT_EQ(0U, Str
.count("zz"));
516 TEST(StringRefTest
, EditDistance
) {
517 StringRef
Hello("hello");
518 EXPECT_EQ(2U, Hello
.edit_distance("hill"));
520 StringRef
Industry("industry");
521 EXPECT_EQ(6U, Industry
.edit_distance("interest"));
523 StringRef
Soylent("soylent green is people");
524 EXPECT_EQ(19U, Soylent
.edit_distance("people soiled our green"));
525 EXPECT_EQ(26U, Soylent
.edit_distance("people soiled our green",
526 /* allow replacements = */ false));
527 EXPECT_EQ(9U, Soylent
.edit_distance("people soiled our green",
528 /* allow replacements = */ true,
529 /* max edit distance = */ 8));
530 EXPECT_EQ(53U, Soylent
.edit_distance("people soiled our green "
531 "people soiled our green "
532 "people soiled our green "));
535 TEST(StringRefTest
, Misc
) {
537 raw_string_ostream
OS(Storage
);
538 OS
<< StringRef("hello");
539 EXPECT_EQ("hello", OS
.str());
542 TEST(StringRefTest
, Hashing
) {
543 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
544 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
545 std::string S
= "hello world";
546 hash_code H
= hash_value(S
);
547 EXPECT_EQ(H
, hash_value(StringRef("hello world")));
548 EXPECT_EQ(H
, hash_value(StringRef(S
)));
549 EXPECT_NE(H
, hash_value(StringRef("hello worl")));
550 EXPECT_EQ(hash_value(std::string("hello worl")),
551 hash_value(StringRef("hello worl")));
552 EXPECT_NE(H
, hash_value(StringRef("hello world ")));
553 EXPECT_EQ(hash_value(std::string("hello world ")),
554 hash_value(StringRef("hello world ")));
555 EXPECT_EQ(H
, hash_value(StringRef("hello world\0")));
556 EXPECT_NE(hash_value(std::string("ello worl")),
557 hash_value(StringRef("hello world").slice(1, -1)));
560 struct UnsignedPair
{
569 , {"4294967295", 4294967295ULL}
570 , {"4294967296", 4294967296ULL}
571 , {"18446744073709551615", 18446744073709551615ULL}
591 , {"2147483647", 2147483647LL}
592 , {"2147483648", 2147483648LL}
593 , {"-2147483648", -2147483648LL}
594 , {"-2147483649", -2147483649LL}
595 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
604 TEST(StringRefTest
, getAsInteger
) {
610 for (size_t i
= 0; i
< array_lengthof(Unsigned
); ++i
) {
611 bool U8Success
= StringRef(Unsigned
[i
].Str
).getAsInteger(0, U8
);
612 if (static_cast<uint8_t>(Unsigned
[i
].Expected
) == Unsigned
[i
].Expected
) {
613 ASSERT_FALSE(U8Success
);
614 EXPECT_EQ(U8
, Unsigned
[i
].Expected
);
616 ASSERT_TRUE(U8Success
);
618 bool U16Success
= StringRef(Unsigned
[i
].Str
).getAsInteger(0, U16
);
619 if (static_cast<uint16_t>(Unsigned
[i
].Expected
) == Unsigned
[i
].Expected
) {
620 ASSERT_FALSE(U16Success
);
621 EXPECT_EQ(U16
, Unsigned
[i
].Expected
);
623 ASSERT_TRUE(U16Success
);
625 bool U32Success
= StringRef(Unsigned
[i
].Str
).getAsInteger(0, U32
);
626 if (static_cast<uint32_t>(Unsigned
[i
].Expected
) == Unsigned
[i
].Expected
) {
627 ASSERT_FALSE(U32Success
);
628 EXPECT_EQ(U32
, Unsigned
[i
].Expected
);
630 ASSERT_TRUE(U32Success
);
632 bool U64Success
= StringRef(Unsigned
[i
].Str
).getAsInteger(0, U64
);
633 if (static_cast<uint64_t>(Unsigned
[i
].Expected
) == Unsigned
[i
].Expected
) {
634 ASSERT_FALSE(U64Success
);
635 EXPECT_EQ(U64
, Unsigned
[i
].Expected
);
637 ASSERT_TRUE(U64Success
);
646 for (size_t i
= 0; i
< array_lengthof(Signed
); ++i
) {
647 bool S8Success
= StringRef(Signed
[i
].Str
).getAsInteger(0, S8
);
648 if (static_cast<int8_t>(Signed
[i
].Expected
) == Signed
[i
].Expected
) {
649 ASSERT_FALSE(S8Success
);
650 EXPECT_EQ(S8
, Signed
[i
].Expected
);
652 ASSERT_TRUE(S8Success
);
654 bool S16Success
= StringRef(Signed
[i
].Str
).getAsInteger(0, S16
);
655 if (static_cast<int16_t>(Signed
[i
].Expected
) == Signed
[i
].Expected
) {
656 ASSERT_FALSE(S16Success
);
657 EXPECT_EQ(S16
, Signed
[i
].Expected
);
659 ASSERT_TRUE(S16Success
);
661 bool S32Success
= StringRef(Signed
[i
].Str
).getAsInteger(0, S32
);
662 if (static_cast<int32_t>(Signed
[i
].Expected
) == Signed
[i
].Expected
) {
663 ASSERT_FALSE(S32Success
);
664 EXPECT_EQ(S32
, Signed
[i
].Expected
);
666 ASSERT_TRUE(S32Success
);
668 bool S64Success
= StringRef(Signed
[i
].Str
).getAsInteger(0, S64
);
669 if (static_cast<int64_t>(Signed
[i
].Expected
) == Signed
[i
].Expected
) {
670 ASSERT_FALSE(S64Success
);
671 EXPECT_EQ(S64
, Signed
[i
].Expected
);
673 ASSERT_TRUE(S64Success
);
679 static const char* BadStrings
[] = {
681 , "18446744073709551617" // value just over max
682 , "123456789012345678901" // value way too large
683 , "4t23v" // illegal decimal characters
684 , "0x123W56" // illegal hex characters
685 , "0b2" // illegal bin characters
686 , "08" // illegal oct characters
687 , "0o8" // illegal oct characters
688 , "-123" // negative unsigned value
694 TEST(StringRefTest
, getAsUnsignedIntegerBadStrings
) {
695 unsigned long long U64
;
696 for (size_t i
= 0; i
< array_lengthof(BadStrings
); ++i
) {
697 bool IsBadNumber
= StringRef(BadStrings
[i
]).getAsInteger(0, U64
);
698 ASSERT_TRUE(IsBadNumber
);
702 struct ConsumeUnsignedPair
{
705 const char *Leftover
;
706 } ConsumeUnsigned
[] = {
710 {"65535", 65535, ""},
711 {"65536", 65536, ""},
712 {"4294967295", 4294967295ULL, ""},
713 {"4294967296", 4294967296ULL, ""},
714 {"255A376", 255, "A376"},
715 {"18446744073709551615", 18446744073709551615ULL, ""},
716 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
719 {"0x42-0x34", 66, "-0x34"},
720 {"0b101010", 42, ""},
721 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit
722 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit
723 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
725 struct ConsumeSignedPair
{
728 const char *Leftover
;
729 } ConsumeSigned
[] = {
736 {"127-1", 127, "-1"},
737 {"128-1", 128, "-1"},
740 {"-128-1", -128, "-1"},
741 {"-129-1", -129, "-1"},
742 {"32767", 32767, ""},
743 {"32768", 32768, ""},
744 {"32767-1", 32767, "-1"},
745 {"32768-1", 32768, "-1"},
746 {"-32768", -32768, ""},
747 {"-32769", -32769, ""},
748 {"-32768-1", -32768, "-1"},
749 {"-32769-1", -32769, "-1"},
750 {"2147483647", 2147483647LL, ""},
751 {"2147483648", 2147483648LL, ""},
752 {"2147483647-1", 2147483647LL, "-1"},
753 {"2147483648-1", 2147483648LL, "-1"},
754 {"-2147483648", -2147483648LL, ""},
755 {"-2147483649", -2147483649LL, ""},
756 {"-2147483648-1", -2147483648LL, "-1"},
757 {"-2147483649-1", -2147483649LL, "-1"},
758 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
759 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
763 {"0x42-1", 66, "-1"},
764 {"0b101010", 42, ""},
765 {"0b101010-1", 42, "-1"},
767 {"-042-1", -34, "-1"},
769 {"-0x42-1", -66, "-1"},
770 {"-0b101010", -42, ""},
771 {"-0b101010-1", -42, "-1"}};
773 TEST(StringRefTest
, consumeIntegerUnsigned
) {
779 for (size_t i
= 0; i
< array_lengthof(ConsumeUnsigned
); ++i
) {
780 StringRef Str
= ConsumeUnsigned
[i
].Str
;
781 bool U8Success
= Str
.consumeInteger(0, U8
);
782 if (static_cast<uint8_t>(ConsumeUnsigned
[i
].Expected
) ==
783 ConsumeUnsigned
[i
].Expected
) {
784 ASSERT_FALSE(U8Success
);
785 EXPECT_EQ(U8
, ConsumeUnsigned
[i
].Expected
);
786 EXPECT_EQ(Str
, ConsumeUnsigned
[i
].Leftover
);
788 ASSERT_TRUE(U8Success
);
791 Str
= ConsumeUnsigned
[i
].Str
;
792 bool U16Success
= Str
.consumeInteger(0, U16
);
793 if (static_cast<uint16_t>(ConsumeUnsigned
[i
].Expected
) ==
794 ConsumeUnsigned
[i
].Expected
) {
795 ASSERT_FALSE(U16Success
);
796 EXPECT_EQ(U16
, ConsumeUnsigned
[i
].Expected
);
797 EXPECT_EQ(Str
, ConsumeUnsigned
[i
].Leftover
);
799 ASSERT_TRUE(U16Success
);
802 Str
= ConsumeUnsigned
[i
].Str
;
803 bool U32Success
= Str
.consumeInteger(0, U32
);
804 if (static_cast<uint32_t>(ConsumeUnsigned
[i
].Expected
) ==
805 ConsumeUnsigned
[i
].Expected
) {
806 ASSERT_FALSE(U32Success
);
807 EXPECT_EQ(U32
, ConsumeUnsigned
[i
].Expected
);
808 EXPECT_EQ(Str
, ConsumeUnsigned
[i
].Leftover
);
810 ASSERT_TRUE(U32Success
);
813 Str
= ConsumeUnsigned
[i
].Str
;
814 bool U64Success
= Str
.consumeInteger(0, U64
);
815 if (static_cast<uint64_t>(ConsumeUnsigned
[i
].Expected
) ==
816 ConsumeUnsigned
[i
].Expected
) {
817 ASSERT_FALSE(U64Success
);
818 EXPECT_EQ(U64
, ConsumeUnsigned
[i
].Expected
);
819 EXPECT_EQ(Str
, ConsumeUnsigned
[i
].Leftover
);
821 ASSERT_TRUE(U64Success
);
826 TEST(StringRefTest
, consumeIntegerSigned
) {
832 for (size_t i
= 0; i
< array_lengthof(ConsumeSigned
); ++i
) {
833 StringRef Str
= ConsumeSigned
[i
].Str
;
834 bool S8Success
= Str
.consumeInteger(0, S8
);
835 if (static_cast<int8_t>(ConsumeSigned
[i
].Expected
) ==
836 ConsumeSigned
[i
].Expected
) {
837 ASSERT_FALSE(S8Success
);
838 EXPECT_EQ(S8
, ConsumeSigned
[i
].Expected
);
839 EXPECT_EQ(Str
, ConsumeSigned
[i
].Leftover
);
841 ASSERT_TRUE(S8Success
);
844 Str
= ConsumeSigned
[i
].Str
;
845 bool S16Success
= Str
.consumeInteger(0, S16
);
846 if (static_cast<int16_t>(ConsumeSigned
[i
].Expected
) ==
847 ConsumeSigned
[i
].Expected
) {
848 ASSERT_FALSE(S16Success
);
849 EXPECT_EQ(S16
, ConsumeSigned
[i
].Expected
);
850 EXPECT_EQ(Str
, ConsumeSigned
[i
].Leftover
);
852 ASSERT_TRUE(S16Success
);
855 Str
= ConsumeSigned
[i
].Str
;
856 bool S32Success
= Str
.consumeInteger(0, S32
);
857 if (static_cast<int32_t>(ConsumeSigned
[i
].Expected
) ==
858 ConsumeSigned
[i
].Expected
) {
859 ASSERT_FALSE(S32Success
);
860 EXPECT_EQ(S32
, ConsumeSigned
[i
].Expected
);
861 EXPECT_EQ(Str
, ConsumeSigned
[i
].Leftover
);
863 ASSERT_TRUE(S32Success
);
866 Str
= ConsumeSigned
[i
].Str
;
867 bool S64Success
= Str
.consumeInteger(0, S64
);
868 if (static_cast<int64_t>(ConsumeSigned
[i
].Expected
) ==
869 ConsumeSigned
[i
].Expected
) {
870 ASSERT_FALSE(S64Success
);
871 EXPECT_EQ(S64
, ConsumeSigned
[i
].Expected
);
872 EXPECT_EQ(Str
, ConsumeSigned
[i
].Leftover
);
874 ASSERT_TRUE(S64Success
);
879 struct GetDoubleStrings
{
884 } DoubleStrings
[] = {{"0", false, false, 0.0},
885 {"0.0", false, false, 0.0},
886 {"-0.0", false, false, -0.0},
887 {"123.45", false, true, 123.45},
888 {"123.45", true, false, 123.45},
889 {"1.8e308", true, false, std::numeric_limits
<double>::infinity()},
890 {"1.8e308", false, true, std::numeric_limits
<double>::infinity()},
891 {"0x0.0000000000001P-1023", false, true, 0.0},
892 {"0x0.0000000000001P-1023", true, false, 0.0},
895 TEST(StringRefTest
, getAsDouble
) {
896 for (const auto &Entry
: DoubleStrings
) {
898 StringRef
S(Entry
.Str
);
899 EXPECT_EQ(Entry
.ShouldFail
, S
.getAsDouble(Result
, Entry
.AllowInexact
));
900 if (!Entry
.ShouldFail
) {
901 EXPECT_EQ(Result
, Entry
.D
);
906 static const char *join_input
[] = { "a", "b", "c" };
907 static const char join_result1
[] = "a";
908 static const char join_result2
[] = "a:b:c";
909 static const char join_result3
[] = "a::b::c";
911 TEST(StringRefTest
, joinStrings
) {
912 std::vector
<StringRef
> v1
;
913 std::vector
<std::string
> v2
;
914 for (size_t i
= 0; i
< array_lengthof(join_input
); ++i
) {
915 v1
.push_back(join_input
[i
]);
916 v2
.push_back(join_input
[i
]);
919 bool v1_join1
= join(v1
.begin(), v1
.begin() + 1, ":") == join_result1
;
920 EXPECT_TRUE(v1_join1
);
921 bool v1_join2
= join(v1
.begin(), v1
.end(), ":") == join_result2
;
922 EXPECT_TRUE(v1_join2
);
923 bool v1_join3
= join(v1
.begin(), v1
.end(), "::") == join_result3
;
924 EXPECT_TRUE(v1_join3
);
926 bool v2_join1
= join(v2
.begin(), v2
.begin() + 1, ":") == join_result1
;
927 EXPECT_TRUE(v2_join1
);
928 bool v2_join2
= join(v2
.begin(), v2
.end(), ":") == join_result2
;
929 EXPECT_TRUE(v2_join2
);
930 bool v2_join3
= join(v2
.begin(), v2
.end(), "::") == join_result3
;
931 EXPECT_TRUE(v2_join3
);
932 v2_join3
= join(v2
, "::") == join_result3
;
933 EXPECT_TRUE(v2_join3
);
937 TEST(StringRefTest
, AllocatorCopy
) {
938 BumpPtrAllocator Alloc
;
939 // First test empty strings. We don't want these to allocate anything on the
941 StringRef StrEmpty
= "";
942 StringRef StrEmptyc
= StrEmpty
.copy(Alloc
);
943 EXPECT_TRUE(StrEmpty
.equals(StrEmptyc
));
944 EXPECT_EQ(StrEmptyc
.data(), nullptr);
945 EXPECT_EQ(StrEmptyc
.size(), 0u);
946 EXPECT_EQ(Alloc
.getTotalMemory(), 0u);
948 StringRef Str1
= "hello";
949 StringRef Str2
= "bye";
950 StringRef Str1c
= Str1
.copy(Alloc
);
951 StringRef Str2c
= Str2
.copy(Alloc
);
952 EXPECT_TRUE(Str1
.equals(Str1c
));
953 EXPECT_NE(Str1
.data(), Str1c
.data());
954 EXPECT_TRUE(Str2
.equals(Str2c
));
955 EXPECT_NE(Str2
.data(), Str2c
.data());
958 TEST(StringRefTest
, Drop
) {
959 StringRef
Test("StringRefTest::Drop");
961 StringRef Dropped
= Test
.drop_front(5);
962 EXPECT_EQ(Dropped
, "gRefTest::Drop");
964 Dropped
= Test
.drop_back(5);
965 EXPECT_EQ(Dropped
, "StringRefTest:");
967 Dropped
= Test
.drop_front(0);
968 EXPECT_EQ(Dropped
, Test
);
970 Dropped
= Test
.drop_back(0);
971 EXPECT_EQ(Dropped
, Test
);
973 Dropped
= Test
.drop_front(Test
.size());
974 EXPECT_TRUE(Dropped
.empty());
976 Dropped
= Test
.drop_back(Test
.size());
977 EXPECT_TRUE(Dropped
.empty());
980 TEST(StringRefTest
, Take
) {
981 StringRef
Test("StringRefTest::Take");
983 StringRef Taken
= Test
.take_front(5);
984 EXPECT_EQ(Taken
, "Strin");
986 Taken
= Test
.take_back(5);
987 EXPECT_EQ(Taken
, ":Take");
989 Taken
= Test
.take_front(Test
.size());
990 EXPECT_EQ(Taken
, Test
);
992 Taken
= Test
.take_back(Test
.size());
993 EXPECT_EQ(Taken
, Test
);
995 Taken
= Test
.take_front(0);
996 EXPECT_TRUE(Taken
.empty());
998 Taken
= Test
.take_back(0);
999 EXPECT_TRUE(Taken
.empty());
1002 TEST(StringRefTest
, FindIf
) {
1003 StringRef
Punct("Test.String");
1004 StringRef
NoPunct("ABCDEFG");
1007 auto IsPunct
= [](char c
) { return ::ispunct(c
); };
1008 auto IsAlpha
= [](char c
) { return ::isalpha(c
); };
1009 EXPECT_EQ(4U, Punct
.find_if(IsPunct
));
1010 EXPECT_EQ(StringRef::npos
, NoPunct
.find_if(IsPunct
));
1011 EXPECT_EQ(StringRef::npos
, Empty
.find_if(IsPunct
));
1013 EXPECT_EQ(4U, Punct
.find_if_not(IsAlpha
));
1014 EXPECT_EQ(StringRef::npos
, NoPunct
.find_if_not(IsAlpha
));
1015 EXPECT_EQ(StringRef::npos
, Empty
.find_if_not(IsAlpha
));
1018 TEST(StringRefTest
, TakeWhileUntil
) {
1019 StringRef
Test("String With 1 Number");
1021 StringRef Taken
= Test
.take_while([](char c
) { return ::isdigit(c
); });
1022 EXPECT_EQ("", Taken
);
1024 Taken
= Test
.take_until([](char c
) { return ::isdigit(c
); });
1025 EXPECT_EQ("String With ", Taken
);
1027 Taken
= Test
.take_while([](char c
) { return true; });
1028 EXPECT_EQ(Test
, Taken
);
1030 Taken
= Test
.take_until([](char c
) { return true; });
1031 EXPECT_EQ("", Taken
);
1034 Taken
= Test
.take_while([](char c
) { return true; });
1035 EXPECT_EQ("", Taken
);
1038 TEST(StringRefTest
, DropWhileUntil
) {
1039 StringRef
Test("String With 1 Number");
1041 StringRef Taken
= Test
.drop_while([](char c
) { return ::isdigit(c
); });
1042 EXPECT_EQ(Test
, Taken
);
1044 Taken
= Test
.drop_until([](char c
) { return ::isdigit(c
); });
1045 EXPECT_EQ("1 Number", Taken
);
1047 Taken
= Test
.drop_while([](char c
) { return true; });
1048 EXPECT_EQ("", Taken
);
1050 Taken
= Test
.drop_until([](char c
) { return true; });
1051 EXPECT_EQ(Test
, Taken
);
1053 StringRef EmptyString
= "";
1054 Taken
= EmptyString
.drop_while([](char c
) { return true; });
1055 EXPECT_EQ("", Taken
);
1058 TEST(StringRefTest
, StringLiteral
) {
1059 constexpr StringLiteral Strings
[] = {"Foo", "Bar"};
1060 EXPECT_EQ(StringRef("Foo"), Strings
[0]);
1061 EXPECT_EQ(StringRef("Bar"), Strings
[1]);
1064 static_assert(is_trivially_copyable
<StringRef
>::value
, "trivially copyable");
1066 } // end anonymous namespace