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/StringExtras.h"
12 #include "llvm/ADT/Hashing.h"
13 #include "llvm/ADT/SmallVector.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("aA"));
65 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
67 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
68 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
69 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
70 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
71 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
72 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
73 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
74 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
75 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
76 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
77 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
78 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
79 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
80 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
81 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
82 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
83 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
86 TEST(StringRefTest, Operators) {
87 EXPECT_EQ("", StringRef());
88 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
89 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
90 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
91 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
92 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
93 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
94 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
95 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
96 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
97 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
98 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
99 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
100 EXPECT_EQ('a', StringRef("aab")[1]);
103 TEST(StringRefTest, Substr) {
104 StringRef Str("hello");
105 EXPECT_EQ("lo", Str.substr(3));
106 EXPECT_EQ("", Str.substr(100));
107 EXPECT_EQ("hello", Str.substr(0, 100));
108 EXPECT_EQ("o", Str.substr(4, 10));
111 TEST(StringRefTest, Slice) {
112 StringRef Str("hello");
113 EXPECT_EQ("l", Str.slice(2, 3));
114 EXPECT_EQ("ell", Str.slice(1, 4));
115 EXPECT_EQ("llo", Str.slice(2, 100));
116 EXPECT_EQ("", Str.slice(2, 1));
117 EXPECT_EQ("", Str.slice(10, 20));
120 TEST(StringRefTest, Split) {
121 StringRef Str("hello");
122 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
124 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
126 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
128 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
130 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
133 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
135 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
137 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
139 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
141 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
145 TEST(StringRefTest, Split2) {
146 SmallVector<StringRef, 5> parts;
147 SmallVector<StringRef, 5> expected;
149 expected.push_back("ab"); expected.push_back("c");
150 StringRef(",ab,,c,").split(parts, ",", -1, false);
151 EXPECT_TRUE(parts == expected);
153 expected.clear(); parts.clear();
154 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
155 expected.push_back("c"); expected.push_back("");
156 StringRef(",ab,,c,").split(parts, ",", -1, true);
157 EXPECT_TRUE(parts == expected);
159 expected.clear(); parts.clear();
160 expected.push_back("");
161 StringRef("").split(parts, ",", -1, true);
162 EXPECT_TRUE(parts == expected);
164 expected.clear(); parts.clear();
165 StringRef("").split(parts, ",", -1, false);
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 expected.push_back(""); expected.push_back("");
174 StringRef(",").split(parts, ",", -1, true);
175 EXPECT_TRUE(parts == expected);
177 expected.clear(); parts.clear();
178 expected.push_back("a"); expected.push_back("b");
179 StringRef("a,b").split(parts, ",", -1, true);
180 EXPECT_TRUE(parts == expected);
183 expected.clear(); parts.clear();
184 expected.push_back("a,,b,c");
185 StringRef("a,,b,c").split(parts, ",", 0, true);
186 EXPECT_TRUE(parts == expected);
188 expected.clear(); parts.clear();
189 expected.push_back("a,,b,c");
190 StringRef("a,,b,c").split(parts, ",", 0, false);
191 EXPECT_TRUE(parts == expected);
193 expected.clear(); parts.clear();
194 expected.push_back("a"); expected.push_back(",b,c");
195 StringRef("a,,b,c").split(parts, ",", 1, true);
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, false);
201 EXPECT_TRUE(parts == expected);
203 expected.clear(); parts.clear();
204 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
205 StringRef("a,,b,c").split(parts, ",", 2, true);
206 EXPECT_TRUE(parts == expected);
208 expected.clear(); parts.clear();
209 expected.push_back("a"); expected.push_back("b,c");
210 StringRef("a,,b,c").split(parts, ",", 2, false);
211 EXPECT_TRUE(parts == expected);
213 expected.clear(); parts.clear();
214 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
215 expected.push_back("c");
216 StringRef("a,,b,c").split(parts, ",", 3, true);
217 EXPECT_TRUE(parts == expected);
219 expected.clear(); parts.clear();
220 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
221 StringRef("a,,b,c").split(parts, ",", 3, false);
222 EXPECT_TRUE(parts == expected);
225 TEST(StringRefTest, Trim) {
226 StringRef Str0("hello");
227 StringRef Str1(" hello ");
228 StringRef Str2(" hello ");
230 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
231 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
232 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
233 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
234 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
235 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
236 EXPECT_EQ(StringRef("hello"), Str0.trim());
237 EXPECT_EQ(StringRef("hello"), Str1.trim());
238 EXPECT_EQ(StringRef("hello"), Str2.trim());
240 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
242 EXPECT_EQ(StringRef(""), StringRef("").trim());
243 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
244 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
245 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
246 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
249 TEST(StringRefTest, StartsWith) {
250 StringRef Str("hello");
251 EXPECT_TRUE(Str.startswith("he"));
252 EXPECT_FALSE(Str.startswith("helloworld"));
253 EXPECT_FALSE(Str.startswith("hi"));
256 TEST(StringRefTest, EndsWith) {
257 StringRef Str("hello");
258 EXPECT_TRUE(Str.endswith("lo"));
259 EXPECT_FALSE(Str.endswith("helloworld"));
260 EXPECT_FALSE(Str.endswith("worldhello"));
261 EXPECT_FALSE(Str.endswith("so"));
264 TEST(StringRefTest, Find) {
265 StringRef Str("hello");
266 EXPECT_EQ(2U, Str.find('l'));
267 EXPECT_EQ(StringRef::npos, Str.find('z'));
268 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
269 EXPECT_EQ(0U, Str.find("hello"));
270 EXPECT_EQ(1U, Str.find("ello"));
271 EXPECT_EQ(StringRef::npos, Str.find("zz"));
272 EXPECT_EQ(2U, Str.find("ll", 2));
273 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
274 EXPECT_EQ(0U, Str.find(""));
275 StringRef LongStr("hellx xello hell ello world foo bar hello");
276 EXPECT_EQ(36U, LongStr.find("hello"));
277 EXPECT_EQ(28U, LongStr.find("foo"));
278 EXPECT_EQ(12U, LongStr.find("hell", 2));
279 EXPECT_EQ(0U, LongStr.find(""));
281 EXPECT_EQ(3U, Str.rfind('l'));
282 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
283 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
284 EXPECT_EQ(0U, Str.rfind("hello"));
285 EXPECT_EQ(1U, Str.rfind("ello"));
286 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
288 EXPECT_EQ(2U, Str.find_first_of('l'));
289 EXPECT_EQ(1U, Str.find_first_of("el"));
290 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
292 EXPECT_EQ(1U, Str.find_first_not_of('h'));
293 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
294 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
296 EXPECT_EQ(3U, Str.find_last_not_of('o'));
297 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
298 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
301 TEST(StringRefTest, Count) {
302 StringRef Str("hello");
303 EXPECT_EQ(2U, Str.count('l'));
304 EXPECT_EQ(1U, Str.count('o'));
305 EXPECT_EQ(0U, Str.count('z'));
306 EXPECT_EQ(0U, Str.count("helloworld"));
307 EXPECT_EQ(1U, Str.count("hello"));
308 EXPECT_EQ(1U, Str.count("ello"));
309 EXPECT_EQ(0U, Str.count("zz"));
312 TEST(StringRefTest, EditDistance) {
313 StringRef Str("hello");
314 EXPECT_EQ(2U, Str.edit_distance("hill"));
317 TEST(StringRefTest, Misc) {
319 raw_string_ostream OS(Storage);
320 OS << StringRef("hello");
321 EXPECT_EQ("hello", OS.str());
324 TEST(StringRefTest, Hashing) {
325 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
326 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
327 std::string S = "hello world";
328 hash_code H = hash_value(S);
329 EXPECT_EQ(H, hash_value(StringRef("hello world")));
330 EXPECT_EQ(H, hash_value(StringRef(S)));
331 EXPECT_NE(H, hash_value(StringRef("hello worl")));
332 EXPECT_EQ(hash_value(std::string("hello worl")),
333 hash_value(StringRef("hello worl")));
334 EXPECT_NE(H, hash_value(StringRef("hello world ")));
335 EXPECT_EQ(hash_value(std::string("hello world ")),
336 hash_value(StringRef("hello world ")));
337 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
338 EXPECT_NE(hash_value(std::string("ello worl")),
339 hash_value(StringRef("hello world").slice(1, -1)));
342 struct UnsignedPair {
351 , {"4294967295", 4294967295ULL}
352 , {"4294967296", 4294967296ULL}
353 , {"18446744073709551615", 18446744073709551615ULL}
373 , {"2147483647", 2147483647LL}
374 , {"2147483648", 2147483648LL}
375 , {"-2147483648", -2147483648LL}
376 , {"-2147483649", -2147483649LL}
377 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
386 TEST(StringRefTest, getAsInteger) {
392 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
393 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
394 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
395 ASSERT_FALSE(U8Success);
396 EXPECT_EQ(U8, Unsigned[i].Expected);
398 ASSERT_TRUE(U8Success);
400 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
401 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
402 ASSERT_FALSE(U16Success);
403 EXPECT_EQ(U16, Unsigned[i].Expected);
405 ASSERT_TRUE(U16Success);
407 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
408 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
409 ASSERT_FALSE(U32Success);
410 EXPECT_EQ(U32, Unsigned[i].Expected);
412 ASSERT_TRUE(U32Success);
414 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
415 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
416 ASSERT_FALSE(U64Success);
417 EXPECT_EQ(U64, Unsigned[i].Expected);
419 ASSERT_TRUE(U64Success);
428 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
429 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
430 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
431 ASSERT_FALSE(S8Success);
432 EXPECT_EQ(S8, Signed[i].Expected);
434 ASSERT_TRUE(S8Success);
436 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
437 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
438 ASSERT_FALSE(S16Success);
439 EXPECT_EQ(S16, Signed[i].Expected);
441 ASSERT_TRUE(S16Success);
443 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
444 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
445 ASSERT_FALSE(S32Success);
446 EXPECT_EQ(S32, Signed[i].Expected);
448 ASSERT_TRUE(S32Success);
450 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
451 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
452 ASSERT_FALSE(S64Success);
453 EXPECT_EQ(S64, Signed[i].Expected);
455 ASSERT_TRUE(S64Success);
461 static const char* BadStrings[] = {
462 "18446744073709551617" // value just over max
463 , "123456789012345678901" // value way too large
464 , "4t23v" // illegal decimal characters
465 , "0x123W56" // illegal hex characters
466 , "0b2" // illegal bin characters
467 , "08" // illegal oct characters
468 , "0o8" // illegal oct characters
469 , "-123" // negative unsigned value
473 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
474 unsigned long long U64;
475 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
476 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
477 ASSERT_TRUE(IsBadNumber);
481 static const char *join_input[] = { "a", "b", "c" };
482 static const char join_result1[] = "a";
483 static const char join_result2[] = "a:b:c";
484 static const char join_result3[] = "a::b::c";
486 TEST(StringRefTest, joinStrings) {
487 std::vector<StringRef> v1;
488 std::vector<std::string> v2;
489 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
490 v1.push_back(join_input[i]);
491 v2.push_back(join_input[i]);
494 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
495 EXPECT_TRUE(v1_join1);
496 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
497 EXPECT_TRUE(v1_join2);
498 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
499 EXPECT_TRUE(v1_join3);
501 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
502 EXPECT_TRUE(v2_join1);
503 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
504 EXPECT_TRUE(v2_join2);
505 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
506 EXPECT_TRUE(v2_join3);
509 } // end anonymous namespace