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 << ")";
36 TEST(StringRefTest, Construction) {
37 EXPECT_EQ("", StringRef());
38 EXPECT_EQ("hello", StringRef("hello"));
39 EXPECT_EQ("hello", StringRef("hello world", 5));
40 EXPECT_EQ("hello", StringRef(std::string("hello")));
43 TEST(StringRefTest, Iteration) {
45 const char *p = "hello";
46 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
50 TEST(StringRefTest, StringOps) {
51 const char *p = "hello";
52 EXPECT_EQ(p, StringRef(p, 0).data());
53 EXPECT_TRUE(StringRef().empty());
54 EXPECT_EQ((size_t) 5, StringRef("hello").size());
55 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
56 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
57 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
58 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
59 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
60 EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
62 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
63 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
64 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
65 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
66 EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
67 EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
68 EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
69 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
70 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
72 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
73 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
74 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
75 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
76 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
77 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
78 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
79 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
80 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
81 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
82 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
83 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
84 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
85 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
86 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
87 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
88 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
91 TEST(StringRefTest, Operators) {
92 EXPECT_EQ("", StringRef());
93 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
94 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
95 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
96 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
97 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
98 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
99 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
100 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
101 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
102 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
103 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
104 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
105 EXPECT_EQ('a', StringRef("aab")[1]);
108 TEST(StringRefTest, Substr) {
109 StringRef Str("hello");
110 EXPECT_EQ("lo", Str.substr(3));
111 EXPECT_EQ("", Str.substr(100));
112 EXPECT_EQ("hello", Str.substr(0, 100));
113 EXPECT_EQ("o", Str.substr(4, 10));
116 TEST(StringRefTest, Slice) {
117 StringRef Str("hello");
118 EXPECT_EQ("l", Str.slice(2, 3));
119 EXPECT_EQ("ell", Str.slice(1, 4));
120 EXPECT_EQ("llo", Str.slice(2, 100));
121 EXPECT_EQ("", Str.slice(2, 1));
122 EXPECT_EQ("", Str.slice(10, 20));
125 TEST(StringRefTest, Split) {
126 StringRef Str("hello");
127 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
129 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
131 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
133 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
135 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
138 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
140 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
142 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
144 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
146 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
150 TEST(StringRefTest, Split2) {
151 SmallVector<StringRef, 5> parts;
152 SmallVector<StringRef, 5> expected;
154 expected.push_back("ab"); expected.push_back("c");
155 StringRef(",ab,,c,").split(parts, ",", -1, false);
156 EXPECT_TRUE(parts == expected);
158 expected.clear(); parts.clear();
159 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
160 expected.push_back("c"); expected.push_back("");
161 StringRef(",ab,,c,").split(parts, ",", -1, true);
162 EXPECT_TRUE(parts == expected);
164 expected.clear(); parts.clear();
165 expected.push_back("");
166 StringRef("").split(parts, ",", -1, true);
167 EXPECT_TRUE(parts == expected);
169 expected.clear(); parts.clear();
170 StringRef("").split(parts, ",", -1, false);
171 EXPECT_TRUE(parts == expected);
173 expected.clear(); parts.clear();
174 StringRef(",").split(parts, ",", -1, false);
175 EXPECT_TRUE(parts == expected);
177 expected.clear(); parts.clear();
178 expected.push_back(""); expected.push_back("");
179 StringRef(",").split(parts, ",", -1, true);
180 EXPECT_TRUE(parts == expected);
182 expected.clear(); parts.clear();
183 expected.push_back("a"); expected.push_back("b");
184 StringRef("a,b").split(parts, ",", -1, true);
185 EXPECT_TRUE(parts == expected);
188 expected.clear(); parts.clear();
189 expected.push_back("a,,b,c");
190 StringRef("a,,b,c").split(parts, ",", 0, true);
191 EXPECT_TRUE(parts == expected);
193 expected.clear(); parts.clear();
194 expected.push_back("a,,b,c");
195 StringRef("a,,b,c").split(parts, ",", 0, false);
196 EXPECT_TRUE(parts == expected);
198 expected.clear(); parts.clear();
199 expected.push_back("a"); expected.push_back(",b,c");
200 StringRef("a,,b,c").split(parts, ",", 1, true);
201 EXPECT_TRUE(parts == expected);
203 expected.clear(); parts.clear();
204 expected.push_back("a"); expected.push_back(",b,c");
205 StringRef("a,,b,c").split(parts, ",", 1, false);
206 EXPECT_TRUE(parts == expected);
208 expected.clear(); parts.clear();
209 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
210 StringRef("a,,b,c").split(parts, ",", 2, true);
211 EXPECT_TRUE(parts == expected);
213 expected.clear(); parts.clear();
214 expected.push_back("a"); expected.push_back("b,c");
215 StringRef("a,,b,c").split(parts, ",", 2, false);
216 EXPECT_TRUE(parts == expected);
218 expected.clear(); parts.clear();
219 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
220 expected.push_back("c");
221 StringRef("a,,b,c").split(parts, ",", 3, true);
222 EXPECT_TRUE(parts == expected);
224 expected.clear(); parts.clear();
225 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
226 StringRef("a,,b,c").split(parts, ",", 3, false);
227 EXPECT_TRUE(parts == expected);
229 expected.clear(); parts.clear();
230 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
231 StringRef("a,,b,c").split(parts, ',', 3, false);
232 EXPECT_TRUE(parts == expected);
235 TEST(StringRefTest, Trim) {
236 StringRef Str0("hello");
237 StringRef Str1(" hello ");
238 StringRef Str2(" hello ");
240 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
241 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
242 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
243 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
244 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
245 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
246 EXPECT_EQ(StringRef("hello"), Str0.trim());
247 EXPECT_EQ(StringRef("hello"), Str1.trim());
248 EXPECT_EQ(StringRef("hello"), Str2.trim());
250 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
252 EXPECT_EQ(StringRef(""), StringRef("").trim());
253 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
254 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
255 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
256 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
259 TEST(StringRefTest, StartsWith) {
260 StringRef Str("hello");
261 EXPECT_TRUE(Str.startswith(""));
262 EXPECT_TRUE(Str.startswith("he"));
263 EXPECT_FALSE(Str.startswith("helloworld"));
264 EXPECT_FALSE(Str.startswith("hi"));
267 TEST(StringRefTest, StartsWithLower) {
268 StringRef Str("heLLo");
269 EXPECT_TRUE(Str.startswith_lower(""));
270 EXPECT_TRUE(Str.startswith_lower("he"));
271 EXPECT_TRUE(Str.startswith_lower("hell"));
272 EXPECT_TRUE(Str.startswith_lower("HELlo"));
273 EXPECT_FALSE(Str.startswith_lower("helloworld"));
274 EXPECT_FALSE(Str.startswith_lower("hi"));
277 TEST(StringRefTest, EndsWith) {
278 StringRef Str("hello");
279 EXPECT_TRUE(Str.endswith(""));
280 EXPECT_TRUE(Str.endswith("lo"));
281 EXPECT_FALSE(Str.endswith("helloworld"));
282 EXPECT_FALSE(Str.endswith("worldhello"));
283 EXPECT_FALSE(Str.endswith("so"));
286 TEST(StringRefTest, EndsWithLower) {
287 StringRef Str("heLLo");
288 EXPECT_TRUE(Str.endswith_lower(""));
289 EXPECT_TRUE(Str.endswith_lower("lo"));
290 EXPECT_TRUE(Str.endswith_lower("LO"));
291 EXPECT_TRUE(Str.endswith_lower("ELlo"));
292 EXPECT_FALSE(Str.endswith_lower("helloworld"));
293 EXPECT_FALSE(Str.endswith_lower("hi"));
296 TEST(StringRefTest, Find) {
297 StringRef Str("hello");
298 EXPECT_EQ(2U, Str.find('l'));
299 EXPECT_EQ(StringRef::npos, Str.find('z'));
300 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
301 EXPECT_EQ(0U, Str.find("hello"));
302 EXPECT_EQ(1U, Str.find("ello"));
303 EXPECT_EQ(StringRef::npos, Str.find("zz"));
304 EXPECT_EQ(2U, Str.find("ll", 2));
305 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
306 EXPECT_EQ(0U, Str.find(""));
307 StringRef LongStr("hellx xello hell ello world foo bar hello");
308 EXPECT_EQ(36U, LongStr.find("hello"));
309 EXPECT_EQ(28U, LongStr.find("foo"));
310 EXPECT_EQ(12U, LongStr.find("hell", 2));
311 EXPECT_EQ(0U, LongStr.find(""));
313 EXPECT_EQ(3U, Str.rfind('l'));
314 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
315 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
316 EXPECT_EQ(0U, Str.rfind("hello"));
317 EXPECT_EQ(1U, Str.rfind("ello"));
318 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
320 EXPECT_EQ(2U, Str.find_first_of('l'));
321 EXPECT_EQ(1U, Str.find_first_of("el"));
322 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
324 EXPECT_EQ(1U, Str.find_first_not_of('h'));
325 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
326 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
328 EXPECT_EQ(3U, Str.find_last_not_of('o'));
329 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
330 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
333 TEST(StringRefTest, Count) {
334 StringRef Str("hello");
335 EXPECT_EQ(2U, Str.count('l'));
336 EXPECT_EQ(1U, Str.count('o'));
337 EXPECT_EQ(0U, Str.count('z'));
338 EXPECT_EQ(0U, Str.count("helloworld"));
339 EXPECT_EQ(1U, Str.count("hello"));
340 EXPECT_EQ(1U, Str.count("ello"));
341 EXPECT_EQ(0U, Str.count("zz"));
344 TEST(StringRefTest, EditDistance) {
345 StringRef Str("hello");
346 EXPECT_EQ(2U, Str.edit_distance("hill"));
349 TEST(StringRefTest, Misc) {
351 raw_string_ostream OS(Storage);
352 OS << StringRef("hello");
353 EXPECT_EQ("hello", OS.str());
356 TEST(StringRefTest, Hashing) {
357 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
358 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
359 std::string S = "hello world";
360 hash_code H = hash_value(S);
361 EXPECT_EQ(H, hash_value(StringRef("hello world")));
362 EXPECT_EQ(H, hash_value(StringRef(S)));
363 EXPECT_NE(H, hash_value(StringRef("hello worl")));
364 EXPECT_EQ(hash_value(std::string("hello worl")),
365 hash_value(StringRef("hello worl")));
366 EXPECT_NE(H, hash_value(StringRef("hello world ")));
367 EXPECT_EQ(hash_value(std::string("hello world ")),
368 hash_value(StringRef("hello world ")));
369 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
370 EXPECT_NE(hash_value(std::string("ello worl")),
371 hash_value(StringRef("hello world").slice(1, -1)));
374 struct UnsignedPair {
383 , {"4294967295", 4294967295ULL}
384 , {"4294967296", 4294967296ULL}
385 , {"18446744073709551615", 18446744073709551615ULL}
405 , {"2147483647", 2147483647LL}
406 , {"2147483648", 2147483648LL}
407 , {"-2147483648", -2147483648LL}
408 , {"-2147483649", -2147483649LL}
409 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
418 TEST(StringRefTest, getAsInteger) {
424 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
425 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
426 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
427 ASSERT_FALSE(U8Success);
428 EXPECT_EQ(U8, Unsigned[i].Expected);
430 ASSERT_TRUE(U8Success);
432 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
433 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
434 ASSERT_FALSE(U16Success);
435 EXPECT_EQ(U16, Unsigned[i].Expected);
437 ASSERT_TRUE(U16Success);
439 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
440 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
441 ASSERT_FALSE(U32Success);
442 EXPECT_EQ(U32, Unsigned[i].Expected);
444 ASSERT_TRUE(U32Success);
446 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
447 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
448 ASSERT_FALSE(U64Success);
449 EXPECT_EQ(U64, Unsigned[i].Expected);
451 ASSERT_TRUE(U64Success);
460 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
461 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
462 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
463 ASSERT_FALSE(S8Success);
464 EXPECT_EQ(S8, Signed[i].Expected);
466 ASSERT_TRUE(S8Success);
468 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
469 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
470 ASSERT_FALSE(S16Success);
471 EXPECT_EQ(S16, Signed[i].Expected);
473 ASSERT_TRUE(S16Success);
475 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
476 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
477 ASSERT_FALSE(S32Success);
478 EXPECT_EQ(S32, Signed[i].Expected);
480 ASSERT_TRUE(S32Success);
482 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
483 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
484 ASSERT_FALSE(S64Success);
485 EXPECT_EQ(S64, Signed[i].Expected);
487 ASSERT_TRUE(S64Success);
493 static const char* BadStrings[] = {
494 "18446744073709551617" // value just over max
495 , "123456789012345678901" // value way too large
496 , "4t23v" // illegal decimal characters
497 , "0x123W56" // illegal hex characters
498 , "0b2" // illegal bin characters
499 , "08" // illegal oct characters
500 , "0o8" // illegal oct characters
501 , "-123" // negative unsigned value
505 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
506 unsigned long long U64;
507 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
508 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
509 ASSERT_TRUE(IsBadNumber);
513 static const char *join_input[] = { "a", "b", "c" };
514 static const char join_result1[] = "a";
515 static const char join_result2[] = "a:b:c";
516 static const char join_result3[] = "a::b::c";
518 TEST(StringRefTest, joinStrings) {
519 std::vector<StringRef> v1;
520 std::vector<std::string> v2;
521 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
522 v1.push_back(join_input[i]);
523 v2.push_back(join_input[i]);
526 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
527 EXPECT_TRUE(v1_join1);
528 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
529 EXPECT_TRUE(v1_join2);
530 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
531 EXPECT_TRUE(v1_join3);
533 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
534 EXPECT_TRUE(v2_join1);
535 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
536 EXPECT_TRUE(v2_join2);
537 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
538 EXPECT_TRUE(v2_join3);
542 TEST(StringRefTest, AllocatorCopy) {
543 BumpPtrAllocator Alloc;
544 StringRef Str1 = "hello";
545 StringRef Str2 = "bye";
546 StringRef Str1c = Str1.copy(Alloc);
547 StringRef Str2c = Str2.copy(Alloc);
548 EXPECT_TRUE(Str1.equals(Str1c));
549 EXPECT_NE(Str1.data(), Str1c.data());
550 EXPECT_TRUE(Str2.equals(Str2c));
551 EXPECT_NE(Str2.data(), Str2c.data());
555 } // end anonymous namespace