blob: 6354026d7aeb49091d04db4b556593e08dc6b213 [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"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000012#include "llvm/ADT/STLExtras.h"
Rafael Espindola7c685492009-11-13 02:18:25 +000013#include "llvm/ADT/SmallVector.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000014#include "llvm/ADT/StringExtras.h"
Nick Kledzik4d6d9812014-02-05 22:22:56 +000015#include "llvm/Support/Allocator.h"
Daniel Dunbare23388b2009-07-22 17:13:20 +000016#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000017#include "gtest/gtest.h"
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000018using namespace llvm;
19
Douglas Gregor4ee2cf62009-12-24 21:15:37 +000020namespace llvm {
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000021
Daniel Dunbar44981682009-09-16 22:38:48 +000022std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
Chris Lattner2114b762011-01-27 07:35:27 +000023 OS << S.str();
Daniel Dunbar44981682009-09-16 22:38:48 +000024 return OS;
25}
26
27std::ostream &operator<<(std::ostream &OS,
28 const std::pair<StringRef, StringRef> &P) {
29 OS << "(" << P.first << ", " << P.second << ")";
30 return OS;
31}
32
Douglas Gregor4ee2cf62009-12-24 21:15:37 +000033}
34
35namespace {
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000036TEST(StringRefTest, Construction) {
Daniel Dunbar44981682009-09-16 22:38:48 +000037 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")));
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000041}
42
43TEST(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)
Daniel Dunbar44981682009-09-16 22:38:48 +000047 EXPECT_EQ(*it, *p);
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000048}
49
50TEST(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"));
Benjamin Kramerb04d4af2010-08-26 14:21:08 +000060 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"));
Rui Ueyama00e24e42013-10-30 18:32:26 +000066 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"));
Benjamin Kramerb04d4af2010-08-26 14:21:08 +000069 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
70 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
Jakob Stoklund Olesend1d7ed62010-05-26 21:47:28 +000071
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"));
Benjamin Kramer9bf03802010-08-26 15:25:35 +000082 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
Jakob Stoklund Olesenc874e2d2011-09-30 17:03:55 +000083 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"));
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000089}
90
91TEST(StringRefTest, Operators) {
Daniel Dunbar44981682009-09-16 22:38:48 +000092 EXPECT_EQ("", StringRef());
Daniel Dunbar25f9fc52009-07-21 07:28:51 +000093 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"));
Daniel Dunbar44981682009-09-16 22:38:48 +0000101 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
Daniel Dunbar25f9fc52009-07-21 07:28:51 +0000102 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
Daniel Dunbar44981682009-09-16 22:38:48 +0000108TEST(StringRefTest, Substr) {
Daniel Dunbar1f982102009-07-21 09:18:49 +0000109 StringRef Str("hello");
Daniel Dunbar44981682009-09-16 22:38:48 +0000110 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}
Daniel Dunbar1f982102009-07-21 09:18:49 +0000115
Daniel Dunbar44981682009-09-16 22:38:48 +0000116TEST(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}
Daniel Dunbar56563f32009-07-26 03:18:15 +0000124
Daniel Dunbar44981682009-09-16 22:38:48 +0000125TEST(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'));
Daniel Dunbar56563f32009-07-26 03:18:15 +0000137
Daniel Dunbar44981682009-09-16 22:38:48 +0000138 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
Rafael Espindolad554e442009-11-13 01:24:40 +0000150TEST(StringRefTest, Split2) {
Rafael Espindola7c685492009-11-13 02:18:25 +0000151 SmallVector<StringRef, 5> parts;
152 SmallVector<StringRef, 5> expected;
Rafael Espindolad554e442009-11-13 01:24:40 +0000153
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
Rafael Espindolaff2c72b2009-11-13 04:55:09 +0000182 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
Rafael Espindolad554e442009-11-13 01:24:40 +0000187 // 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);
Chandler Carruth47712172015-09-10 06:07:03 +0000228
229 expected.clear(); parts.clear();
230 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
231 StringRef("a,,b,c").split(parts, ',', 3, false);
232 EXPECT_TRUE(parts == expected);
Chandler Carruth4425c912015-09-10 07:51:37 +0000233
234 expected.clear(); parts.clear();
235 expected.push_back("");
236 StringRef().split(parts, ",", 0, true);
237 EXPECT_TRUE(parts == expected);
238
239 expected.clear(); parts.clear();
240 expected.push_back(StringRef());
241 StringRef("").split(parts, ",", 0, true);
242 EXPECT_TRUE(parts == expected);
243
244 expected.clear(); parts.clear();
245 StringRef("").split(parts, ",", 0, false);
246 EXPECT_TRUE(parts == expected);
247 StringRef().split(parts, ",", 0, false);
248 EXPECT_TRUE(parts == expected);
249
250 expected.clear(); parts.clear();
251 expected.push_back("a");
252 expected.push_back("");
253 expected.push_back("b");
254 expected.push_back("c,d");
255 StringRef("a,,b,c,d").split(parts, ",", 3, true);
256 EXPECT_TRUE(parts == expected);
257
258 expected.clear(); parts.clear();
259 expected.push_back("");
260 StringRef().split(parts, ',', 0, true);
261 EXPECT_TRUE(parts == expected);
262
263 expected.clear(); parts.clear();
264 expected.push_back(StringRef());
265 StringRef("").split(parts, ',', 0, true);
266 EXPECT_TRUE(parts == expected);
267
268 expected.clear(); parts.clear();
269 StringRef("").split(parts, ',', 0, false);
270 EXPECT_TRUE(parts == expected);
271 StringRef().split(parts, ',', 0, false);
272 EXPECT_TRUE(parts == expected);
273
274 expected.clear(); parts.clear();
275 expected.push_back("a");
276 expected.push_back("");
277 expected.push_back("b");
278 expected.push_back("c,d");
279 StringRef("a,,b,c,d").split(parts, ',', 3, true);
280 EXPECT_TRUE(parts == expected);
Rafael Espindolad554e442009-11-13 01:24:40 +0000281}
282
Michael J. Spencer93303812012-05-11 22:08:50 +0000283TEST(StringRefTest, Trim) {
284 StringRef Str0("hello");
285 StringRef Str1(" hello ");
286 StringRef Str2(" hello ");
287
288 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
289 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
290 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
291 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
292 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
293 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
294 EXPECT_EQ(StringRef("hello"), Str0.trim());
295 EXPECT_EQ(StringRef("hello"), Str1.trim());
296 EXPECT_EQ(StringRef("hello"), Str2.trim());
297
298 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
299
300 EXPECT_EQ(StringRef(""), StringRef("").trim());
301 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
302 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
303 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
Vedant Kumarf114b402016-02-16 01:48:39 +0000304 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
Michael J. Spencer93303812012-05-11 22:08:50 +0000305}
306
Daniel Dunbar44981682009-09-16 22:38:48 +0000307TEST(StringRefTest, StartsWith) {
308 StringRef Str("hello");
Rui Ueyamab6decb02013-10-28 22:42:54 +0000309 EXPECT_TRUE(Str.startswith(""));
Daniel Dunbar1f982102009-07-21 09:18:49 +0000310 EXPECT_TRUE(Str.startswith("he"));
311 EXPECT_FALSE(Str.startswith("helloworld"));
312 EXPECT_FALSE(Str.startswith("hi"));
Daniel Dunbar44981682009-09-16 22:38:48 +0000313}
Daniel Dunbare23388b2009-07-22 17:13:20 +0000314
Rui Ueyama00e24e42013-10-30 18:32:26 +0000315TEST(StringRefTest, StartsWithLower) {
316 StringRef Str("heLLo");
317 EXPECT_TRUE(Str.startswith_lower(""));
318 EXPECT_TRUE(Str.startswith_lower("he"));
319 EXPECT_TRUE(Str.startswith_lower("hell"));
320 EXPECT_TRUE(Str.startswith_lower("HELlo"));
321 EXPECT_FALSE(Str.startswith_lower("helloworld"));
322 EXPECT_FALSE(Str.startswith_lower("hi"));
323}
324
Eli Friedman00879d82009-12-21 06:49:24 +0000325TEST(StringRefTest, EndsWith) {
326 StringRef Str("hello");
Rui Ueyamab6decb02013-10-28 22:42:54 +0000327 EXPECT_TRUE(Str.endswith(""));
Eli Friedman00879d82009-12-21 06:49:24 +0000328 EXPECT_TRUE(Str.endswith("lo"));
329 EXPECT_FALSE(Str.endswith("helloworld"));
330 EXPECT_FALSE(Str.endswith("worldhello"));
331 EXPECT_FALSE(Str.endswith("so"));
332}
333
Rui Ueyama00e24e42013-10-30 18:32:26 +0000334TEST(StringRefTest, EndsWithLower) {
335 StringRef Str("heLLo");
336 EXPECT_TRUE(Str.endswith_lower(""));
337 EXPECT_TRUE(Str.endswith_lower("lo"));
338 EXPECT_TRUE(Str.endswith_lower("LO"));
339 EXPECT_TRUE(Str.endswith_lower("ELlo"));
340 EXPECT_FALSE(Str.endswith_lower("helloworld"));
341 EXPECT_FALSE(Str.endswith_lower("hi"));
342}
343
Daniel Dunbar44981682009-09-16 22:38:48 +0000344TEST(StringRefTest, Find) {
345 StringRef Str("hello");
346 EXPECT_EQ(2U, Str.find('l'));
347 EXPECT_EQ(StringRef::npos, Str.find('z'));
348 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
349 EXPECT_EQ(0U, Str.find("hello"));
350 EXPECT_EQ(1U, Str.find("ello"));
351 EXPECT_EQ(StringRef::npos, Str.find("zz"));
Daniel Dunbar9806e4a2009-11-11 00:28:53 +0000352 EXPECT_EQ(2U, Str.find("ll", 2));
353 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
Benjamin Kramer4d681d72011-10-15 10:08:31 +0000354 EXPECT_EQ(0U, Str.find(""));
355 StringRef LongStr("hellx xello hell ello world foo bar hello");
356 EXPECT_EQ(36U, LongStr.find("hello"));
357 EXPECT_EQ(28U, LongStr.find("foo"));
358 EXPECT_EQ(12U, LongStr.find("hell", 2));
359 EXPECT_EQ(0U, LongStr.find(""));
Daniel Dunbar44981682009-09-16 22:38:48 +0000360
361 EXPECT_EQ(3U, Str.rfind('l'));
362 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
363 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
364 EXPECT_EQ(0U, Str.rfind("hello"));
365 EXPECT_EQ(1U, Str.rfind("ello"));
366 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
Daniel Dunbar9806e4a2009-11-11 00:28:53 +0000367
368 EXPECT_EQ(2U, Str.find_first_of('l'));
369 EXPECT_EQ(1U, Str.find_first_of("el"));
370 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
371
372 EXPECT_EQ(1U, Str.find_first_not_of('h'));
373 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
374 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
Michael J. Spencer93303812012-05-11 22:08:50 +0000375
376 EXPECT_EQ(3U, Str.find_last_not_of('o'));
377 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
378 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
Daniel Dunbar44981682009-09-16 22:38:48 +0000379}
380
381TEST(StringRefTest, Count) {
382 StringRef Str("hello");
383 EXPECT_EQ(2U, Str.count('l'));
384 EXPECT_EQ(1U, Str.count('o'));
385 EXPECT_EQ(0U, Str.count('z'));
386 EXPECT_EQ(0U, Str.count("helloworld"));
387 EXPECT_EQ(1U, Str.count("hello"));
388 EXPECT_EQ(1U, Str.count("ello"));
389 EXPECT_EQ(0U, Str.count("zz"));
390}
391
Douglas Gregor5639af42009-12-31 04:24:34 +0000392TEST(StringRefTest, EditDistance) {
393 StringRef Str("hello");
Benjamin Kramer738800d2009-12-31 16:27:13 +0000394 EXPECT_EQ(2U, Str.edit_distance("hill"));
Douglas Gregor5639af42009-12-31 04:24:34 +0000395}
396
Daniel Dunbar44981682009-09-16 22:38:48 +0000397TEST(StringRefTest, Misc) {
Daniel Dunbare23388b2009-07-22 17:13:20 +0000398 std::string Storage;
399 raw_string_ostream OS(Storage);
400 OS << StringRef("hello");
401 EXPECT_EQ("hello", OS.str());
Daniel Dunbar1f982102009-07-21 09:18:49 +0000402}
403
Chandler Carruthca99ad32012-03-04 10:55:27 +0000404TEST(StringRefTest, Hashing) {
405 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
406 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
407 std::string S = "hello world";
408 hash_code H = hash_value(S);
409 EXPECT_EQ(H, hash_value(StringRef("hello world")));
410 EXPECT_EQ(H, hash_value(StringRef(S)));
411 EXPECT_NE(H, hash_value(StringRef("hello worl")));
412 EXPECT_EQ(hash_value(std::string("hello worl")),
413 hash_value(StringRef("hello worl")));
414 EXPECT_NE(H, hash_value(StringRef("hello world ")));
415 EXPECT_EQ(hash_value(std::string("hello world ")),
416 hash_value(StringRef("hello world ")));
417 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
418 EXPECT_NE(hash_value(std::string("ello worl")),
419 hash_value(StringRef("hello world").slice(1, -1)));
420}
421
Michael J. Spencercfa95f62012-03-10 23:02:54 +0000422struct UnsignedPair {
423 const char *Str;
424 uint64_t Expected;
425} Unsigned[] =
426 { {"0", 0}
427 , {"255", 255}
428 , {"256", 256}
429 , {"65535", 65535}
430 , {"65536", 65536}
Michael J. Spencer914dc772012-03-11 00:51:01 +0000431 , {"4294967295", 4294967295ULL}
432 , {"4294967296", 4294967296ULL}
Michael J. Spencercfa95f62012-03-10 23:02:54 +0000433 , {"18446744073709551615", 18446744073709551615ULL}
434 , {"042", 34}
435 , {"0x42", 66}
436 , {"0b101010", 42}
437 };
438
439struct SignedPair {
440 const char *Str;
441 int64_t Expected;
442} Signed[] =
443 { {"0", 0}
444 , {"-0", 0}
445 , {"127", 127}
446 , {"128", 128}
447 , {"-128", -128}
448 , {"-129", -129}
449 , {"32767", 32767}
450 , {"32768", 32768}
451 , {"-32768", -32768}
452 , {"-32769", -32769}
Michael J. Spencer914dc772012-03-11 00:51:01 +0000453 , {"2147483647", 2147483647LL}
454 , {"2147483648", 2147483648LL}
Michael J. Spencercfa95f62012-03-10 23:02:54 +0000455 , {"-2147483648", -2147483648LL}
456 , {"-2147483649", -2147483649LL}
457 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
458 , {"042", 34}
459 , {"0x42", 66}
460 , {"0b101010", 42}
461 , {"-042", -34}
462 , {"-0x42", -66}
463 , {"-0b101010", -42}
464 };
465
466TEST(StringRefTest, getAsInteger) {
467 uint8_t U8;
468 uint16_t U16;
469 uint32_t U32;
470 uint64_t U64;
471
472 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
473 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
474 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
475 ASSERT_FALSE(U8Success);
476 EXPECT_EQ(U8, Unsigned[i].Expected);
477 } else {
478 ASSERT_TRUE(U8Success);
479 }
480 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
481 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
482 ASSERT_FALSE(U16Success);
483 EXPECT_EQ(U16, Unsigned[i].Expected);
484 } else {
485 ASSERT_TRUE(U16Success);
486 }
487 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
488 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
489 ASSERT_FALSE(U32Success);
490 EXPECT_EQ(U32, Unsigned[i].Expected);
491 } else {
492 ASSERT_TRUE(U32Success);
493 }
494 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
495 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
496 ASSERT_FALSE(U64Success);
497 EXPECT_EQ(U64, Unsigned[i].Expected);
498 } else {
499 ASSERT_TRUE(U64Success);
500 }
501 }
502
503 int8_t S8;
504 int16_t S16;
505 int32_t S32;
506 int64_t S64;
507
508 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
509 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
510 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
511 ASSERT_FALSE(S8Success);
512 EXPECT_EQ(S8, Signed[i].Expected);
513 } else {
514 ASSERT_TRUE(S8Success);
515 }
516 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
517 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
518 ASSERT_FALSE(S16Success);
519 EXPECT_EQ(S16, Signed[i].Expected);
520 } else {
521 ASSERT_TRUE(S16Success);
522 }
523 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
524 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
525 ASSERT_FALSE(S32Success);
526 EXPECT_EQ(S32, Signed[i].Expected);
527 } else {
528 ASSERT_TRUE(S32Success);
529 }
530 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
531 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
532 ASSERT_FALSE(S64Success);
533 EXPECT_EQ(S64, Signed[i].Expected);
534 } else {
535 ASSERT_TRUE(S64Success);
536 }
537 }
538}
539
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000540
541static const char* BadStrings[] = {
542 "18446744073709551617" // value just over max
543 , "123456789012345678901" // value way too large
544 , "4t23v" // illegal decimal characters
545 , "0x123W56" // illegal hex characters
Benjamin Kramerd95ceff2012-10-03 18:54:36 +0000546 , "0b2" // illegal bin characters
547 , "08" // illegal oct characters
548 , "0o8" // illegal oct characters
549 , "-123" // negative unsigned value
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000550};
551
552
553TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
Nick Kledzik85a62b12012-10-03 19:27:25 +0000554 unsigned long long U64;
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000555 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
Benjamin Kramerd95ceff2012-10-03 18:54:36 +0000556 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000557 ASSERT_TRUE(IsBadNumber);
558 }
559}
560
Joerg Sonnenberger6c4dc2b2013-09-03 20:43:54 +0000561static const char *join_input[] = { "a", "b", "c" };
562static const char join_result1[] = "a";
563static const char join_result2[] = "a:b:c";
564static const char join_result3[] = "a::b::c";
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000565
Joerg Sonnenberger6c4dc2b2013-09-03 20:43:54 +0000566TEST(StringRefTest, joinStrings) {
567 std::vector<StringRef> v1;
568 std::vector<std::string> v2;
569 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
570 v1.push_back(join_input[i]);
571 v2.push_back(join_input[i]);
572 }
573
574 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
575 EXPECT_TRUE(v1_join1);
576 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
577 EXPECT_TRUE(v1_join2);
578 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
579 EXPECT_TRUE(v1_join3);
580
581 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
582 EXPECT_TRUE(v2_join1);
583 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
584 EXPECT_TRUE(v2_join2);
585 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
586 EXPECT_TRUE(v2_join3);
587}
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000588
Nick Kledzik4d6d9812014-02-05 22:22:56 +0000589
590TEST(StringRefTest, AllocatorCopy) {
591 BumpPtrAllocator Alloc;
592 StringRef Str1 = "hello";
593 StringRef Str2 = "bye";
594 StringRef Str1c = Str1.copy(Alloc);
595 StringRef Str2c = Str2.copy(Alloc);
596 EXPECT_TRUE(Str1.equals(Str1c));
597 EXPECT_NE(Str1.data(), Str1c.data());
598 EXPECT_TRUE(Str2.equals(Str2c));
599 EXPECT_NE(Str2.data(), Str2c.data());
600}
601
602
Daniel Dunbar25f9fc52009-07-21 07:28:51 +0000603} // end anonymous namespace