blob: 4087d6c99a93fb971ee697ac16b58c5b56c14c39 [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"
Benjamin Kramer15c435a2014-04-12 16:15:53 +000012#include "llvm/ADT/STLExtras.h"
Rafael Espindolac78c0c92009-11-13 02:18:25 +000013#include "llvm/ADT/SmallVector.h"
Chandler Carruth974a4452014-01-07 11:48:04 +000014#include "llvm/ADT/StringExtras.h"
Nick Kledzik81477522014-02-05 22:22:56 +000015#include "llvm/Support/Allocator.h"
Daniel Dunbardbe77cf2009-07-22 17:13:20 +000016#include "llvm/Support/raw_ostream.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000017#include "gtest/gtest.h"
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000018using namespace llvm;
19
Douglas Gregorc883ad22009-12-24 21:15:37 +000020namespace llvm {
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000021
Daniel Dunbare6551282009-09-16 22:38:48 +000022std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
Chris Lattnerb3371cd2011-01-27 07:35:27 +000023 OS << S.str();
Daniel Dunbare6551282009-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 Gregorc883ad22009-12-24 21:15:37 +000033}
34
Jordan Rosefd34be22016-11-07 20:40:16 +000035// Check that we can't accidentally assign a temporary std::string to a
36// StringRef. (Unfortunately we can't make use of the same thing with
37// constructors.)
38//
39// Disable this check under MSVC; even MSVC 2015 isn't consistent between
40// std::is_assignable and actually writing such an assignment.
41#if !defined(_MSC_VER)
42static_assert(
Richard Smithb13ea4e2018-02-02 22:29:54 +000043 !std::is_assignable<StringRef&, std::string>::value,
Jordan Rosefd34be22016-11-07 20:40:16 +000044 "Assigning from prvalue std::string");
45static_assert(
Richard Smithb13ea4e2018-02-02 22:29:54 +000046 !std::is_assignable<StringRef&, std::string &&>::value,
Jordan Rosefd34be22016-11-07 20:40:16 +000047 "Assigning from xvalue std::string");
48static_assert(
Richard Smithb13ea4e2018-02-02 22:29:54 +000049 std::is_assignable<StringRef&, std::string &>::value,
Jordan Rosefd34be22016-11-07 20:40:16 +000050 "Assigning from lvalue std::string");
51static_assert(
Richard Smithb13ea4e2018-02-02 22:29:54 +000052 std::is_assignable<StringRef&, const char *>::value,
Jordan Rosefd34be22016-11-07 20:40:16 +000053 "Assigning from prvalue C string");
54static_assert(
Richard Smithb13ea4e2018-02-02 22:29:54 +000055 std::is_assignable<StringRef&, const char * &&>::value,
Jordan Rosefd34be22016-11-07 20:40:16 +000056 "Assigning from xvalue C string");
57static_assert(
Richard Smithb13ea4e2018-02-02 22:29:54 +000058 std::is_assignable<StringRef&, const char * &>::value,
Jordan Rosefd34be22016-11-07 20:40:16 +000059 "Assigning from lvalue C string");
60#endif
61
62
Douglas Gregorc883ad22009-12-24 21:15:37 +000063namespace {
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000064TEST(StringRefTest, Construction) {
Daniel Dunbare6551282009-09-16 22:38:48 +000065 EXPECT_EQ("", StringRef());
66 EXPECT_EQ("hello", StringRef("hello"));
67 EXPECT_EQ("hello", StringRef("hello world", 5));
68 EXPECT_EQ("hello", StringRef(std::string("hello")));
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000069}
70
Jordan Rosefd34be22016-11-07 20:40:16 +000071TEST(StringRefTest, EmptyInitializerList) {
72 StringRef S = {};
73 EXPECT_TRUE(S.empty());
74
75 S = {};
76 EXPECT_TRUE(S.empty());
77}
78
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000079TEST(StringRefTest, Iteration) {
80 StringRef S("hello");
81 const char *p = "hello";
82 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
Daniel Dunbare6551282009-09-16 22:38:48 +000083 EXPECT_EQ(*it, *p);
Daniel Dunbar4cf95d72009-07-21 07:28:51 +000084}
85
86TEST(StringRefTest, StringOps) {
87 const char *p = "hello";
88 EXPECT_EQ(p, StringRef(p, 0).data());
89 EXPECT_TRUE(StringRef().empty());
90 EXPECT_EQ((size_t) 5, StringRef("hello").size());
91 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
92 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
93 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
94 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
95 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
Benjamin Kramer0043e352010-08-26 14:21:08 +000096 EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
97
98 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
99 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
100 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
101 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
Rui Ueyamaf34c3ca2013-10-30 18:32:26 +0000102 EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
103 EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
104 EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
Benjamin Kramer0043e352010-08-26 14:21:08 +0000105 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
106 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +0000107
108 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
109 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
110 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
111 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
112 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
113 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
114 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
115 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
116 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
117 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
Benjamin Kramer837bccd2010-08-26 15:25:35 +0000118 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
Jakob Stoklund Olesen7850dd02011-09-30 17:03:55 +0000119 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
120 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
121 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
122 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
123 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
124 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
Daniel Dunbar4cf95d72009-07-21 07:28:51 +0000125}
126
127TEST(StringRefTest, Operators) {
Daniel Dunbare6551282009-09-16 22:38:48 +0000128 EXPECT_EQ("", StringRef());
Daniel Dunbar4cf95d72009-07-21 07:28:51 +0000129 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
130 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
131 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
132 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
133 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
134 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
135 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
136 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
Daniel Dunbare6551282009-09-16 22:38:48 +0000137 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
Daniel Dunbar4cf95d72009-07-21 07:28:51 +0000138 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
139 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
140 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
141 EXPECT_EQ('a', StringRef("aab")[1]);
142}
143
Daniel Dunbare6551282009-09-16 22:38:48 +0000144TEST(StringRefTest, Substr) {
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000145 StringRef Str("hello");
Daniel Dunbare6551282009-09-16 22:38:48 +0000146 EXPECT_EQ("lo", Str.substr(3));
147 EXPECT_EQ("", Str.substr(100));
148 EXPECT_EQ("hello", Str.substr(0, 100));
149 EXPECT_EQ("o", Str.substr(4, 10));
150}
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000151
Daniel Dunbare6551282009-09-16 22:38:48 +0000152TEST(StringRefTest, Slice) {
153 StringRef Str("hello");
154 EXPECT_EQ("l", Str.slice(2, 3));
155 EXPECT_EQ("ell", Str.slice(1, 4));
156 EXPECT_EQ("llo", Str.slice(2, 100));
157 EXPECT_EQ("", Str.slice(2, 1));
158 EXPECT_EQ("", Str.slice(10, 20));
159}
Daniel Dunbard61918f2009-07-26 03:18:15 +0000160
Daniel Dunbare6551282009-09-16 22:38:48 +0000161TEST(StringRefTest, Split) {
162 StringRef Str("hello");
163 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
164 Str.split('X'));
165 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
166 Str.split('e'));
167 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
168 Str.split('h'));
169 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
170 Str.split('l'));
171 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
172 Str.split('o'));
Daniel Dunbard61918f2009-07-26 03:18:15 +0000173
Daniel Dunbare6551282009-09-16 22:38:48 +0000174 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
175 Str.rsplit('X'));
176 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
177 Str.rsplit('e'));
178 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
179 Str.rsplit('h'));
180 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
181 Str.rsplit('l'));
182 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
183 Str.rsplit('o'));
Henry Wongbff55302018-06-08 12:42:12 +0000184
185 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("o")),
186 Str.rsplit("ll"));
187 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
188 Str.rsplit("h"));
189 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
190 Str.rsplit("o"));
191 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
192 Str.rsplit("::"));
193 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
194 Str.rsplit("l"));
Daniel Dunbare6551282009-09-16 22:38:48 +0000195}
196
Rafael Espindola5ccac242009-11-13 01:24:40 +0000197TEST(StringRefTest, Split2) {
Rafael Espindolac78c0c92009-11-13 02:18:25 +0000198 SmallVector<StringRef, 5> parts;
199 SmallVector<StringRef, 5> expected;
Rafael Espindola5ccac242009-11-13 01:24:40 +0000200
201 expected.push_back("ab"); expected.push_back("c");
202 StringRef(",ab,,c,").split(parts, ",", -1, false);
203 EXPECT_TRUE(parts == expected);
204
205 expected.clear(); parts.clear();
206 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
207 expected.push_back("c"); expected.push_back("");
208 StringRef(",ab,,c,").split(parts, ",", -1, true);
209 EXPECT_TRUE(parts == expected);
210
211 expected.clear(); parts.clear();
212 expected.push_back("");
213 StringRef("").split(parts, ",", -1, true);
214 EXPECT_TRUE(parts == expected);
215
216 expected.clear(); parts.clear();
217 StringRef("").split(parts, ",", -1, false);
218 EXPECT_TRUE(parts == expected);
219
220 expected.clear(); parts.clear();
221 StringRef(",").split(parts, ",", -1, false);
222 EXPECT_TRUE(parts == expected);
223
224 expected.clear(); parts.clear();
225 expected.push_back(""); expected.push_back("");
226 StringRef(",").split(parts, ",", -1, true);
227 EXPECT_TRUE(parts == expected);
228
Rafael Espindola20fd4ec2009-11-13 04:55:09 +0000229 expected.clear(); parts.clear();
230 expected.push_back("a"); expected.push_back("b");
231 StringRef("a,b").split(parts, ",", -1, true);
232 EXPECT_TRUE(parts == expected);
233
Rafael Espindola5ccac242009-11-13 01:24:40 +0000234 // Test MaxSplit
235 expected.clear(); parts.clear();
236 expected.push_back("a,,b,c");
237 StringRef("a,,b,c").split(parts, ",", 0, true);
238 EXPECT_TRUE(parts == expected);
239
240 expected.clear(); parts.clear();
241 expected.push_back("a,,b,c");
242 StringRef("a,,b,c").split(parts, ",", 0, false);
243 EXPECT_TRUE(parts == expected);
244
245 expected.clear(); parts.clear();
246 expected.push_back("a"); expected.push_back(",b,c");
247 StringRef("a,,b,c").split(parts, ",", 1, true);
248 EXPECT_TRUE(parts == expected);
249
250 expected.clear(); parts.clear();
251 expected.push_back("a"); expected.push_back(",b,c");
252 StringRef("a,,b,c").split(parts, ",", 1, false);
253 EXPECT_TRUE(parts == expected);
254
255 expected.clear(); parts.clear();
256 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
257 StringRef("a,,b,c").split(parts, ",", 2, true);
258 EXPECT_TRUE(parts == expected);
259
260 expected.clear(); parts.clear();
261 expected.push_back("a"); expected.push_back("b,c");
262 StringRef("a,,b,c").split(parts, ",", 2, false);
263 EXPECT_TRUE(parts == expected);
264
265 expected.clear(); parts.clear();
266 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
267 expected.push_back("c");
268 StringRef("a,,b,c").split(parts, ",", 3, true);
269 EXPECT_TRUE(parts == expected);
270
271 expected.clear(); parts.clear();
272 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
273 StringRef("a,,b,c").split(parts, ",", 3, false);
274 EXPECT_TRUE(parts == expected);
Chandler Carruth82c83562015-09-10 06:07:03 +0000275
276 expected.clear(); parts.clear();
277 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
278 StringRef("a,,b,c").split(parts, ',', 3, false);
279 EXPECT_TRUE(parts == expected);
Chandler Carruthf41971f2015-09-10 07:51:37 +0000280
281 expected.clear(); parts.clear();
282 expected.push_back("");
283 StringRef().split(parts, ",", 0, true);
284 EXPECT_TRUE(parts == expected);
285
286 expected.clear(); parts.clear();
287 expected.push_back(StringRef());
288 StringRef("").split(parts, ",", 0, true);
289 EXPECT_TRUE(parts == expected);
290
291 expected.clear(); parts.clear();
292 StringRef("").split(parts, ",", 0, false);
293 EXPECT_TRUE(parts == expected);
294 StringRef().split(parts, ",", 0, false);
295 EXPECT_TRUE(parts == expected);
296
297 expected.clear(); parts.clear();
298 expected.push_back("a");
299 expected.push_back("");
300 expected.push_back("b");
301 expected.push_back("c,d");
302 StringRef("a,,b,c,d").split(parts, ",", 3, true);
303 EXPECT_TRUE(parts == expected);
304
305 expected.clear(); parts.clear();
306 expected.push_back("");
307 StringRef().split(parts, ',', 0, true);
308 EXPECT_TRUE(parts == expected);
309
310 expected.clear(); parts.clear();
311 expected.push_back(StringRef());
312 StringRef("").split(parts, ',', 0, true);
313 EXPECT_TRUE(parts == expected);
314
315 expected.clear(); parts.clear();
316 StringRef("").split(parts, ',', 0, false);
317 EXPECT_TRUE(parts == expected);
318 StringRef().split(parts, ',', 0, false);
319 EXPECT_TRUE(parts == expected);
320
321 expected.clear(); parts.clear();
322 expected.push_back("a");
323 expected.push_back("");
324 expected.push_back("b");
325 expected.push_back("c,d");
326 StringRef("a,,b,c,d").split(parts, ',', 3, true);
327 EXPECT_TRUE(parts == expected);
Rafael Espindola5ccac242009-11-13 01:24:40 +0000328}
329
Michael J. Spencerb0940b42012-05-11 22:08:50 +0000330TEST(StringRefTest, Trim) {
331 StringRef Str0("hello");
332 StringRef Str1(" hello ");
333 StringRef Str2(" hello ");
334
335 EXPECT_EQ(StringRef("hello"), Str0.rtrim());
336 EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
337 EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
338 EXPECT_EQ(StringRef("hello"), Str0.ltrim());
339 EXPECT_EQ(StringRef("hello "), Str1.ltrim());
340 EXPECT_EQ(StringRef("hello "), Str2.ltrim());
341 EXPECT_EQ(StringRef("hello"), Str0.trim());
342 EXPECT_EQ(StringRef("hello"), Str1.trim());
343 EXPECT_EQ(StringRef("hello"), Str2.trim());
344
345 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
346
347 EXPECT_EQ(StringRef(""), StringRef("").trim());
348 EXPECT_EQ(StringRef(""), StringRef(" ").trim());
349 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
350 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
Vedant Kumar9ae3e2c2016-02-16 01:48:39 +0000351 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
Michael J. Spencerb0940b42012-05-11 22:08:50 +0000352}
353
Daniel Dunbare6551282009-09-16 22:38:48 +0000354TEST(StringRefTest, StartsWith) {
355 StringRef Str("hello");
Rui Ueyamabc850d02013-10-28 22:42:54 +0000356 EXPECT_TRUE(Str.startswith(""));
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000357 EXPECT_TRUE(Str.startswith("he"));
358 EXPECT_FALSE(Str.startswith("helloworld"));
359 EXPECT_FALSE(Str.startswith("hi"));
Daniel Dunbare6551282009-09-16 22:38:48 +0000360}
Daniel Dunbardbe77cf2009-07-22 17:13:20 +0000361
Rui Ueyamaf34c3ca2013-10-30 18:32:26 +0000362TEST(StringRefTest, StartsWithLower) {
363 StringRef Str("heLLo");
364 EXPECT_TRUE(Str.startswith_lower(""));
365 EXPECT_TRUE(Str.startswith_lower("he"));
366 EXPECT_TRUE(Str.startswith_lower("hell"));
367 EXPECT_TRUE(Str.startswith_lower("HELlo"));
368 EXPECT_FALSE(Str.startswith_lower("helloworld"));
369 EXPECT_FALSE(Str.startswith_lower("hi"));
370}
371
Chandler Carruthd9a77522016-07-31 02:19:13 +0000372TEST(StringRefTest, ConsumeFront) {
373 StringRef Str("hello");
374 EXPECT_TRUE(Str.consume_front(""));
375 EXPECT_EQ("hello", Str);
376 EXPECT_TRUE(Str.consume_front("he"));
377 EXPECT_EQ("llo", Str);
378 EXPECT_FALSE(Str.consume_front("lloworld"));
379 EXPECT_EQ("llo", Str);
380 EXPECT_FALSE(Str.consume_front("lol"));
381 EXPECT_EQ("llo", Str);
382 EXPECT_TRUE(Str.consume_front("llo"));
383 EXPECT_EQ("", Str);
384 EXPECT_FALSE(Str.consume_front("o"));
385 EXPECT_TRUE(Str.consume_front(""));
386}
387
Eli Friedmand5b1f8a2009-12-21 06:49:24 +0000388TEST(StringRefTest, EndsWith) {
389 StringRef Str("hello");
Rui Ueyamabc850d02013-10-28 22:42:54 +0000390 EXPECT_TRUE(Str.endswith(""));
Eli Friedmand5b1f8a2009-12-21 06:49:24 +0000391 EXPECT_TRUE(Str.endswith("lo"));
392 EXPECT_FALSE(Str.endswith("helloworld"));
393 EXPECT_FALSE(Str.endswith("worldhello"));
394 EXPECT_FALSE(Str.endswith("so"));
395}
396
Rui Ueyamaf34c3ca2013-10-30 18:32:26 +0000397TEST(StringRefTest, EndsWithLower) {
398 StringRef Str("heLLo");
399 EXPECT_TRUE(Str.endswith_lower(""));
400 EXPECT_TRUE(Str.endswith_lower("lo"));
401 EXPECT_TRUE(Str.endswith_lower("LO"));
402 EXPECT_TRUE(Str.endswith_lower("ELlo"));
403 EXPECT_FALSE(Str.endswith_lower("helloworld"));
404 EXPECT_FALSE(Str.endswith_lower("hi"));
405}
406
Chandler Carruthd9a77522016-07-31 02:19:13 +0000407TEST(StringRefTest, ConsumeBack) {
408 StringRef Str("hello");
409 EXPECT_TRUE(Str.consume_back(""));
410 EXPECT_EQ("hello", Str);
411 EXPECT_TRUE(Str.consume_back("lo"));
412 EXPECT_EQ("hel", Str);
413 EXPECT_FALSE(Str.consume_back("helhel"));
414 EXPECT_EQ("hel", Str);
415 EXPECT_FALSE(Str.consume_back("hle"));
416 EXPECT_EQ("hel", Str);
417 EXPECT_TRUE(Str.consume_back("hel"));
418 EXPECT_EQ("", Str);
419 EXPECT_FALSE(Str.consume_back("h"));
420 EXPECT_TRUE(Str.consume_back(""));
421}
422
Daniel Dunbare6551282009-09-16 22:38:48 +0000423TEST(StringRefTest, Find) {
Zachary Turnerfd342822016-11-12 17:17:12 +0000424 StringRef Str("helloHELLO");
425 StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
426
427 struct {
428 StringRef Str;
429 char C;
430 std::size_t From;
431 std::size_t Pos;
432 std::size_t LowerPos;
433 } CharExpectations[] = {
434 {Str, 'h', 0U, 0U, 0U},
435 {Str, 'e', 0U, 1U, 1U},
436 {Str, 'l', 0U, 2U, 2U},
437 {Str, 'l', 3U, 3U, 3U},
438 {Str, 'o', 0U, 4U, 4U},
439 {Str, 'L', 0U, 7U, 2U},
440 {Str, 'z', 0U, StringRef::npos, StringRef::npos},
441 };
442
443 struct {
444 StringRef Str;
445 llvm::StringRef S;
446 std::size_t From;
447 std::size_t Pos;
448 std::size_t LowerPos;
449 } StrExpectations[] = {
450 {Str, "helloword", 0, StringRef::npos, StringRef::npos},
451 {Str, "hello", 0, 0U, 0U},
452 {Str, "ello", 0, 1U, 1U},
453 {Str, "zz", 0, StringRef::npos, StringRef::npos},
454 {Str, "ll", 2U, 2U, 2U},
455 {Str, "ll", 3U, StringRef::npos, 7U},
456 {Str, "LL", 2U, 7U, 2U},
457 {Str, "LL", 3U, 7U, 7U},
458 {Str, "", 0U, 0U, 0U},
459 {LongStr, "hello", 0U, 36U, 36U},
460 {LongStr, "foo", 0U, 28U, 28U},
461 {LongStr, "hell", 2U, 12U, 12U},
462 {LongStr, "HELL", 2U, 42U, 12U},
463 {LongStr, "", 0U, 0U, 0U}};
464
465 for (auto &E : CharExpectations) {
466 EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From));
467 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.C, E.From));
468 EXPECT_EQ(E.LowerPos, E.Str.find_lower(toupper(E.C), E.From));
469 }
470
471 for (auto &E : StrExpectations) {
472 EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From));
473 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S, E.From));
474 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S.upper(), E.From));
475 }
Daniel Dunbare6551282009-09-16 22:38:48 +0000476
477 EXPECT_EQ(3U, Str.rfind('l'));
478 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
479 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
480 EXPECT_EQ(0U, Str.rfind("hello"));
481 EXPECT_EQ(1U, Str.rfind("ello"));
482 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000483
Zachary Turnerfd342822016-11-12 17:17:12 +0000484 EXPECT_EQ(8U, Str.rfind_lower('l'));
485 EXPECT_EQ(8U, Str.rfind_lower('L'));
486 EXPECT_EQ(StringRef::npos, Str.rfind_lower('z'));
487 EXPECT_EQ(StringRef::npos, Str.rfind_lower("HELLOWORLD"));
488 EXPECT_EQ(5U, Str.rfind("HELLO"));
489 EXPECT_EQ(6U, Str.rfind("ELLO"));
490 EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));
491
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000492 EXPECT_EQ(2U, Str.find_first_of('l'));
493 EXPECT_EQ(1U, Str.find_first_of("el"));
494 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
495
Zachary Turnerfd342822016-11-12 17:17:12 +0000496 Str = "hello";
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000497 EXPECT_EQ(1U, Str.find_first_not_of('h'));
498 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
499 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
Michael J. Spencerb0940b42012-05-11 22:08:50 +0000500
501 EXPECT_EQ(3U, Str.find_last_not_of('o'));
502 EXPECT_EQ(1U, Str.find_last_not_of("lo"));
503 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
Daniel Dunbare6551282009-09-16 22:38:48 +0000504}
505
506TEST(StringRefTest, Count) {
507 StringRef Str("hello");
508 EXPECT_EQ(2U, Str.count('l'));
509 EXPECT_EQ(1U, Str.count('o'));
510 EXPECT_EQ(0U, Str.count('z'));
511 EXPECT_EQ(0U, Str.count("helloworld"));
512 EXPECT_EQ(1U, Str.count("hello"));
513 EXPECT_EQ(1U, Str.count("ello"));
514 EXPECT_EQ(0U, Str.count("zz"));
515}
516
Douglas Gregor7e54d5b2009-12-31 04:24:34 +0000517TEST(StringRefTest, EditDistance) {
Alex Denisov1ad79db2017-04-14 08:34:32 +0000518 StringRef Hello("hello");
519 EXPECT_EQ(2U, Hello.edit_distance("hill"));
520
521 StringRef Industry("industry");
522 EXPECT_EQ(6U, Industry.edit_distance("interest"));
523
524 StringRef Soylent("soylent green is people");
525 EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green"));
526 EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green",
527 /* allow replacements = */ false));
528 EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green",
529 /* allow replacements = */ true,
530 /* max edit distance = */ 8));
531 EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green "
532 "people soiled our green "
533 "people soiled our green "));
Douglas Gregor7e54d5b2009-12-31 04:24:34 +0000534}
535
Daniel Dunbare6551282009-09-16 22:38:48 +0000536TEST(StringRefTest, Misc) {
Daniel Dunbardbe77cf2009-07-22 17:13:20 +0000537 std::string Storage;
538 raw_string_ostream OS(Storage);
539 OS << StringRef("hello");
540 EXPECT_EQ("hello", OS.str());
Daniel Dunbarf5fdf732009-07-21 09:18:49 +0000541}
542
Chandler Carruth528f0bb2012-03-04 10:55:27 +0000543TEST(StringRefTest, Hashing) {
544 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
545 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
546 std::string S = "hello world";
547 hash_code H = hash_value(S);
548 EXPECT_EQ(H, hash_value(StringRef("hello world")));
549 EXPECT_EQ(H, hash_value(StringRef(S)));
550 EXPECT_NE(H, hash_value(StringRef("hello worl")));
551 EXPECT_EQ(hash_value(std::string("hello worl")),
552 hash_value(StringRef("hello worl")));
553 EXPECT_NE(H, hash_value(StringRef("hello world ")));
554 EXPECT_EQ(hash_value(std::string("hello world ")),
555 hash_value(StringRef("hello world ")));
556 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
557 EXPECT_NE(hash_value(std::string("ello worl")),
558 hash_value(StringRef("hello world").slice(1, -1)));
559}
560
Michael J. Spencer9130b422012-03-10 23:02:54 +0000561struct UnsignedPair {
562 const char *Str;
563 uint64_t Expected;
564} Unsigned[] =
565 { {"0", 0}
566 , {"255", 255}
567 , {"256", 256}
568 , {"65535", 65535}
569 , {"65536", 65536}
Michael J. Spencerdbb4b2f2012-03-11 00:51:01 +0000570 , {"4294967295", 4294967295ULL}
571 , {"4294967296", 4294967296ULL}
Michael J. Spencer9130b422012-03-10 23:02:54 +0000572 , {"18446744073709551615", 18446744073709551615ULL}
573 , {"042", 34}
574 , {"0x42", 66}
575 , {"0b101010", 42}
576 };
577
578struct SignedPair {
579 const char *Str;
580 int64_t Expected;
581} Signed[] =
582 { {"0", 0}
583 , {"-0", 0}
584 , {"127", 127}
585 , {"128", 128}
586 , {"-128", -128}
587 , {"-129", -129}
588 , {"32767", 32767}
589 , {"32768", 32768}
590 , {"-32768", -32768}
591 , {"-32769", -32769}
Michael J. Spencerdbb4b2f2012-03-11 00:51:01 +0000592 , {"2147483647", 2147483647LL}
593 , {"2147483648", 2147483648LL}
Michael J. Spencer9130b422012-03-10 23:02:54 +0000594 , {"-2147483648", -2147483648LL}
595 , {"-2147483649", -2147483649LL}
596 , {"-9223372036854775808", -(9223372036854775807LL) - 1}
597 , {"042", 34}
598 , {"0x42", 66}
599 , {"0b101010", 42}
600 , {"-042", -34}
601 , {"-0x42", -66}
602 , {"-0b101010", -42}
603 };
604
605TEST(StringRefTest, getAsInteger) {
606 uint8_t U8;
607 uint16_t U16;
608 uint32_t U32;
609 uint64_t U64;
610
611 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
612 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
613 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
614 ASSERT_FALSE(U8Success);
615 EXPECT_EQ(U8, Unsigned[i].Expected);
616 } else {
617 ASSERT_TRUE(U8Success);
618 }
619 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
620 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
621 ASSERT_FALSE(U16Success);
622 EXPECT_EQ(U16, Unsigned[i].Expected);
623 } else {
624 ASSERT_TRUE(U16Success);
625 }
626 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
627 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
628 ASSERT_FALSE(U32Success);
629 EXPECT_EQ(U32, Unsigned[i].Expected);
630 } else {
631 ASSERT_TRUE(U32Success);
632 }
633 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
634 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
635 ASSERT_FALSE(U64Success);
636 EXPECT_EQ(U64, Unsigned[i].Expected);
637 } else {
638 ASSERT_TRUE(U64Success);
639 }
640 }
641
642 int8_t S8;
643 int16_t S16;
644 int32_t S32;
645 int64_t S64;
646
647 for (size_t i = 0; i < array_lengthof(Signed); ++i) {
648 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
649 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
650 ASSERT_FALSE(S8Success);
651 EXPECT_EQ(S8, Signed[i].Expected);
652 } else {
653 ASSERT_TRUE(S8Success);
654 }
655 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
656 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
657 ASSERT_FALSE(S16Success);
658 EXPECT_EQ(S16, Signed[i].Expected);
659 } else {
660 ASSERT_TRUE(S16Success);
661 }
662 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
663 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
664 ASSERT_FALSE(S32Success);
665 EXPECT_EQ(S32, Signed[i].Expected);
666 } else {
667 ASSERT_TRUE(S32Success);
668 }
669 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
670 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
671 ASSERT_FALSE(S64Success);
672 EXPECT_EQ(S64, Signed[i].Expected);
673 } else {
674 ASSERT_TRUE(S64Success);
675 }
676 }
677}
678
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000679
680static const char* BadStrings[] = {
Zachary Turner20bb0322016-09-22 15:55:05 +0000681 "" // empty string
682 , "18446744073709551617" // value just over max
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000683 , "123456789012345678901" // value way too large
684 , "4t23v" // illegal decimal characters
685 , "0x123W56" // illegal hex characters
Benjamin Kramere25de4a2012-10-03 18:54:36 +0000686 , "0b2" // illegal bin characters
687 , "08" // illegal oct characters
688 , "0o8" // illegal oct characters
689 , "-123" // negative unsigned value
Zachary Turner09666272016-09-22 19:21:32 +0000690 , "0x"
691 , "0b"
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000692};
693
694
695TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
Nick Kledzik436eaa82012-10-03 19:27:25 +0000696 unsigned long long U64;
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000697 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
Benjamin Kramere25de4a2012-10-03 18:54:36 +0000698 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000699 ASSERT_TRUE(IsBadNumber);
700 }
701}
702
Zachary Turnerfb27f552016-09-22 15:05:19 +0000703struct ConsumeUnsignedPair {
704 const char *Str;
705 uint64_t Expected;
706 const char *Leftover;
707} ConsumeUnsigned[] = {
708 {"0", 0, ""},
709 {"255", 255, ""},
710 {"256", 256, ""},
711 {"65535", 65535, ""},
712 {"65536", 65536, ""},
713 {"4294967295", 4294967295ULL, ""},
714 {"4294967296", 4294967296ULL, ""},
715 {"255A376", 255, "A376"},
716 {"18446744073709551615", 18446744073709551615ULL, ""},
717 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
718 {"042", 34, ""},
719 {"0x42", 66, ""},
720 {"0x42-0x34", 66, "-0x34"},
721 {"0b101010", 42, ""},
722 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit
723 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit
724 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
725
726struct ConsumeSignedPair {
727 const char *Str;
728 int64_t Expected;
729 const char *Leftover;
730} ConsumeSigned[] = {
731 {"0", 0, ""},
732 {"-0", 0, ""},
733 {"0-1", 0, "-1"},
734 {"-0-1", 0, "-1"},
735 {"127", 127, ""},
736 {"128", 128, ""},
737 {"127-1", 127, "-1"},
738 {"128-1", 128, "-1"},
739 {"-128", -128, ""},
740 {"-129", -129, ""},
741 {"-128-1", -128, "-1"},
742 {"-129-1", -129, "-1"},
743 {"32767", 32767, ""},
744 {"32768", 32768, ""},
745 {"32767-1", 32767, "-1"},
746 {"32768-1", 32768, "-1"},
747 {"-32768", -32768, ""},
748 {"-32769", -32769, ""},
749 {"-32768-1", -32768, "-1"},
750 {"-32769-1", -32769, "-1"},
751 {"2147483647", 2147483647LL, ""},
752 {"2147483648", 2147483648LL, ""},
753 {"2147483647-1", 2147483647LL, "-1"},
754 {"2147483648-1", 2147483648LL, "-1"},
755 {"-2147483648", -2147483648LL, ""},
756 {"-2147483649", -2147483649LL, ""},
757 {"-2147483648-1", -2147483648LL, "-1"},
758 {"-2147483649-1", -2147483649LL, "-1"},
759 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
760 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
761 {"042", 34, ""},
762 {"042-1", 34, "-1"},
763 {"0x42", 66, ""},
764 {"0x42-1", 66, "-1"},
765 {"0b101010", 42, ""},
766 {"0b101010-1", 42, "-1"},
767 {"-042", -34, ""},
768 {"-042-1", -34, "-1"},
769 {"-0x42", -66, ""},
770 {"-0x42-1", -66, "-1"},
771 {"-0b101010", -42, ""},
772 {"-0b101010-1", -42, "-1"}};
773
774TEST(StringRefTest, consumeIntegerUnsigned) {
775 uint8_t U8;
776 uint16_t U16;
777 uint32_t U32;
778 uint64_t U64;
779
780 for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) {
781 StringRef Str = ConsumeUnsigned[i].Str;
782 bool U8Success = Str.consumeInteger(0, U8);
783 if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==
784 ConsumeUnsigned[i].Expected) {
785 ASSERT_FALSE(U8Success);
786 EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);
787 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
788 } else {
789 ASSERT_TRUE(U8Success);
790 }
791
792 Str = ConsumeUnsigned[i].Str;
793 bool U16Success = Str.consumeInteger(0, U16);
794 if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==
795 ConsumeUnsigned[i].Expected) {
796 ASSERT_FALSE(U16Success);
797 EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);
798 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
799 } else {
800 ASSERT_TRUE(U16Success);
801 }
802
803 Str = ConsumeUnsigned[i].Str;
804 bool U32Success = Str.consumeInteger(0, U32);
805 if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==
806 ConsumeUnsigned[i].Expected) {
807 ASSERT_FALSE(U32Success);
808 EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);
809 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
810 } else {
811 ASSERT_TRUE(U32Success);
812 }
813
814 Str = ConsumeUnsigned[i].Str;
815 bool U64Success = Str.consumeInteger(0, U64);
816 if (static_cast<uint64_t>(ConsumeUnsigned[i].Expected) ==
817 ConsumeUnsigned[i].Expected) {
818 ASSERT_FALSE(U64Success);
819 EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);
820 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
821 } else {
822 ASSERT_TRUE(U64Success);
823 }
824 }
825}
826
827TEST(StringRefTest, consumeIntegerSigned) {
828 int8_t S8;
829 int16_t S16;
830 int32_t S32;
831 int64_t S64;
832
833 for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) {
834 StringRef Str = ConsumeSigned[i].Str;
835 bool S8Success = Str.consumeInteger(0, S8);
836 if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
837 ConsumeSigned[i].Expected) {
838 ASSERT_FALSE(S8Success);
839 EXPECT_EQ(S8, ConsumeSigned[i].Expected);
840 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
841 } else {
842 ASSERT_TRUE(S8Success);
843 }
844
845 Str = ConsumeSigned[i].Str;
846 bool S16Success = Str.consumeInteger(0, S16);
847 if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
848 ConsumeSigned[i].Expected) {
849 ASSERT_FALSE(S16Success);
850 EXPECT_EQ(S16, ConsumeSigned[i].Expected);
851 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
852 } else {
853 ASSERT_TRUE(S16Success);
854 }
855
856 Str = ConsumeSigned[i].Str;
857 bool S32Success = Str.consumeInteger(0, S32);
858 if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
859 ConsumeSigned[i].Expected) {
860 ASSERT_FALSE(S32Success);
861 EXPECT_EQ(S32, ConsumeSigned[i].Expected);
862 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
863 } else {
864 ASSERT_TRUE(S32Success);
865 }
866
867 Str = ConsumeSigned[i].Str;
868 bool S64Success = Str.consumeInteger(0, S64);
869 if (static_cast<int64_t>(ConsumeSigned[i].Expected) ==
870 ConsumeSigned[i].Expected) {
871 ASSERT_FALSE(S64Success);
872 EXPECT_EQ(S64, ConsumeSigned[i].Expected);
873 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
874 } else {
875 ASSERT_TRUE(S64Success);
876 }
877 }
878}
879
Zachary Turnerba164812017-02-14 19:06:37 +0000880struct GetDoubleStrings {
881 const char *Str;
882 bool AllowInexact;
883 bool ShouldFail;
884 double D;
885} DoubleStrings[] = {{"0", false, false, 0.0},
886 {"0.0", false, false, 0.0},
887 {"-0.0", false, false, -0.0},
888 {"123.45", false, true, 123.45},
Serguei Katkov643edab2017-12-19 04:27:39 +0000889 {"123.45", true, false, 123.45},
890 {"1.8e308", true, false, std::numeric_limits<double>::infinity()},
891 {"1.8e308", false, true, std::numeric_limits<double>::infinity()},
892 {"0x0.0000000000001P-1023", false, true, 0.0},
893 {"0x0.0000000000001P-1023", true, false, 0.0},
894 };
Zachary Turnerba164812017-02-14 19:06:37 +0000895
896TEST(StringRefTest, getAsDouble) {
897 for (const auto &Entry : DoubleStrings) {
898 double Result;
899 StringRef S(Entry.Str);
900 EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
Galina Kistanova7438bc92017-06-15 21:00:40 +0000901 if (!Entry.ShouldFail) {
Zachary Turnerba164812017-02-14 19:06:37 +0000902 EXPECT_EQ(Result, Entry.D);
Galina Kistanova7438bc92017-06-15 21:00:40 +0000903 }
Zachary Turnerba164812017-02-14 19:06:37 +0000904 }
905}
906
Joerg Sonnenbergerf57a80f2013-09-03 20:43:54 +0000907static const char *join_input[] = { "a", "b", "c" };
908static const char join_result1[] = "a";
909static const char join_result2[] = "a:b:c";
910static const char join_result3[] = "a::b::c";
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000911
Joerg Sonnenbergerf57a80f2013-09-03 20:43:54 +0000912TEST(StringRefTest, joinStrings) {
913 std::vector<StringRef> v1;
914 std::vector<std::string> v2;
915 for (size_t i = 0; i < array_lengthof(join_input); ++i) {
916 v1.push_back(join_input[i]);
917 v2.push_back(join_input[i]);
918 }
919
920 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
921 EXPECT_TRUE(v1_join1);
922 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
923 EXPECT_TRUE(v1_join2);
924 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
925 EXPECT_TRUE(v1_join3);
926
927 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
928 EXPECT_TRUE(v2_join1);
929 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
930 EXPECT_TRUE(v2_join2);
931 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
932 EXPECT_TRUE(v2_join3);
Zachary Turner700f6ad2017-03-21 19:35:05 +0000933 v2_join3 = join(v2, "::") == join_result3;
934 EXPECT_TRUE(v2_join3);
Joerg Sonnenbergerf57a80f2013-09-03 20:43:54 +0000935}
Nick Kledzik7a0f86f2012-10-03 18:15:27 +0000936
Nick Kledzik81477522014-02-05 22:22:56 +0000937
938TEST(StringRefTest, AllocatorCopy) {
939 BumpPtrAllocator Alloc;
Pete Cooper552f3d82016-03-23 21:49:31 +0000940 // First test empty strings. We don't want these to allocate anything on the
941 // allocator.
942 StringRef StrEmpty = "";
943 StringRef StrEmptyc = StrEmpty.copy(Alloc);
944 EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
945 EXPECT_EQ(StrEmptyc.data(), nullptr);
946 EXPECT_EQ(StrEmptyc.size(), 0u);
947 EXPECT_EQ(Alloc.getTotalMemory(), 0u);
948
Nick Kledzik81477522014-02-05 22:22:56 +0000949 StringRef Str1 = "hello";
950 StringRef Str2 = "bye";
951 StringRef Str1c = Str1.copy(Alloc);
952 StringRef Str2c = Str2.copy(Alloc);
953 EXPECT_TRUE(Str1.equals(Str1c));
954 EXPECT_NE(Str1.data(), Str1c.data());
955 EXPECT_TRUE(Str2.equals(Str2c));
956 EXPECT_NE(Str2.data(), Str2c.data());
957}
958
Zachary Turnerbb0403b2016-08-30 17:29:59 +0000959TEST(StringRefTest, Drop) {
960 StringRef Test("StringRefTest::Drop");
961
962 StringRef Dropped = Test.drop_front(5);
963 EXPECT_EQ(Dropped, "gRefTest::Drop");
964
965 Dropped = Test.drop_back(5);
966 EXPECT_EQ(Dropped, "StringRefTest:");
967
968 Dropped = Test.drop_front(0);
969 EXPECT_EQ(Dropped, Test);
970
971 Dropped = Test.drop_back(0);
972 EXPECT_EQ(Dropped, Test);
973
974 Dropped = Test.drop_front(Test.size());
975 EXPECT_TRUE(Dropped.empty());
976
977 Dropped = Test.drop_back(Test.size());
978 EXPECT_TRUE(Dropped.empty());
979}
980
981TEST(StringRefTest, Take) {
982 StringRef Test("StringRefTest::Take");
983
984 StringRef Taken = Test.take_front(5);
985 EXPECT_EQ(Taken, "Strin");
986
987 Taken = Test.take_back(5);
988 EXPECT_EQ(Taken, ":Take");
989
990 Taken = Test.take_front(Test.size());
991 EXPECT_EQ(Taken, Test);
992
993 Taken = Test.take_back(Test.size());
994 EXPECT_EQ(Taken, Test);
995
996 Taken = Test.take_front(0);
997 EXPECT_TRUE(Taken.empty());
998
999 Taken = Test.take_back(0);
1000 EXPECT_TRUE(Taken.empty());
1001}
Nick Kledzik81477522014-02-05 22:22:56 +00001002
Zachary Turner568a8f42016-09-25 03:27:29 +00001003TEST(StringRefTest, FindIf) {
1004 StringRef Punct("Test.String");
1005 StringRef NoPunct("ABCDEFG");
1006 StringRef Empty;
1007
1008 auto IsPunct = [](char c) { return ::ispunct(c); };
1009 auto IsAlpha = [](char c) { return ::isalpha(c); };
Zachary Turner3ae26e92016-09-25 03:57:34 +00001010 EXPECT_EQ(4U, Punct.find_if(IsPunct));
Zachary Turner568a8f42016-09-25 03:27:29 +00001011 EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
1012 EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
1013
Zachary Turner3ae26e92016-09-25 03:57:34 +00001014 EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
Zachary Turner568a8f42016-09-25 03:27:29 +00001015 EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
1016 EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
1017}
1018
1019TEST(StringRefTest, TakeWhileUntil) {
1020 StringRef Test("String With 1 Number");
1021
1022 StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
1023 EXPECT_EQ("", Taken);
1024
1025 Taken = Test.take_until([](char c) { return ::isdigit(c); });
1026 EXPECT_EQ("String With ", Taken);
1027
1028 Taken = Test.take_while([](char c) { return true; });
1029 EXPECT_EQ(Test, Taken);
1030
1031 Taken = Test.take_until([](char c) { return true; });
1032 EXPECT_EQ("", Taken);
1033
1034 Test = "";
1035 Taken = Test.take_while([](char c) { return true; });
1036 EXPECT_EQ("", Taken);
1037}
1038
1039TEST(StringRefTest, DropWhileUntil) {
1040 StringRef Test("String With 1 Number");
1041
1042 StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
1043 EXPECT_EQ(Test, Taken);
1044
1045 Taken = Test.drop_until([](char c) { return ::isdigit(c); });
1046 EXPECT_EQ("1 Number", Taken);
1047
1048 Taken = Test.drop_while([](char c) { return true; });
1049 EXPECT_EQ("", Taken);
1050
1051 Taken = Test.drop_until([](char c) { return true; });
1052 EXPECT_EQ(Test, Taken);
1053
1054 StringRef EmptyString = "";
1055 Taken = EmptyString.drop_while([](char c) { return true; });
1056 EXPECT_EQ("", Taken);
1057}
1058
Zachary Turner2dcb6972016-12-13 17:03:49 +00001059TEST(StringRefTest, StringLiteral) {
1060 constexpr StringLiteral Strings[] = {"Foo", "Bar"};
1061 EXPECT_EQ(StringRef("Foo"), Strings[0]);
1062 EXPECT_EQ(StringRef("Bar"), Strings[1]);
1063}
1064
Daniel Dunbar4cf95d72009-07-21 07:28:51 +00001065} // end anonymous namespace