blob: 0ab8fcf6f002ec935a055007b43c133bd1bc17cc [file] [log] [blame]
Daniel Dunbar25f9fc52009-07-21 07:28:51 +00001//===- 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
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000010#include "llvm/ADT/StringRef.h"
Chandler Carruthca99ad32012-03-04 10:55:27 +000011#include "llvm/ADT/Hashing.h"
Rafael Espindola7c685492009-11-13 02:18:25 +000012#include "llvm/ADT/SmallVector.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000013#include "llvm/ADT/StringExtras.h"
Daniel Dunbare23388b2009-07-22 17:13:20 +000014#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000015#include "gtest/gtest.h"
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000016using namespace llvm;
17
Douglas Gregor4ee2cf62009-12-24 21:15:37 +000018namespace llvm {
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000019
Daniel Dunbar44981682009-09-16 22:38:48 +000020std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
Chris Lattner2114b762011-01-27 07:35:27 +000021 OS << S.str();
Daniel Dunbar44981682009-09-16 22:38:48 +000022 return OS;
23}
24
25std::ostream &operator<<(std::ostream &OS,
26 const std::pair<StringRef, StringRef> &P) {
27 OS << "(" << P.first << ", " << P.second << ")";
28 return OS;
29}
30
Douglas Gregor4ee2cf62009-12-24 21:15:37 +000031}
32
33namespace {
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000034TEST(StringRefTest, Construction) {
Daniel Dunbar44981682009-09-16 22:38:48 +000035 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")));
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000039}
40
41TEST(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)
Daniel Dunbar44981682009-09-16 22:38:48 +000045 EXPECT_EQ(*it, *p);
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000046}
47
48TEST(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"));
Benjamin Kramerb04d4af2010-08-26 14:21:08 +000058 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"));
Rui Ueyama00e24e42013-10-30 18:32:26 +000064 EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
65 EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
66 EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
Benjamin Kramerb04d4af2010-08-26 14:21:08 +000067 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
68 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
Jakob Stoklund Olesend1d7ed62010-05-26 21:47:28 +000069
70 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
71 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
72 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
73 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
74 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
75 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
76 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
77 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
78 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
79 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
Benjamin Kramer9bf03802010-08-26 15:25:35 +000080 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
Jakob Stoklund Olesenc874e2d2011-09-30 17:03:55 +000081 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
82 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
83 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
84 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
85 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
86 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000087}
88
89TEST(StringRefTest, Operators) {
Daniel Dunbar44981682009-09-16 22:38:48 +000090 EXPECT_EQ("", StringRef());
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000091 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
92 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
93 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
94 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
95 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
96 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
97 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
98 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
Daniel Dunbar44981682009-09-16 22:38:48 +000099 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
Daniel Dunbar25f9fc52009-07-21 07:28:51 +0000100 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
101 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
102 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
103 EXPECT_EQ('a', StringRef("aab")[1]);
104}
105
Daniel Dunbar44981682009-09-16 22:38:48 +0000106TEST(StringRefTest, Substr) {
Daniel Dunbar1f982102009-07-21 09:18:49 +0000107 StringRef Str("hello");
Daniel Dunbar44981682009-09-16 22:38:48 +0000108 EXPECT_EQ("lo", Str.substr(3));
109 EXPECT_EQ("", Str.substr(100));
110 EXPECT_EQ("hello", Str.substr(0, 100));
111 EXPECT_EQ("o", Str.substr(4, 10));
112}
Daniel Dunbar1f982102009-07-21 09:18:49 +0000113
Daniel Dunbar44981682009-09-16 22:38:48 +0000114TEST(StringRefTest, Slice) {
115 StringRef Str("hello");
116 EXPECT_EQ("l", Str.slice(2, 3));
117 EXPECT_EQ("ell", Str.slice(1, 4));
118 EXPECT_EQ("llo", Str.slice(2, 100));
119 EXPECT_EQ("", Str.slice(2, 1));
120 EXPECT_EQ("", Str.slice(10, 20));
121}
Daniel Dunbar56563f32009-07-26 03:18:15 +0000122
Daniel Dunbar44981682009-09-16 22:38:48 +0000123TEST(StringRefTest, Split) {
124 StringRef Str("hello");
125 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
126 Str.split('X'));
127 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
128 Str.split('e'));
129 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
130 Str.split('h'));
131 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
132 Str.split('l'));
133 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
134 Str.split('o'));
Daniel Dunbar56563f32009-07-26 03:18:15 +0000135
Daniel Dunbar44981682009-09-16 22:38:48 +0000136 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
137 Str.rsplit('X'));
138 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
139 Str.rsplit('e'));
140 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
141 Str.rsplit('h'));
142 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
143 Str.rsplit('l'));
144 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
145 Str.rsplit('o'));
146}
147
Rafael Espindolad554e442009-11-13 01:24:40 +0000148TEST(StringRefTest, Split2) {
Rafael Espindola7c685492009-11-13 02:18:25 +0000149 SmallVector<StringRef, 5> parts;
150 SmallVector<StringRef, 5> expected;
Rafael Espindolad554e442009-11-13 01:24:40 +0000151
152 expected.push_back("ab"); expected.push_back("c");
153 StringRef(",ab,,c,").split(parts, ",", -1, false);
154 EXPECT_TRUE(parts == expected);
155
156 expected.clear(); parts.clear();
157 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
158 expected.push_back("c"); expected.push_back("");
159 StringRef(",ab,,c,").split(parts, ",", -1, true);
160 EXPECT_TRUE(parts == expected);
161
162 expected.clear(); parts.clear();
163 expected.push_back("");
164 StringRef("").split(parts, ",", -1, true);
165 EXPECT_TRUE(parts == expected);
166
167 expected.clear(); parts.clear();
168 StringRef("").split(parts, ",", -1, false);
169 EXPECT_TRUE(parts == expected);
170
171 expected.clear(); parts.clear();
172 StringRef(",").split(parts, ",", -1, false);
173 EXPECT_TRUE(parts == expected);
174
175 expected.clear(); parts.clear();
176 expected.push_back(""); expected.push_back("");
177 StringRef(",").split(parts, ",", -1, true);
178 EXPECT_TRUE(parts == expected);
179
Rafael Espindolaff2c72b2009-11-13 04:55:09 +0000180 expected.clear(); parts.clear();
181 expected.push_back("a"); expected.push_back("b");
182 StringRef("a,b").split(parts, ",", -1, true);
183 EXPECT_TRUE(parts == expected);
184
Rafael Espindolad554e442009-11-13 01:24:40 +0000185 // Test MaxSplit
186 expected.clear(); parts.clear();
187 expected.push_back("a,,b,c");
188 StringRef("a,,b,c").split(parts, ",", 0, true);
189 EXPECT_TRUE(parts == expected);
190
191 expected.clear(); parts.clear();
192 expected.push_back("a,,b,c");
193 StringRef("a,,b,c").split(parts, ",", 0, false);
194 EXPECT_TRUE(parts == expected);
195
196 expected.clear(); parts.clear();
197 expected.push_back("a"); expected.push_back(",b,c");
198 StringRef("a,,b,c").split(parts, ",", 1, true);
199 EXPECT_TRUE(parts == expected);
200
201 expected.clear(); parts.clear();
202 expected.push_back("a"); expected.push_back(",b,c");
203 StringRef("a,,b,c").split(parts, ",", 1, false);
204 EXPECT_TRUE(parts == expected);
205
206 expected.clear(); parts.clear();
207 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
208 StringRef("a,,b,c").split(parts, ",", 2, true);
209 EXPECT_TRUE(parts == expected);
210
211 expected.clear(); parts.clear();
212 expected.push_back("a"); expected.push_back("b,c");
213 StringRef("a,,b,c").split(parts, ",", 2, false);
214 EXPECT_TRUE(parts == expected);
215
216 expected.clear(); parts.clear();
217 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
218 expected.push_back("c");
219 StringRef("a,,b,c").split(parts, ",", 3, true);
220 EXPECT_TRUE(parts == expected);
221
222 expected.clear(); parts.clear();
223 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
224 StringRef("a,,b,c").split(parts, ",", 3, false);
225 EXPECT_TRUE(parts == expected);
226}
227
Michael J. Spencer93303812012-05-11 22:08:50 +0000228TEST(StringRefTest, Trim) {
229 StringRef Str0("hello");
230 StringRef Str1(" hello ");
231 StringRef Str2(" hello ");
232
233 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
234 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
235 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
236 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
237 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
238 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
239 EXPECT_EQ(StringRef("hello"), Str0.trim());
240 EXPECT_EQ(StringRef("hello"), Str1.trim());
241 EXPECT_EQ(StringRef("hello"), Str2.trim());
242
243 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
244
245 EXPECT_EQ(StringRef(""), StringRef("").trim());
246 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
247 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
248 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
249 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
250}
251
Daniel Dunbar44981682009-09-16 22:38:48 +0000252TEST(StringRefTest, StartsWith) {
253 StringRef Str("hello");
Rui Ueyamab6decb02013-10-28 22:42:54 +0000254 EXPECT_TRUE(Str.startswith(""));
Daniel Dunbar1f982102009-07-21 09:18:49 +0000255 EXPECT_TRUE(Str.startswith("he"));
256 EXPECT_FALSE(Str.startswith("helloworld"));
257 EXPECT_FALSE(Str.startswith("hi"));
Daniel Dunbar44981682009-09-16 22:38:48 +0000258}
Daniel Dunbare23388b2009-07-22 17:13:20 +0000259
Rui Ueyama00e24e42013-10-30 18:32:26 +0000260TEST(StringRefTest, StartsWithLower) {
261 StringRef Str("heLLo");
262 EXPECT_TRUE(Str.startswith_lower(""));
263 EXPECT_TRUE(Str.startswith_lower("he"));
264 EXPECT_TRUE(Str.startswith_lower("hell"));
265 EXPECT_TRUE(Str.startswith_lower("HELlo"));
266 EXPECT_FALSE(Str.startswith_lower("helloworld"));
267 EXPECT_FALSE(Str.startswith_lower("hi"));
268}
269
Eli Friedman00879d82009-12-21 06:49:24 +0000270TEST(StringRefTest, EndsWith) {
271 StringRef Str("hello");
Rui Ueyamab6decb02013-10-28 22:42:54 +0000272 EXPECT_TRUE(Str.endswith(""));
Eli Friedman00879d82009-12-21 06:49:24 +0000273 EXPECT_TRUE(Str.endswith("lo"));
274 EXPECT_FALSE(Str.endswith("helloworld"));
275 EXPECT_FALSE(Str.endswith("worldhello"));
276 EXPECT_FALSE(Str.endswith("so"));
277}
278
Rui Ueyama00e24e42013-10-30 18:32:26 +0000279TEST(StringRefTest, EndsWithLower) {
280 StringRef Str("heLLo");
281 EXPECT_TRUE(Str.endswith_lower(""));
282 EXPECT_TRUE(Str.endswith_lower("lo"));
283 EXPECT_TRUE(Str.endswith_lower("LO"));
284 EXPECT_TRUE(Str.endswith_lower("ELlo"));
285 EXPECT_FALSE(Str.endswith_lower("helloworld"));
286 EXPECT_FALSE(Str.endswith_lower("hi"));
287}
288
Daniel Dunbar44981682009-09-16 22:38:48 +0000289TEST(StringRefTest, Find) {
290 StringRef Str("hello");
291 EXPECT_EQ(2U, Str.find('l'));
292 EXPECT_EQ(StringRef::npos, Str.find('z'));
293 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
294 EXPECT_EQ(0U, Str.find("hello"));
295 EXPECT_EQ(1U, Str.find("ello"));
296 EXPECT_EQ(StringRef::npos, Str.find("zz"));
Daniel Dunbar9806e4a2009-11-11 00:28:53 +0000297 EXPECT_EQ(2U, Str.find("ll", 2));
298 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
Benjamin Kramer4d681d72011-10-15 10:08:31 +0000299 EXPECT_EQ(0U, Str.find(""));
300 StringRef LongStr("hellx xello hell ello world foo bar hello");
301 EXPECT_EQ(36U, LongStr.find("hello"));
302 EXPECT_EQ(28U, LongStr.find("foo"));
303 EXPECT_EQ(12U, LongStr.find("hell", 2));
304 EXPECT_EQ(0U, LongStr.find(""));
Daniel Dunbar44981682009-09-16 22:38:48 +0000305
306 EXPECT_EQ(3U, Str.rfind('l'));
307 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
308 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
309 EXPECT_EQ(0U, Str.rfind("hello"));
310 EXPECT_EQ(1U, Str.rfind("ello"));
311 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
Daniel Dunbar9806e4a2009-11-11 00:28:53 +0000312
313 EXPECT_EQ(2U, Str.find_first_of('l'));
314 EXPECT_EQ(1U, Str.find_first_of("el"));
315 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
316
317 EXPECT_EQ(1U, Str.find_first_not_of('h'));
318 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
319 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
Michael J. Spencer93303812012-05-11 22:08:50 +0000320
321 EXPECT_EQ(3U, Str.find_last_not_of('o'));
322 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
323 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
Daniel Dunbar44981682009-09-16 22:38:48 +0000324}
325
326TEST(StringRefTest, Count) {
327 StringRef Str("hello");
328 EXPECT_EQ(2U, Str.count('l'));
329 EXPECT_EQ(1U, Str.count('o'));
330 EXPECT_EQ(0U, Str.count('z'));
331 EXPECT_EQ(0U, Str.count("helloworld"));
332 EXPECT_EQ(1U, Str.count("hello"));
333 EXPECT_EQ(1U, Str.count("ello"));
334 EXPECT_EQ(0U, Str.count("zz"));
335}
336
Douglas Gregor5639af42009-12-31 04:24:34 +0000337TEST(StringRefTest, EditDistance) {
338 StringRef Str("hello");
Benjamin Kramer738800d2009-12-31 16:27:13 +0000339 EXPECT_EQ(2U, Str.edit_distance("hill"));
Douglas Gregor5639af42009-12-31 04:24:34 +0000340}
341
Daniel Dunbar44981682009-09-16 22:38:48 +0000342TEST(StringRefTest, Misc) {
Daniel Dunbare23388b2009-07-22 17:13:20 +0000343 std::string Storage;
344 raw_string_ostream OS(Storage);
345 OS << StringRef("hello");
346 EXPECT_EQ("hello", OS.str());
Daniel Dunbar1f982102009-07-21 09:18:49 +0000347}
348
Chandler Carruthca99ad32012-03-04 10:55:27 +0000349TEST(StringRefTest, Hashing) {
350 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
351 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
352 std::string S = "hello world";
353 hash_code H = hash_value(S);
354 EXPECT_EQ(H, hash_value(StringRef("hello world")));
355 EXPECT_EQ(H, hash_value(StringRef(S)));
356 EXPECT_NE(H, hash_value(StringRef("hello worl")));
357 EXPECT_EQ(hash_value(std::string("hello worl")),
358 hash_value(StringRef("hello worl")));
359 EXPECT_NE(H, hash_value(StringRef("hello world ")));
360 EXPECT_EQ(hash_value(std::string("hello world ")),
361 hash_value(StringRef("hello world ")));
362 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
363 EXPECT_NE(hash_value(std::string("ello worl")),
364 hash_value(StringRef("hello world").slice(1, -1)));
365}
366
Michael J. Spencercfa95f62012-03-10 23:02:54 +0000367struct UnsignedPair {
368 const char *Str;
369 uint64_t Expected;
370} Unsigned[] =
371 { {"0", 0}
372 , {"255", 255}
373 , {"256", 256}
374 , {"65535", 65535}
375 , {"65536", 65536}
Michael J. Spencer914dc772012-03-11 00:51:01 +0000376 , {"4294967295", 4294967295ULL}
377 , {"4294967296", 4294967296ULL}
Michael J. Spencercfa95f62012-03-10 23:02:54 +0000378 , {"18446744073709551615", 18446744073709551615ULL}
379 , {"042", 34}
380 , {"0x42", 66}
381 , {"0b101010", 42}
382 };
383
384struct SignedPair {
385 const char *Str;
386 int64_t Expected;
387} Signed[] =
388 { {"0", 0}
389 , {"-0", 0}
390 , {"127", 127}
391 , {"128", 128}
392 , {"-128", -128}
393 , {"-129", -129}
394 , {"32767", 32767}
395 , {"32768", 32768}
396 , {"-32768", -32768}
397 , {"-32769", -32769}
Michael J. Spencer914dc772012-03-11 00:51:01 +0000398 , {"2147483647", 2147483647LL}
399 , {"2147483648", 2147483648LL}
Michael J. Spencercfa95f62012-03-10 23:02:54 +0000400 , {"-2147483648", -2147483648LL}
401 , {"-2147483649", -2147483649LL}
402 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
403 , {"042", 34}
404 , {"0x42", 66}
405 , {"0b101010", 42}
406 , {"-042", -34}
407 , {"-0x42", -66}
408 , {"-0b101010", -42}
409 };
410
411TEST(StringRefTest, getAsInteger) {
412 uint8_t U8;
413 uint16_t U16;
414 uint32_t U32;
415 uint64_t U64;
416
417 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
418 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
419 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
420 ASSERT_FALSE(U8Success);
421 EXPECT_EQ(U8, Unsigned[i].Expected);
422 } else {
423 ASSERT_TRUE(U8Success);
424 }
425 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
426 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
427 ASSERT_FALSE(U16Success);
428 EXPECT_EQ(U16, Unsigned[i].Expected);
429 } else {
430 ASSERT_TRUE(U16Success);
431 }
432 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
433 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
434 ASSERT_FALSE(U32Success);
435 EXPECT_EQ(U32, Unsigned[i].Expected);
436 } else {
437 ASSERT_TRUE(U32Success);
438 }
439 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
440 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
441 ASSERT_FALSE(U64Success);
442 EXPECT_EQ(U64, Unsigned[i].Expected);
443 } else {
444 ASSERT_TRUE(U64Success);
445 }
446 }
447
448 int8_t S8;
449 int16_t S16;
450 int32_t S32;
451 int64_t S64;
452
453 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
454 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
455 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
456 ASSERT_FALSE(S8Success);
457 EXPECT_EQ(S8, Signed[i].Expected);
458 } else {
459 ASSERT_TRUE(S8Success);
460 }
461 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
462 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
463 ASSERT_FALSE(S16Success);
464 EXPECT_EQ(S16, Signed[i].Expected);
465 } else {
466 ASSERT_TRUE(S16Success);
467 }
468 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
469 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
470 ASSERT_FALSE(S32Success);
471 EXPECT_EQ(S32, Signed[i].Expected);
472 } else {
473 ASSERT_TRUE(S32Success);
474 }
475 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
476 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
477 ASSERT_FALSE(S64Success);
478 EXPECT_EQ(S64, Signed[i].Expected);
479 } else {
480 ASSERT_TRUE(S64Success);
481 }
482 }
483}
484
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000485
486static const char* BadStrings[] = {
487 "18446744073709551617" // value just over max
488 , "123456789012345678901" // value way too large
489 , "4t23v" // illegal decimal characters
490 , "0x123W56" // illegal hex characters
Benjamin Kramerd95ceff2012-10-03 18:54:36 +0000491 , "0b2" // illegal bin characters
492 , "08" // illegal oct characters
493 , "0o8" // illegal oct characters
494 , "-123" // negative unsigned value
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000495};
496
497
498TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
Nick Kledzik85a62b12012-10-03 19:27:25 +0000499 unsigned long long U64;
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000500 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
Benjamin Kramerd95ceff2012-10-03 18:54:36 +0000501 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000502 ASSERT_TRUE(IsBadNumber);
503 }
504}
505
Joerg Sonnenberger6c4dc2b2013-09-03 20:43:54 +0000506static const char *join_input[] = { "a", "b", "c" };
507static const char join_result1[] = "a";
508static const char join_result2[] = "a:b:c";
509static const char join_result3[] = "a::b::c";
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000510
Joerg Sonnenberger6c4dc2b2013-09-03 20:43:54 +0000511TEST(StringRefTest, joinStrings) {
512 std::vector<StringRef> v1;
513 std::vector<std::string> v2;
514 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
515 v1.push_back(join_input[i]);
516 v2.push_back(join_input[i]);
517 }
518
519 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
520 EXPECT_TRUE(v1_join1);
521 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
522 EXPECT_TRUE(v1_join2);
523 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
524 EXPECT_TRUE(v1_join3);
525
526 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
527 EXPECT_TRUE(v2_join1);
528 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
529 EXPECT_TRUE(v2_join2);
530 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
531 EXPECT_TRUE(v2_join3);
532}
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000533
Daniel Dunbar25f9fc52009-07-21 07:28:51 +0000534} // end anonymous namespace