blob: fa87cd0e2c8673d3f7ec4bf51f707904f480c591 [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"
Chandler Carruth528f0bb2012-03-04 10:55:27 +000011#include "llvm/ADT/Hashing.h"
Rafael Espindolac78c0c92009-11-13 02:18:25 +000012#include "llvm/ADT/SmallVector.h"
Daniel Dunbardbe77cf2009-07-22 17:13:20 +000013#include "llvm/Support/raw_ostream.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000014#include "gtest/gtest.h"
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000015using namespace llvm;
16
Douglas Gregorc883ad22009-12-24 21:15:37 +000017namespace llvm {
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000018
Daniel Dunbare6551282009-09-16 22:38:48 +000019std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
Chris Lattnerb3371cd2011-01-27 07:35:27 +000020 OS << S.str();
Daniel Dunbare6551282009-09-16 22:38:48 +000021 return OS;
22}
23
24std::ostream &operator<<(std::ostream &OS,
25 const std::pair<StringRef, StringRef> &P) {
26 OS << "(" << P.first << ", " << P.second << ")";
27 return OS;
28}
29
Douglas Gregorc883ad22009-12-24 21:15:37 +000030}
31
32namespace {
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000033TEST(StringRefTest, Construction) {
Daniel Dunbare6551282009-09-16 22:38:48 +000034 EXPECT_EQ("", StringRef());
35 EXPECT_EQ("hello", StringRef("hello"));
36 EXPECT_EQ("hello", StringRef("hello world", 5));
37 EXPECT_EQ("hello", StringRef(std::string("hello")));
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000038}
39
40TEST(StringRefTest, Iteration) {
41 StringRef S("hello");
42 const char *p = "hello";
43 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
Daniel Dunbare6551282009-09-16 22:38:48 +000044 EXPECT_EQ(*it, *p);
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000045}
46
47TEST(StringRefTest, StringOps) {
48 const char *p = "hello";
49 EXPECT_EQ(p, StringRef(p, 0).data());
50 EXPECT_TRUE(StringRef().empty());
51 EXPECT_EQ((size_t) 5, StringRef("hello").size());
52 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
53 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
54 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
55 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
56 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
Benjamin Kramer0043e352010-08-26 14:21:08 +000057 EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
58
59 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
60 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
61 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
62 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
63 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
64 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +000065
66 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
67 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
68 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
69 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
70 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
71 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
72 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
73 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
74 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
75 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
Benjamin Kramer837bccd2010-08-26 15:25:35 +000076 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
Jakob Stoklund Olesen7850dd02011-09-30 17:03:55 +000077 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
78 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
79 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
80 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
81 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
82 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000083}
84
85TEST(StringRefTest, Operators) {
Daniel Dunbare6551282009-09-16 22:38:48 +000086 EXPECT_EQ("", StringRef());
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000087 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
88 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
89 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
90 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
91 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
92 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
93 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
94 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
Daniel Dunbare6551282009-09-16 22:38:48 +000095 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000096 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
97 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
98 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
99 EXPECT_EQ('a', StringRef("aab")[1]);
100}
101
Daniel Dunbare6551282009-09-16 22:38:48 +0000102TEST(StringRefTest, Substr) {
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000103 StringRef Str("hello");
Daniel Dunbare6551282009-09-16 22:38:48 +0000104 EXPECT_EQ("lo", Str.substr(3));
105 EXPECT_EQ("", Str.substr(100));
106 EXPECT_EQ("hello", Str.substr(0, 100));
107 EXPECT_EQ("o", Str.substr(4, 10));
108}
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000109
Daniel Dunbare6551282009-09-16 22:38:48 +0000110TEST(StringRefTest, Slice) {
111 StringRef Str("hello");
112 EXPECT_EQ("l", Str.slice(2, 3));
113 EXPECT_EQ("ell", Str.slice(1, 4));
114 EXPECT_EQ("llo", Str.slice(2, 100));
115 EXPECT_EQ("", Str.slice(2, 1));
116 EXPECT_EQ("", Str.slice(10, 20));
117}
Daniel Dunbard61918f2009-07-26 03:18:15 +0000118
Daniel Dunbare6551282009-09-16 22:38:48 +0000119TEST(StringRefTest, Split) {
120 StringRef Str("hello");
121 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
122 Str.split('X'));
123 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
124 Str.split('e'));
125 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
126 Str.split('h'));
127 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
128 Str.split('l'));
129 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
130 Str.split('o'));
Daniel Dunbard61918f2009-07-26 03:18:15 +0000131
Daniel Dunbare6551282009-09-16 22:38:48 +0000132 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
133 Str.rsplit('X'));
134 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
135 Str.rsplit('e'));
136 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
137 Str.rsplit('h'));
138 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
139 Str.rsplit('l'));
140 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
141 Str.rsplit('o'));
142}
143
Rafael Espindola5ccac242009-11-13 01:24:40 +0000144TEST(StringRefTest, Split2) {
Rafael Espindolac78c0c92009-11-13 02:18:25 +0000145 SmallVector<StringRef, 5> parts;
146 SmallVector<StringRef, 5> expected;
Rafael Espindola5ccac242009-11-13 01:24:40 +0000147
148 expected.push_back("ab"); expected.push_back("c");
149 StringRef(",ab,,c,").split(parts, ",", -1, false);
150 EXPECT_TRUE(parts == expected);
151
152 expected.clear(); parts.clear();
153 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
154 expected.push_back("c"); expected.push_back("");
155 StringRef(",ab,,c,").split(parts, ",", -1, true);
156 EXPECT_TRUE(parts == expected);
157
158 expected.clear(); parts.clear();
159 expected.push_back("");
160 StringRef("").split(parts, ",", -1, true);
161 EXPECT_TRUE(parts == expected);
162
163 expected.clear(); parts.clear();
164 StringRef("").split(parts, ",", -1, false);
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 expected.push_back(""); expected.push_back("");
173 StringRef(",").split(parts, ",", -1, true);
174 EXPECT_TRUE(parts == expected);
175
Rafael Espindola20fd4ec2009-11-13 04:55:09 +0000176 expected.clear(); parts.clear();
177 expected.push_back("a"); expected.push_back("b");
178 StringRef("a,b").split(parts, ",", -1, true);
179 EXPECT_TRUE(parts == expected);
180
Rafael Espindola5ccac242009-11-13 01:24:40 +0000181 // Test MaxSplit
182 expected.clear(); parts.clear();
183 expected.push_back("a,,b,c");
184 StringRef("a,,b,c").split(parts, ",", 0, true);
185 EXPECT_TRUE(parts == expected);
186
187 expected.clear(); parts.clear();
188 expected.push_back("a,,b,c");
189 StringRef("a,,b,c").split(parts, ",", 0, false);
190 EXPECT_TRUE(parts == expected);
191
192 expected.clear(); parts.clear();
193 expected.push_back("a"); expected.push_back(",b,c");
194 StringRef("a,,b,c").split(parts, ",", 1, true);
195 EXPECT_TRUE(parts == expected);
196
197 expected.clear(); parts.clear();
198 expected.push_back("a"); expected.push_back(",b,c");
199 StringRef("a,,b,c").split(parts, ",", 1, false);
200 EXPECT_TRUE(parts == expected);
201
202 expected.clear(); parts.clear();
203 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
204 StringRef("a,,b,c").split(parts, ",", 2, true);
205 EXPECT_TRUE(parts == expected);
206
207 expected.clear(); parts.clear();
208 expected.push_back("a"); expected.push_back("b,c");
209 StringRef("a,,b,c").split(parts, ",", 2, false);
210 EXPECT_TRUE(parts == expected);
211
212 expected.clear(); parts.clear();
213 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
214 expected.push_back("c");
215 StringRef("a,,b,c").split(parts, ",", 3, true);
216 EXPECT_TRUE(parts == expected);
217
218 expected.clear(); parts.clear();
219 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
220 StringRef("a,,b,c").split(parts, ",", 3, false);
221 EXPECT_TRUE(parts == expected);
222}
223
Michael J. Spencerb0940b42012-05-11 22:08:50 +0000224TEST(StringRefTest, Trim) {
225 StringRef Str0("hello");
226 StringRef Str1(" hello ");
227 StringRef Str2(" hello ");
228
229 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
230 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
231 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
232 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
233 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
234 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
235 EXPECT_EQ(StringRef("hello"), Str0.trim());
236 EXPECT_EQ(StringRef("hello"), Str1.trim());
237 EXPECT_EQ(StringRef("hello"), Str2.trim());
238
239 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
240
241 EXPECT_EQ(StringRef(""), StringRef("").trim());
242 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
243 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
244 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
245 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
246}
247
Daniel Dunbare6551282009-09-16 22:38:48 +0000248TEST(StringRefTest, StartsWith) {
249 StringRef Str("hello");
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000250 EXPECT_TRUE(Str.startswith("he"));
251 EXPECT_FALSE(Str.startswith("helloworld"));
252 EXPECT_FALSE(Str.startswith("hi"));
Daniel Dunbare6551282009-09-16 22:38:48 +0000253}
Daniel Dunbardbe77cf2009-07-22 17:13:20 +0000254
Eli Friedmand5b1f8a2009-12-21 06:49:24 +0000255TEST(StringRefTest, EndsWith) {
256 StringRef Str("hello");
257 EXPECT_TRUE(Str.endswith("lo"));
258 EXPECT_FALSE(Str.endswith("helloworld"));
259 EXPECT_FALSE(Str.endswith("worldhello"));
260 EXPECT_FALSE(Str.endswith("so"));
261}
262
Daniel Dunbare6551282009-09-16 22:38:48 +0000263TEST(StringRefTest, Find) {
264 StringRef Str("hello");
265 EXPECT_EQ(2U, Str.find('l'));
266 EXPECT_EQ(StringRef::npos, Str.find('z'));
267 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
268 EXPECT_EQ(0U, Str.find("hello"));
269 EXPECT_EQ(1U, Str.find("ello"));
270 EXPECT_EQ(StringRef::npos, Str.find("zz"));
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000271 EXPECT_EQ(2U, Str.find("ll", 2));
272 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
Benjamin Kramer6e6a5582011-10-15 10:08:31 +0000273 EXPECT_EQ(0U, Str.find(""));
274 StringRef LongStr("hellx xello hell ello world foo bar hello");
275 EXPECT_EQ(36U, LongStr.find("hello"));
276 EXPECT_EQ(28U, LongStr.find("foo"));
277 EXPECT_EQ(12U, LongStr.find("hell", 2));
278 EXPECT_EQ(0U, LongStr.find(""));
Daniel Dunbare6551282009-09-16 22:38:48 +0000279
280 EXPECT_EQ(3U, Str.rfind('l'));
281 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
282 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
283 EXPECT_EQ(0U, Str.rfind("hello"));
284 EXPECT_EQ(1U, Str.rfind("ello"));
285 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000286
287 EXPECT_EQ(2U, Str.find_first_of('l'));
288 EXPECT_EQ(1U, Str.find_first_of("el"));
289 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
290
291 EXPECT_EQ(1U, Str.find_first_not_of('h'));
292 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
293 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
Michael J. Spencerb0940b42012-05-11 22:08:50 +0000294
295 EXPECT_EQ(3U, Str.find_last_not_of('o'));
296 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
297 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
Daniel Dunbare6551282009-09-16 22:38:48 +0000298}
299
300TEST(StringRefTest, Count) {
301 StringRef Str("hello");
302 EXPECT_EQ(2U, Str.count('l'));
303 EXPECT_EQ(1U, Str.count('o'));
304 EXPECT_EQ(0U, Str.count('z'));
305 EXPECT_EQ(0U, Str.count("helloworld"));
306 EXPECT_EQ(1U, Str.count("hello"));
307 EXPECT_EQ(1U, Str.count("ello"));
308 EXPECT_EQ(0U, Str.count("zz"));
309}
310
Douglas Gregor7e54d5b2009-12-31 04:24:34 +0000311TEST(StringRefTest, EditDistance) {
312 StringRef Str("hello");
Benjamin Kramer47604672009-12-31 16:27:13 +0000313 EXPECT_EQ(2U, Str.edit_distance("hill"));
Douglas Gregor7e54d5b2009-12-31 04:24:34 +0000314}
315
Daniel Dunbare6551282009-09-16 22:38:48 +0000316TEST(StringRefTest, Misc) {
Daniel Dunbardbe77cf2009-07-22 17:13:20 +0000317 std::string Storage;
318 raw_string_ostream OS(Storage);
319 OS << StringRef("hello");
320 EXPECT_EQ("hello", OS.str());
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000321}
322
Chandler Carruth528f0bb2012-03-04 10:55:27 +0000323TEST(StringRefTest, Hashing) {
324 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
325 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
326 std::string S = "hello world";
327 hash_code H = hash_value(S);
328 EXPECT_EQ(H, hash_value(StringRef("hello world")));
329 EXPECT_EQ(H, hash_value(StringRef(S)));
330 EXPECT_NE(H, hash_value(StringRef("hello worl")));
331 EXPECT_EQ(hash_value(std::string("hello worl")),
332 hash_value(StringRef("hello worl")));
333 EXPECT_NE(H, hash_value(StringRef("hello world ")));
334 EXPECT_EQ(hash_value(std::string("hello world ")),
335 hash_value(StringRef("hello world ")));
336 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
337 EXPECT_NE(hash_value(std::string("ello worl")),
338 hash_value(StringRef("hello world").slice(1, -1)));
339}
340
Michael J. Spencer9130b422012-03-10 23:02:54 +0000341struct UnsignedPair {
342 const char *Str;
343 uint64_t Expected;
344} Unsigned[] =
345 { {"0", 0}
346 , {"255", 255}
347 , {"256", 256}
348 , {"65535", 65535}
349 , {"65536", 65536}
Michael J. Spencerdbb4b2f2012-03-11 00:51:01 +0000350 , {"4294967295", 4294967295ULL}
351 , {"4294967296", 4294967296ULL}
Michael J. Spencer9130b422012-03-10 23:02:54 +0000352 , {"18446744073709551615", 18446744073709551615ULL}
353 , {"042", 34}
354 , {"0x42", 66}
355 , {"0b101010", 42}
356 };
357
358struct SignedPair {
359 const char *Str;
360 int64_t Expected;
361} Signed[] =
362 { {"0", 0}
363 , {"-0", 0}
364 , {"127", 127}
365 , {"128", 128}
366 , {"-128", -128}
367 , {"-129", -129}
368 , {"32767", 32767}
369 , {"32768", 32768}
370 , {"-32768", -32768}
371 , {"-32769", -32769}
Michael J. Spencerdbb4b2f2012-03-11 00:51:01 +0000372 , {"2147483647", 2147483647LL}
373 , {"2147483648", 2147483648LL}
Michael J. Spencer9130b422012-03-10 23:02:54 +0000374 , {"-2147483648", -2147483648LL}
375 , {"-2147483649", -2147483649LL}
376 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
377 , {"042", 34}
378 , {"0x42", 66}
379 , {"0b101010", 42}
380 , {"-042", -34}
381 , {"-0x42", -66}
382 , {"-0b101010", -42}
383 };
384
385TEST(StringRefTest, getAsInteger) {
386 uint8_t U8;
387 uint16_t U16;
388 uint32_t U32;
389 uint64_t U64;
390
391 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
392 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
393 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
394 ASSERT_FALSE(U8Success);
395 EXPECT_EQ(U8, Unsigned[i].Expected);
396 } else {
397 ASSERT_TRUE(U8Success);
398 }
399 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
400 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
401 ASSERT_FALSE(U16Success);
402 EXPECT_EQ(U16, Unsigned[i].Expected);
403 } else {
404 ASSERT_TRUE(U16Success);
405 }
406 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
407 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
408 ASSERT_FALSE(U32Success);
409 EXPECT_EQ(U32, Unsigned[i].Expected);
410 } else {
411 ASSERT_TRUE(U32Success);
412 }
413 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
414 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
415 ASSERT_FALSE(U64Success);
416 EXPECT_EQ(U64, Unsigned[i].Expected);
417 } else {
418 ASSERT_TRUE(U64Success);
419 }
420 }
421
422 int8_t S8;
423 int16_t S16;
424 int32_t S32;
425 int64_t S64;
426
427 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
428 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
429 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
430 ASSERT_FALSE(S8Success);
431 EXPECT_EQ(S8, Signed[i].Expected);
432 } else {
433 ASSERT_TRUE(S8Success);
434 }
435 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
436 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
437 ASSERT_FALSE(S16Success);
438 EXPECT_EQ(S16, Signed[i].Expected);
439 } else {
440 ASSERT_TRUE(S16Success);
441 }
442 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
443 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
444 ASSERT_FALSE(S32Success);
445 EXPECT_EQ(S32, Signed[i].Expected);
446 } else {
447 ASSERT_TRUE(S32Success);
448 }
449 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
450 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
451 ASSERT_FALSE(S64Success);
452 EXPECT_EQ(S64, Signed[i].Expected);
453 } else {
454 ASSERT_TRUE(S64Success);
455 }
456 }
457}
458
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000459
460static const char* BadStrings[] = {
461 "18446744073709551617" // value just over max
462 , "123456789012345678901" // value way too large
463 , "4t23v" // illegal decimal characters
464 , "0x123W56" // illegal hex characters
Benjamin Kramere25de4a2012-10-03 18:54:36 +0000465 , "0b2" // illegal bin characters
466 , "08" // illegal oct characters
467 , "0o8" // illegal oct characters
468 , "-123" // negative unsigned value
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000469};
470
471
472TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
Nick Kledzik436eaa82012-10-03 19:27:25 +0000473 unsigned long long U64;
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000474 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
Benjamin Kramere25de4a2012-10-03 18:54:36 +0000475 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000476 ASSERT_TRUE(IsBadNumber);
477 }
478}
479
480
481
Daniel Dunbar4cf95d72009-07-21 07:28:51 +0000482} // end anonymous namespace