1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/Hashing.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/Support/Allocator.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h"
22 std::ostream
&operator<<(std::ostream
&OS
, const StringRef
&S
) {
27 std::ostream
&operator<<(std::ostream
&OS
,
28 const std::pair
<StringRef
, StringRef
> &P
) {
29 OS
<< "(" << P
.first
<< ", " << P
.second
<< ")";
35 // Check that we can't accidentally assign a temporary std::string to a
36 // StringRef. (Unfortunately we can't make use of the same thing with
39 // Disable this check under MSVC; even MSVC 2015 isn't consistent between
40 // std::is_assignable and actually writing such an assignment.
41 #if !defined(_MSC_VER)
43 !std::is_assignable
<StringRef
&, std::string
>::value
,
44 "Assigning from prvalue std::string");
46 !std::is_assignable
<StringRef
&, std::string
&&>::value
,
47 "Assigning from xvalue std::string");
49 std::is_assignable
<StringRef
&, std::string
&>::value
,
50 "Assigning from lvalue std::string");
52 std::is_assignable
<StringRef
&, const char *>::value
,
53 "Assigning from prvalue C string");
55 std::is_assignable
<StringRef
&, const char * &&>::value
,
56 "Assigning from xvalue C string");
58 std::is_assignable
<StringRef
&, const char * &>::value
,
59 "Assigning from lvalue C string");
64 TEST(StringRefTest
, Construction
) {
65 EXPECT_EQ("", StringRef());
66 EXPECT_EQ("hello", StringRef("hello"));
67 EXPECT_EQ("hello", StringRef("hello world", 5));
68 EXPECT_EQ("hello", StringRef(std::string("hello")));
71 TEST(StringRefTest
, EmptyInitializerList
) {
73 EXPECT_TRUE(S
.empty());
76 EXPECT_TRUE(S
.empty());
79 TEST(StringRefTest
, Iteration
) {
81 const char *p
= "hello";
82 for (const char *it
= S
.begin(), *ie
= S
.end(); it
!= ie
; ++it
, ++p
)
86 TEST(StringRefTest
, StringOps
) {
87 const char *p
= "hello";
88 EXPECT_EQ(p
, StringRef(p
, 0).data());
89 EXPECT_TRUE(StringRef().empty());
90 EXPECT_EQ((size_t) 5, StringRef("hello").size());
91 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
92 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
93 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
94 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
95 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
96 EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
98 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
99 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
100 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
101 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
102 EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
103 EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
104 EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
105 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
106 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
108 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
109 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
110 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
111 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
112 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
113 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
114 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
115 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
116 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
117 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
118 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
119 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
120 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
121 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
122 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
123 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
124 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
127 TEST(StringRefTest
, Operators
) {
128 EXPECT_EQ("", StringRef());
129 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
130 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
131 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
132 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
133 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
134 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
135 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
136 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
137 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
138 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
139 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
140 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
141 EXPECT_EQ('a', StringRef("aab")[1]);
144 TEST(StringRefTest
, Substr
) {
145 StringRef
Str("hello");
146 EXPECT_EQ("lo", Str
.substr(3));
147 EXPECT_EQ("", Str
.substr(100));
148 EXPECT_EQ("hello", Str
.substr(0, 100));
149 EXPECT_EQ("o", Str
.substr(4, 10));
152 TEST(StringRefTest
, Slice
) {
153 StringRef
Str("hello");
154 EXPECT_EQ("l", Str
.slice(2, 3));
155 EXPECT_EQ("ell", Str
.slice(1, 4));
156 EXPECT_EQ("llo", Str
.slice(2, 100));
157 EXPECT_EQ("", Str
.slice(2, 1));
158 EXPECT_EQ("", Str
.slice(10, 20));
161 TEST(StringRefTest
, Split
) {
162 StringRef
Str("hello");
163 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
165 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
167 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
169 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
171 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
174 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
176 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
178 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
180 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
182 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
185 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("o")),
187 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
189 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
191 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
193 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
197 TEST(StringRefTest
, Split2
) {
198 SmallVector
<StringRef
, 5> parts
;
199 SmallVector
<StringRef
, 5> expected
;
201 expected
.push_back("ab"); expected
.push_back("c");
202 StringRef(",ab,,c,").split(parts
, ",", -1, false);
203 EXPECT_TRUE(parts
== expected
);
205 expected
.clear(); parts
.clear();
206 expected
.push_back(""); expected
.push_back("ab"); expected
.push_back("");
207 expected
.push_back("c"); expected
.push_back("");
208 StringRef(",ab,,c,").split(parts
, ",", -1, true);
209 EXPECT_TRUE(parts
== expected
);
211 expected
.clear(); parts
.clear();
212 expected
.push_back("");
213 StringRef("").split(parts
, ",", -1, true);
214 EXPECT_TRUE(parts
== expected
);
216 expected
.clear(); parts
.clear();
217 StringRef("").split(parts
, ",", -1, false);
218 EXPECT_TRUE(parts
== expected
);
220 expected
.clear(); parts
.clear();
221 StringRef(",").split(parts
, ",", -1, false);
222 EXPECT_TRUE(parts
== expected
);
224 expected
.clear(); parts
.clear();
225 expected
.push_back(""); expected
.push_back("");
226 StringRef(",").split(parts
, ",", -1, true);
227 EXPECT_TRUE(parts
== expected
);
229 expected
.clear(); parts
.clear();
230 expected
.push_back("a"); expected
.push_back("b");
231 StringRef("a,b").split(parts
, ",", -1, true);
232 EXPECT_TRUE(parts
== expected
);
235 expected
.clear(); parts
.clear();
236 expected
.push_back("a,,b,c");
237 StringRef("a,,b,c").split(parts
, ",", 0, true);
238 EXPECT_TRUE(parts
== expected
);
240 expected
.clear(); parts
.clear();
241 expected
.push_back("a,,b,c");
242 StringRef("a,,b,c").split(parts
, ",", 0, false);
243 EXPECT_TRUE(parts
== expected
);
245 expected
.clear(); parts
.clear();
246 expected
.push_back("a"); expected
.push_back(",b,c");
247 StringRef("a,,b,c").split(parts
, ",", 1, true);
248 EXPECT_TRUE(parts
== expected
);
250 expected
.clear(); parts
.clear();
251 expected
.push_back("a"); expected
.push_back(",b,c");
252 StringRef("a,,b,c").split(parts
, ",", 1, false);
253 EXPECT_TRUE(parts
== expected
);
255 expected
.clear(); parts
.clear();
256 expected
.push_back("a"); expected
.push_back(""); expected
.push_back("b,c");
257 StringRef("a,,b,c").split(parts
, ",", 2, true);
258 EXPECT_TRUE(parts
== expected
);
260 expected
.clear(); parts
.clear();
261 expected
.push_back("a"); expected
.push_back("b,c");
262 StringRef("a,,b,c").split(parts
, ",", 2, false);
263 EXPECT_TRUE(parts
== expected
);
265 expected
.clear(); parts
.clear();
266 expected
.push_back("a"); expected
.push_back(""); expected
.push_back("b");
267 expected
.push_back("c");
268 StringRef("a,,b,c").split(parts
, ",", 3, true);
269 EXPECT_TRUE(parts
== expected
);
271 expected
.clear(); parts
.clear();
272 expected
.push_back("a"); expected
.push_back("b"); expected
.push_back("c");
273 StringRef("a,,b,c").split(parts
, ",", 3, false);
274 EXPECT_TRUE(parts
== expected
);
276 expected
.clear(); parts
.clear();
277 expected
.push_back("a"); expected
.push_back("b"); expected
.push_back("c");
278 StringRef("a,,b,c").split(parts
, ',', 3, false);
279 EXPECT_TRUE(parts
== expected
);
281 expected
.clear(); parts
.clear();
282 expected
.push_back("");
283 StringRef().split(parts
, ",", 0, true);
284 EXPECT_TRUE(parts
== expected
);
286 expected
.clear(); parts
.clear();
287 expected
.push_back(StringRef());
288 StringRef("").split(parts
, ",", 0, true);
289 EXPECT_TRUE(parts
== expected
);
291 expected
.clear(); parts
.clear();
292 StringRef("").split(parts
, ",", 0, false);
293 EXPECT_TRUE(parts
== expected
);
294 StringRef().split(parts
, ",", 0, false);
295 EXPECT_TRUE(parts
== expected
);
297 expected
.clear(); parts
.clear();
298 expected
.push_back("a");
299 expected
.push_back("");
300 expected
.push_back("b");
301 expected
.push_back("c,d");
302 StringRef("a,,b,c,d").split(parts
, ",", 3, true);
303 EXPECT_TRUE(parts
== expected
);
305 expected
.clear(); parts
.clear();
306 expected
.push_back("");
307 StringRef().split(parts
, ',', 0, true);
308 EXPECT_TRUE(parts
== expected
);
310 expected
.clear(); parts
.clear();
311 expected
.push_back(StringRef());
312 StringRef("").split(parts
, ',', 0, true);
313 EXPECT_TRUE(parts
== expected
);
315 expected
.clear(); parts
.clear();
316 StringRef("").split(parts
, ',', 0, false);
317 EXPECT_TRUE(parts
== expected
);
318 StringRef().split(parts
, ',', 0, false);
319 EXPECT_TRUE(parts
== expected
);
321 expected
.clear(); parts
.clear();
322 expected
.push_back("a");
323 expected
.push_back("");
324 expected
.push_back("b");
325 expected
.push_back("c,d");
326 StringRef("a,,b,c,d").split(parts
, ',', 3, true);
327 EXPECT_TRUE(parts
== expected
);
330 TEST(StringRefTest
, Trim
) {
331 StringRef
Str0("hello");
332 StringRef
Str1(" hello ");
333 StringRef
Str2(" hello ");
335 EXPECT_EQ(StringRef("hello"), Str0
.rtrim());
336 EXPECT_EQ(StringRef(" hello"), Str1
.rtrim());
337 EXPECT_EQ(StringRef(" hello"), Str2
.rtrim());
338 EXPECT_EQ(StringRef("hello"), Str0
.ltrim());
339 EXPECT_EQ(StringRef("hello "), Str1
.ltrim());
340 EXPECT_EQ(StringRef("hello "), Str2
.ltrim());
341 EXPECT_EQ(StringRef("hello"), Str0
.trim());
342 EXPECT_EQ(StringRef("hello"), Str1
.trim());
343 EXPECT_EQ(StringRef("hello"), Str2
.trim());
345 EXPECT_EQ(StringRef("ello"), Str0
.trim("hhhhhhhhhhh"));
347 EXPECT_EQ(StringRef(""), StringRef("").trim());
348 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
349 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
350 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
351 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
354 TEST(StringRefTest
, StartsWith
) {
355 StringRef
Str("hello");
356 EXPECT_TRUE(Str
.startswith(""));
357 EXPECT_TRUE(Str
.startswith("he"));
358 EXPECT_FALSE(Str
.startswith("helloworld"));
359 EXPECT_FALSE(Str
.startswith("hi"));
362 TEST(StringRefTest
, StartsWithLower
) {
363 StringRef
Str("heLLo");
364 EXPECT_TRUE(Str
.startswith_lower(""));
365 EXPECT_TRUE(Str
.startswith_lower("he"));
366 EXPECT_TRUE(Str
.startswith_lower("hell"));
367 EXPECT_TRUE(Str
.startswith_lower("HELlo"));
368 EXPECT_FALSE(Str
.startswith_lower("helloworld"));
369 EXPECT_FALSE(Str
.startswith_lower("hi"));
372 TEST(StringRefTest
, ConsumeFront
) {
373 StringRef
Str("hello");
374 EXPECT_TRUE(Str
.consume_front(""));
375 EXPECT_EQ("hello", Str
);
376 EXPECT_TRUE(Str
.consume_front("he"));
377 EXPECT_EQ("llo", Str
);
378 EXPECT_FALSE(Str
.consume_front("lloworld"));
379 EXPECT_EQ("llo", Str
);
380 EXPECT_FALSE(Str
.consume_front("lol"));
381 EXPECT_EQ("llo", Str
);
382 EXPECT_TRUE(Str
.consume_front("llo"));
384 EXPECT_FALSE(Str
.consume_front("o"));
385 EXPECT_TRUE(Str
.consume_front(""));
388 TEST(StringRefTest
, EndsWith
) {
389 StringRef
Str("hello");
390 EXPECT_TRUE(Str
.endswith(""));
391 EXPECT_TRUE(Str
.endswith("lo"));
392 EXPECT_FALSE(Str
.endswith("helloworld"));
393 EXPECT_FALSE(Str
.endswith("worldhello"));
394 EXPECT_FALSE(Str
.endswith("so"));
397 TEST(StringRefTest
, EndsWithLower
) {
398 StringRef
Str("heLLo");
399 EXPECT_TRUE(Str
.endswith_lower(""));
400 EXPECT_TRUE(Str
.endswith_lower("lo"));
401 EXPECT_TRUE(Str
.endswith_lower("LO"));
402 EXPECT_TRUE(Str
.endswith_lower("ELlo"));
403 EXPECT_FALSE(Str
.endswith_lower("helloworld"));
404 EXPECT_FALSE(Str
.endswith_lower("hi"));
407 TEST(StringRefTest
, ConsumeBack
) {
408 StringRef
Str("hello");
409 EXPECT_TRUE(Str
.consume_back(""));
410 EXPECT_EQ("hello", Str
);
411 EXPECT_TRUE(Str
.consume_back("lo"));
412 EXPECT_EQ("hel", Str
);
413 EXPECT_FALSE(Str
.consume_back("helhel"));
414 EXPECT_EQ("hel", Str
);
415 EXPECT_FALSE(Str
.consume_back("hle"));
416 EXPECT_EQ("hel", Str
);
417 EXPECT_TRUE(Str
.consume_back("hel"));
419 EXPECT_FALSE(Str
.consume_back("h"));
420 EXPECT_TRUE(Str
.consume_back(""));
423 TEST(StringRefTest
, Find
) {
424 StringRef
Str("helloHELLO");
425 StringRef
LongStr("hellx xello hell ello world foo bar hello HELLO");
432 std::size_t LowerPos
;
433 } CharExpectations
[] = {
434 {Str
, 'h', 0U, 0U, 0U},
435 {Str
, 'e', 0U, 1U, 1U},
436 {Str
, 'l', 0U, 2U, 2U},
437 {Str
, 'l', 3U, 3U, 3U},
438 {Str
, 'o', 0U, 4U, 4U},
439 {Str
, 'L', 0U, 7U, 2U},
440 {Str
, 'z', 0U, StringRef::npos
, StringRef::npos
},
448 std::size_t LowerPos
;
449 } StrExpectations
[] = {
450 {Str
, "helloword", 0, StringRef::npos
, StringRef::npos
},
451 {Str
, "hello", 0, 0U, 0U},
452 {Str
, "ello", 0, 1U, 1U},
453 {Str
, "zz", 0, StringRef::npos
, StringRef::npos
},
454 {Str
, "ll", 2U, 2U, 2U},
455 {Str
, "ll", 3U, StringRef::npos
, 7U},
456 {Str
, "LL", 2U, 7U, 2U},
457 {Str
, "LL", 3U, 7U, 7U},
458 {Str
, "", 0U, 0U, 0U},
459 {LongStr
, "hello", 0U, 36U, 36U},
460 {LongStr
, "foo", 0U, 28U, 28U},
461 {LongStr
, "hell", 2U, 12U, 12U},
462 {LongStr
, "HELL", 2U, 42U, 12U},
463 {LongStr
, "", 0U, 0U, 0U}};
465 for (auto &E
: CharExpectations
) {
466 EXPECT_EQ(E
.Pos
, E
.Str
.find(E
.C
, E
.From
));
467 EXPECT_EQ(E
.LowerPos
, E
.Str
.find_lower(E
.C
, E
.From
));
468 EXPECT_EQ(E
.LowerPos
, E
.Str
.find_lower(toupper(E
.C
), E
.From
));
471 for (auto &E
: StrExpectations
) {
472 EXPECT_EQ(E
.Pos
, E
.Str
.find(E
.S
, E
.From
));
473 EXPECT_EQ(E
.LowerPos
, E
.Str
.find_lower(E
.S
, E
.From
));
474 EXPECT_EQ(E
.LowerPos
, E
.Str
.find_lower(E
.S
.upper(), E
.From
));
477 EXPECT_EQ(3U, Str
.rfind('l'));
478 EXPECT_EQ(StringRef::npos
, Str
.rfind('z'));
479 EXPECT_EQ(StringRef::npos
, Str
.rfind("helloworld"));
480 EXPECT_EQ(0U, Str
.rfind("hello"));
481 EXPECT_EQ(1U, Str
.rfind("ello"));
482 EXPECT_EQ(StringRef::npos
, Str
.rfind("zz"));
484 EXPECT_EQ(8U, Str
.rfind_lower('l'));
485 EXPECT_EQ(8U, Str
.rfind_lower('L'));
486 EXPECT_EQ(StringRef::npos
, Str
.rfind_lower('z'));
487 EXPECT_EQ(StringRef::npos
, Str
.rfind_lower("HELLOWORLD"));
488 EXPECT_EQ(5U, Str
.rfind("HELLO"));
489 EXPECT_EQ(6U, Str
.rfind("ELLO"));
490 EXPECT_EQ(StringRef::npos
, Str
.rfind("ZZ"));
492 EXPECT_EQ(2U, Str
.find_first_of('l'));
493 EXPECT_EQ(1U, Str
.find_first_of("el"));
494 EXPECT_EQ(StringRef::npos
, Str
.find_first_of("xyz"));
497 EXPECT_EQ(1U, Str
.find_first_not_of('h'));
498 EXPECT_EQ(4U, Str
.find_first_not_of("hel"));
499 EXPECT_EQ(StringRef::npos
, Str
.find_first_not_of("hello"));
501 EXPECT_EQ(3U, Str
.find_last_not_of('o'));
502 EXPECT_EQ(1U, Str
.find_last_not_of("lo"));
503 EXPECT_EQ(StringRef::npos
, Str
.find_last_not_of("helo"));
506 TEST(StringRefTest
, Count
) {
507 StringRef
Str("hello");
508 EXPECT_EQ(2U, Str
.count('l'));
509 EXPECT_EQ(1U, Str
.count('o'));
510 EXPECT_EQ(0U, Str
.count('z'));
511 EXPECT_EQ(0U, Str
.count("helloworld"));
512 EXPECT_EQ(1U, Str
.count("hello"));
513 EXPECT_EQ(1U, Str
.count("ello"));
514 EXPECT_EQ(0U, Str
.count("zz"));
517 TEST(StringRefTest
, EditDistance
) {
518 StringRef
Hello("hello");
519 EXPECT_EQ(2U, Hello
.edit_distance("hill"));
521 StringRef
Industry("industry");
522 EXPECT_EQ(6U, Industry
.edit_distance("interest"));
524 StringRef
Soylent("soylent green is people");
525 EXPECT_EQ(19U, Soylent
.edit_distance("people soiled our green"));
526 EXPECT_EQ(26U, Soylent
.edit_distance("people soiled our green",
527 /* allow replacements = */ false));
528 EXPECT_EQ(9U, Soylent
.edit_distance("people soiled our green",
529 /* allow replacements = */ true,
530 /* max edit distance = */ 8));
531 EXPECT_EQ(53U, Soylent
.edit_distance("people soiled our green "
532 "people soiled our green "
533 "people soiled our green "));
536 TEST(StringRefTest
, Misc
) {
538 raw_string_ostream
OS(Storage
);
539 OS
<< StringRef("hello");
540 EXPECT_EQ("hello", OS
.str());
543 TEST(StringRefTest
, Hashing
) {
544 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
545 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
546 std::string S
= "hello world";
547 hash_code H
= hash_value(S
);
548 EXPECT_EQ(H
, hash_value(StringRef("hello world")));
549 EXPECT_EQ(H
, hash_value(StringRef(S
)));
550 EXPECT_NE(H
, hash_value(StringRef("hello worl")));
551 EXPECT_EQ(hash_value(std::string("hello worl")),
552 hash_value(StringRef("hello worl")));
553 EXPECT_NE(H
, hash_value(StringRef("hello world ")));
554 EXPECT_EQ(hash_value(std::string("hello world ")),
555 hash_value(StringRef("hello world ")));
556 EXPECT_EQ(H
, hash_value(StringRef("hello world\0")));
557 EXPECT_NE(hash_value(std::string("ello worl")),
558 hash_value(StringRef("hello world").slice(1, -1)));
561 struct UnsignedPair
{
570 , {"4294967295", 4294967295ULL}
571 , {"4294967296", 4294967296ULL}
572 , {"18446744073709551615", 18446744073709551615ULL}
592 , {"2147483647", 2147483647LL}
593 , {"2147483648", 2147483648LL}
594 , {"-2147483648", -2147483648LL}
595 , {"-2147483649", -2147483649LL}
596 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
605 TEST(StringRefTest
, getAsInteger
) {
611 for (size_t i
= 0; i
< array_lengthof(Unsigned
); ++i
) {
612 bool U8Success
= StringRef(Unsigned
[i
].Str
).getAsInteger(0, U8
);
613 if (static_cast<uint8_t>(Unsigned
[i
].Expected
) == Unsigned
[i
].Expected
) {
614 ASSERT_FALSE(U8Success
);
615 EXPECT_EQ(U8
, Unsigned
[i
].Expected
);
617 ASSERT_TRUE(U8Success
);
619 bool U16Success
= StringRef(Unsigned
[i
].Str
).getAsInteger(0, U16
);
620 if (static_cast<uint16_t>(Unsigned
[i
].Expected
) == Unsigned
[i
].Expected
) {
621 ASSERT_FALSE(U16Success
);
622 EXPECT_EQ(U16
, Unsigned
[i
].Expected
);
624 ASSERT_TRUE(U16Success
);
626 bool U32Success
= StringRef(Unsigned
[i
].Str
).getAsInteger(0, U32
);
627 if (static_cast<uint32_t>(Unsigned
[i
].Expected
) == Unsigned
[i
].Expected
) {
628 ASSERT_FALSE(U32Success
);
629 EXPECT_EQ(U32
, Unsigned
[i
].Expected
);
631 ASSERT_TRUE(U32Success
);
633 bool U64Success
= StringRef(Unsigned
[i
].Str
).getAsInteger(0, U64
);
634 if (static_cast<uint64_t>(Unsigned
[i
].Expected
) == Unsigned
[i
].Expected
) {
635 ASSERT_FALSE(U64Success
);
636 EXPECT_EQ(U64
, Unsigned
[i
].Expected
);
638 ASSERT_TRUE(U64Success
);
647 for (size_t i
= 0; i
< array_lengthof(Signed
); ++i
) {
648 bool S8Success
= StringRef(Signed
[i
].Str
).getAsInteger(0, S8
);
649 if (static_cast<int8_t>(Signed
[i
].Expected
) == Signed
[i
].Expected
) {
650 ASSERT_FALSE(S8Success
);
651 EXPECT_EQ(S8
, Signed
[i
].Expected
);
653 ASSERT_TRUE(S8Success
);
655 bool S16Success
= StringRef(Signed
[i
].Str
).getAsInteger(0, S16
);
656 if (static_cast<int16_t>(Signed
[i
].Expected
) == Signed
[i
].Expected
) {
657 ASSERT_FALSE(S16Success
);
658 EXPECT_EQ(S16
, Signed
[i
].Expected
);
660 ASSERT_TRUE(S16Success
);
662 bool S32Success
= StringRef(Signed
[i
].Str
).getAsInteger(0, S32
);
663 if (static_cast<int32_t>(Signed
[i
].Expected
) == Signed
[i
].Expected
) {
664 ASSERT_FALSE(S32Success
);
665 EXPECT_EQ(S32
, Signed
[i
].Expected
);
667 ASSERT_TRUE(S32Success
);
669 bool S64Success
= StringRef(Signed
[i
].Str
).getAsInteger(0, S64
);
670 if (static_cast<int64_t>(Signed
[i
].Expected
) == Signed
[i
].Expected
) {
671 ASSERT_FALSE(S64Success
);
672 EXPECT_EQ(S64
, Signed
[i
].Expected
);
674 ASSERT_TRUE(S64Success
);
680 static const char* BadStrings
[] = {
682 , "18446744073709551617" // value just over max
683 , "123456789012345678901" // value way too large
684 , "4t23v" // illegal decimal characters
685 , "0x123W56" // illegal hex characters
686 , "0b2" // illegal bin characters
687 , "08" // illegal oct characters
688 , "0o8" // illegal oct characters
689 , "-123" // negative unsigned value
695 TEST(StringRefTest
, getAsUnsignedIntegerBadStrings
) {
696 unsigned long long U64
;
697 for (size_t i
= 0; i
< array_lengthof(BadStrings
); ++i
) {
698 bool IsBadNumber
= StringRef(BadStrings
[i
]).getAsInteger(0, U64
);
699 ASSERT_TRUE(IsBadNumber
);
703 struct ConsumeUnsignedPair
{
706 const char *Leftover
;
707 } ConsumeUnsigned
[] = {
711 {"65535", 65535, ""},
712 {"65536", 65536, ""},
713 {"4294967295", 4294967295ULL, ""},
714 {"4294967296", 4294967296ULL, ""},
715 {"255A376", 255, "A376"},
716 {"18446744073709551615", 18446744073709551615ULL, ""},
717 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
720 {"0x42-0x34", 66, "-0x34"},
721 {"0b101010", 42, ""},
722 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit
723 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit
724 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
726 struct ConsumeSignedPair
{
729 const char *Leftover
;
730 } ConsumeSigned
[] = {
737 {"127-1", 127, "-1"},
738 {"128-1", 128, "-1"},
741 {"-128-1", -128, "-1"},
742 {"-129-1", -129, "-1"},
743 {"32767", 32767, ""},
744 {"32768", 32768, ""},
745 {"32767-1", 32767, "-1"},
746 {"32768-1", 32768, "-1"},
747 {"-32768", -32768, ""},
748 {"-32769", -32769, ""},
749 {"-32768-1", -32768, "-1"},
750 {"-32769-1", -32769, "-1"},
751 {"2147483647", 2147483647LL, ""},
752 {"2147483648", 2147483648LL, ""},
753 {"2147483647-1", 2147483647LL, "-1"},
754 {"2147483648-1", 2147483648LL, "-1"},
755 {"-2147483648", -2147483648LL, ""},
756 {"-2147483649", -2147483649LL, ""},
757 {"-2147483648-1", -2147483648LL, "-1"},
758 {"-2147483649-1", -2147483649LL, "-1"},
759 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
760 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
764 {"0x42-1", 66, "-1"},
765 {"0b101010", 42, ""},
766 {"0b101010-1", 42, "-1"},
768 {"-042-1", -34, "-1"},
770 {"-0x42-1", -66, "-1"},
771 {"-0b101010", -42, ""},
772 {"-0b101010-1", -42, "-1"}};
774 TEST(StringRefTest
, consumeIntegerUnsigned
) {
780 for (size_t i
= 0; i
< array_lengthof(ConsumeUnsigned
); ++i
) {
781 StringRef Str
= ConsumeUnsigned
[i
].Str
;
782 bool U8Success
= Str
.consumeInteger(0, U8
);
783 if (static_cast<uint8_t>(ConsumeUnsigned
[i
].Expected
) ==
784 ConsumeUnsigned
[i
].Expected
) {
785 ASSERT_FALSE(U8Success
);
786 EXPECT_EQ(U8
, ConsumeUnsigned
[i
].Expected
);
787 EXPECT_EQ(Str
, ConsumeUnsigned
[i
].Leftover
);
789 ASSERT_TRUE(U8Success
);
792 Str
= ConsumeUnsigned
[i
].Str
;
793 bool U16Success
= Str
.consumeInteger(0, U16
);
794 if (static_cast<uint16_t>(ConsumeUnsigned
[i
].Expected
) ==
795 ConsumeUnsigned
[i
].Expected
) {
796 ASSERT_FALSE(U16Success
);
797 EXPECT_EQ(U16
, ConsumeUnsigned
[i
].Expected
);
798 EXPECT_EQ(Str
, ConsumeUnsigned
[i
].Leftover
);
800 ASSERT_TRUE(U16Success
);
803 Str
= ConsumeUnsigned
[i
].Str
;
804 bool U32Success
= Str
.consumeInteger(0, U32
);
805 if (static_cast<uint32_t>(ConsumeUnsigned
[i
].Expected
) ==
806 ConsumeUnsigned
[i
].Expected
) {
807 ASSERT_FALSE(U32Success
);
808 EXPECT_EQ(U32
, ConsumeUnsigned
[i
].Expected
);
809 EXPECT_EQ(Str
, ConsumeUnsigned
[i
].Leftover
);
811 ASSERT_TRUE(U32Success
);
814 Str
= ConsumeUnsigned
[i
].Str
;
815 bool U64Success
= Str
.consumeInteger(0, U64
);
816 if (static_cast<uint64_t>(ConsumeUnsigned
[i
].Expected
) ==
817 ConsumeUnsigned
[i
].Expected
) {
818 ASSERT_FALSE(U64Success
);
819 EXPECT_EQ(U64
, ConsumeUnsigned
[i
].Expected
);
820 EXPECT_EQ(Str
, ConsumeUnsigned
[i
].Leftover
);
822 ASSERT_TRUE(U64Success
);
827 TEST(StringRefTest
, consumeIntegerSigned
) {
833 for (size_t i
= 0; i
< array_lengthof(ConsumeSigned
); ++i
) {
834 StringRef Str
= ConsumeSigned
[i
].Str
;
835 bool S8Success
= Str
.consumeInteger(0, S8
);
836 if (static_cast<int8_t>(ConsumeSigned
[i
].Expected
) ==
837 ConsumeSigned
[i
].Expected
) {
838 ASSERT_FALSE(S8Success
);
839 EXPECT_EQ(S8
, ConsumeSigned
[i
].Expected
);
840 EXPECT_EQ(Str
, ConsumeSigned
[i
].Leftover
);
842 ASSERT_TRUE(S8Success
);
845 Str
= ConsumeSigned
[i
].Str
;
846 bool S16Success
= Str
.consumeInteger(0, S16
);
847 if (static_cast<int16_t>(ConsumeSigned
[i
].Expected
) ==
848 ConsumeSigned
[i
].Expected
) {
849 ASSERT_FALSE(S16Success
);
850 EXPECT_EQ(S16
, ConsumeSigned
[i
].Expected
);
851 EXPECT_EQ(Str
, ConsumeSigned
[i
].Leftover
);
853 ASSERT_TRUE(S16Success
);
856 Str
= ConsumeSigned
[i
].Str
;
857 bool S32Success
= Str
.consumeInteger(0, S32
);
858 if (static_cast<int32_t>(ConsumeSigned
[i
].Expected
) ==
859 ConsumeSigned
[i
].Expected
) {
860 ASSERT_FALSE(S32Success
);
861 EXPECT_EQ(S32
, ConsumeSigned
[i
].Expected
);
862 EXPECT_EQ(Str
, ConsumeSigned
[i
].Leftover
);
864 ASSERT_TRUE(S32Success
);
867 Str
= ConsumeSigned
[i
].Str
;
868 bool S64Success
= Str
.consumeInteger(0, S64
);
869 if (static_cast<int64_t>(ConsumeSigned
[i
].Expected
) ==
870 ConsumeSigned
[i
].Expected
) {
871 ASSERT_FALSE(S64Success
);
872 EXPECT_EQ(S64
, ConsumeSigned
[i
].Expected
);
873 EXPECT_EQ(Str
, ConsumeSigned
[i
].Leftover
);
875 ASSERT_TRUE(S64Success
);
880 struct GetDoubleStrings
{
885 } DoubleStrings
[] = {{"0", false, false, 0.0},
886 {"0.0", false, false, 0.0},
887 {"-0.0", false, false, -0.0},
888 {"123.45", false, true, 123.45},
889 {"123.45", true, false, 123.45},
890 {"1.8e308", true, false, std::numeric_limits
<double>::infinity()},
891 {"1.8e308", false, true, std::numeric_limits
<double>::infinity()},
892 {"0x0.0000000000001P-1023", false, true, 0.0},
893 {"0x0.0000000000001P-1023", true, false, 0.0},
896 TEST(StringRefTest
, getAsDouble
) {
897 for (const auto &Entry
: DoubleStrings
) {
899 StringRef
S(Entry
.Str
);
900 EXPECT_EQ(Entry
.ShouldFail
, S
.getAsDouble(Result
, Entry
.AllowInexact
));
901 if (!Entry
.ShouldFail
) {
902 EXPECT_EQ(Result
, Entry
.D
);
907 static const char *join_input
[] = { "a", "b", "c" };
908 static const char join_result1
[] = "a";
909 static const char join_result2
[] = "a:b:c";
910 static const char join_result3
[] = "a::b::c";
912 TEST(StringRefTest
, joinStrings
) {
913 std::vector
<StringRef
> v1
;
914 std::vector
<std::string
> v2
;
915 for (size_t i
= 0; i
< array_lengthof(join_input
); ++i
) {
916 v1
.push_back(join_input
[i
]);
917 v2
.push_back(join_input
[i
]);
920 bool v1_join1
= join(v1
.begin(), v1
.begin() + 1, ":") == join_result1
;
921 EXPECT_TRUE(v1_join1
);
922 bool v1_join2
= join(v1
.begin(), v1
.end(), ":") == join_result2
;
923 EXPECT_TRUE(v1_join2
);
924 bool v1_join3
= join(v1
.begin(), v1
.end(), "::") == join_result3
;
925 EXPECT_TRUE(v1_join3
);
927 bool v2_join1
= join(v2
.begin(), v2
.begin() + 1, ":") == join_result1
;
928 EXPECT_TRUE(v2_join1
);
929 bool v2_join2
= join(v2
.begin(), v2
.end(), ":") == join_result2
;
930 EXPECT_TRUE(v2_join2
);
931 bool v2_join3
= join(v2
.begin(), v2
.end(), "::") == join_result3
;
932 EXPECT_TRUE(v2_join3
);
933 v2_join3
= join(v2
, "::") == join_result3
;
934 EXPECT_TRUE(v2_join3
);
938 TEST(StringRefTest
, AllocatorCopy
) {
939 BumpPtrAllocator Alloc
;
940 // First test empty strings. We don't want these to allocate anything on the
942 StringRef StrEmpty
= "";
943 StringRef StrEmptyc
= StrEmpty
.copy(Alloc
);
944 EXPECT_TRUE(StrEmpty
.equals(StrEmptyc
));
945 EXPECT_EQ(StrEmptyc
.data(), nullptr);
946 EXPECT_EQ(StrEmptyc
.size(), 0u);
947 EXPECT_EQ(Alloc
.getTotalMemory(), 0u);
949 StringRef Str1
= "hello";
950 StringRef Str2
= "bye";
951 StringRef Str1c
= Str1
.copy(Alloc
);
952 StringRef Str2c
= Str2
.copy(Alloc
);
953 EXPECT_TRUE(Str1
.equals(Str1c
));
954 EXPECT_NE(Str1
.data(), Str1c
.data());
955 EXPECT_TRUE(Str2
.equals(Str2c
));
956 EXPECT_NE(Str2
.data(), Str2c
.data());
959 TEST(StringRefTest
, Drop
) {
960 StringRef
Test("StringRefTest::Drop");
962 StringRef Dropped
= Test
.drop_front(5);
963 EXPECT_EQ(Dropped
, "gRefTest::Drop");
965 Dropped
= Test
.drop_back(5);
966 EXPECT_EQ(Dropped
, "StringRefTest:");
968 Dropped
= Test
.drop_front(0);
969 EXPECT_EQ(Dropped
, Test
);
971 Dropped
= Test
.drop_back(0);
972 EXPECT_EQ(Dropped
, Test
);
974 Dropped
= Test
.drop_front(Test
.size());
975 EXPECT_TRUE(Dropped
.empty());
977 Dropped
= Test
.drop_back(Test
.size());
978 EXPECT_TRUE(Dropped
.empty());
981 TEST(StringRefTest
, Take
) {
982 StringRef
Test("StringRefTest::Take");
984 StringRef Taken
= Test
.take_front(5);
985 EXPECT_EQ(Taken
, "Strin");
987 Taken
= Test
.take_back(5);
988 EXPECT_EQ(Taken
, ":Take");
990 Taken
= Test
.take_front(Test
.size());
991 EXPECT_EQ(Taken
, Test
);
993 Taken
= Test
.take_back(Test
.size());
994 EXPECT_EQ(Taken
, Test
);
996 Taken
= Test
.take_front(0);
997 EXPECT_TRUE(Taken
.empty());
999 Taken
= Test
.take_back(0);
1000 EXPECT_TRUE(Taken
.empty());
1003 TEST(StringRefTest
, FindIf
) {
1004 StringRef
Punct("Test.String");
1005 StringRef
NoPunct("ABCDEFG");
1008 auto IsPunct
= [](char c
) { return ::ispunct(c
); };
1009 auto IsAlpha
= [](char c
) { return ::isalpha(c
); };
1010 EXPECT_EQ(4U, Punct
.find_if(IsPunct
));
1011 EXPECT_EQ(StringRef::npos
, NoPunct
.find_if(IsPunct
));
1012 EXPECT_EQ(StringRef::npos
, Empty
.find_if(IsPunct
));
1014 EXPECT_EQ(4U, Punct
.find_if_not(IsAlpha
));
1015 EXPECT_EQ(StringRef::npos
, NoPunct
.find_if_not(IsAlpha
));
1016 EXPECT_EQ(StringRef::npos
, Empty
.find_if_not(IsAlpha
));
1019 TEST(StringRefTest
, TakeWhileUntil
) {
1020 StringRef
Test("String With 1 Number");
1022 StringRef Taken
= Test
.take_while([](char c
) { return ::isdigit(c
); });
1023 EXPECT_EQ("", Taken
);
1025 Taken
= Test
.take_until([](char c
) { return ::isdigit(c
); });
1026 EXPECT_EQ("String With ", Taken
);
1028 Taken
= Test
.take_while([](char c
) { return true; });
1029 EXPECT_EQ(Test
, Taken
);
1031 Taken
= Test
.take_until([](char c
) { return true; });
1032 EXPECT_EQ("", Taken
);
1035 Taken
= Test
.take_while([](char c
) { return true; });
1036 EXPECT_EQ("", Taken
);
1039 TEST(StringRefTest
, DropWhileUntil
) {
1040 StringRef
Test("String With 1 Number");
1042 StringRef Taken
= Test
.drop_while([](char c
) { return ::isdigit(c
); });
1043 EXPECT_EQ(Test
, Taken
);
1045 Taken
= Test
.drop_until([](char c
) { return ::isdigit(c
); });
1046 EXPECT_EQ("1 Number", Taken
);
1048 Taken
= Test
.drop_while([](char c
) { return true; });
1049 EXPECT_EQ("", Taken
);
1051 Taken
= Test
.drop_until([](char c
) { return true; });
1052 EXPECT_EQ(Test
, Taken
);
1054 StringRef EmptyString
= "";
1055 Taken
= EmptyString
.drop_while([](char c
) { return true; });
1056 EXPECT_EQ("", Taken
);
1059 TEST(StringRefTest
, StringLiteral
) {
1060 constexpr StringLiteral Strings
[] = {"Foo", "Bar"};
1061 EXPECT_EQ(StringRef("Foo"), Strings
[0]);
1062 EXPECT_EQ(StringRef("Bar"), Strings
[1]);
1065 } // end anonymous namespace