xref: /llvm-project/llvm/unittests/ADT/StringRefTest.cpp (revision fcae62d6eeabf0eb926555d56e6a79d880ef094b)
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 
22 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
23   OS << S.str();
24   return OS;
25 }
26 
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 // Check that we can't accidentally assign a temporary std::string to a
36 // StringRef. (Unfortunately we can't make use of the same thing with
37 // constructors.)
38 //
39 // Disable this check under MSVC; even MSVC 2015 isn't consistent between
40 // std::is_assignable and actually writing such an assignment.
41 #if !defined(_MSC_VER)
42 static_assert(
43     !std::is_assignable<StringRef, std::string>::value,
44     "Assigning from prvalue std::string");
45 static_assert(
46     !std::is_assignable<StringRef, std::string &&>::value,
47     "Assigning from xvalue std::string");
48 static_assert(
49     std::is_assignable<StringRef, std::string &>::value,
50     "Assigning from lvalue std::string");
51 static_assert(
52     std::is_assignable<StringRef, const char *>::value,
53     "Assigning from prvalue C string");
54 static_assert(
55     std::is_assignable<StringRef, const char * &&>::value,
56     "Assigning from xvalue C string");
57 static_assert(
58     std::is_assignable<StringRef, const char * &>::value,
59     "Assigning from lvalue C string");
60 #endif
61 
62 
63 namespace {
64 TEST(StringRefTest, Construction) {
65   EXPECT_EQ("", StringRef());
66   EXPECT_EQ("hello", StringRef("hello"));
67   EXPECT_EQ("hello", StringRef("hello world", 5));
68   EXPECT_EQ("hello", StringRef(std::string("hello")));
69 }
70 
71 TEST(StringRefTest, EmptyInitializerList) {
72   StringRef S = {};
73   EXPECT_TRUE(S.empty());
74 
75   S = {};
76   EXPECT_TRUE(S.empty());
77 }
78 
79 TEST(StringRefTest, Iteration) {
80   StringRef S("hello");
81   const char *p = "hello";
82   for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
83     EXPECT_EQ(*it, *p);
84 }
85 
86 TEST(StringRefTest, StringOps) {
87   const char *p = "hello";
88   EXPECT_EQ(p, StringRef(p, 0).data());
89   EXPECT_TRUE(StringRef().empty());
90   EXPECT_EQ((size_t) 5, StringRef("hello").size());
91   EXPECT_EQ(-1, StringRef("aab").compare("aad"));
92   EXPECT_EQ( 0, StringRef("aab").compare("aab"));
93   EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
94   EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
95   EXPECT_EQ( 1, StringRef("aab").compare("aa"));
96   EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
97 
98   EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
99   EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
100   EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
101   EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
102   EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
103   EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
104   EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
105   EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
106   EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
107 
108   EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
109   EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
110   EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
111   EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
112   EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
113   EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
114   EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
115   EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
116   EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
117   EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
118   EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
119   EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
120   EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
121   EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
122   EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
123   EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
124   EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
125 }
126 
127 TEST(StringRefTest, Operators) {
128   EXPECT_EQ("", StringRef());
129   EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
130   EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
131   EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
132   EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
133   EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
134   EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
135   EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
136   EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
137   EXPECT_EQ(StringRef("aab"), StringRef("aab"));
138   EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
139   EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
140   EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
141   EXPECT_EQ('a', StringRef("aab")[1]);
142 }
143 
144 TEST(StringRefTest, Substr) {
145   StringRef Str("hello");
146   EXPECT_EQ("lo", Str.substr(3));
147   EXPECT_EQ("", Str.substr(100));
148   EXPECT_EQ("hello", Str.substr(0, 100));
149   EXPECT_EQ("o", Str.substr(4, 10));
150 }
151 
152 TEST(StringRefTest, Slice) {
153   StringRef Str("hello");
154   EXPECT_EQ("l", Str.slice(2, 3));
155   EXPECT_EQ("ell", Str.slice(1, 4));
156   EXPECT_EQ("llo", Str.slice(2, 100));
157   EXPECT_EQ("", Str.slice(2, 1));
158   EXPECT_EQ("", Str.slice(10, 20));
159 }
160 
161 TEST(StringRefTest, Split) {
162   StringRef Str("hello");
163   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
164             Str.split('X'));
165   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
166             Str.split('e'));
167   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
168             Str.split('h'));
169   EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
170             Str.split('l'));
171   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
172             Str.split('o'));
173 
174   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
175             Str.rsplit('X'));
176   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
177             Str.rsplit('e'));
178   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
179             Str.rsplit('h'));
180   EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
181             Str.rsplit('l'));
182   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
183             Str.rsplit('o'));
184 }
185 
186 TEST(StringRefTest, Split2) {
187   SmallVector<StringRef, 5> parts;
188   SmallVector<StringRef, 5> expected;
189 
190   expected.push_back("ab"); expected.push_back("c");
191   StringRef(",ab,,c,").split(parts, ",", -1, false);
192   EXPECT_TRUE(parts == expected);
193 
194   expected.clear(); parts.clear();
195   expected.push_back(""); expected.push_back("ab"); expected.push_back("");
196   expected.push_back("c"); expected.push_back("");
197   StringRef(",ab,,c,").split(parts, ",", -1, true);
198   EXPECT_TRUE(parts == expected);
199 
200   expected.clear(); parts.clear();
201   expected.push_back("");
202   StringRef("").split(parts, ",", -1, true);
203   EXPECT_TRUE(parts == expected);
204 
205   expected.clear(); parts.clear();
206   StringRef("").split(parts, ",", -1, false);
207   EXPECT_TRUE(parts == expected);
208 
209   expected.clear(); parts.clear();
210   StringRef(",").split(parts, ",", -1, false);
211   EXPECT_TRUE(parts == expected);
212 
213   expected.clear(); parts.clear();
214   expected.push_back(""); expected.push_back("");
215   StringRef(",").split(parts, ",", -1, true);
216   EXPECT_TRUE(parts == expected);
217 
218   expected.clear(); parts.clear();
219   expected.push_back("a"); expected.push_back("b");
220   StringRef("a,b").split(parts, ",", -1, true);
221   EXPECT_TRUE(parts == expected);
222 
223   // Test MaxSplit
224   expected.clear(); parts.clear();
225   expected.push_back("a,,b,c");
226   StringRef("a,,b,c").split(parts, ",", 0, true);
227   EXPECT_TRUE(parts == expected);
228 
229   expected.clear(); parts.clear();
230   expected.push_back("a,,b,c");
231   StringRef("a,,b,c").split(parts, ",", 0, false);
232   EXPECT_TRUE(parts == expected);
233 
234   expected.clear(); parts.clear();
235   expected.push_back("a"); expected.push_back(",b,c");
236   StringRef("a,,b,c").split(parts, ",", 1, true);
237   EXPECT_TRUE(parts == expected);
238 
239   expected.clear(); parts.clear();
240   expected.push_back("a"); expected.push_back(",b,c");
241   StringRef("a,,b,c").split(parts, ",", 1, false);
242   EXPECT_TRUE(parts == expected);
243 
244   expected.clear(); parts.clear();
245   expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
246   StringRef("a,,b,c").split(parts, ",", 2, true);
247   EXPECT_TRUE(parts == expected);
248 
249   expected.clear(); parts.clear();
250   expected.push_back("a"); expected.push_back("b,c");
251   StringRef("a,,b,c").split(parts, ",", 2, false);
252   EXPECT_TRUE(parts == expected);
253 
254   expected.clear(); parts.clear();
255   expected.push_back("a"); expected.push_back(""); expected.push_back("b");
256   expected.push_back("c");
257   StringRef("a,,b,c").split(parts, ",", 3, true);
258   EXPECT_TRUE(parts == expected);
259 
260   expected.clear(); parts.clear();
261   expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
262   StringRef("a,,b,c").split(parts, ",", 3, false);
263   EXPECT_TRUE(parts == expected);
264 
265   expected.clear(); parts.clear();
266   expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
267   StringRef("a,,b,c").split(parts, ',', 3, false);
268   EXPECT_TRUE(parts == expected);
269 
270   expected.clear(); parts.clear();
271   expected.push_back("");
272   StringRef().split(parts, ",", 0, true);
273   EXPECT_TRUE(parts == expected);
274 
275   expected.clear(); parts.clear();
276   expected.push_back(StringRef());
277   StringRef("").split(parts, ",", 0, true);
278   EXPECT_TRUE(parts == expected);
279 
280   expected.clear(); parts.clear();
281   StringRef("").split(parts, ",", 0, false);
282   EXPECT_TRUE(parts == expected);
283   StringRef().split(parts, ",", 0, false);
284   EXPECT_TRUE(parts == expected);
285 
286   expected.clear(); parts.clear();
287   expected.push_back("a");
288   expected.push_back("");
289   expected.push_back("b");
290   expected.push_back("c,d");
291   StringRef("a,,b,c,d").split(parts, ",", 3, true);
292   EXPECT_TRUE(parts == expected);
293 
294   expected.clear(); parts.clear();
295   expected.push_back("");
296   StringRef().split(parts, ',', 0, true);
297   EXPECT_TRUE(parts == expected);
298 
299   expected.clear(); parts.clear();
300   expected.push_back(StringRef());
301   StringRef("").split(parts, ',', 0, true);
302   EXPECT_TRUE(parts == expected);
303 
304   expected.clear(); parts.clear();
305   StringRef("").split(parts, ',', 0, false);
306   EXPECT_TRUE(parts == expected);
307   StringRef().split(parts, ',', 0, false);
308   EXPECT_TRUE(parts == expected);
309 
310   expected.clear(); parts.clear();
311   expected.push_back("a");
312   expected.push_back("");
313   expected.push_back("b");
314   expected.push_back("c,d");
315   StringRef("a,,b,c,d").split(parts, ',', 3, true);
316   EXPECT_TRUE(parts == expected);
317 }
318 
319 TEST(StringRefTest, Trim) {
320   StringRef Str0("hello");
321   StringRef Str1(" hello ");
322   StringRef Str2("  hello  ");
323 
324   EXPECT_EQ(StringRef("hello"), Str0.rtrim());
325   EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
326   EXPECT_EQ(StringRef("  hello"), Str2.rtrim());
327   EXPECT_EQ(StringRef("hello"), Str0.ltrim());
328   EXPECT_EQ(StringRef("hello "), Str1.ltrim());
329   EXPECT_EQ(StringRef("hello  "), Str2.ltrim());
330   EXPECT_EQ(StringRef("hello"), Str0.trim());
331   EXPECT_EQ(StringRef("hello"), Str1.trim());
332   EXPECT_EQ(StringRef("hello"), Str2.trim());
333 
334   EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
335 
336   EXPECT_EQ(StringRef(""), StringRef("").trim());
337   EXPECT_EQ(StringRef(""), StringRef(" ").trim());
338   EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
339   EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
340   EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
341 }
342 
343 TEST(StringRefTest, StartsWith) {
344   StringRef Str("hello");
345   EXPECT_TRUE(Str.startswith(""));
346   EXPECT_TRUE(Str.startswith("he"));
347   EXPECT_FALSE(Str.startswith("helloworld"));
348   EXPECT_FALSE(Str.startswith("hi"));
349 }
350 
351 TEST(StringRefTest, StartsWithLower) {
352   StringRef Str("heLLo");
353   EXPECT_TRUE(Str.startswith_lower(""));
354   EXPECT_TRUE(Str.startswith_lower("he"));
355   EXPECT_TRUE(Str.startswith_lower("hell"));
356   EXPECT_TRUE(Str.startswith_lower("HELlo"));
357   EXPECT_FALSE(Str.startswith_lower("helloworld"));
358   EXPECT_FALSE(Str.startswith_lower("hi"));
359 }
360 
361 TEST(StringRefTest, ConsumeFront) {
362   StringRef Str("hello");
363   EXPECT_TRUE(Str.consume_front(""));
364   EXPECT_EQ("hello", Str);
365   EXPECT_TRUE(Str.consume_front("he"));
366   EXPECT_EQ("llo", Str);
367   EXPECT_FALSE(Str.consume_front("lloworld"));
368   EXPECT_EQ("llo", Str);
369   EXPECT_FALSE(Str.consume_front("lol"));
370   EXPECT_EQ("llo", Str);
371   EXPECT_TRUE(Str.consume_front("llo"));
372   EXPECT_EQ("", Str);
373   EXPECT_FALSE(Str.consume_front("o"));
374   EXPECT_TRUE(Str.consume_front(""));
375 }
376 
377 TEST(StringRefTest, EndsWith) {
378   StringRef Str("hello");
379   EXPECT_TRUE(Str.endswith(""));
380   EXPECT_TRUE(Str.endswith("lo"));
381   EXPECT_FALSE(Str.endswith("helloworld"));
382   EXPECT_FALSE(Str.endswith("worldhello"));
383   EXPECT_FALSE(Str.endswith("so"));
384 }
385 
386 TEST(StringRefTest, EndsWithLower) {
387   StringRef Str("heLLo");
388   EXPECT_TRUE(Str.endswith_lower(""));
389   EXPECT_TRUE(Str.endswith_lower("lo"));
390   EXPECT_TRUE(Str.endswith_lower("LO"));
391   EXPECT_TRUE(Str.endswith_lower("ELlo"));
392   EXPECT_FALSE(Str.endswith_lower("helloworld"));
393   EXPECT_FALSE(Str.endswith_lower("hi"));
394 }
395 
396 TEST(StringRefTest, ConsumeBack) {
397   StringRef Str("hello");
398   EXPECT_TRUE(Str.consume_back(""));
399   EXPECT_EQ("hello", Str);
400   EXPECT_TRUE(Str.consume_back("lo"));
401   EXPECT_EQ("hel", Str);
402   EXPECT_FALSE(Str.consume_back("helhel"));
403   EXPECT_EQ("hel", Str);
404   EXPECT_FALSE(Str.consume_back("hle"));
405   EXPECT_EQ("hel", Str);
406   EXPECT_TRUE(Str.consume_back("hel"));
407   EXPECT_EQ("", Str);
408   EXPECT_FALSE(Str.consume_back("h"));
409   EXPECT_TRUE(Str.consume_back(""));
410 }
411 
412 TEST(StringRefTest, Find) {
413   StringRef Str("helloHELLO");
414   StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
415 
416   struct {
417     StringRef Str;
418     char C;
419     std::size_t From;
420     std::size_t Pos;
421     std::size_t LowerPos;
422   } CharExpectations[] = {
423       {Str, 'h', 0U, 0U, 0U},
424       {Str, 'e', 0U, 1U, 1U},
425       {Str, 'l', 0U, 2U, 2U},
426       {Str, 'l', 3U, 3U, 3U},
427       {Str, 'o', 0U, 4U, 4U},
428       {Str, 'L', 0U, 7U, 2U},
429       {Str, 'z', 0U, StringRef::npos, StringRef::npos},
430   };
431 
432   struct {
433     StringRef Str;
434     llvm::StringRef S;
435     std::size_t From;
436     std::size_t Pos;
437     std::size_t LowerPos;
438   } StrExpectations[] = {
439       {Str, "helloword", 0, StringRef::npos, StringRef::npos},
440       {Str, "hello", 0, 0U, 0U},
441       {Str, "ello", 0, 1U, 1U},
442       {Str, "zz", 0, StringRef::npos, StringRef::npos},
443       {Str, "ll", 2U, 2U, 2U},
444       {Str, "ll", 3U, StringRef::npos, 7U},
445       {Str, "LL", 2U, 7U, 2U},
446       {Str, "LL", 3U, 7U, 7U},
447       {Str, "", 0U, 0U, 0U},
448       {LongStr, "hello", 0U, 36U, 36U},
449       {LongStr, "foo", 0U, 28U, 28U},
450       {LongStr, "hell", 2U, 12U, 12U},
451       {LongStr, "HELL", 2U, 42U, 12U},
452       {LongStr, "", 0U, 0U, 0U}};
453 
454   for (auto &E : CharExpectations) {
455     EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From));
456     EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.C, E.From));
457     EXPECT_EQ(E.LowerPos, E.Str.find_lower(toupper(E.C), E.From));
458   }
459 
460   for (auto &E : StrExpectations) {
461     EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From));
462     EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S, E.From));
463     EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S.upper(), E.From));
464   }
465 
466   EXPECT_EQ(3U, Str.rfind('l'));
467   EXPECT_EQ(StringRef::npos, Str.rfind('z'));
468   EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
469   EXPECT_EQ(0U, Str.rfind("hello"));
470   EXPECT_EQ(1U, Str.rfind("ello"));
471   EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
472 
473   EXPECT_EQ(8U, Str.rfind_lower('l'));
474   EXPECT_EQ(8U, Str.rfind_lower('L'));
475   EXPECT_EQ(StringRef::npos, Str.rfind_lower('z'));
476   EXPECT_EQ(StringRef::npos, Str.rfind_lower("HELLOWORLD"));
477   EXPECT_EQ(5U, Str.rfind("HELLO"));
478   EXPECT_EQ(6U, Str.rfind("ELLO"));
479   EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));
480 
481   EXPECT_EQ(2U, Str.find_first_of('l'));
482   EXPECT_EQ(1U, Str.find_first_of("el"));
483   EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
484 
485   Str = "hello";
486   EXPECT_EQ(1U, Str.find_first_not_of('h'));
487   EXPECT_EQ(4U, Str.find_first_not_of("hel"));
488   EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
489 
490   EXPECT_EQ(3U, Str.find_last_not_of('o'));
491   EXPECT_EQ(1U, Str.find_last_not_of("lo"));
492   EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
493 }
494 
495 TEST(StringRefTest, Count) {
496   StringRef Str("hello");
497   EXPECT_EQ(2U, Str.count('l'));
498   EXPECT_EQ(1U, Str.count('o'));
499   EXPECT_EQ(0U, Str.count('z'));
500   EXPECT_EQ(0U, Str.count("helloworld"));
501   EXPECT_EQ(1U, Str.count("hello"));
502   EXPECT_EQ(1U, Str.count("ello"));
503   EXPECT_EQ(0U, Str.count("zz"));
504 }
505 
506 TEST(StringRefTest, EditDistance) {
507   StringRef Hello("hello");
508   EXPECT_EQ(2U, Hello.edit_distance("hill"));
509 
510   StringRef Industry("industry");
511   EXPECT_EQ(6U, Industry.edit_distance("interest"));
512 
513   StringRef Soylent("soylent green is people");
514   EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green"));
515   EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green",
516                                       /* allow replacements = */ false));
517   EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green",
518                                       /* allow replacements = */ true,
519                                       /* max edit distance = */ 8));
520   EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green "
521                                        "people soiled our green "
522                                        "people soiled our green "));
523 }
524 
525 TEST(StringRefTest, Misc) {
526   std::string Storage;
527   raw_string_ostream OS(Storage);
528   OS << StringRef("hello");
529   EXPECT_EQ("hello", OS.str());
530 }
531 
532 TEST(StringRefTest, Hashing) {
533   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
534   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
535   std::string S = "hello world";
536   hash_code H = hash_value(S);
537   EXPECT_EQ(H, hash_value(StringRef("hello world")));
538   EXPECT_EQ(H, hash_value(StringRef(S)));
539   EXPECT_NE(H, hash_value(StringRef("hello worl")));
540   EXPECT_EQ(hash_value(std::string("hello worl")),
541             hash_value(StringRef("hello worl")));
542   EXPECT_NE(H, hash_value(StringRef("hello world ")));
543   EXPECT_EQ(hash_value(std::string("hello world ")),
544             hash_value(StringRef("hello world ")));
545   EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
546   EXPECT_NE(hash_value(std::string("ello worl")),
547             hash_value(StringRef("hello world").slice(1, -1)));
548 }
549 
550 struct UnsignedPair {
551   const char *Str;
552   uint64_t Expected;
553 } Unsigned[] =
554   { {"0", 0}
555   , {"255", 255}
556   , {"256", 256}
557   , {"65535", 65535}
558   , {"65536", 65536}
559   , {"4294967295", 4294967295ULL}
560   , {"4294967296", 4294967296ULL}
561   , {"18446744073709551615", 18446744073709551615ULL}
562   , {"042", 34}
563   , {"0x42", 66}
564   , {"0b101010", 42}
565   };
566 
567 struct SignedPair {
568   const char *Str;
569   int64_t Expected;
570 } Signed[] =
571   { {"0", 0}
572   , {"-0", 0}
573   , {"127", 127}
574   , {"128", 128}
575   , {"-128", -128}
576   , {"-129", -129}
577   , {"32767", 32767}
578   , {"32768", 32768}
579   , {"-32768", -32768}
580   , {"-32769", -32769}
581   , {"2147483647", 2147483647LL}
582   , {"2147483648", 2147483648LL}
583   , {"-2147483648", -2147483648LL}
584   , {"-2147483649", -2147483649LL}
585   , {"-9223372036854775808", -(9223372036854775807LL) - 1}
586   , {"042", 34}
587   , {"0x42", 66}
588   , {"0b101010", 42}
589   , {"-042", -34}
590   , {"-0x42", -66}
591   , {"-0b101010", -42}
592   };
593 
594 TEST(StringRefTest, getAsInteger) {
595   uint8_t U8;
596   uint16_t U16;
597   uint32_t U32;
598   uint64_t U64;
599 
600   for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
601     bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
602     if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
603       ASSERT_FALSE(U8Success);
604       EXPECT_EQ(U8, Unsigned[i].Expected);
605     } else {
606       ASSERT_TRUE(U8Success);
607     }
608     bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
609     if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
610       ASSERT_FALSE(U16Success);
611       EXPECT_EQ(U16, Unsigned[i].Expected);
612     } else {
613       ASSERT_TRUE(U16Success);
614     }
615     bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
616     if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
617       ASSERT_FALSE(U32Success);
618       EXPECT_EQ(U32, Unsigned[i].Expected);
619     } else {
620       ASSERT_TRUE(U32Success);
621     }
622     bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
623     if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
624       ASSERT_FALSE(U64Success);
625       EXPECT_EQ(U64, Unsigned[i].Expected);
626     } else {
627       ASSERT_TRUE(U64Success);
628     }
629   }
630 
631   int8_t S8;
632   int16_t S16;
633   int32_t S32;
634   int64_t S64;
635 
636   for (size_t i = 0; i < array_lengthof(Signed); ++i) {
637     bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
638     if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
639       ASSERT_FALSE(S8Success);
640       EXPECT_EQ(S8, Signed[i].Expected);
641     } else {
642       ASSERT_TRUE(S8Success);
643     }
644     bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
645     if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
646       ASSERT_FALSE(S16Success);
647       EXPECT_EQ(S16, Signed[i].Expected);
648     } else {
649       ASSERT_TRUE(S16Success);
650     }
651     bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
652     if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
653       ASSERT_FALSE(S32Success);
654       EXPECT_EQ(S32, Signed[i].Expected);
655     } else {
656       ASSERT_TRUE(S32Success);
657     }
658     bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
659     if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
660       ASSERT_FALSE(S64Success);
661       EXPECT_EQ(S64, Signed[i].Expected);
662     } else {
663       ASSERT_TRUE(S64Success);
664     }
665   }
666 }
667 
668 
669 static const char* BadStrings[] = {
670     ""                      // empty string
671   , "18446744073709551617"  // value just over max
672   , "123456789012345678901" // value way too large
673   , "4t23v"                 // illegal decimal characters
674   , "0x123W56"              // illegal hex characters
675   , "0b2"                   // illegal bin characters
676   , "08"                    // illegal oct characters
677   , "0o8"                   // illegal oct characters
678   , "-123"                  // negative unsigned value
679   , "0x"
680   , "0b"
681 };
682 
683 
684 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
685   unsigned long long U64;
686   for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
687     bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
688     ASSERT_TRUE(IsBadNumber);
689   }
690 }
691 
692 struct ConsumeUnsignedPair {
693   const char *Str;
694   uint64_t Expected;
695   const char *Leftover;
696 } ConsumeUnsigned[] = {
697     {"0", 0, ""},
698     {"255", 255, ""},
699     {"256", 256, ""},
700     {"65535", 65535, ""},
701     {"65536", 65536, ""},
702     {"4294967295", 4294967295ULL, ""},
703     {"4294967296", 4294967296ULL, ""},
704     {"255A376", 255, "A376"},
705     {"18446744073709551615", 18446744073709551615ULL, ""},
706     {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
707     {"042", 34, ""},
708     {"0x42", 66, ""},
709     {"0x42-0x34", 66, "-0x34"},
710     {"0b101010", 42, ""},
711     {"0429F", 042, "9F"},            // Auto-sensed octal radix, invalid digit
712     {"0x42G12", 0x42, "G12"},        // Auto-sensed hex radix, invalid digit
713     {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
714 
715 struct ConsumeSignedPair {
716   const char *Str;
717   int64_t Expected;
718   const char *Leftover;
719 } ConsumeSigned[] = {
720     {"0", 0, ""},
721     {"-0", 0, ""},
722     {"0-1", 0, "-1"},
723     {"-0-1", 0, "-1"},
724     {"127", 127, ""},
725     {"128", 128, ""},
726     {"127-1", 127, "-1"},
727     {"128-1", 128, "-1"},
728     {"-128", -128, ""},
729     {"-129", -129, ""},
730     {"-128-1", -128, "-1"},
731     {"-129-1", -129, "-1"},
732     {"32767", 32767, ""},
733     {"32768", 32768, ""},
734     {"32767-1", 32767, "-1"},
735     {"32768-1", 32768, "-1"},
736     {"-32768", -32768, ""},
737     {"-32769", -32769, ""},
738     {"-32768-1", -32768, "-1"},
739     {"-32769-1", -32769, "-1"},
740     {"2147483647", 2147483647LL, ""},
741     {"2147483648", 2147483648LL, ""},
742     {"2147483647-1", 2147483647LL, "-1"},
743     {"2147483648-1", 2147483648LL, "-1"},
744     {"-2147483648", -2147483648LL, ""},
745     {"-2147483649", -2147483649LL, ""},
746     {"-2147483648-1", -2147483648LL, "-1"},
747     {"-2147483649-1", -2147483649LL, "-1"},
748     {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
749     {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
750     {"042", 34, ""},
751     {"042-1", 34, "-1"},
752     {"0x42", 66, ""},
753     {"0x42-1", 66, "-1"},
754     {"0b101010", 42, ""},
755     {"0b101010-1", 42, "-1"},
756     {"-042", -34, ""},
757     {"-042-1", -34, "-1"},
758     {"-0x42", -66, ""},
759     {"-0x42-1", -66, "-1"},
760     {"-0b101010", -42, ""},
761     {"-0b101010-1", -42, "-1"}};
762 
763 TEST(StringRefTest, consumeIntegerUnsigned) {
764   uint8_t U8;
765   uint16_t U16;
766   uint32_t U32;
767   uint64_t U64;
768 
769   for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) {
770     StringRef Str = ConsumeUnsigned[i].Str;
771     bool U8Success = Str.consumeInteger(0, U8);
772     if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==
773         ConsumeUnsigned[i].Expected) {
774       ASSERT_FALSE(U8Success);
775       EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);
776       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
777     } else {
778       ASSERT_TRUE(U8Success);
779     }
780 
781     Str = ConsumeUnsigned[i].Str;
782     bool U16Success = Str.consumeInteger(0, U16);
783     if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==
784         ConsumeUnsigned[i].Expected) {
785       ASSERT_FALSE(U16Success);
786       EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);
787       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
788     } else {
789       ASSERT_TRUE(U16Success);
790     }
791 
792     Str = ConsumeUnsigned[i].Str;
793     bool U32Success = Str.consumeInteger(0, U32);
794     if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==
795         ConsumeUnsigned[i].Expected) {
796       ASSERT_FALSE(U32Success);
797       EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);
798       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
799     } else {
800       ASSERT_TRUE(U32Success);
801     }
802 
803     Str = ConsumeUnsigned[i].Str;
804     bool U64Success = Str.consumeInteger(0, U64);
805     if (static_cast<uint64_t>(ConsumeUnsigned[i].Expected) ==
806         ConsumeUnsigned[i].Expected) {
807       ASSERT_FALSE(U64Success);
808       EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);
809       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
810     } else {
811       ASSERT_TRUE(U64Success);
812     }
813   }
814 }
815 
816 TEST(StringRefTest, consumeIntegerSigned) {
817   int8_t S8;
818   int16_t S16;
819   int32_t S32;
820   int64_t S64;
821 
822   for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) {
823     StringRef Str = ConsumeSigned[i].Str;
824     bool S8Success = Str.consumeInteger(0, S8);
825     if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
826         ConsumeSigned[i].Expected) {
827       ASSERT_FALSE(S8Success);
828       EXPECT_EQ(S8, ConsumeSigned[i].Expected);
829       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
830     } else {
831       ASSERT_TRUE(S8Success);
832     }
833 
834     Str = ConsumeSigned[i].Str;
835     bool S16Success = Str.consumeInteger(0, S16);
836     if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
837         ConsumeSigned[i].Expected) {
838       ASSERT_FALSE(S16Success);
839       EXPECT_EQ(S16, ConsumeSigned[i].Expected);
840       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
841     } else {
842       ASSERT_TRUE(S16Success);
843     }
844 
845     Str = ConsumeSigned[i].Str;
846     bool S32Success = Str.consumeInteger(0, S32);
847     if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
848         ConsumeSigned[i].Expected) {
849       ASSERT_FALSE(S32Success);
850       EXPECT_EQ(S32, ConsumeSigned[i].Expected);
851       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
852     } else {
853       ASSERT_TRUE(S32Success);
854     }
855 
856     Str = ConsumeSigned[i].Str;
857     bool S64Success = Str.consumeInteger(0, S64);
858     if (static_cast<int64_t>(ConsumeSigned[i].Expected) ==
859         ConsumeSigned[i].Expected) {
860       ASSERT_FALSE(S64Success);
861       EXPECT_EQ(S64, ConsumeSigned[i].Expected);
862       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
863     } else {
864       ASSERT_TRUE(S64Success);
865     }
866   }
867 }
868 
869 struct GetDoubleStrings {
870   const char *Str;
871   bool AllowInexact;
872   bool ShouldFail;
873   double D;
874 } DoubleStrings[] = {{"0", false, false, 0.0},
875                      {"0.0", false, false, 0.0},
876                      {"-0.0", false, false, -0.0},
877                      {"123.45", false, true, 123.45},
878                      {"123.45", true, false, 123.45}};
879 
880 TEST(StringRefTest, getAsDouble) {
881   for (const auto &Entry : DoubleStrings) {
882     double Result;
883     StringRef S(Entry.Str);
884     EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
885     if (!Entry.ShouldFail) {
886       EXPECT_EQ(Result, Entry.D);
887     }
888   }
889 }
890 
891 static const char *join_input[] = { "a", "b", "c" };
892 static const char join_result1[] = "a";
893 static const char join_result2[] = "a:b:c";
894 static const char join_result3[] = "a::b::c";
895 
896 TEST(StringRefTest, joinStrings) {
897   std::vector<StringRef> v1;
898   std::vector<std::string> v2;
899   for (size_t i = 0; i < array_lengthof(join_input); ++i) {
900     v1.push_back(join_input[i]);
901     v2.push_back(join_input[i]);
902   }
903 
904   bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
905   EXPECT_TRUE(v1_join1);
906   bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
907   EXPECT_TRUE(v1_join2);
908   bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
909   EXPECT_TRUE(v1_join3);
910 
911   bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
912   EXPECT_TRUE(v2_join1);
913   bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
914   EXPECT_TRUE(v2_join2);
915   bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
916   EXPECT_TRUE(v2_join3);
917   v2_join3 = join(v2, "::") == join_result3;
918   EXPECT_TRUE(v2_join3);
919 }
920 
921 
922 TEST(StringRefTest, AllocatorCopy) {
923   BumpPtrAllocator Alloc;
924   // First test empty strings.  We don't want these to allocate anything on the
925   // allocator.
926   StringRef StrEmpty = "";
927   StringRef StrEmptyc = StrEmpty.copy(Alloc);
928   EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
929   EXPECT_EQ(StrEmptyc.data(), nullptr);
930   EXPECT_EQ(StrEmptyc.size(), 0u);
931   EXPECT_EQ(Alloc.getTotalMemory(), 0u);
932 
933   StringRef Str1 = "hello";
934   StringRef Str2 = "bye";
935   StringRef Str1c = Str1.copy(Alloc);
936   StringRef Str2c = Str2.copy(Alloc);
937   EXPECT_TRUE(Str1.equals(Str1c));
938   EXPECT_NE(Str1.data(), Str1c.data());
939   EXPECT_TRUE(Str2.equals(Str2c));
940   EXPECT_NE(Str2.data(), Str2c.data());
941 }
942 
943 TEST(StringRefTest, Drop) {
944   StringRef Test("StringRefTest::Drop");
945 
946   StringRef Dropped = Test.drop_front(5);
947   EXPECT_EQ(Dropped, "gRefTest::Drop");
948 
949   Dropped = Test.drop_back(5);
950   EXPECT_EQ(Dropped, "StringRefTest:");
951 
952   Dropped = Test.drop_front(0);
953   EXPECT_EQ(Dropped, Test);
954 
955   Dropped = Test.drop_back(0);
956   EXPECT_EQ(Dropped, Test);
957 
958   Dropped = Test.drop_front(Test.size());
959   EXPECT_TRUE(Dropped.empty());
960 
961   Dropped = Test.drop_back(Test.size());
962   EXPECT_TRUE(Dropped.empty());
963 }
964 
965 TEST(StringRefTest, Take) {
966   StringRef Test("StringRefTest::Take");
967 
968   StringRef Taken = Test.take_front(5);
969   EXPECT_EQ(Taken, "Strin");
970 
971   Taken = Test.take_back(5);
972   EXPECT_EQ(Taken, ":Take");
973 
974   Taken = Test.take_front(Test.size());
975   EXPECT_EQ(Taken, Test);
976 
977   Taken = Test.take_back(Test.size());
978   EXPECT_EQ(Taken, Test);
979 
980   Taken = Test.take_front(0);
981   EXPECT_TRUE(Taken.empty());
982 
983   Taken = Test.take_back(0);
984   EXPECT_TRUE(Taken.empty());
985 }
986 
987 TEST(StringRefTest, FindIf) {
988   StringRef Punct("Test.String");
989   StringRef NoPunct("ABCDEFG");
990   StringRef Empty;
991 
992   auto IsPunct = [](char c) { return ::ispunct(c); };
993   auto IsAlpha = [](char c) { return ::isalpha(c); };
994   EXPECT_EQ(4U, Punct.find_if(IsPunct));
995   EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
996   EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
997 
998   EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
999   EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
1000   EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
1001 }
1002 
1003 TEST(StringRefTest, TakeWhileUntil) {
1004   StringRef Test("String With 1 Number");
1005 
1006   StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
1007   EXPECT_EQ("", Taken);
1008 
1009   Taken = Test.take_until([](char c) { return ::isdigit(c); });
1010   EXPECT_EQ("String With ", Taken);
1011 
1012   Taken = Test.take_while([](char c) { return true; });
1013   EXPECT_EQ(Test, Taken);
1014 
1015   Taken = Test.take_until([](char c) { return true; });
1016   EXPECT_EQ("", Taken);
1017 
1018   Test = "";
1019   Taken = Test.take_while([](char c) { return true; });
1020   EXPECT_EQ("", Taken);
1021 }
1022 
1023 TEST(StringRefTest, DropWhileUntil) {
1024   StringRef Test("String With 1 Number");
1025 
1026   StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
1027   EXPECT_EQ(Test, Taken);
1028 
1029   Taken = Test.drop_until([](char c) { return ::isdigit(c); });
1030   EXPECT_EQ("1 Number", Taken);
1031 
1032   Taken = Test.drop_while([](char c) { return true; });
1033   EXPECT_EQ("", Taken);
1034 
1035   Taken = Test.drop_until([](char c) { return true; });
1036   EXPECT_EQ(Test, Taken);
1037 
1038   StringRef EmptyString = "";
1039   Taken = EmptyString.drop_while([](char c) { return true; });
1040   EXPECT_EQ("", Taken);
1041 }
1042 
1043 TEST(StringRefTest, StringLiteral) {
1044   constexpr StringLiteral Strings[] = {"Foo", "Bar"};
1045   EXPECT_EQ(StringRef("Foo"), Strings[0]);
1046   EXPECT_EQ(StringRef("Bar"), Strings[1]);
1047 }
1048 
1049 } // end anonymous namespace
1050