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/raw_ostream.h"
15 #include "gtest/gtest.h"
20 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
25 std::ostream &operator<<(std::ostream &OS,
26 const std::pair<StringRef, StringRef> &P) {
27 OS << "(" << P.first << ", " << P.second << ")";
34 TEST(StringRefTest, Construction) {
35 EXPECT_EQ("", StringRef());
36 EXPECT_EQ("hello", StringRef("hello"));
37 EXPECT_EQ("hello", StringRef("hello world", 5));
38 EXPECT_EQ("hello", StringRef(std::string("hello")));
41 TEST(StringRefTest, Iteration) {
43 const char *p = "hello";
44 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
48 TEST(StringRefTest, StringOps) {
49 const char *p = "hello";
50 EXPECT_EQ(p, StringRef(p, 0).data());
51 EXPECT_TRUE(StringRef().empty());
52 EXPECT_EQ((size_t) 5, StringRef("hello").size());
53 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
54 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
55 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
56 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
57 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
58 EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
60 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
61 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
62 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
63 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
64 EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
65 EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
66 EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
67 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
68 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
70 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
71 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
72 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
73 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
74 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
75 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
76 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
77 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
78 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
79 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
80 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
81 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
82 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
83 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
84 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
85 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
86 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
89 TEST(StringRefTest, Operators) {
90 EXPECT_EQ("", StringRef());
91 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
92 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
93 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
94 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
95 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
96 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
97 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
98 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
99 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
100 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
101 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
102 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
103 EXPECT_EQ('a', StringRef("aab")[1]);
106 TEST(StringRefTest, Substr) {
107 StringRef Str("hello");
108 EXPECT_EQ("lo", Str.substr(3));
109 EXPECT_EQ("", Str.substr(100));
110 EXPECT_EQ("hello", Str.substr(0, 100));
111 EXPECT_EQ("o", Str.substr(4, 10));
114 TEST(StringRefTest, Slice) {
115 StringRef Str("hello");
116 EXPECT_EQ("l", Str.slice(2, 3));
117 EXPECT_EQ("ell", Str.slice(1, 4));
118 EXPECT_EQ("llo", Str.slice(2, 100));
119 EXPECT_EQ("", Str.slice(2, 1));
120 EXPECT_EQ("", Str.slice(10, 20));
123 TEST(StringRefTest, Split) {
124 StringRef Str("hello");
125 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
127 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
129 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
131 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
133 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
136 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
138 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
140 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
142 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
144 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
148 TEST(StringRefTest, Split2) {
149 SmallVector<StringRef, 5> parts;
150 SmallVector<StringRef, 5> expected;
152 expected.push_back("ab"); expected.push_back("c");
153 StringRef(",ab,,c,").split(parts, ",", -1, false);
154 EXPECT_TRUE(parts == expected);
156 expected.clear(); parts.clear();
157 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
158 expected.push_back("c"); expected.push_back("");
159 StringRef(",ab,,c,").split(parts, ",", -1, true);
160 EXPECT_TRUE(parts == expected);
162 expected.clear(); parts.clear();
163 expected.push_back("");
164 StringRef("").split(parts, ",", -1, true);
165 EXPECT_TRUE(parts == expected);
167 expected.clear(); parts.clear();
168 StringRef("").split(parts, ",", -1, false);
169 EXPECT_TRUE(parts == expected);
171 expected.clear(); parts.clear();
172 StringRef(",").split(parts, ",", -1, false);
173 EXPECT_TRUE(parts == expected);
175 expected.clear(); parts.clear();
176 expected.push_back(""); expected.push_back("");
177 StringRef(",").split(parts, ",", -1, true);
178 EXPECT_TRUE(parts == expected);
180 expected.clear(); parts.clear();
181 expected.push_back("a"); expected.push_back("b");
182 StringRef("a,b").split(parts, ",", -1, true);
183 EXPECT_TRUE(parts == expected);
186 expected.clear(); parts.clear();
187 expected.push_back("a,,b,c");
188 StringRef("a,,b,c").split(parts, ",", 0, true);
189 EXPECT_TRUE(parts == expected);
191 expected.clear(); parts.clear();
192 expected.push_back("a,,b,c");
193 StringRef("a,,b,c").split(parts, ",", 0, false);
194 EXPECT_TRUE(parts == expected);
196 expected.clear(); parts.clear();
197 expected.push_back("a"); expected.push_back(",b,c");
198 StringRef("a,,b,c").split(parts, ",", 1, true);
199 EXPECT_TRUE(parts == expected);
201 expected.clear(); parts.clear();
202 expected.push_back("a"); expected.push_back(",b,c");
203 StringRef("a,,b,c").split(parts, ",", 1, false);
204 EXPECT_TRUE(parts == expected);
206 expected.clear(); parts.clear();
207 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
208 StringRef("a,,b,c").split(parts, ",", 2, true);
209 EXPECT_TRUE(parts == expected);
211 expected.clear(); parts.clear();
212 expected.push_back("a"); expected.push_back("b,c");
213 StringRef("a,,b,c").split(parts, ",", 2, false);
214 EXPECT_TRUE(parts == expected);
216 expected.clear(); parts.clear();
217 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
218 expected.push_back("c");
219 StringRef("a,,b,c").split(parts, ",", 3, true);
220 EXPECT_TRUE(parts == expected);
222 expected.clear(); parts.clear();
223 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
224 StringRef("a,,b,c").split(parts, ",", 3, false);
225 EXPECT_TRUE(parts == expected);
228 TEST(StringRefTest, Trim) {
229 StringRef Str0("hello");
230 StringRef Str1(" hello ");
231 StringRef Str2(" hello ");
233 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
234 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
235 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
236 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
237 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
238 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
239 EXPECT_EQ(StringRef("hello"), Str0.trim());
240 EXPECT_EQ(StringRef("hello"), Str1.trim());
241 EXPECT_EQ(StringRef("hello"), Str2.trim());
243 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
245 EXPECT_EQ(StringRef(""), StringRef("").trim());
246 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
247 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
248 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
249 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
252 TEST(StringRefTest, StartsWith) {
253 StringRef Str("hello");
254 EXPECT_TRUE(Str.startswith(""));
255 EXPECT_TRUE(Str.startswith("he"));
256 EXPECT_FALSE(Str.startswith("helloworld"));
257 EXPECT_FALSE(Str.startswith("hi"));
260 TEST(StringRefTest, StartsWithLower) {
261 StringRef Str("heLLo");
262 EXPECT_TRUE(Str.startswith_lower(""));
263 EXPECT_TRUE(Str.startswith_lower("he"));
264 EXPECT_TRUE(Str.startswith_lower("hell"));
265 EXPECT_TRUE(Str.startswith_lower("HELlo"));
266 EXPECT_FALSE(Str.startswith_lower("helloworld"));
267 EXPECT_FALSE(Str.startswith_lower("hi"));
270 TEST(StringRefTest, EndsWith) {
271 StringRef Str("hello");
272 EXPECT_TRUE(Str.endswith(""));
273 EXPECT_TRUE(Str.endswith("lo"));
274 EXPECT_FALSE(Str.endswith("helloworld"));
275 EXPECT_FALSE(Str.endswith("worldhello"));
276 EXPECT_FALSE(Str.endswith("so"));
279 TEST(StringRefTest, EndsWithLower) {
280 StringRef Str("heLLo");
281 EXPECT_TRUE(Str.endswith_lower(""));
282 EXPECT_TRUE(Str.endswith_lower("lo"));
283 EXPECT_TRUE(Str.endswith_lower("LO"));
284 EXPECT_TRUE(Str.endswith_lower("ELlo"));
285 EXPECT_FALSE(Str.endswith_lower("helloworld"));
286 EXPECT_FALSE(Str.endswith_lower("hi"));
289 TEST(StringRefTest, Find) {
290 StringRef Str("hello");
291 EXPECT_EQ(2U, Str.find('l'));
292 EXPECT_EQ(StringRef::npos, Str.find('z'));
293 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
294 EXPECT_EQ(0U, Str.find("hello"));
295 EXPECT_EQ(1U, Str.find("ello"));
296 EXPECT_EQ(StringRef::npos, Str.find("zz"));
297 EXPECT_EQ(2U, Str.find("ll", 2));
298 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
299 EXPECT_EQ(0U, Str.find(""));
300 StringRef LongStr("hellx xello hell ello world foo bar hello");
301 EXPECT_EQ(36U, LongStr.find("hello"));
302 EXPECT_EQ(28U, LongStr.find("foo"));
303 EXPECT_EQ(12U, LongStr.find("hell", 2));
304 EXPECT_EQ(0U, LongStr.find(""));
306 EXPECT_EQ(3U, Str.rfind('l'));
307 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
308 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
309 EXPECT_EQ(0U, Str.rfind("hello"));
310 EXPECT_EQ(1U, Str.rfind("ello"));
311 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
313 EXPECT_EQ(2U, Str.find_first_of('l'));
314 EXPECT_EQ(1U, Str.find_first_of("el"));
315 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
317 EXPECT_EQ(1U, Str.find_first_not_of('h'));
318 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
319 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
321 EXPECT_EQ(3U, Str.find_last_not_of('o'));
322 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
323 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
326 TEST(StringRefTest, Count) {
327 StringRef Str("hello");
328 EXPECT_EQ(2U, Str.count('l'));
329 EXPECT_EQ(1U, Str.count('o'));
330 EXPECT_EQ(0U, Str.count('z'));
331 EXPECT_EQ(0U, Str.count("helloworld"));
332 EXPECT_EQ(1U, Str.count("hello"));
333 EXPECT_EQ(1U, Str.count("ello"));
334 EXPECT_EQ(0U, Str.count("zz"));
337 TEST(StringRefTest, EditDistance) {
338 StringRef Str("hello");
339 EXPECT_EQ(2U, Str.edit_distance("hill"));
342 TEST(StringRefTest, Misc) {
344 raw_string_ostream OS(Storage);
345 OS << StringRef("hello");
346 EXPECT_EQ("hello", OS.str());
349 TEST(StringRefTest, Hashing) {
350 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
351 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
352 std::string S = "hello world";
353 hash_code H = hash_value(S);
354 EXPECT_EQ(H, hash_value(StringRef("hello world")));
355 EXPECT_EQ(H, hash_value(StringRef(S)));
356 EXPECT_NE(H, hash_value(StringRef("hello worl")));
357 EXPECT_EQ(hash_value(std::string("hello worl")),
358 hash_value(StringRef("hello worl")));
359 EXPECT_NE(H, hash_value(StringRef("hello world ")));
360 EXPECT_EQ(hash_value(std::string("hello world ")),
361 hash_value(StringRef("hello world ")));
362 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
363 EXPECT_NE(hash_value(std::string("ello worl")),
364 hash_value(StringRef("hello world").slice(1, -1)));
367 struct UnsignedPair {
376 , {"4294967295", 4294967295ULL}
377 , {"4294967296", 4294967296ULL}
378 , {"18446744073709551615", 18446744073709551615ULL}
398 , {"2147483647", 2147483647LL}
399 , {"2147483648", 2147483648LL}
400 , {"-2147483648", -2147483648LL}
401 , {"-2147483649", -2147483649LL}
402 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
411 TEST(StringRefTest, getAsInteger) {
417 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
418 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
419 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
420 ASSERT_FALSE(U8Success);
421 EXPECT_EQ(U8, Unsigned[i].Expected);
423 ASSERT_TRUE(U8Success);
425 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
426 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
427 ASSERT_FALSE(U16Success);
428 EXPECT_EQ(U16, Unsigned[i].Expected);
430 ASSERT_TRUE(U16Success);
432 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
433 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
434 ASSERT_FALSE(U32Success);
435 EXPECT_EQ(U32, Unsigned[i].Expected);
437 ASSERT_TRUE(U32Success);
439 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
440 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
441 ASSERT_FALSE(U64Success);
442 EXPECT_EQ(U64, Unsigned[i].Expected);
444 ASSERT_TRUE(U64Success);
453 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
454 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
455 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
456 ASSERT_FALSE(S8Success);
457 EXPECT_EQ(S8, Signed[i].Expected);
459 ASSERT_TRUE(S8Success);
461 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
462 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
463 ASSERT_FALSE(S16Success);
464 EXPECT_EQ(S16, Signed[i].Expected);
466 ASSERT_TRUE(S16Success);
468 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
469 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
470 ASSERT_FALSE(S32Success);
471 EXPECT_EQ(S32, Signed[i].Expected);
473 ASSERT_TRUE(S32Success);
475 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
476 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
477 ASSERT_FALSE(S64Success);
478 EXPECT_EQ(S64, Signed[i].Expected);
480 ASSERT_TRUE(S64Success);
486 static const char* BadStrings[] = {
487 "18446744073709551617" // value just over max
488 , "123456789012345678901" // value way too large
489 , "4t23v" // illegal decimal characters
490 , "0x123W56" // illegal hex characters
491 , "0b2" // illegal bin characters
492 , "08" // illegal oct characters
493 , "0o8" // illegal oct characters
494 , "-123" // negative unsigned value
498 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
499 unsigned long long U64;
500 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
501 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
502 ASSERT_TRUE(IsBadNumber);
506 static const char *join_input[] = { "a", "b", "c" };
507 static const char join_result1[] = "a";
508 static const char join_result2[] = "a:b:c";
509 static const char join_result3[] = "a::b::c";
511 TEST(StringRefTest, joinStrings) {
512 std::vector<StringRef> v1;
513 std::vector<std::string> v2;
514 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
515 v1.push_back(join_input[i]);
516 v2.push_back(join_input[i]);
519 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
520 EXPECT_TRUE(v1_join1);
521 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
522 EXPECT_TRUE(v1_join2);
523 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
524 EXPECT_TRUE(v1_join3);
526 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
527 EXPECT_TRUE(v2_join1);
528 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
529 EXPECT_TRUE(v2_join2);
530 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
531 EXPECT_TRUE(v2_join3);
534 } // end anonymous namespace