blob: 3cb6143a42b6d4d9799f94cf55cea1e74815f8f1 [file] [log] [blame]
Daniel Dunbar4cf95d72009-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 Dunbar4cf95d72009-07-21 07:28:51 +000010#include "llvm/ADT/StringRef.h"
Joerg Sonnenbergerf57a80f2013-09-03 20:43:54 +000011#include "llvm/ADT/StringExtras.h"
Chandler Carruth528f0bb2012-03-04 10:55:27 +000012#include "llvm/ADT/Hashing.h"
Rafael Espindolac78c0c92009-11-13 02:18:25 +000013#include "llvm/ADT/SmallVector.h"
Daniel Dunbardbe77cf2009-07-22 17:13:20 +000014#include "llvm/Support/raw_ostream.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000015#include "gtest/gtest.h"
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000016using namespace llvm;
17
Douglas Gregorc883ad22009-12-24 21:15:37 +000018namespace llvm {
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000019
Daniel Dunbare6551282009-09-16 22:38:48 +000020std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
Chris Lattnerb3371cd2011-01-27 07:35:27 +000021 OS << S.str();
Daniel Dunbare6551282009-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 Gregorc883ad22009-12-24 21:15:37 +000031}
32
33namespace {
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000034TEST(StringRefTest, Construction) {
Daniel Dunbare6551282009-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 Dunbar4cf95d72009-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 Dunbare6551282009-09-16 22:38:48 +000045 EXPECT_EQ(*it, *p);
Daniel Dunbar4cf95d72009-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 Kramer0043e352010-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"));
64 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
65 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +000066
67 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
68 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
69 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
70 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
71 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
72 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
73 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
74 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
75 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
76 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
Benjamin Kramer837bccd2010-08-26 15:25:35 +000077 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
Jakob Stoklund Olesen7850dd02011-09-30 17:03:55 +000078 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
79 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
80 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
81 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
82 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
83 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000084}
85
86TEST(StringRefTest, Operators) {
Daniel Dunbare6551282009-09-16 22:38:48 +000087 EXPECT_EQ("", StringRef());
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000088 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
89 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
90 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
91 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
92 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
93 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
94 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
95 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
Daniel Dunbare6551282009-09-16 22:38:48 +000096 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000097 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
98 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
99 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
100 EXPECT_EQ('a', StringRef("aab")[1]);
101}
102
Daniel Dunbare6551282009-09-16 22:38:48 +0000103TEST(StringRefTest, Substr) {
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000104 StringRef Str("hello");
Daniel Dunbare6551282009-09-16 22:38:48 +0000105 EXPECT_EQ("lo", Str.substr(3));
106 EXPECT_EQ("", Str.substr(100));
107 EXPECT_EQ("hello", Str.substr(0, 100));
108 EXPECT_EQ("o", Str.substr(4, 10));
109}
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000110
Daniel Dunbare6551282009-09-16 22:38:48 +0000111TEST(StringRefTest, Slice) {
112 StringRef Str("hello");
113 EXPECT_EQ("l", Str.slice(2, 3));
114 EXPECT_EQ("ell", Str.slice(1, 4));
115 EXPECT_EQ("llo", Str.slice(2, 100));
116 EXPECT_EQ("", Str.slice(2, 1));
117 EXPECT_EQ("", Str.slice(10, 20));
118}
Daniel Dunbard61918f2009-07-26 03:18:15 +0000119
Daniel Dunbare6551282009-09-16 22:38:48 +0000120TEST(StringRefTest, Split) {
121 StringRef Str("hello");
122 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
123 Str.split('X'));
124 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
125 Str.split('e'));
126 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
127 Str.split('h'));
128 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
129 Str.split('l'));
130 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
131 Str.split('o'));
Daniel Dunbard61918f2009-07-26 03:18:15 +0000132
Daniel Dunbare6551282009-09-16 22:38:48 +0000133 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
134 Str.rsplit('X'));
135 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
136 Str.rsplit('e'));
137 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
138 Str.rsplit('h'));
139 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
140 Str.rsplit('l'));
141 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
142 Str.rsplit('o'));
143}
144
Rafael Espindola5ccac242009-11-13 01:24:40 +0000145TEST(StringRefTest, Split2) {
Rafael Espindolac78c0c92009-11-13 02:18:25 +0000146 SmallVector<StringRef, 5> parts;
147 SmallVector<StringRef, 5> expected;
Rafael Espindola5ccac242009-11-13 01:24:40 +0000148
149 expected.push_back("ab"); expected.push_back("c");
150 StringRef(",ab,,c,").split(parts, ",", -1, false);
151 EXPECT_TRUE(parts == expected);
152
153 expected.clear(); parts.clear();
154 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
155 expected.push_back("c"); expected.push_back("");
156 StringRef(",ab,,c,").split(parts, ",", -1, true);
157 EXPECT_TRUE(parts == expected);
158
159 expected.clear(); parts.clear();
160 expected.push_back("");
161 StringRef("").split(parts, ",", -1, true);
162 EXPECT_TRUE(parts == expected);
163
164 expected.clear(); parts.clear();
165 StringRef("").split(parts, ",", -1, false);
166 EXPECT_TRUE(parts == expected);
167
168 expected.clear(); parts.clear();
169 StringRef(",").split(parts, ",", -1, false);
170 EXPECT_TRUE(parts == expected);
171
172 expected.clear(); parts.clear();
173 expected.push_back(""); expected.push_back("");
174 StringRef(",").split(parts, ",", -1, true);
175 EXPECT_TRUE(parts == expected);
176
Rafael Espindola20fd4ec2009-11-13 04:55:09 +0000177 expected.clear(); parts.clear();
178 expected.push_back("a"); expected.push_back("b");
179 StringRef("a,b").split(parts, ",", -1, true);
180 EXPECT_TRUE(parts == expected);
181
Rafael Espindola5ccac242009-11-13 01:24:40 +0000182 // Test MaxSplit
183 expected.clear(); parts.clear();
184 expected.push_back("a,,b,c");
185 StringRef("a,,b,c").split(parts, ",", 0, true);
186 EXPECT_TRUE(parts == expected);
187
188 expected.clear(); parts.clear();
189 expected.push_back("a,,b,c");
190 StringRef("a,,b,c").split(parts, ",", 0, false);
191 EXPECT_TRUE(parts == expected);
192
193 expected.clear(); parts.clear();
194 expected.push_back("a"); expected.push_back(",b,c");
195 StringRef("a,,b,c").split(parts, ",", 1, true);
196 EXPECT_TRUE(parts == expected);
197
198 expected.clear(); parts.clear();
199 expected.push_back("a"); expected.push_back(",b,c");
200 StringRef("a,,b,c").split(parts, ",", 1, false);
201 EXPECT_TRUE(parts == expected);
202
203 expected.clear(); parts.clear();
204 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
205 StringRef("a,,b,c").split(parts, ",", 2, true);
206 EXPECT_TRUE(parts == expected);
207
208 expected.clear(); parts.clear();
209 expected.push_back("a"); expected.push_back("b,c");
210 StringRef("a,,b,c").split(parts, ",", 2, false);
211 EXPECT_TRUE(parts == expected);
212
213 expected.clear(); parts.clear();
214 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
215 expected.push_back("c");
216 StringRef("a,,b,c").split(parts, ",", 3, true);
217 EXPECT_TRUE(parts == expected);
218
219 expected.clear(); parts.clear();
220 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
221 StringRef("a,,b,c").split(parts, ",", 3, false);
222 EXPECT_TRUE(parts == expected);
223}
224
Michael J. Spencerb0940b42012-05-11 22:08:50 +0000225TEST(StringRefTest, Trim) {
226 StringRef Str0("hello");
227 StringRef Str1(" hello ");
228 StringRef Str2(" hello ");
229
230 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
231 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
232 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
233 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
234 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
235 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
236 EXPECT_EQ(StringRef("hello"), Str0.trim());
237 EXPECT_EQ(StringRef("hello"), Str1.trim());
238 EXPECT_EQ(StringRef("hello"), Str2.trim());
239
240 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
241
242 EXPECT_EQ(StringRef(""), StringRef("").trim());
243 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
244 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
245 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
246 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
247}
248
Daniel Dunbare6551282009-09-16 22:38:48 +0000249TEST(StringRefTest, StartsWith) {
250 StringRef Str("hello");
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000251 EXPECT_TRUE(Str.startswith("he"));
252 EXPECT_FALSE(Str.startswith("helloworld"));
253 EXPECT_FALSE(Str.startswith("hi"));
Daniel Dunbare6551282009-09-16 22:38:48 +0000254}
Daniel Dunbardbe77cf2009-07-22 17:13:20 +0000255
Eli Friedmand5b1f8a2009-12-21 06:49:24 +0000256TEST(StringRefTest, EndsWith) {
257 StringRef Str("hello");
258 EXPECT_TRUE(Str.endswith("lo"));
259 EXPECT_FALSE(Str.endswith("helloworld"));
260 EXPECT_FALSE(Str.endswith("worldhello"));
261 EXPECT_FALSE(Str.endswith("so"));
262}
263
Daniel Dunbare6551282009-09-16 22:38:48 +0000264TEST(StringRefTest, Find) {
265 StringRef Str("hello");
266 EXPECT_EQ(2U, Str.find('l'));
267 EXPECT_EQ(StringRef::npos, Str.find('z'));
268 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
269 EXPECT_EQ(0U, Str.find("hello"));
270 EXPECT_EQ(1U, Str.find("ello"));
271 EXPECT_EQ(StringRef::npos, Str.find("zz"));
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000272 EXPECT_EQ(2U, Str.find("ll", 2));
273 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
Benjamin Kramer6e6a5582011-10-15 10:08:31 +0000274 EXPECT_EQ(0U, Str.find(""));
275 StringRef LongStr("hellx xello hell ello world foo bar hello");
276 EXPECT_EQ(36U, LongStr.find("hello"));
277 EXPECT_EQ(28U, LongStr.find("foo"));
278 EXPECT_EQ(12U, LongStr.find("hell", 2));
279 EXPECT_EQ(0U, LongStr.find(""));
Daniel Dunbare6551282009-09-16 22:38:48 +0000280
281 EXPECT_EQ(3U, Str.rfind('l'));
282 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
283 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
284 EXPECT_EQ(0U, Str.rfind("hello"));
285 EXPECT_EQ(1U, Str.rfind("ello"));
286 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000287
288 EXPECT_EQ(2U, Str.find_first_of('l'));
289 EXPECT_EQ(1U, Str.find_first_of("el"));
290 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
291
292 EXPECT_EQ(1U, Str.find_first_not_of('h'));
293 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
294 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
Michael J. Spencerb0940b42012-05-11 22:08:50 +0000295
296 EXPECT_EQ(3U, Str.find_last_not_of('o'));
297 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
298 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
Daniel Dunbare6551282009-09-16 22:38:48 +0000299}
300
301TEST(StringRefTest, Count) {
302 StringRef Str("hello");
303 EXPECT_EQ(2U, Str.count('l'));
304 EXPECT_EQ(1U, Str.count('o'));
305 EXPECT_EQ(0U, Str.count('z'));
306 EXPECT_EQ(0U, Str.count("helloworld"));
307 EXPECT_EQ(1U, Str.count("hello"));
308 EXPECT_EQ(1U, Str.count("ello"));
309 EXPECT_EQ(0U, Str.count("zz"));
310}
311
Douglas Gregor7e54d5b2009-12-31 04:24:34 +0000312TEST(StringRefTest, EditDistance) {
313 StringRef Str("hello");
Benjamin Kramer47604672009-12-31 16:27:13 +0000314 EXPECT_EQ(2U, Str.edit_distance("hill"));
Douglas Gregor7e54d5b2009-12-31 04:24:34 +0000315}
316
Daniel Dunbare6551282009-09-16 22:38:48 +0000317TEST(StringRefTest, Misc) {
Daniel Dunbardbe77cf2009-07-22 17:13:20 +0000318 std::string Storage;
319 raw_string_ostream OS(Storage);
320 OS << StringRef("hello");
321 EXPECT_EQ("hello", OS.str());
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000322}
323
Chandler Carruth528f0bb2012-03-04 10:55:27 +0000324TEST(StringRefTest, Hashing) {
325 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
326 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
327 std::string S = "hello world";
328 hash_code H = hash_value(S);
329 EXPECT_EQ(H, hash_value(StringRef("hello world")));
330 EXPECT_EQ(H, hash_value(StringRef(S)));
331 EXPECT_NE(H, hash_value(StringRef("hello worl")));
332 EXPECT_EQ(hash_value(std::string("hello worl")),
333 hash_value(StringRef("hello worl")));
334 EXPECT_NE(H, hash_value(StringRef("hello world ")));
335 EXPECT_EQ(hash_value(std::string("hello world ")),
336 hash_value(StringRef("hello world ")));
337 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
338 EXPECT_NE(hash_value(std::string("ello worl")),
339 hash_value(StringRef("hello world").slice(1, -1)));
340}
341
Michael J. Spencer9130b422012-03-10 23:02:54 +0000342struct UnsignedPair {
343 const char *Str;
344 uint64_t Expected;
345} Unsigned[] =
346 { {"0", 0}
347 , {"255", 255}
348 , {"256", 256}
349 , {"65535", 65535}
350 , {"65536", 65536}
Michael J. Spencerdbb4b2f2012-03-11 00:51:01 +0000351 , {"4294967295", 4294967295ULL}
352 , {"4294967296", 4294967296ULL}
Michael J. Spencer9130b422012-03-10 23:02:54 +0000353 , {"18446744073709551615", 18446744073709551615ULL}
354 , {"042", 34}
355 , {"0x42", 66}
356 , {"0b101010", 42}
357 };
358
359struct SignedPair {
360 const char *Str;
361 int64_t Expected;
362} Signed[] =
363 { {"0", 0}
364 , {"-0", 0}
365 , {"127", 127}
366 , {"128", 128}
367 , {"-128", -128}
368 , {"-129", -129}
369 , {"32767", 32767}
370 , {"32768", 32768}
371 , {"-32768", -32768}
372 , {"-32769", -32769}
Michael J. Spencerdbb4b2f2012-03-11 00:51:01 +0000373 , {"2147483647", 2147483647LL}
374 , {"2147483648", 2147483648LL}
Michael J. Spencer9130b422012-03-10 23:02:54 +0000375 , {"-2147483648", -2147483648LL}
376 , {"-2147483649", -2147483649LL}
377 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
378 , {"042", 34}
379 , {"0x42", 66}
380 , {"0b101010", 42}
381 , {"-042", -34}
382 , {"-0x42", -66}
383 , {"-0b101010", -42}
384 };
385
386TEST(StringRefTest, getAsInteger) {
387 uint8_t U8;
388 uint16_t U16;
389 uint32_t U32;
390 uint64_t U64;
391
392 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
393 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
394 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
395 ASSERT_FALSE(U8Success);
396 EXPECT_EQ(U8, Unsigned[i].Expected);
397 } else {
398 ASSERT_TRUE(U8Success);
399 }
400 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
401 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
402 ASSERT_FALSE(U16Success);
403 EXPECT_EQ(U16, Unsigned[i].Expected);
404 } else {
405 ASSERT_TRUE(U16Success);
406 }
407 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
408 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
409 ASSERT_FALSE(U32Success);
410 EXPECT_EQ(U32, Unsigned[i].Expected);
411 } else {
412 ASSERT_TRUE(U32Success);
413 }
414 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
415 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
416 ASSERT_FALSE(U64Success);
417 EXPECT_EQ(U64, Unsigned[i].Expected);
418 } else {
419 ASSERT_TRUE(U64Success);
420 }
421 }
422
423 int8_t S8;
424 int16_t S16;
425 int32_t S32;
426 int64_t S64;
427
428 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
429 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
430 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
431 ASSERT_FALSE(S8Success);
432 EXPECT_EQ(S8, Signed[i].Expected);
433 } else {
434 ASSERT_TRUE(S8Success);
435 }
436 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
437 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
438 ASSERT_FALSE(S16Success);
439 EXPECT_EQ(S16, Signed[i].Expected);
440 } else {
441 ASSERT_TRUE(S16Success);
442 }
443 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
444 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
445 ASSERT_FALSE(S32Success);
446 EXPECT_EQ(S32, Signed[i].Expected);
447 } else {
448 ASSERT_TRUE(S32Success);
449 }
450 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
451 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
452 ASSERT_FALSE(S64Success);
453 EXPECT_EQ(S64, Signed[i].Expected);
454 } else {
455 ASSERT_TRUE(S64Success);
456 }
457 }
458}
459
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000460
461static const char* BadStrings[] = {
462 "18446744073709551617" // value just over max
463 , "123456789012345678901" // value way too large
464 , "4t23v" // illegal decimal characters
465 , "0x123W56" // illegal hex characters
Benjamin Kramere25de4a2012-10-03 18:54:36 +0000466 , "0b2" // illegal bin characters
467 , "08" // illegal oct characters
468 , "0o8" // illegal oct characters
469 , "-123" // negative unsigned value
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000470};
471
472
473TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
Nick Kledzik436eaa82012-10-03 19:27:25 +0000474 unsigned long long U64;
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000475 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
Benjamin Kramere25de4a2012-10-03 18:54:36 +0000476 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000477 ASSERT_TRUE(IsBadNumber);
478 }
479}
480
Joerg Sonnenbergerf57a80f2013-09-03 20:43:54 +0000481static const char *join_input[] = { "a", "b", "c" };
482static const char join_result1[] = "a";
483static const char join_result2[] = "a:b:c";
484static const char join_result3[] = "a::b::c";
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000485
Joerg Sonnenbergerf57a80f2013-09-03 20:43:54 +0000486TEST(StringRefTest, joinStrings) {
487 std::vector<StringRef> v1;
488 std::vector<std::string> v2;
489 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
490 v1.push_back(join_input[i]);
491 v2.push_back(join_input[i]);
492 }
493
494 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
495 EXPECT_TRUE(v1_join1);
496 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
497 EXPECT_TRUE(v1_join2);
498 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
499 EXPECT_TRUE(v1_join3);
500
501 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
502 EXPECT_TRUE(v2_join1);
503 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
504 EXPECT_TRUE(v2_join2);
505 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
506 EXPECT_TRUE(v2_join3);
507}
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000508
Daniel Dunbar4cf95d72009-07-21 07:28:51 +0000509} // end anonymous namespace