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/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 << ")";
35 TEST(StringRefTest, Construction) {
36 EXPECT_EQ("", StringRef());
37 EXPECT_EQ("hello", StringRef("hello"));
38 EXPECT_EQ("hello", StringRef("hello world", 5));
39 EXPECT_EQ("hello", StringRef(std::string("hello")));
42 TEST(StringRefTest, Iteration) {
44 const char *p = "hello";
45 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
49 TEST(StringRefTest, StringOps) {
50 const char *p = "hello";
51 EXPECT_EQ(p, StringRef(p, 0).data());
52 EXPECT_TRUE(StringRef().empty());
53 EXPECT_EQ((size_t) 5, StringRef("hello").size());
54 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
55 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
56 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
57 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
58 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
59 EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
61 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
62 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
63 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
64 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
65 EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
66 EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
67 EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
68 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
69 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
71 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
72 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
73 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
74 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
75 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
76 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
77 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
78 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
79 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
80 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
81 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
82 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
83 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
84 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
85 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
86 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
87 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
90 TEST(StringRefTest, Operators) {
91 EXPECT_EQ("", StringRef());
92 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
93 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
94 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
95 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
96 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
97 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
98 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
99 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
100 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
101 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
102 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
103 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
104 EXPECT_EQ('a', StringRef("aab")[1]);
107 TEST(StringRefTest, Substr) {
108 StringRef Str("hello");
109 EXPECT_EQ("lo", Str.substr(3));
110 EXPECT_EQ("", Str.substr(100));
111 EXPECT_EQ("hello", Str.substr(0, 100));
112 EXPECT_EQ("o", Str.substr(4, 10));
115 TEST(StringRefTest, Slice) {
116 StringRef Str("hello");
117 EXPECT_EQ("l", Str.slice(2, 3));
118 EXPECT_EQ("ell", Str.slice(1, 4));
119 EXPECT_EQ("llo", Str.slice(2, 100));
120 EXPECT_EQ("", Str.slice(2, 1));
121 EXPECT_EQ("", Str.slice(10, 20));
124 TEST(StringRefTest, Split) {
125 StringRef Str("hello");
126 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
128 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
130 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
132 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
134 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
137 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
139 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
141 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
143 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
145 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
149 TEST(StringRefTest, Split2) {
150 SmallVector<StringRef, 5> parts;
151 SmallVector<StringRef, 5> expected;
153 expected.push_back("ab"); expected.push_back("c");
154 StringRef(",ab,,c,").split(parts, ",", -1, false);
155 EXPECT_TRUE(parts == expected);
157 expected.clear(); parts.clear();
158 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
159 expected.push_back("c"); expected.push_back("");
160 StringRef(",ab,,c,").split(parts, ",", -1, true);
161 EXPECT_TRUE(parts == expected);
163 expected.clear(); parts.clear();
164 expected.push_back("");
165 StringRef("").split(parts, ",", -1, true);
166 EXPECT_TRUE(parts == expected);
168 expected.clear(); parts.clear();
169 StringRef("").split(parts, ",", -1, false);
170 EXPECT_TRUE(parts == expected);
172 expected.clear(); parts.clear();
173 StringRef(",").split(parts, ",", -1, false);
174 EXPECT_TRUE(parts == expected);
176 expected.clear(); parts.clear();
177 expected.push_back(""); expected.push_back("");
178 StringRef(",").split(parts, ",", -1, true);
179 EXPECT_TRUE(parts == expected);
181 expected.clear(); parts.clear();
182 expected.push_back("a"); expected.push_back("b");
183 StringRef("a,b").split(parts, ",", -1, true);
184 EXPECT_TRUE(parts == expected);
187 expected.clear(); parts.clear();
188 expected.push_back("a,,b,c");
189 StringRef("a,,b,c").split(parts, ",", 0, true);
190 EXPECT_TRUE(parts == expected);
192 expected.clear(); parts.clear();
193 expected.push_back("a,,b,c");
194 StringRef("a,,b,c").split(parts, ",", 0, false);
195 EXPECT_TRUE(parts == expected);
197 expected.clear(); parts.clear();
198 expected.push_back("a"); expected.push_back(",b,c");
199 StringRef("a,,b,c").split(parts, ",", 1, true);
200 EXPECT_TRUE(parts == expected);
202 expected.clear(); parts.clear();
203 expected.push_back("a"); expected.push_back(",b,c");
204 StringRef("a,,b,c").split(parts, ",", 1, false);
205 EXPECT_TRUE(parts == expected);
207 expected.clear(); parts.clear();
208 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
209 StringRef("a,,b,c").split(parts, ",", 2, true);
210 EXPECT_TRUE(parts == expected);
212 expected.clear(); parts.clear();
213 expected.push_back("a"); expected.push_back("b,c");
214 StringRef("a,,b,c").split(parts, ",", 2, false);
215 EXPECT_TRUE(parts == expected);
217 expected.clear(); parts.clear();
218 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
219 expected.push_back("c");
220 StringRef("a,,b,c").split(parts, ",", 3, true);
221 EXPECT_TRUE(parts == expected);
223 expected.clear(); parts.clear();
224 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
225 StringRef("a,,b,c").split(parts, ",", 3, false);
226 EXPECT_TRUE(parts == expected);
229 TEST(StringRefTest, Trim) {
230 StringRef Str0("hello");
231 StringRef Str1(" hello ");
232 StringRef Str2(" hello ");
234 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
235 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
236 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
237 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
238 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
239 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
240 EXPECT_EQ(StringRef("hello"), Str0.trim());
241 EXPECT_EQ(StringRef("hello"), Str1.trim());
242 EXPECT_EQ(StringRef("hello"), Str2.trim());
244 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
246 EXPECT_EQ(StringRef(""), StringRef("").trim());
247 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
248 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
249 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
250 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
253 TEST(StringRefTest, StartsWith) {
254 StringRef Str("hello");
255 EXPECT_TRUE(Str.startswith(""));
256 EXPECT_TRUE(Str.startswith("he"));
257 EXPECT_FALSE(Str.startswith("helloworld"));
258 EXPECT_FALSE(Str.startswith("hi"));
261 TEST(StringRefTest, StartsWithLower) {
262 StringRef Str("heLLo");
263 EXPECT_TRUE(Str.startswith_lower(""));
264 EXPECT_TRUE(Str.startswith_lower("he"));
265 EXPECT_TRUE(Str.startswith_lower("hell"));
266 EXPECT_TRUE(Str.startswith_lower("HELlo"));
267 EXPECT_FALSE(Str.startswith_lower("helloworld"));
268 EXPECT_FALSE(Str.startswith_lower("hi"));
271 TEST(StringRefTest, EndsWith) {
272 StringRef Str("hello");
273 EXPECT_TRUE(Str.endswith(""));
274 EXPECT_TRUE(Str.endswith("lo"));
275 EXPECT_FALSE(Str.endswith("helloworld"));
276 EXPECT_FALSE(Str.endswith("worldhello"));
277 EXPECT_FALSE(Str.endswith("so"));
280 TEST(StringRefTest, EndsWithLower) {
281 StringRef Str("heLLo");
282 EXPECT_TRUE(Str.endswith_lower(""));
283 EXPECT_TRUE(Str.endswith_lower("lo"));
284 EXPECT_TRUE(Str.endswith_lower("LO"));
285 EXPECT_TRUE(Str.endswith_lower("ELlo"));
286 EXPECT_FALSE(Str.endswith_lower("helloworld"));
287 EXPECT_FALSE(Str.endswith_lower("hi"));
290 TEST(StringRefTest, Find) {
291 StringRef Str("hello");
292 EXPECT_EQ(2U, Str.find('l'));
293 EXPECT_EQ(StringRef::npos, Str.find('z'));
294 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
295 EXPECT_EQ(0U, Str.find("hello"));
296 EXPECT_EQ(1U, Str.find("ello"));
297 EXPECT_EQ(StringRef::npos, Str.find("zz"));
298 EXPECT_EQ(2U, Str.find("ll", 2));
299 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
300 EXPECT_EQ(0U, Str.find(""));
301 StringRef LongStr("hellx xello hell ello world foo bar hello");
302 EXPECT_EQ(36U, LongStr.find("hello"));
303 EXPECT_EQ(28U, LongStr.find("foo"));
304 EXPECT_EQ(12U, LongStr.find("hell", 2));
305 EXPECT_EQ(0U, LongStr.find(""));
307 EXPECT_EQ(3U, Str.rfind('l'));
308 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
309 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
310 EXPECT_EQ(0U, Str.rfind("hello"));
311 EXPECT_EQ(1U, Str.rfind("ello"));
312 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
314 EXPECT_EQ(2U, Str.find_first_of('l'));
315 EXPECT_EQ(1U, Str.find_first_of("el"));
316 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
318 EXPECT_EQ(1U, Str.find_first_not_of('h'));
319 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
320 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
322 EXPECT_EQ(3U, Str.find_last_not_of('o'));
323 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
324 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
327 TEST(StringRefTest, Count) {
328 StringRef Str("hello");
329 EXPECT_EQ(2U, Str.count('l'));
330 EXPECT_EQ(1U, Str.count('o'));
331 EXPECT_EQ(0U, Str.count('z'));
332 EXPECT_EQ(0U, Str.count("helloworld"));
333 EXPECT_EQ(1U, Str.count("hello"));
334 EXPECT_EQ(1U, Str.count("ello"));
335 EXPECT_EQ(0U, Str.count("zz"));
338 TEST(StringRefTest, EditDistance) {
339 StringRef Str("hello");
340 EXPECT_EQ(2U, Str.edit_distance("hill"));
343 TEST(StringRefTest, Misc) {
345 raw_string_ostream OS(Storage);
346 OS << StringRef("hello");
347 EXPECT_EQ("hello", OS.str());
350 TEST(StringRefTest, Hashing) {
351 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
352 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
353 std::string S = "hello world";
354 hash_code H = hash_value(S);
355 EXPECT_EQ(H, hash_value(StringRef("hello world")));
356 EXPECT_EQ(H, hash_value(StringRef(S)));
357 EXPECT_NE(H, hash_value(StringRef("hello worl")));
358 EXPECT_EQ(hash_value(std::string("hello worl")),
359 hash_value(StringRef("hello worl")));
360 EXPECT_NE(H, hash_value(StringRef("hello world ")));
361 EXPECT_EQ(hash_value(std::string("hello world ")),
362 hash_value(StringRef("hello world ")));
363 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
364 EXPECT_NE(hash_value(std::string("ello worl")),
365 hash_value(StringRef("hello world").slice(1, -1)));
368 struct UnsignedPair {
377 , {"4294967295", 4294967295ULL}
378 , {"4294967296", 4294967296ULL}
379 , {"18446744073709551615", 18446744073709551615ULL}
399 , {"2147483647", 2147483647LL}
400 , {"2147483648", 2147483648LL}
401 , {"-2147483648", -2147483648LL}
402 , {"-2147483649", -2147483649LL}
403 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
412 TEST(StringRefTest, getAsInteger) {
418 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
419 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
420 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
421 ASSERT_FALSE(U8Success);
422 EXPECT_EQ(U8, Unsigned[i].Expected);
424 ASSERT_TRUE(U8Success);
426 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
427 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
428 ASSERT_FALSE(U16Success);
429 EXPECT_EQ(U16, Unsigned[i].Expected);
431 ASSERT_TRUE(U16Success);
433 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
434 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
435 ASSERT_FALSE(U32Success);
436 EXPECT_EQ(U32, Unsigned[i].Expected);
438 ASSERT_TRUE(U32Success);
440 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
441 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
442 ASSERT_FALSE(U64Success);
443 EXPECT_EQ(U64, Unsigned[i].Expected);
445 ASSERT_TRUE(U64Success);
454 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
455 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
456 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
457 ASSERT_FALSE(S8Success);
458 EXPECT_EQ(S8, Signed[i].Expected);
460 ASSERT_TRUE(S8Success);
462 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
463 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
464 ASSERT_FALSE(S16Success);
465 EXPECT_EQ(S16, Signed[i].Expected);
467 ASSERT_TRUE(S16Success);
469 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
470 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
471 ASSERT_FALSE(S32Success);
472 EXPECT_EQ(S32, Signed[i].Expected);
474 ASSERT_TRUE(S32Success);
476 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
477 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
478 ASSERT_FALSE(S64Success);
479 EXPECT_EQ(S64, Signed[i].Expected);
481 ASSERT_TRUE(S64Success);
487 static const char* BadStrings[] = {
488 "18446744073709551617" // value just over max
489 , "123456789012345678901" // value way too large
490 , "4t23v" // illegal decimal characters
491 , "0x123W56" // illegal hex characters
492 , "0b2" // illegal bin characters
493 , "08" // illegal oct characters
494 , "0o8" // illegal oct characters
495 , "-123" // negative unsigned value
499 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
500 unsigned long long U64;
501 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
502 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
503 ASSERT_TRUE(IsBadNumber);
507 static const char *join_input[] = { "a", "b", "c" };
508 static const char join_result1[] = "a";
509 static const char join_result2[] = "a:b:c";
510 static const char join_result3[] = "a::b::c";
512 TEST(StringRefTest, joinStrings) {
513 std::vector<StringRef> v1;
514 std::vector<std::string> v2;
515 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
516 v1.push_back(join_input[i]);
517 v2.push_back(join_input[i]);
520 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
521 EXPECT_TRUE(v1_join1);
522 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
523 EXPECT_TRUE(v1_join2);
524 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
525 EXPECT_TRUE(v1_join3);
527 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
528 EXPECT_TRUE(v2_join1);
529 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
530 EXPECT_TRUE(v2_join2);
531 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
532 EXPECT_TRUE(v2_join3);
536 TEST(StringRefTest, AllocatorCopy) {
537 BumpPtrAllocator Alloc;
538 StringRef Str1 = "hello";
539 StringRef Str2 = "bye";
540 StringRef Str1c = Str1.copy(Alloc);
541 StringRef Str2c = Str2.copy(Alloc);
542 EXPECT_TRUE(Str1.equals(Str1c));
543 EXPECT_NE(Str1.data(), Str1c.data());
544 EXPECT_TRUE(Str2.equals(Str2c));
545 EXPECT_NE(Str2.data(), Str2c.data());
549 } // end anonymous namespace