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/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"
18 using namespace llvm;
19 
20 namespace llvm {
21 
operator <<(std::ostream & OS,const StringRef & S)22 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
23   OS << S.str();
24   return OS;
25 }
26 
operator <<(std::ostream & OS,const std::pair<StringRef,StringRef> & P)27 std::ostream &operator<<(std::ostream &OS,
28                          const std::pair<StringRef, StringRef> &P) {
29   OS << "(" << P.first << ", " << P.second << ")";
30   return OS;
31 }
32 
33 }
34 
35 namespace {
TEST(StringRefTest,Construction)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")));
41 }
42 
TEST(StringRefTest,Iteration)43 TEST(StringRefTest, Iteration) {
44   StringRef S("hello");
45   const char *p = "hello";
46   for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
47     EXPECT_EQ(*it, *p);
48 }
49 
TEST(StringRefTest,StringOps)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"));
61 
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"));
71 
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"));
89 }
90 
TEST(StringRefTest,Operators)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]);
106 }
107 
TEST(StringRefTest,Substr)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));
114 }
115 
TEST(StringRefTest,Slice)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));
123 }
124 
TEST(StringRefTest,Split)125 TEST(StringRefTest, Split) {
126   StringRef Str("hello");
127   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
128             Str.split('X'));
129   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
130             Str.split('e'));
131   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
132             Str.split('h'));
133   EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
134             Str.split('l'));
135   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
136             Str.split('o'));
137 
138   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
139             Str.rsplit('X'));
140   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
141             Str.rsplit('e'));
142   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
143             Str.rsplit('h'));
144   EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
145             Str.rsplit('l'));
146   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
147             Str.rsplit('o'));
148 }
149 
TEST(StringRefTest,Split2)150 TEST(StringRefTest, Split2) {
151   SmallVector<StringRef, 5> parts;
152   SmallVector<StringRef, 5> expected;
153 
154   expected.push_back("ab"); expected.push_back("c");
155   StringRef(",ab,,c,").split(parts, ",", -1, false);
156   EXPECT_TRUE(parts == expected);
157 
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);
163 
164   expected.clear(); parts.clear();
165   expected.push_back("");
166   StringRef("").split(parts, ",", -1, true);
167   EXPECT_TRUE(parts == expected);
168 
169   expected.clear(); parts.clear();
170   StringRef("").split(parts, ",", -1, false);
171   EXPECT_TRUE(parts == expected);
172 
173   expected.clear(); parts.clear();
174   StringRef(",").split(parts, ",", -1, false);
175   EXPECT_TRUE(parts == expected);
176 
177   expected.clear(); parts.clear();
178   expected.push_back(""); expected.push_back("");
179   StringRef(",").split(parts, ",", -1, true);
180   EXPECT_TRUE(parts == expected);
181 
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);
186 
187   // Test MaxSplit
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);
192 
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);
197 
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);
202 
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);
207 
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);
212 
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);
217 
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);
223 
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);
228 
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);
233 
234   expected.clear(); parts.clear();
235   expected.push_back("");
236   StringRef().split(parts, ",", 0, true);
237   EXPECT_TRUE(parts == expected);
238 
239   expected.clear(); parts.clear();
240   expected.push_back(StringRef());
241   StringRef("").split(parts, ",", 0, true);
242   EXPECT_TRUE(parts == expected);
243 
244   expected.clear(); parts.clear();
245   StringRef("").split(parts, ",", 0, false);
246   EXPECT_TRUE(parts == expected);
247   StringRef().split(parts, ",", 0, false);
248   EXPECT_TRUE(parts == expected);
249 
250   expected.clear(); parts.clear();
251   expected.push_back("a");
252   expected.push_back("");
253   expected.push_back("b");
254   expected.push_back("c,d");
255   StringRef("a,,b,c,d").split(parts, ",", 3, true);
256   EXPECT_TRUE(parts == expected);
257 
258   expected.clear(); parts.clear();
259   expected.push_back("");
260   StringRef().split(parts, ',', 0, true);
261   EXPECT_TRUE(parts == expected);
262 
263   expected.clear(); parts.clear();
264   expected.push_back(StringRef());
265   StringRef("").split(parts, ',', 0, true);
266   EXPECT_TRUE(parts == expected);
267 
268   expected.clear(); parts.clear();
269   StringRef("").split(parts, ',', 0, false);
270   EXPECT_TRUE(parts == expected);
271   StringRef().split(parts, ',', 0, false);
272   EXPECT_TRUE(parts == expected);
273 
274   expected.clear(); parts.clear();
275   expected.push_back("a");
276   expected.push_back("");
277   expected.push_back("b");
278   expected.push_back("c,d");
279   StringRef("a,,b,c,d").split(parts, ',', 3, true);
280   EXPECT_TRUE(parts == expected);
281 }
282 
TEST(StringRefTest,Trim)283 TEST(StringRefTest, Trim) {
284   StringRef Str0("hello");
285   StringRef Str1(" hello ");
286   StringRef Str2("  hello  ");
287 
288   EXPECT_EQ(StringRef("hello"), Str0.rtrim());
289   EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
290   EXPECT_EQ(StringRef("  hello"), Str2.rtrim());
291   EXPECT_EQ(StringRef("hello"), Str0.ltrim());
292   EXPECT_EQ(StringRef("hello "), Str1.ltrim());
293   EXPECT_EQ(StringRef("hello  "), Str2.ltrim());
294   EXPECT_EQ(StringRef("hello"), Str0.trim());
295   EXPECT_EQ(StringRef("hello"), Str1.trim());
296   EXPECT_EQ(StringRef("hello"), Str2.trim());
297 
298   EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
299 
300   EXPECT_EQ(StringRef(""), StringRef("").trim());
301   EXPECT_EQ(StringRef(""), StringRef(" ").trim());
302   EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
303   EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
304   EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
305 }
306 
TEST(StringRefTest,StartsWith)307 TEST(StringRefTest, StartsWith) {
308   StringRef Str("hello");
309   EXPECT_TRUE(Str.startswith(""));
310   EXPECT_TRUE(Str.startswith("he"));
311   EXPECT_FALSE(Str.startswith("helloworld"));
312   EXPECT_FALSE(Str.startswith("hi"));
313 }
314 
TEST(StringRefTest,StartsWithLower)315 TEST(StringRefTest, StartsWithLower) {
316   StringRef Str("heLLo");
317   EXPECT_TRUE(Str.startswith_lower(""));
318   EXPECT_TRUE(Str.startswith_lower("he"));
319   EXPECT_TRUE(Str.startswith_lower("hell"));
320   EXPECT_TRUE(Str.startswith_lower("HELlo"));
321   EXPECT_FALSE(Str.startswith_lower("helloworld"));
322   EXPECT_FALSE(Str.startswith_lower("hi"));
323 }
324 
TEST(StringRefTest,EndsWith)325 TEST(StringRefTest, EndsWith) {
326   StringRef Str("hello");
327   EXPECT_TRUE(Str.endswith(""));
328   EXPECT_TRUE(Str.endswith("lo"));
329   EXPECT_FALSE(Str.endswith("helloworld"));
330   EXPECT_FALSE(Str.endswith("worldhello"));
331   EXPECT_FALSE(Str.endswith("so"));
332 }
333 
TEST(StringRefTest,EndsWithLower)334 TEST(StringRefTest, EndsWithLower) {
335   StringRef Str("heLLo");
336   EXPECT_TRUE(Str.endswith_lower(""));
337   EXPECT_TRUE(Str.endswith_lower("lo"));
338   EXPECT_TRUE(Str.endswith_lower("LO"));
339   EXPECT_TRUE(Str.endswith_lower("ELlo"));
340   EXPECT_FALSE(Str.endswith_lower("helloworld"));
341   EXPECT_FALSE(Str.endswith_lower("hi"));
342 }
343 
TEST(StringRefTest,Find)344 TEST(StringRefTest, Find) {
345   StringRef Str("hello");
346   EXPECT_EQ(2U, Str.find('l'));
347   EXPECT_EQ(StringRef::npos, Str.find('z'));
348   EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
349   EXPECT_EQ(0U, Str.find("hello"));
350   EXPECT_EQ(1U, Str.find("ello"));
351   EXPECT_EQ(StringRef::npos, Str.find("zz"));
352   EXPECT_EQ(2U, Str.find("ll", 2));
353   EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
354   EXPECT_EQ(0U, Str.find(""));
355   StringRef LongStr("hellx xello hell ello world foo bar hello");
356   EXPECT_EQ(36U, LongStr.find("hello"));
357   EXPECT_EQ(28U, LongStr.find("foo"));
358   EXPECT_EQ(12U, LongStr.find("hell", 2));
359   EXPECT_EQ(0U, LongStr.find(""));
360 
361   EXPECT_EQ(3U, Str.rfind('l'));
362   EXPECT_EQ(StringRef::npos, Str.rfind('z'));
363   EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
364   EXPECT_EQ(0U, Str.rfind("hello"));
365   EXPECT_EQ(1U, Str.rfind("ello"));
366   EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
367 
368   EXPECT_EQ(2U, Str.find_first_of('l'));
369   EXPECT_EQ(1U, Str.find_first_of("el"));
370   EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
371 
372   EXPECT_EQ(1U, Str.find_first_not_of('h'));
373   EXPECT_EQ(4U, Str.find_first_not_of("hel"));
374   EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
375 
376   EXPECT_EQ(3U, Str.find_last_not_of('o'));
377   EXPECT_EQ(1U, Str.find_last_not_of("lo"));
378   EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
379 }
380 
TEST(StringRefTest,Count)381 TEST(StringRefTest, Count) {
382   StringRef Str("hello");
383   EXPECT_EQ(2U, Str.count('l'));
384   EXPECT_EQ(1U, Str.count('o'));
385   EXPECT_EQ(0U, Str.count('z'));
386   EXPECT_EQ(0U, Str.count("helloworld"));
387   EXPECT_EQ(1U, Str.count("hello"));
388   EXPECT_EQ(1U, Str.count("ello"));
389   EXPECT_EQ(0U, Str.count("zz"));
390 }
391 
TEST(StringRefTest,EditDistance)392 TEST(StringRefTest, EditDistance) {
393   StringRef Str("hello");
394   EXPECT_EQ(2U, Str.edit_distance("hill"));
395 }
396 
TEST(StringRefTest,Misc)397 TEST(StringRefTest, Misc) {
398   std::string Storage;
399   raw_string_ostream OS(Storage);
400   OS << StringRef("hello");
401   EXPECT_EQ("hello", OS.str());
402 }
403 
TEST(StringRefTest,Hashing)404 TEST(StringRefTest, Hashing) {
405   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
406   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
407   std::string S = "hello world";
408   hash_code H = hash_value(S);
409   EXPECT_EQ(H, hash_value(StringRef("hello world")));
410   EXPECT_EQ(H, hash_value(StringRef(S)));
411   EXPECT_NE(H, hash_value(StringRef("hello worl")));
412   EXPECT_EQ(hash_value(std::string("hello worl")),
413             hash_value(StringRef("hello worl")));
414   EXPECT_NE(H, hash_value(StringRef("hello world ")));
415   EXPECT_EQ(hash_value(std::string("hello world ")),
416             hash_value(StringRef("hello world ")));
417   EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
418   EXPECT_NE(hash_value(std::string("ello worl")),
419             hash_value(StringRef("hello world").slice(1, -1)));
420 }
421 
422 struct UnsignedPair {
423   const char *Str;
424   uint64_t Expected;
425 } Unsigned[] =
426   { {"0", 0}
427   , {"255", 255}
428   , {"256", 256}
429   , {"65535", 65535}
430   , {"65536", 65536}
431   , {"4294967295", 4294967295ULL}
432   , {"4294967296", 4294967296ULL}
433   , {"18446744073709551615", 18446744073709551615ULL}
434   , {"042", 34}
435   , {"0x42", 66}
436   , {"0b101010", 42}
437   };
438 
439 struct SignedPair {
440   const char *Str;
441   int64_t Expected;
442 } Signed[] =
443   { {"0", 0}
444   , {"-0", 0}
445   , {"127", 127}
446   , {"128", 128}
447   , {"-128", -128}
448   , {"-129", -129}
449   , {"32767", 32767}
450   , {"32768", 32768}
451   , {"-32768", -32768}
452   , {"-32769", -32769}
453   , {"2147483647", 2147483647LL}
454   , {"2147483648", 2147483648LL}
455   , {"-2147483648", -2147483648LL}
456   , {"-2147483649", -2147483649LL}
457   , {"-9223372036854775808", -(9223372036854775807LL) - 1}
458   , {"042", 34}
459   , {"0x42", 66}
460   , {"0b101010", 42}
461   , {"-042", -34}
462   , {"-0x42", -66}
463   , {"-0b101010", -42}
464   };
465 
TEST(StringRefTest,getAsInteger)466 TEST(StringRefTest, getAsInteger) {
467   uint8_t U8;
468   uint16_t U16;
469   uint32_t U32;
470   uint64_t U64;
471 
472   for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
473     bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
474     if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
475       ASSERT_FALSE(U8Success);
476       EXPECT_EQ(U8, Unsigned[i].Expected);
477     } else {
478       ASSERT_TRUE(U8Success);
479     }
480     bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
481     if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
482       ASSERT_FALSE(U16Success);
483       EXPECT_EQ(U16, Unsigned[i].Expected);
484     } else {
485       ASSERT_TRUE(U16Success);
486     }
487     bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
488     if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
489       ASSERT_FALSE(U32Success);
490       EXPECT_EQ(U32, Unsigned[i].Expected);
491     } else {
492       ASSERT_TRUE(U32Success);
493     }
494     bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
495     if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
496       ASSERT_FALSE(U64Success);
497       EXPECT_EQ(U64, Unsigned[i].Expected);
498     } else {
499       ASSERT_TRUE(U64Success);
500     }
501   }
502 
503   int8_t S8;
504   int16_t S16;
505   int32_t S32;
506   int64_t S64;
507 
508   for (size_t i = 0; i < array_lengthof(Signed); ++i) {
509     bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
510     if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
511       ASSERT_FALSE(S8Success);
512       EXPECT_EQ(S8, Signed[i].Expected);
513     } else {
514       ASSERT_TRUE(S8Success);
515     }
516     bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
517     if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
518       ASSERT_FALSE(S16Success);
519       EXPECT_EQ(S16, Signed[i].Expected);
520     } else {
521       ASSERT_TRUE(S16Success);
522     }
523     bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
524     if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
525       ASSERT_FALSE(S32Success);
526       EXPECT_EQ(S32, Signed[i].Expected);
527     } else {
528       ASSERT_TRUE(S32Success);
529     }
530     bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
531     if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
532       ASSERT_FALSE(S64Success);
533       EXPECT_EQ(S64, Signed[i].Expected);
534     } else {
535       ASSERT_TRUE(S64Success);
536     }
537   }
538 }
539 
540 
541 static const char* BadStrings[] = {
542     "18446744073709551617"  // value just over max
543   , "123456789012345678901" // value way too large
544   , "4t23v"                 // illegal decimal characters
545   , "0x123W56"              // illegal hex characters
546   , "0b2"                   // illegal bin characters
547   , "08"                    // illegal oct characters
548   , "0o8"                   // illegal oct characters
549   , "-123"                  // negative unsigned value
550 };
551 
552 
TEST(StringRefTest,getAsUnsignedIntegerBadStrings)553 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
554   unsigned long long U64;
555   for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
556     bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
557     ASSERT_TRUE(IsBadNumber);
558   }
559 }
560 
561 static const char *join_input[] = { "a", "b", "c" };
562 static const char join_result1[] = "a";
563 static const char join_result2[] = "a:b:c";
564 static const char join_result3[] = "a::b::c";
565 
TEST(StringRefTest,joinStrings)566 TEST(StringRefTest, joinStrings) {
567   std::vector<StringRef> v1;
568   std::vector<std::string> v2;
569   for (size_t i = 0; i < array_lengthof(join_input); ++i) {
570     v1.push_back(join_input[i]);
571     v2.push_back(join_input[i]);
572   }
573 
574   bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
575   EXPECT_TRUE(v1_join1);
576   bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
577   EXPECT_TRUE(v1_join2);
578   bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
579   EXPECT_TRUE(v1_join3);
580 
581   bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
582   EXPECT_TRUE(v2_join1);
583   bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
584   EXPECT_TRUE(v2_join2);
585   bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
586   EXPECT_TRUE(v2_join3);
587 }
588 
589 
TEST(StringRefTest,AllocatorCopy)590 TEST(StringRefTest, AllocatorCopy) {
591   BumpPtrAllocator Alloc;
592   StringRef Str1 = "hello";
593   StringRef Str2 = "bye";
594   StringRef Str1c = Str1.copy(Alloc);
595   StringRef Str2c = Str2.copy(Alloc);
596   EXPECT_TRUE(Str1.equals(Str1c));
597   EXPECT_NE(Str1.data(), Str1c.data());
598   EXPECT_TRUE(Str2.equals(Str2c));
599   EXPECT_NE(Str2.data(), Str2c.data());
600 }
601 
602 
603 } // end anonymous namespace
604