blob: ca6b011d6959c668715ced238aaf9d1f77cb83fe [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
Chandler Carruth974c67e2016-07-31 02:19:13 +0000325TEST(StringRefTest, ConsumeFront) {
326 StringRef Str("hello");
327 EXPECT_TRUE(Str.consume_front(""));
328 EXPECT_EQ("hello", Str);
329 EXPECT_TRUE(Str.consume_front("he"));
330 EXPECT_EQ("llo", Str);
331 EXPECT_FALSE(Str.consume_front("lloworld"));
332 EXPECT_EQ("llo", Str);
333 EXPECT_FALSE(Str.consume_front("lol"));
334 EXPECT_EQ("llo", Str);
335 EXPECT_TRUE(Str.consume_front("llo"));
336 EXPECT_EQ("", Str);
337 EXPECT_FALSE(Str.consume_front("o"));
338 EXPECT_TRUE(Str.consume_front(""));
339}
340
Eli Friedman00879d82009-12-21 06:49:24 +0000341TEST(StringRefTest, EndsWith) {
342 StringRef Str("hello");
Rui Ueyamab6decb02013-10-28 22:42:54 +0000343 EXPECT_TRUE(Str.endswith(""));
Eli Friedman00879d82009-12-21 06:49:24 +0000344 EXPECT_TRUE(Str.endswith("lo"));
345 EXPECT_FALSE(Str.endswith("helloworld"));
346 EXPECT_FALSE(Str.endswith("worldhello"));
347 EXPECT_FALSE(Str.endswith("so"));
348}
349
Rui Ueyama00e24e42013-10-30 18:32:26 +0000350TEST(StringRefTest, EndsWithLower) {
351 StringRef Str("heLLo");
352 EXPECT_TRUE(Str.endswith_lower(""));
353 EXPECT_TRUE(Str.endswith_lower("lo"));
354 EXPECT_TRUE(Str.endswith_lower("LO"));
355 EXPECT_TRUE(Str.endswith_lower("ELlo"));
356 EXPECT_FALSE(Str.endswith_lower("helloworld"));
357 EXPECT_FALSE(Str.endswith_lower("hi"));
358}
359
Chandler Carruth974c67e2016-07-31 02:19:13 +0000360TEST(StringRefTest, ConsumeBack) {
361 StringRef Str("hello");
362 EXPECT_TRUE(Str.consume_back(""));
363 EXPECT_EQ("hello", Str);
364 EXPECT_TRUE(Str.consume_back("lo"));
365 EXPECT_EQ("hel", Str);
366 EXPECT_FALSE(Str.consume_back("helhel"));
367 EXPECT_EQ("hel", Str);
368 EXPECT_FALSE(Str.consume_back("hle"));
369 EXPECT_EQ("hel", Str);
370 EXPECT_TRUE(Str.consume_back("hel"));
371 EXPECT_EQ("", Str);
372 EXPECT_FALSE(Str.consume_back("h"));
373 EXPECT_TRUE(Str.consume_back(""));
374}
375
Daniel Dunbar44981682009-09-16 22:38:48 +0000376TEST(StringRefTest, Find) {
377 StringRef Str("hello");
378 EXPECT_EQ(2U, Str.find('l'));
379 EXPECT_EQ(StringRef::npos, Str.find('z'));
380 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
381 EXPECT_EQ(0U, Str.find("hello"));
382 EXPECT_EQ(1U, Str.find("ello"));
383 EXPECT_EQ(StringRef::npos, Str.find("zz"));
Daniel Dunbar9806e4a2009-11-11 00:28:53 +0000384 EXPECT_EQ(2U, Str.find("ll", 2));
385 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
Benjamin Kramer4d681d72011-10-15 10:08:31 +0000386 EXPECT_EQ(0U, Str.find(""));
387 StringRef LongStr("hellx xello hell ello world foo bar hello");
388 EXPECT_EQ(36U, LongStr.find("hello"));
389 EXPECT_EQ(28U, LongStr.find("foo"));
390 EXPECT_EQ(12U, LongStr.find("hell", 2));
391 EXPECT_EQ(0U, LongStr.find(""));
Daniel Dunbar44981682009-09-16 22:38:48 +0000392
393 EXPECT_EQ(3U, Str.rfind('l'));
394 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
395 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
396 EXPECT_EQ(0U, Str.rfind("hello"));
397 EXPECT_EQ(1U, Str.rfind("ello"));
398 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
Daniel Dunbar9806e4a2009-11-11 00:28:53 +0000399
400 EXPECT_EQ(2U, Str.find_first_of('l'));
401 EXPECT_EQ(1U, Str.find_first_of("el"));
402 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
403
404 EXPECT_EQ(1U, Str.find_first_not_of('h'));
405 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
406 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
Michael J. Spencer93303812012-05-11 22:08:50 +0000407
408 EXPECT_EQ(3U, Str.find_last_not_of('o'));
409 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
410 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
Daniel Dunbar44981682009-09-16 22:38:48 +0000411}
412
413TEST(StringRefTest, Count) {
414 StringRef Str("hello");
415 EXPECT_EQ(2U, Str.count('l'));
416 EXPECT_EQ(1U, Str.count('o'));
417 EXPECT_EQ(0U, Str.count('z'));
418 EXPECT_EQ(0U, Str.count("helloworld"));
419 EXPECT_EQ(1U, Str.count("hello"));
420 EXPECT_EQ(1U, Str.count("ello"));
421 EXPECT_EQ(0U, Str.count("zz"));
422}
423
Douglas Gregor5639af42009-12-31 04:24:34 +0000424TEST(StringRefTest, EditDistance) {
425 StringRef Str("hello");
Benjamin Kramer738800d2009-12-31 16:27:13 +0000426 EXPECT_EQ(2U, Str.edit_distance("hill"));
Douglas Gregor5639af42009-12-31 04:24:34 +0000427}
428
Daniel Dunbar44981682009-09-16 22:38:48 +0000429TEST(StringRefTest, Misc) {
Daniel Dunbare23388b2009-07-22 17:13:20 +0000430 std::string Storage;
431 raw_string_ostream OS(Storage);
432 OS << StringRef("hello");
433 EXPECT_EQ("hello", OS.str());
Daniel Dunbar1f982102009-07-21 09:18:49 +0000434}
435
Chandler Carruthca99ad32012-03-04 10:55:27 +0000436TEST(StringRefTest, Hashing) {
437 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
438 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
439 std::string S = "hello world";
440 hash_code H = hash_value(S);
441 EXPECT_EQ(H, hash_value(StringRef("hello world")));
442 EXPECT_EQ(H, hash_value(StringRef(S)));
443 EXPECT_NE(H, hash_value(StringRef("hello worl")));
444 EXPECT_EQ(hash_value(std::string("hello worl")),
445 hash_value(StringRef("hello worl")));
446 EXPECT_NE(H, hash_value(StringRef("hello world ")));
447 EXPECT_EQ(hash_value(std::string("hello world ")),
448 hash_value(StringRef("hello world ")));
449 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
450 EXPECT_NE(hash_value(std::string("ello worl")),
451 hash_value(StringRef("hello world").slice(1, -1)));
452}
453
Michael J. Spencercfa95f62012-03-10 23:02:54 +0000454struct UnsignedPair {
455 const char *Str;
456 uint64_t Expected;
457} Unsigned[] =
458 { {"0", 0}
459 , {"255", 255}
460 , {"256", 256}
461 , {"65535", 65535}
462 , {"65536", 65536}
Michael J. Spencer914dc772012-03-11 00:51:01 +0000463 , {"4294967295", 4294967295ULL}
464 , {"4294967296", 4294967296ULL}
Michael J. Spencercfa95f62012-03-10 23:02:54 +0000465 , {"18446744073709551615", 18446744073709551615ULL}
466 , {"042", 34}
467 , {"0x42", 66}
468 , {"0b101010", 42}
469 };
470
471struct SignedPair {
472 const char *Str;
473 int64_t Expected;
474} Signed[] =
475 { {"0", 0}
476 , {"-0", 0}
477 , {"127", 127}
478 , {"128", 128}
479 , {"-128", -128}
480 , {"-129", -129}
481 , {"32767", 32767}
482 , {"32768", 32768}
483 , {"-32768", -32768}
484 , {"-32769", -32769}
Michael J. Spencer914dc772012-03-11 00:51:01 +0000485 , {"2147483647", 2147483647LL}
486 , {"2147483648", 2147483648LL}
Michael J. Spencercfa95f62012-03-10 23:02:54 +0000487 , {"-2147483648", -2147483648LL}
488 , {"-2147483649", -2147483649LL}
489 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
490 , {"042", 34}
491 , {"0x42", 66}
492 , {"0b101010", 42}
493 , {"-042", -34}
494 , {"-0x42", -66}
495 , {"-0b101010", -42}
496 };
497
498TEST(StringRefTest, getAsInteger) {
499 uint8_t U8;
500 uint16_t U16;
501 uint32_t U32;
502 uint64_t U64;
503
504 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
505 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
506 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
507 ASSERT_FALSE(U8Success);
508 EXPECT_EQ(U8, Unsigned[i].Expected);
509 } else {
510 ASSERT_TRUE(U8Success);
511 }
512 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
513 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
514 ASSERT_FALSE(U16Success);
515 EXPECT_EQ(U16, Unsigned[i].Expected);
516 } else {
517 ASSERT_TRUE(U16Success);
518 }
519 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
520 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
521 ASSERT_FALSE(U32Success);
522 EXPECT_EQ(U32, Unsigned[i].Expected);
523 } else {
524 ASSERT_TRUE(U32Success);
525 }
526 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
527 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
528 ASSERT_FALSE(U64Success);
529 EXPECT_EQ(U64, Unsigned[i].Expected);
530 } else {
531 ASSERT_TRUE(U64Success);
532 }
533 }
534
535 int8_t S8;
536 int16_t S16;
537 int32_t S32;
538 int64_t S64;
539
540 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
541 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
542 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
543 ASSERT_FALSE(S8Success);
544 EXPECT_EQ(S8, Signed[i].Expected);
545 } else {
546 ASSERT_TRUE(S8Success);
547 }
548 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
549 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
550 ASSERT_FALSE(S16Success);
551 EXPECT_EQ(S16, Signed[i].Expected);
552 } else {
553 ASSERT_TRUE(S16Success);
554 }
555 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
556 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
557 ASSERT_FALSE(S32Success);
558 EXPECT_EQ(S32, Signed[i].Expected);
559 } else {
560 ASSERT_TRUE(S32Success);
561 }
562 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
563 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
564 ASSERT_FALSE(S64Success);
565 EXPECT_EQ(S64, Signed[i].Expected);
566 } else {
567 ASSERT_TRUE(S64Success);
568 }
569 }
570}
571
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000572
573static const char* BadStrings[] = {
Zachary Turnerd5d57632016-09-22 15:55:05 +0000574 "" // empty string
575 , "18446744073709551617" // value just over max
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000576 , "123456789012345678901" // value way too large
577 , "4t23v" // illegal decimal characters
578 , "0x123W56" // illegal hex characters
Benjamin Kramerd95ceff2012-10-03 18:54:36 +0000579 , "0b2" // illegal bin characters
580 , "08" // illegal oct characters
581 , "0o8" // illegal oct characters
582 , "-123" // negative unsigned value
Zachary Turneraec851c2016-09-22 19:21:32 +0000583 , "0x"
584 , "0b"
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000585};
586
587
588TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
Nick Kledzik85a62b12012-10-03 19:27:25 +0000589 unsigned long long U64;
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000590 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
Benjamin Kramerd95ceff2012-10-03 18:54:36 +0000591 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000592 ASSERT_TRUE(IsBadNumber);
593 }
594}
595
Zachary Turner65fd2fc2016-09-22 15:05:19 +0000596struct ConsumeUnsignedPair {
597 const char *Str;
598 uint64_t Expected;
599 const char *Leftover;
600} ConsumeUnsigned[] = {
601 {"0", 0, ""},
602 {"255", 255, ""},
603 {"256", 256, ""},
604 {"65535", 65535, ""},
605 {"65536", 65536, ""},
606 {"4294967295", 4294967295ULL, ""},
607 {"4294967296", 4294967296ULL, ""},
608 {"255A376", 255, "A376"},
609 {"18446744073709551615", 18446744073709551615ULL, ""},
610 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
611 {"042", 34, ""},
612 {"0x42", 66, ""},
613 {"0x42-0x34", 66, "-0x34"},
614 {"0b101010", 42, ""},
615 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit
616 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit
617 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
618
619struct ConsumeSignedPair {
620 const char *Str;
621 int64_t Expected;
622 const char *Leftover;
623} ConsumeSigned[] = {
624 {"0", 0, ""},
625 {"-0", 0, ""},
626 {"0-1", 0, "-1"},
627 {"-0-1", 0, "-1"},
628 {"127", 127, ""},
629 {"128", 128, ""},
630 {"127-1", 127, "-1"},
631 {"128-1", 128, "-1"},
632 {"-128", -128, ""},
633 {"-129", -129, ""},
634 {"-128-1", -128, "-1"},
635 {"-129-1", -129, "-1"},
636 {"32767", 32767, ""},
637 {"32768", 32768, ""},
638 {"32767-1", 32767, "-1"},
639 {"32768-1", 32768, "-1"},
640 {"-32768", -32768, ""},
641 {"-32769", -32769, ""},
642 {"-32768-1", -32768, "-1"},
643 {"-32769-1", -32769, "-1"},
644 {"2147483647", 2147483647LL, ""},
645 {"2147483648", 2147483648LL, ""},
646 {"2147483647-1", 2147483647LL, "-1"},
647 {"2147483648-1", 2147483648LL, "-1"},
648 {"-2147483648", -2147483648LL, ""},
649 {"-2147483649", -2147483649LL, ""},
650 {"-2147483648-1", -2147483648LL, "-1"},
651 {"-2147483649-1", -2147483649LL, "-1"},
652 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
653 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
654 {"042", 34, ""},
655 {"042-1", 34, "-1"},
656 {"0x42", 66, ""},
657 {"0x42-1", 66, "-1"},
658 {"0b101010", 42, ""},
659 {"0b101010-1", 42, "-1"},
660 {"-042", -34, ""},
661 {"-042-1", -34, "-1"},
662 {"-0x42", -66, ""},
663 {"-0x42-1", -66, "-1"},
664 {"-0b101010", -42, ""},
665 {"-0b101010-1", -42, "-1"}};
666
667TEST(StringRefTest, consumeIntegerUnsigned) {
668 uint8_t U8;
669 uint16_t U16;
670 uint32_t U32;
671 uint64_t U64;
672
673 for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) {
674 StringRef Str = ConsumeUnsigned[i].Str;
675 bool U8Success = Str.consumeInteger(0, U8);
676 if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==
677 ConsumeUnsigned[i].Expected) {
678 ASSERT_FALSE(U8Success);
679 EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);
680 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
681 } else {
682 ASSERT_TRUE(U8Success);
683 }
684
685 Str = ConsumeUnsigned[i].Str;
686 bool U16Success = Str.consumeInteger(0, U16);
687 if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==
688 ConsumeUnsigned[i].Expected) {
689 ASSERT_FALSE(U16Success);
690 EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);
691 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
692 } else {
693 ASSERT_TRUE(U16Success);
694 }
695
696 Str = ConsumeUnsigned[i].Str;
697 bool U32Success = Str.consumeInteger(0, U32);
698 if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==
699 ConsumeUnsigned[i].Expected) {
700 ASSERT_FALSE(U32Success);
701 EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);
702 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
703 } else {
704 ASSERT_TRUE(U32Success);
705 }
706
707 Str = ConsumeUnsigned[i].Str;
708 bool U64Success = Str.consumeInteger(0, U64);
709 if (static_cast<uint64_t>(ConsumeUnsigned[i].Expected) ==
710 ConsumeUnsigned[i].Expected) {
711 ASSERT_FALSE(U64Success);
712 EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);
713 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
714 } else {
715 ASSERT_TRUE(U64Success);
716 }
717 }
718}
719
720TEST(StringRefTest, consumeIntegerSigned) {
721 int8_t S8;
722 int16_t S16;
723 int32_t S32;
724 int64_t S64;
725
726 for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) {
727 StringRef Str = ConsumeSigned[i].Str;
728 bool S8Success = Str.consumeInteger(0, S8);
729 if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
730 ConsumeSigned[i].Expected) {
731 ASSERT_FALSE(S8Success);
732 EXPECT_EQ(S8, ConsumeSigned[i].Expected);
733 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
734 } else {
735 ASSERT_TRUE(S8Success);
736 }
737
738 Str = ConsumeSigned[i].Str;
739 bool S16Success = Str.consumeInteger(0, S16);
740 if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
741 ConsumeSigned[i].Expected) {
742 ASSERT_FALSE(S16Success);
743 EXPECT_EQ(S16, ConsumeSigned[i].Expected);
744 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
745 } else {
746 ASSERT_TRUE(S16Success);
747 }
748
749 Str = ConsumeSigned[i].Str;
750 bool S32Success = Str.consumeInteger(0, S32);
751 if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
752 ConsumeSigned[i].Expected) {
753 ASSERT_FALSE(S32Success);
754 EXPECT_EQ(S32, ConsumeSigned[i].Expected);
755 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
756 } else {
757 ASSERT_TRUE(S32Success);
758 }
759
760 Str = ConsumeSigned[i].Str;
761 bool S64Success = Str.consumeInteger(0, S64);
762 if (static_cast<int64_t>(ConsumeSigned[i].Expected) ==
763 ConsumeSigned[i].Expected) {
764 ASSERT_FALSE(S64Success);
765 EXPECT_EQ(S64, ConsumeSigned[i].Expected);
766 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
767 } else {
768 ASSERT_TRUE(S64Success);
769 }
770 }
771}
772
Joerg Sonnenberger6c4dc2b2013-09-03 20:43:54 +0000773static const char *join_input[] = { "a", "b", "c" };
774static const char join_result1[] = "a";
775static const char join_result2[] = "a:b:c";
776static const char join_result3[] = "a::b::c";
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000777
Joerg Sonnenberger6c4dc2b2013-09-03 20:43:54 +0000778TEST(StringRefTest, joinStrings) {
779 std::vector<StringRef> v1;
780 std::vector<std::string> v2;
781 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
782 v1.push_back(join_input[i]);
783 v2.push_back(join_input[i]);
784 }
785
786 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
787 EXPECT_TRUE(v1_join1);
788 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
789 EXPECT_TRUE(v1_join2);
790 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
791 EXPECT_TRUE(v1_join3);
792
793 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
794 EXPECT_TRUE(v2_join1);
795 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
796 EXPECT_TRUE(v2_join2);
797 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
798 EXPECT_TRUE(v2_join3);
799}
Nick Kledzikf8a75ee2012-10-03 18:15:27 +0000800
Nick Kledzik4d6d9812014-02-05 22:22:56 +0000801
802TEST(StringRefTest, AllocatorCopy) {
803 BumpPtrAllocator Alloc;
Pete Cooperb08d9062016-03-23 21:49:31 +0000804 // First test empty strings. We don't want these to allocate anything on the
805 // allocator.
806 StringRef StrEmpty = "";
807 StringRef StrEmptyc = StrEmpty.copy(Alloc);
808 EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
809 EXPECT_EQ(StrEmptyc.data(), nullptr);
810 EXPECT_EQ(StrEmptyc.size(), 0u);
811 EXPECT_EQ(Alloc.getTotalMemory(), 0u);
812
Nick Kledzik4d6d9812014-02-05 22:22:56 +0000813 StringRef Str1 = "hello";
814 StringRef Str2 = "bye";
815 StringRef Str1c = Str1.copy(Alloc);
816 StringRef Str2c = Str2.copy(Alloc);
817 EXPECT_TRUE(Str1.equals(Str1c));
818 EXPECT_NE(Str1.data(), Str1c.data());
819 EXPECT_TRUE(Str2.equals(Str2c));
820 EXPECT_NE(Str2.data(), Str2c.data());
821}
822
Zachary Turner84fc0592016-08-30 17:29:59 +0000823TEST(StringRefTest, Drop) {
824 StringRef Test("StringRefTest::Drop");
825
826 StringRef Dropped = Test.drop_front(5);
827 EXPECT_EQ(Dropped, "gRefTest::Drop");
828
829 Dropped = Test.drop_back(5);
830 EXPECT_EQ(Dropped, "StringRefTest:");
831
832 Dropped = Test.drop_front(0);
833 EXPECT_EQ(Dropped, Test);
834
835 Dropped = Test.drop_back(0);
836 EXPECT_EQ(Dropped, Test);
837
838 Dropped = Test.drop_front(Test.size());
839 EXPECT_TRUE(Dropped.empty());
840
841 Dropped = Test.drop_back(Test.size());
842 EXPECT_TRUE(Dropped.empty());
843}
844
845TEST(StringRefTest, Take) {
846 StringRef Test("StringRefTest::Take");
847
848 StringRef Taken = Test.take_front(5);
849 EXPECT_EQ(Taken, "Strin");
850
851 Taken = Test.take_back(5);
852 EXPECT_EQ(Taken, ":Take");
853
854 Taken = Test.take_front(Test.size());
855 EXPECT_EQ(Taken, Test);
856
857 Taken = Test.take_back(Test.size());
858 EXPECT_EQ(Taken, Test);
859
860 Taken = Test.take_front(0);
861 EXPECT_TRUE(Taken.empty());
862
863 Taken = Test.take_back(0);
864 EXPECT_TRUE(Taken.empty());
865}
Nick Kledzik4d6d9812014-02-05 22:22:56 +0000866
Zachary Turner84505f92016-09-25 03:27:29 +0000867TEST(StringRefTest, FindIf) {
868 StringRef Punct("Test.String");
869 StringRef NoPunct("ABCDEFG");
870 StringRef Empty;
871
872 auto IsPunct = [](char c) { return ::ispunct(c); };
873 auto IsAlpha = [](char c) { return ::isalpha(c); };
Zachary Turnerc3618892016-09-25 03:57:34 +0000874 EXPECT_EQ(4U, Punct.find_if(IsPunct));
Zachary Turner84505f92016-09-25 03:27:29 +0000875 EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
876 EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
877
Zachary Turnerc3618892016-09-25 03:57:34 +0000878 EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
Zachary Turner84505f92016-09-25 03:27:29 +0000879 EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
880 EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
881}
882
883TEST(StringRefTest, TakeWhileUntil) {
884 StringRef Test("String With 1 Number");
885
886 StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
887 EXPECT_EQ("", Taken);
888
889 Taken = Test.take_until([](char c) { return ::isdigit(c); });
890 EXPECT_EQ("String With ", Taken);
891
892 Taken = Test.take_while([](char c) { return true; });
893 EXPECT_EQ(Test, Taken);
894
895 Taken = Test.take_until([](char c) { return true; });
896 EXPECT_EQ("", Taken);
897
898 Test = "";
899 Taken = Test.take_while([](char c) { return true; });
900 EXPECT_EQ("", Taken);
901}
902
903TEST(StringRefTest, DropWhileUntil) {
904 StringRef Test("String With 1 Number");
905
906 StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
907 EXPECT_EQ(Test, Taken);
908
909 Taken = Test.drop_until([](char c) { return ::isdigit(c); });
910 EXPECT_EQ("1 Number", Taken);
911
912 Taken = Test.drop_while([](char c) { return true; });
913 EXPECT_EQ("", Taken);
914
915 Taken = Test.drop_until([](char c) { return true; });
916 EXPECT_EQ(Test, Taken);
917
918 StringRef EmptyString = "";
919 Taken = EmptyString.drop_while([](char c) { return true; });
920 EXPECT_EQ("", Taken);
921}
922
Daniel Dunbar25f9fc52009-07-21 07:28:51 +0000923} // end anonymous namespace