07fba49684cef032b93ddfa642c35d0beba84462
[oota-llvm.git] / unittests / ADT / StringRefTest.cpp
1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
16 using namespace llvm;
17
18 namespace llvm {
19
20 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
21   OS << S.str();
22   return OS;
23 }
24
25 std::ostream &operator<<(std::ostream &OS,
26                          const std::pair<StringRef, StringRef> &P) {
27   OS << "(" << P.first << ", " << P.second << ")";
28   return OS;
29 }
30
31 }
32
33 namespace {
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")));
39 }
40
41 TEST(StringRefTest, Iteration) {
42   StringRef S("hello");
43   const char *p = "hello";
44   for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
45     EXPECT_EQ(*it, *p);
46 }
47
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"));
59
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"));
66
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"));
84 }
85
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]);
101 }
102
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));
109 }
110
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));
118 }
119
120 TEST(StringRefTest, Split) {
121   StringRef Str("hello");
122   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
123             Str.split('X'));
124   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
125             Str.split('e'));
126   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
127             Str.split('h'));
128   EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
129             Str.split('l'));
130   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
131             Str.split('o'));
132
133   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
134             Str.rsplit('X'));
135   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
136             Str.rsplit('e'));
137   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
138             Str.rsplit('h'));
139   EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
140             Str.rsplit('l'));
141   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
142             Str.rsplit('o'));
143 }
144
145 TEST(StringRefTest, Split2) {
146   SmallVector<StringRef, 5> parts;
147   SmallVector<StringRef, 5> expected;
148
149   expected.push_back("ab"); expected.push_back("c");
150   StringRef(",ab,,c,").split(parts, ",", -1, false);
151   EXPECT_TRUE(parts == expected);
152
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);
158
159   expected.clear(); parts.clear();
160   expected.push_back("");
161   StringRef("").split(parts, ",", -1, true);
162   EXPECT_TRUE(parts == expected);
163
164   expected.clear(); parts.clear();
165   StringRef("").split(parts, ",", -1, false);
166   EXPECT_TRUE(parts == expected);
167
168   expected.clear(); parts.clear();
169   StringRef(",").split(parts, ",", -1, false);
170   EXPECT_TRUE(parts == expected);
171
172   expected.clear(); parts.clear();
173   expected.push_back(""); expected.push_back("");
174   StringRef(",").split(parts, ",", -1, true);
175   EXPECT_TRUE(parts == expected);
176
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);
181
182   // Test MaxSplit
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);
187
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);
192
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);
197
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);
202
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);
207
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);
212
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);
218
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);
223 }
224
225 TEST(StringRefTest, Trim) {
226   StringRef Str0("hello");
227   StringRef Str1(" hello ");
228   StringRef Str2("  hello  ");
229
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());
239
240   EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
241
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)));
247 }
248
249 TEST(StringRefTest, StartsWith) {
250   StringRef Str("hello");
251   EXPECT_TRUE(Str.startswith(""));
252   EXPECT_TRUE(Str.startswith("he"));
253   EXPECT_FALSE(Str.startswith("helloworld"));
254   EXPECT_FALSE(Str.startswith("hi"));
255 }
256
257 TEST(StringRefTest, EndsWith) {
258   StringRef Str("hello");
259   EXPECT_TRUE(Str.endswith(""));
260   EXPECT_TRUE(Str.endswith("lo"));
261   EXPECT_FALSE(Str.endswith("helloworld"));
262   EXPECT_FALSE(Str.endswith("worldhello"));
263   EXPECT_FALSE(Str.endswith("so"));
264 }
265
266 TEST(StringRefTest, Find) {
267   StringRef Str("hello");
268   EXPECT_EQ(2U, Str.find('l'));
269   EXPECT_EQ(StringRef::npos, Str.find('z'));
270   EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
271   EXPECT_EQ(0U, Str.find("hello"));
272   EXPECT_EQ(1U, Str.find("ello"));
273   EXPECT_EQ(StringRef::npos, Str.find("zz"));
274   EXPECT_EQ(2U, Str.find("ll", 2));
275   EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
276   EXPECT_EQ(0U, Str.find(""));
277   StringRef LongStr("hellx xello hell ello world foo bar hello");
278   EXPECT_EQ(36U, LongStr.find("hello"));
279   EXPECT_EQ(28U, LongStr.find("foo"));
280   EXPECT_EQ(12U, LongStr.find("hell", 2));
281   EXPECT_EQ(0U, LongStr.find(""));
282
283   EXPECT_EQ(3U, Str.rfind('l'));
284   EXPECT_EQ(StringRef::npos, Str.rfind('z'));
285   EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
286   EXPECT_EQ(0U, Str.rfind("hello"));
287   EXPECT_EQ(1U, Str.rfind("ello"));
288   EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
289
290   EXPECT_EQ(2U, Str.find_first_of('l'));
291   EXPECT_EQ(1U, Str.find_first_of("el"));
292   EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
293
294   EXPECT_EQ(1U, Str.find_first_not_of('h'));
295   EXPECT_EQ(4U, Str.find_first_not_of("hel"));
296   EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
297
298   EXPECT_EQ(3U, Str.find_last_not_of('o'));
299   EXPECT_EQ(1U, Str.find_last_not_of("lo"));
300   EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
301 }
302
303 TEST(StringRefTest, Count) {
304   StringRef Str("hello");
305   EXPECT_EQ(2U, Str.count('l'));
306   EXPECT_EQ(1U, Str.count('o'));
307   EXPECT_EQ(0U, Str.count('z'));
308   EXPECT_EQ(0U, Str.count("helloworld"));
309   EXPECT_EQ(1U, Str.count("hello"));
310   EXPECT_EQ(1U, Str.count("ello"));
311   EXPECT_EQ(0U, Str.count("zz"));
312 }
313
314 TEST(StringRefTest, EditDistance) {
315   StringRef Str("hello");
316   EXPECT_EQ(2U, Str.edit_distance("hill"));
317 }
318
319 TEST(StringRefTest, Misc) {
320   std::string Storage;
321   raw_string_ostream OS(Storage);
322   OS << StringRef("hello");
323   EXPECT_EQ("hello", OS.str());
324 }
325
326 TEST(StringRefTest, Hashing) {
327   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
328   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
329   std::string S = "hello world";
330   hash_code H = hash_value(S);
331   EXPECT_EQ(H, hash_value(StringRef("hello world")));
332   EXPECT_EQ(H, hash_value(StringRef(S)));
333   EXPECT_NE(H, hash_value(StringRef("hello worl")));
334   EXPECT_EQ(hash_value(std::string("hello worl")),
335             hash_value(StringRef("hello worl")));
336   EXPECT_NE(H, hash_value(StringRef("hello world ")));
337   EXPECT_EQ(hash_value(std::string("hello world ")),
338             hash_value(StringRef("hello world ")));
339   EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
340   EXPECT_NE(hash_value(std::string("ello worl")),
341             hash_value(StringRef("hello world").slice(1, -1)));
342 }
343
344 struct UnsignedPair {
345   const char *Str;
346   uint64_t Expected;
347 } Unsigned[] =
348   { {"0", 0}
349   , {"255", 255}
350   , {"256", 256}
351   , {"65535", 65535}
352   , {"65536", 65536}
353   , {"4294967295", 4294967295ULL}
354   , {"4294967296", 4294967296ULL}
355   , {"18446744073709551615", 18446744073709551615ULL}
356   , {"042", 34}
357   , {"0x42", 66}
358   , {"0b101010", 42}
359   };
360
361 struct SignedPair {
362   const char *Str;
363   int64_t Expected;
364 } Signed[] =
365   { {"0", 0}
366   , {"-0", 0}
367   , {"127", 127}
368   , {"128", 128}
369   , {"-128", -128}
370   , {"-129", -129}
371   , {"32767", 32767}
372   , {"32768", 32768}
373   , {"-32768", -32768}
374   , {"-32769", -32769}
375   , {"2147483647", 2147483647LL}
376   , {"2147483648", 2147483648LL}
377   , {"-2147483648", -2147483648LL}
378   , {"-2147483649", -2147483649LL}
379   , {"-9223372036854775808", -(9223372036854775807LL) - 1}
380   , {"042", 34}
381   , {"0x42", 66}
382   , {"0b101010", 42}
383   , {"-042", -34}
384   , {"-0x42", -66}
385   , {"-0b101010", -42}
386   };
387
388 TEST(StringRefTest, getAsInteger) {
389   uint8_t U8;
390   uint16_t U16;
391   uint32_t U32;
392   uint64_t U64;
393
394   for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
395     bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
396     if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
397       ASSERT_FALSE(U8Success);
398       EXPECT_EQ(U8, Unsigned[i].Expected);
399     } else {
400       ASSERT_TRUE(U8Success);
401     }
402     bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
403     if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
404       ASSERT_FALSE(U16Success);
405       EXPECT_EQ(U16, Unsigned[i].Expected);
406     } else {
407       ASSERT_TRUE(U16Success);
408     }
409     bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
410     if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
411       ASSERT_FALSE(U32Success);
412       EXPECT_EQ(U32, Unsigned[i].Expected);
413     } else {
414       ASSERT_TRUE(U32Success);
415     }
416     bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
417     if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
418       ASSERT_FALSE(U64Success);
419       EXPECT_EQ(U64, Unsigned[i].Expected);
420     } else {
421       ASSERT_TRUE(U64Success);
422     }
423   }
424
425   int8_t S8;
426   int16_t S16;
427   int32_t S32;
428   int64_t S64;
429
430   for (size_t i = 0; i < array_lengthof(Signed); ++i) {
431     bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
432     if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
433       ASSERT_FALSE(S8Success);
434       EXPECT_EQ(S8, Signed[i].Expected);
435     } else {
436       ASSERT_TRUE(S8Success);
437     }
438     bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
439     if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
440       ASSERT_FALSE(S16Success);
441       EXPECT_EQ(S16, Signed[i].Expected);
442     } else {
443       ASSERT_TRUE(S16Success);
444     }
445     bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
446     if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
447       ASSERT_FALSE(S32Success);
448       EXPECT_EQ(S32, Signed[i].Expected);
449     } else {
450       ASSERT_TRUE(S32Success);
451     }
452     bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
453     if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
454       ASSERT_FALSE(S64Success);
455       EXPECT_EQ(S64, Signed[i].Expected);
456     } else {
457       ASSERT_TRUE(S64Success);
458     }
459   }
460 }
461
462
463 static const char* BadStrings[] = {
464     "18446744073709551617"  // value just over max
465   , "123456789012345678901" // value way too large
466   , "4t23v"                 // illegal decimal characters
467   , "0x123W56"              // illegal hex characters
468   , "0b2"                   // illegal bin characters
469   , "08"                    // illegal oct characters
470   , "0o8"                   // illegal oct characters
471   , "-123"                  // negative unsigned value
472 };
473
474
475 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
476   unsigned long long U64;
477   for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
478     bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
479     ASSERT_TRUE(IsBadNumber);
480   }
481 }
482
483 static const char *join_input[] = { "a", "b", "c" };
484 static const char join_result1[] = "a";
485 static const char join_result2[] = "a:b:c";
486 static const char join_result3[] = "a::b::c";
487
488 TEST(StringRefTest, joinStrings) {
489   std::vector<StringRef> v1;
490   std::vector<std::string> v2;
491   for (size_t i = 0; i < array_lengthof(join_input); ++i) {
492     v1.push_back(join_input[i]);
493     v2.push_back(join_input[i]);
494   }
495
496   bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
497   EXPECT_TRUE(v1_join1);
498   bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
499   EXPECT_TRUE(v1_join2);
500   bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
501   EXPECT_TRUE(v1_join3);
502
503   bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
504   EXPECT_TRUE(v2_join1);
505   bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
506   EXPECT_TRUE(v2_join2);
507   bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
508   EXPECT_TRUE(v2_join3);
509 }
510
511 } // end anonymous namespace