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
TEST(StringRefTest,Trim)230 TEST(StringRefTest, Trim) {
231 StringRef Str0("hello");
232 StringRef Str1(" hello ");
233 StringRef Str2(" hello ");
234
235 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
236 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
237 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
238 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
239 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
240 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
241 EXPECT_EQ(StringRef("hello"), Str0.trim());
242 EXPECT_EQ(StringRef("hello"), Str1.trim());
243 EXPECT_EQ(StringRef("hello"), Str2.trim());
244
245 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
246
247 EXPECT_EQ(StringRef(""), StringRef("").trim());
248 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
249 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
250 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
251 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
252 }
253
TEST(StringRefTest,StartsWith)254 TEST(StringRefTest, StartsWith) {
255 StringRef Str("hello");
256 EXPECT_TRUE(Str.startswith(""));
257 EXPECT_TRUE(Str.startswith("he"));
258 EXPECT_FALSE(Str.startswith("helloworld"));
259 EXPECT_FALSE(Str.startswith("hi"));
260 }
261
TEST(StringRefTest,StartsWithLower)262 TEST(StringRefTest, StartsWithLower) {
263 StringRef Str("heLLo");
264 EXPECT_TRUE(Str.startswith_lower(""));
265 EXPECT_TRUE(Str.startswith_lower("he"));
266 EXPECT_TRUE(Str.startswith_lower("hell"));
267 EXPECT_TRUE(Str.startswith_lower("HELlo"));
268 EXPECT_FALSE(Str.startswith_lower("helloworld"));
269 EXPECT_FALSE(Str.startswith_lower("hi"));
270 }
271
TEST(StringRefTest,EndsWith)272 TEST(StringRefTest, EndsWith) {
273 StringRef Str("hello");
274 EXPECT_TRUE(Str.endswith(""));
275 EXPECT_TRUE(Str.endswith("lo"));
276 EXPECT_FALSE(Str.endswith("helloworld"));
277 EXPECT_FALSE(Str.endswith("worldhello"));
278 EXPECT_FALSE(Str.endswith("so"));
279 }
280
TEST(StringRefTest,EndsWithLower)281 TEST(StringRefTest, EndsWithLower) {
282 StringRef Str("heLLo");
283 EXPECT_TRUE(Str.endswith_lower(""));
284 EXPECT_TRUE(Str.endswith_lower("lo"));
285 EXPECT_TRUE(Str.endswith_lower("LO"));
286 EXPECT_TRUE(Str.endswith_lower("ELlo"));
287 EXPECT_FALSE(Str.endswith_lower("helloworld"));
288 EXPECT_FALSE(Str.endswith_lower("hi"));
289 }
290
TEST(StringRefTest,Find)291 TEST(StringRefTest, Find) {
292 StringRef Str("hello");
293 EXPECT_EQ(2U, Str.find('l'));
294 EXPECT_EQ(StringRef::npos, Str.find('z'));
295 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
296 EXPECT_EQ(0U, Str.find("hello"));
297 EXPECT_EQ(1U, Str.find("ello"));
298 EXPECT_EQ(StringRef::npos, Str.find("zz"));
299 EXPECT_EQ(2U, Str.find("ll", 2));
300 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
301 EXPECT_EQ(0U, Str.find(""));
302 StringRef LongStr("hellx xello hell ello world foo bar hello");
303 EXPECT_EQ(36U, LongStr.find("hello"));
304 EXPECT_EQ(28U, LongStr.find("foo"));
305 EXPECT_EQ(12U, LongStr.find("hell", 2));
306 EXPECT_EQ(0U, LongStr.find(""));
307
308 EXPECT_EQ(3U, Str.rfind('l'));
309 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
310 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
311 EXPECT_EQ(0U, Str.rfind("hello"));
312 EXPECT_EQ(1U, Str.rfind("ello"));
313 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
314
315 EXPECT_EQ(2U, Str.find_first_of('l'));
316 EXPECT_EQ(1U, Str.find_first_of("el"));
317 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
318
319 EXPECT_EQ(1U, Str.find_first_not_of('h'));
320 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
321 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
322
323 EXPECT_EQ(3U, Str.find_last_not_of('o'));
324 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
325 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
326 }
327
TEST(StringRefTest,Count)328 TEST(StringRefTest, Count) {
329 StringRef Str("hello");
330 EXPECT_EQ(2U, Str.count('l'));
331 EXPECT_EQ(1U, Str.count('o'));
332 EXPECT_EQ(0U, Str.count('z'));
333 EXPECT_EQ(0U, Str.count("helloworld"));
334 EXPECT_EQ(1U, Str.count("hello"));
335 EXPECT_EQ(1U, Str.count("ello"));
336 EXPECT_EQ(0U, Str.count("zz"));
337 }
338
TEST(StringRefTest,EditDistance)339 TEST(StringRefTest, EditDistance) {
340 StringRef Str("hello");
341 EXPECT_EQ(2U, Str.edit_distance("hill"));
342 }
343
TEST(StringRefTest,Misc)344 TEST(StringRefTest, Misc) {
345 std::string Storage;
346 raw_string_ostream OS(Storage);
347 OS << StringRef("hello");
348 EXPECT_EQ("hello", OS.str());
349 }
350
TEST(StringRefTest,Hashing)351 TEST(StringRefTest, Hashing) {
352 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
353 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
354 std::string S = "hello world";
355 hash_code H = hash_value(S);
356 EXPECT_EQ(H, hash_value(StringRef("hello world")));
357 EXPECT_EQ(H, hash_value(StringRef(S)));
358 EXPECT_NE(H, hash_value(StringRef("hello worl")));
359 EXPECT_EQ(hash_value(std::string("hello worl")),
360 hash_value(StringRef("hello worl")));
361 EXPECT_NE(H, hash_value(StringRef("hello world ")));
362 EXPECT_EQ(hash_value(std::string("hello world ")),
363 hash_value(StringRef("hello world ")));
364 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
365 EXPECT_NE(hash_value(std::string("ello worl")),
366 hash_value(StringRef("hello world").slice(1, -1)));
367 }
368
369 struct UnsignedPair {
370 const char *Str;
371 uint64_t Expected;
372 } Unsigned[] =
373 { {"0", 0}
374 , {"255", 255}
375 , {"256", 256}
376 , {"65535", 65535}
377 , {"65536", 65536}
378 , {"4294967295", 4294967295ULL}
379 , {"4294967296", 4294967296ULL}
380 , {"18446744073709551615", 18446744073709551615ULL}
381 , {"042", 34}
382 , {"0x42", 66}
383 , {"0b101010", 42}
384 };
385
386 struct SignedPair {
387 const char *Str;
388 int64_t Expected;
389 } Signed[] =
390 { {"0", 0}
391 , {"-0", 0}
392 , {"127", 127}
393 , {"128", 128}
394 , {"-128", -128}
395 , {"-129", -129}
396 , {"32767", 32767}
397 , {"32768", 32768}
398 , {"-32768", -32768}
399 , {"-32769", -32769}
400 , {"2147483647", 2147483647LL}
401 , {"2147483648", 2147483648LL}
402 , {"-2147483648", -2147483648LL}
403 , {"-2147483649", -2147483649LL}
404 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
405 , {"042", 34}
406 , {"0x42", 66}
407 , {"0b101010", 42}
408 , {"-042", -34}
409 , {"-0x42", -66}
410 , {"-0b101010", -42}
411 };
412
TEST(StringRefTest,getAsInteger)413 TEST(StringRefTest, getAsInteger) {
414 uint8_t U8;
415 uint16_t U16;
416 uint32_t U32;
417 uint64_t U64;
418
419 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
420 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
421 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
422 ASSERT_FALSE(U8Success);
423 EXPECT_EQ(U8, Unsigned[i].Expected);
424 } else {
425 ASSERT_TRUE(U8Success);
426 }
427 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
428 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
429 ASSERT_FALSE(U16Success);
430 EXPECT_EQ(U16, Unsigned[i].Expected);
431 } else {
432 ASSERT_TRUE(U16Success);
433 }
434 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
435 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
436 ASSERT_FALSE(U32Success);
437 EXPECT_EQ(U32, Unsigned[i].Expected);
438 } else {
439 ASSERT_TRUE(U32Success);
440 }
441 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
442 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
443 ASSERT_FALSE(U64Success);
444 EXPECT_EQ(U64, Unsigned[i].Expected);
445 } else {
446 ASSERT_TRUE(U64Success);
447 }
448 }
449
450 int8_t S8;
451 int16_t S16;
452 int32_t S32;
453 int64_t S64;
454
455 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
456 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
457 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
458 ASSERT_FALSE(S8Success);
459 EXPECT_EQ(S8, Signed[i].Expected);
460 } else {
461 ASSERT_TRUE(S8Success);
462 }
463 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
464 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
465 ASSERT_FALSE(S16Success);
466 EXPECT_EQ(S16, Signed[i].Expected);
467 } else {
468 ASSERT_TRUE(S16Success);
469 }
470 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
471 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
472 ASSERT_FALSE(S32Success);
473 EXPECT_EQ(S32, Signed[i].Expected);
474 } else {
475 ASSERT_TRUE(S32Success);
476 }
477 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
478 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
479 ASSERT_FALSE(S64Success);
480 EXPECT_EQ(S64, Signed[i].Expected);
481 } else {
482 ASSERT_TRUE(S64Success);
483 }
484 }
485 }
486
487
488 static const char* BadStrings[] = {
489 "18446744073709551617" // value just over max
490 , "123456789012345678901" // value way too large
491 , "4t23v" // illegal decimal characters
492 , "0x123W56" // illegal hex characters
493 , "0b2" // illegal bin characters
494 , "08" // illegal oct characters
495 , "0o8" // illegal oct characters
496 , "-123" // negative unsigned value
497 };
498
499
TEST(StringRefTest,getAsUnsignedIntegerBadStrings)500 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
501 unsigned long long U64;
502 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
503 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
504 ASSERT_TRUE(IsBadNumber);
505 }
506 }
507
508 static const char *join_input[] = { "a", "b", "c" };
509 static const char join_result1[] = "a";
510 static const char join_result2[] = "a:b:c";
511 static const char join_result3[] = "a::b::c";
512
TEST(StringRefTest,joinStrings)513 TEST(StringRefTest, joinStrings) {
514 std::vector<StringRef> v1;
515 std::vector<std::string> v2;
516 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
517 v1.push_back(join_input[i]);
518 v2.push_back(join_input[i]);
519 }
520
521 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
522 EXPECT_TRUE(v1_join1);
523 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
524 EXPECT_TRUE(v1_join2);
525 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
526 EXPECT_TRUE(v1_join3);
527
528 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
529 EXPECT_TRUE(v2_join1);
530 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
531 EXPECT_TRUE(v2_join2);
532 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
533 EXPECT_TRUE(v2_join3);
534 }
535
536
TEST(StringRefTest,AllocatorCopy)537 TEST(StringRefTest, AllocatorCopy) {
538 BumpPtrAllocator Alloc;
539 StringRef Str1 = "hello";
540 StringRef Str2 = "bye";
541 StringRef Str1c = Str1.copy(Alloc);
542 StringRef Str2c = Str2.copy(Alloc);
543 EXPECT_TRUE(Str1.equals(Str1c));
544 EXPECT_NE(Str1.data(), Str1c.data());
545 EXPECT_TRUE(Str2.equals(Str2c));
546 EXPECT_NE(Str2.data(), Str2c.data());
547 }
548
549
550 } // end anonymous namespace
551