1f4a2713aSLionel Sambuc //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc #include "llvm/ADT/StringRef.h"
11f4a2713aSLionel Sambuc #include "llvm/ADT/Hashing.h"
12*0a6a1f1dSLionel Sambuc #include "llvm/ADT/STLExtras.h"
13f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
14*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringExtras.h"
15*0a6a1f1dSLionel Sambuc #include "llvm/Support/Allocator.h"
16f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
17f4a2713aSLionel Sambuc #include "gtest/gtest.h"
18f4a2713aSLionel Sambuc using namespace llvm;
19f4a2713aSLionel Sambuc
20f4a2713aSLionel Sambuc namespace llvm {
21f4a2713aSLionel Sambuc
operator <<(std::ostream & OS,const StringRef & S)22f4a2713aSLionel Sambuc std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
23f4a2713aSLionel Sambuc OS << S.str();
24f4a2713aSLionel Sambuc return OS;
25f4a2713aSLionel Sambuc }
26f4a2713aSLionel Sambuc
operator <<(std::ostream & OS,const std::pair<StringRef,StringRef> & P)27f4a2713aSLionel Sambuc std::ostream &operator<<(std::ostream &OS,
28f4a2713aSLionel Sambuc const std::pair<StringRef, StringRef> &P) {
29f4a2713aSLionel Sambuc OS << "(" << P.first << ", " << P.second << ")";
30f4a2713aSLionel Sambuc return OS;
31f4a2713aSLionel Sambuc }
32f4a2713aSLionel Sambuc
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc namespace {
TEST(StringRefTest,Construction)36f4a2713aSLionel Sambuc TEST(StringRefTest, Construction) {
37f4a2713aSLionel Sambuc EXPECT_EQ("", StringRef());
38f4a2713aSLionel Sambuc EXPECT_EQ("hello", StringRef("hello"));
39f4a2713aSLionel Sambuc EXPECT_EQ("hello", StringRef("hello world", 5));
40f4a2713aSLionel Sambuc EXPECT_EQ("hello", StringRef(std::string("hello")));
41f4a2713aSLionel Sambuc }
42f4a2713aSLionel Sambuc
TEST(StringRefTest,Iteration)43f4a2713aSLionel Sambuc TEST(StringRefTest, Iteration) {
44f4a2713aSLionel Sambuc StringRef S("hello");
45f4a2713aSLionel Sambuc const char *p = "hello";
46f4a2713aSLionel Sambuc for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
47f4a2713aSLionel Sambuc EXPECT_EQ(*it, *p);
48f4a2713aSLionel Sambuc }
49f4a2713aSLionel Sambuc
TEST(StringRefTest,StringOps)50f4a2713aSLionel Sambuc TEST(StringRefTest, StringOps) {
51f4a2713aSLionel Sambuc const char *p = "hello";
52f4a2713aSLionel Sambuc EXPECT_EQ(p, StringRef(p, 0).data());
53f4a2713aSLionel Sambuc EXPECT_TRUE(StringRef().empty());
54f4a2713aSLionel Sambuc EXPECT_EQ((size_t) 5, StringRef("hello").size());
55f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("aab").compare("aad"));
56f4a2713aSLionel Sambuc EXPECT_EQ( 0, StringRef("aab").compare("aab"));
57f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
58f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
59f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("aab").compare("aa"));
60f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
63f4a2713aSLionel Sambuc EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
64f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
65f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
66f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
67f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
68f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
69f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
70f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
71f4a2713aSLionel Sambuc
72f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
73f4a2713aSLionel Sambuc EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
74f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
75f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
76f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
77f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
78f4a2713aSLionel Sambuc EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
79f4a2713aSLionel Sambuc EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
80f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
81f4a2713aSLionel Sambuc EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
82f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
83f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
84f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
85f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
86f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
87f4a2713aSLionel Sambuc EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
88f4a2713aSLionel Sambuc EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc
TEST(StringRefTest,Operators)91f4a2713aSLionel Sambuc TEST(StringRefTest, Operators) {
92f4a2713aSLionel Sambuc EXPECT_EQ("", StringRef());
93f4a2713aSLionel Sambuc EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
94f4a2713aSLionel Sambuc EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
95f4a2713aSLionel Sambuc EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
96f4a2713aSLionel Sambuc EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
97f4a2713aSLionel Sambuc EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
98f4a2713aSLionel Sambuc EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
99f4a2713aSLionel Sambuc EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
100f4a2713aSLionel Sambuc EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
101f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("aab"), StringRef("aab"));
102f4a2713aSLionel Sambuc EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
103f4a2713aSLionel Sambuc EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
104f4a2713aSLionel Sambuc EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
105f4a2713aSLionel Sambuc EXPECT_EQ('a', StringRef("aab")[1]);
106f4a2713aSLionel Sambuc }
107f4a2713aSLionel Sambuc
TEST(StringRefTest,Substr)108f4a2713aSLionel Sambuc TEST(StringRefTest, Substr) {
109f4a2713aSLionel Sambuc StringRef Str("hello");
110f4a2713aSLionel Sambuc EXPECT_EQ("lo", Str.substr(3));
111f4a2713aSLionel Sambuc EXPECT_EQ("", Str.substr(100));
112f4a2713aSLionel Sambuc EXPECT_EQ("hello", Str.substr(0, 100));
113f4a2713aSLionel Sambuc EXPECT_EQ("o", Str.substr(4, 10));
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc
TEST(StringRefTest,Slice)116f4a2713aSLionel Sambuc TEST(StringRefTest, Slice) {
117f4a2713aSLionel Sambuc StringRef Str("hello");
118f4a2713aSLionel Sambuc EXPECT_EQ("l", Str.slice(2, 3));
119f4a2713aSLionel Sambuc EXPECT_EQ("ell", Str.slice(1, 4));
120f4a2713aSLionel Sambuc EXPECT_EQ("llo", Str.slice(2, 100));
121f4a2713aSLionel Sambuc EXPECT_EQ("", Str.slice(2, 1));
122f4a2713aSLionel Sambuc EXPECT_EQ("", Str.slice(10, 20));
123f4a2713aSLionel Sambuc }
124f4a2713aSLionel Sambuc
TEST(StringRefTest,Split)125f4a2713aSLionel Sambuc TEST(StringRefTest, Split) {
126f4a2713aSLionel Sambuc StringRef Str("hello");
127f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
128f4a2713aSLionel Sambuc Str.split('X'));
129f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
130f4a2713aSLionel Sambuc Str.split('e'));
131f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
132f4a2713aSLionel Sambuc Str.split('h'));
133f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
134f4a2713aSLionel Sambuc Str.split('l'));
135f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
136f4a2713aSLionel Sambuc Str.split('o'));
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
139f4a2713aSLionel Sambuc Str.rsplit('X'));
140f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
141f4a2713aSLionel Sambuc Str.rsplit('e'));
142f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
143f4a2713aSLionel Sambuc Str.rsplit('h'));
144f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
145f4a2713aSLionel Sambuc Str.rsplit('l'));
146f4a2713aSLionel Sambuc EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
147f4a2713aSLionel Sambuc Str.rsplit('o'));
148f4a2713aSLionel Sambuc }
149f4a2713aSLionel Sambuc
TEST(StringRefTest,Split2)150f4a2713aSLionel Sambuc TEST(StringRefTest, Split2) {
151f4a2713aSLionel Sambuc SmallVector<StringRef, 5> parts;
152f4a2713aSLionel Sambuc SmallVector<StringRef, 5> expected;
153f4a2713aSLionel Sambuc
154f4a2713aSLionel Sambuc expected.push_back("ab"); expected.push_back("c");
155f4a2713aSLionel Sambuc StringRef(",ab,,c,").split(parts, ",", -1, false);
156f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc expected.clear(); parts.clear();
159f4a2713aSLionel Sambuc expected.push_back(""); expected.push_back("ab"); expected.push_back("");
160f4a2713aSLionel Sambuc expected.push_back("c"); expected.push_back("");
161f4a2713aSLionel Sambuc StringRef(",ab,,c,").split(parts, ",", -1, true);
162f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
163f4a2713aSLionel Sambuc
164f4a2713aSLionel Sambuc expected.clear(); parts.clear();
165f4a2713aSLionel Sambuc expected.push_back("");
166f4a2713aSLionel Sambuc StringRef("").split(parts, ",", -1, true);
167f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
168f4a2713aSLionel Sambuc
169f4a2713aSLionel Sambuc expected.clear(); parts.clear();
170f4a2713aSLionel Sambuc StringRef("").split(parts, ",", -1, false);
171f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
172f4a2713aSLionel Sambuc
173f4a2713aSLionel Sambuc expected.clear(); parts.clear();
174f4a2713aSLionel Sambuc StringRef(",").split(parts, ",", -1, false);
175f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
176f4a2713aSLionel Sambuc
177f4a2713aSLionel Sambuc expected.clear(); parts.clear();
178f4a2713aSLionel Sambuc expected.push_back(""); expected.push_back("");
179f4a2713aSLionel Sambuc StringRef(",").split(parts, ",", -1, true);
180f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
181f4a2713aSLionel Sambuc
182f4a2713aSLionel Sambuc expected.clear(); parts.clear();
183f4a2713aSLionel Sambuc expected.push_back("a"); expected.push_back("b");
184f4a2713aSLionel Sambuc StringRef("a,b").split(parts, ",", -1, true);
185f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc // Test MaxSplit
188f4a2713aSLionel Sambuc expected.clear(); parts.clear();
189f4a2713aSLionel Sambuc expected.push_back("a,,b,c");
190f4a2713aSLionel Sambuc StringRef("a,,b,c").split(parts, ",", 0, true);
191f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
192f4a2713aSLionel Sambuc
193f4a2713aSLionel Sambuc expected.clear(); parts.clear();
194f4a2713aSLionel Sambuc expected.push_back("a,,b,c");
195f4a2713aSLionel Sambuc StringRef("a,,b,c").split(parts, ",", 0, false);
196f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
197f4a2713aSLionel Sambuc
198f4a2713aSLionel Sambuc expected.clear(); parts.clear();
199f4a2713aSLionel Sambuc expected.push_back("a"); expected.push_back(",b,c");
200f4a2713aSLionel Sambuc StringRef("a,,b,c").split(parts, ",", 1, true);
201f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
202f4a2713aSLionel Sambuc
203f4a2713aSLionel Sambuc expected.clear(); parts.clear();
204f4a2713aSLionel Sambuc expected.push_back("a"); expected.push_back(",b,c");
205f4a2713aSLionel Sambuc StringRef("a,,b,c").split(parts, ",", 1, false);
206f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
207f4a2713aSLionel Sambuc
208f4a2713aSLionel Sambuc expected.clear(); parts.clear();
209f4a2713aSLionel Sambuc expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
210f4a2713aSLionel Sambuc StringRef("a,,b,c").split(parts, ",", 2, true);
211f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
212f4a2713aSLionel Sambuc
213f4a2713aSLionel Sambuc expected.clear(); parts.clear();
214f4a2713aSLionel Sambuc expected.push_back("a"); expected.push_back("b,c");
215f4a2713aSLionel Sambuc StringRef("a,,b,c").split(parts, ",", 2, false);
216f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
217f4a2713aSLionel Sambuc
218f4a2713aSLionel Sambuc expected.clear(); parts.clear();
219f4a2713aSLionel Sambuc expected.push_back("a"); expected.push_back(""); expected.push_back("b");
220f4a2713aSLionel Sambuc expected.push_back("c");
221f4a2713aSLionel Sambuc StringRef("a,,b,c").split(parts, ",", 3, true);
222f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
223f4a2713aSLionel Sambuc
224f4a2713aSLionel Sambuc expected.clear(); parts.clear();
225f4a2713aSLionel Sambuc expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
226f4a2713aSLionel Sambuc StringRef("a,,b,c").split(parts, ",", 3, false);
227f4a2713aSLionel Sambuc EXPECT_TRUE(parts == expected);
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc
TEST(StringRefTest,Trim)230f4a2713aSLionel Sambuc TEST(StringRefTest, Trim) {
231f4a2713aSLionel Sambuc StringRef Str0("hello");
232f4a2713aSLionel Sambuc StringRef Str1(" hello ");
233f4a2713aSLionel Sambuc StringRef Str2(" hello ");
234f4a2713aSLionel Sambuc
235f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("hello"), Str0.rtrim());
236f4a2713aSLionel Sambuc EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
237f4a2713aSLionel Sambuc EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
238f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("hello"), Str0.ltrim());
239f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("hello "), Str1.ltrim());
240f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("hello "), Str2.ltrim());
241f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("hello"), Str0.trim());
242f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("hello"), Str1.trim());
243f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("hello"), Str2.trim());
244f4a2713aSLionel Sambuc
245f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
246f4a2713aSLionel Sambuc
247f4a2713aSLionel Sambuc EXPECT_EQ(StringRef(""), StringRef("").trim());
248f4a2713aSLionel Sambuc EXPECT_EQ(StringRef(""), StringRef(" ").trim());
249f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
250f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
251f4a2713aSLionel Sambuc EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1)));
252f4a2713aSLionel Sambuc }
253f4a2713aSLionel Sambuc
TEST(StringRefTest,StartsWith)254f4a2713aSLionel Sambuc TEST(StringRefTest, StartsWith) {
255f4a2713aSLionel Sambuc StringRef Str("hello");
256f4a2713aSLionel Sambuc EXPECT_TRUE(Str.startswith(""));
257f4a2713aSLionel Sambuc EXPECT_TRUE(Str.startswith("he"));
258f4a2713aSLionel Sambuc EXPECT_FALSE(Str.startswith("helloworld"));
259f4a2713aSLionel Sambuc EXPECT_FALSE(Str.startswith("hi"));
260f4a2713aSLionel Sambuc }
261f4a2713aSLionel Sambuc
TEST(StringRefTest,StartsWithLower)262f4a2713aSLionel Sambuc TEST(StringRefTest, StartsWithLower) {
263f4a2713aSLionel Sambuc StringRef Str("heLLo");
264f4a2713aSLionel Sambuc EXPECT_TRUE(Str.startswith_lower(""));
265f4a2713aSLionel Sambuc EXPECT_TRUE(Str.startswith_lower("he"));
266f4a2713aSLionel Sambuc EXPECT_TRUE(Str.startswith_lower("hell"));
267f4a2713aSLionel Sambuc EXPECT_TRUE(Str.startswith_lower("HELlo"));
268f4a2713aSLionel Sambuc EXPECT_FALSE(Str.startswith_lower("helloworld"));
269f4a2713aSLionel Sambuc EXPECT_FALSE(Str.startswith_lower("hi"));
270f4a2713aSLionel Sambuc }
271f4a2713aSLionel Sambuc
TEST(StringRefTest,EndsWith)272f4a2713aSLionel Sambuc TEST(StringRefTest, EndsWith) {
273f4a2713aSLionel Sambuc StringRef Str("hello");
274f4a2713aSLionel Sambuc EXPECT_TRUE(Str.endswith(""));
275f4a2713aSLionel Sambuc EXPECT_TRUE(Str.endswith("lo"));
276f4a2713aSLionel Sambuc EXPECT_FALSE(Str.endswith("helloworld"));
277f4a2713aSLionel Sambuc EXPECT_FALSE(Str.endswith("worldhello"));
278f4a2713aSLionel Sambuc EXPECT_FALSE(Str.endswith("so"));
279f4a2713aSLionel Sambuc }
280f4a2713aSLionel Sambuc
TEST(StringRefTest,EndsWithLower)281f4a2713aSLionel Sambuc TEST(StringRefTest, EndsWithLower) {
282f4a2713aSLionel Sambuc StringRef Str("heLLo");
283f4a2713aSLionel Sambuc EXPECT_TRUE(Str.endswith_lower(""));
284f4a2713aSLionel Sambuc EXPECT_TRUE(Str.endswith_lower("lo"));
285f4a2713aSLionel Sambuc EXPECT_TRUE(Str.endswith_lower("LO"));
286f4a2713aSLionel Sambuc EXPECT_TRUE(Str.endswith_lower("ELlo"));
287f4a2713aSLionel Sambuc EXPECT_FALSE(Str.endswith_lower("helloworld"));
288f4a2713aSLionel Sambuc EXPECT_FALSE(Str.endswith_lower("hi"));
289f4a2713aSLionel Sambuc }
290f4a2713aSLionel Sambuc
TEST(StringRefTest,Find)291f4a2713aSLionel Sambuc TEST(StringRefTest, Find) {
292f4a2713aSLionel Sambuc StringRef Str("hello");
293f4a2713aSLionel Sambuc EXPECT_EQ(2U, Str.find('l'));
294f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.find('z'));
295f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
296f4a2713aSLionel Sambuc EXPECT_EQ(0U, Str.find("hello"));
297f4a2713aSLionel Sambuc EXPECT_EQ(1U, Str.find("ello"));
298f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.find("zz"));
299f4a2713aSLionel Sambuc EXPECT_EQ(2U, Str.find("ll", 2));
300f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
301f4a2713aSLionel Sambuc EXPECT_EQ(0U, Str.find(""));
302f4a2713aSLionel Sambuc StringRef LongStr("hellx xello hell ello world foo bar hello");
303f4a2713aSLionel Sambuc EXPECT_EQ(36U, LongStr.find("hello"));
304f4a2713aSLionel Sambuc EXPECT_EQ(28U, LongStr.find("foo"));
305f4a2713aSLionel Sambuc EXPECT_EQ(12U, LongStr.find("hell", 2));
306f4a2713aSLionel Sambuc EXPECT_EQ(0U, LongStr.find(""));
307f4a2713aSLionel Sambuc
308f4a2713aSLionel Sambuc EXPECT_EQ(3U, Str.rfind('l'));
309f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.rfind('z'));
310f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
311f4a2713aSLionel Sambuc EXPECT_EQ(0U, Str.rfind("hello"));
312f4a2713aSLionel Sambuc EXPECT_EQ(1U, Str.rfind("ello"));
313f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
314f4a2713aSLionel Sambuc
315f4a2713aSLionel Sambuc EXPECT_EQ(2U, Str.find_first_of('l'));
316f4a2713aSLionel Sambuc EXPECT_EQ(1U, Str.find_first_of("el"));
317f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
318f4a2713aSLionel Sambuc
319f4a2713aSLionel Sambuc EXPECT_EQ(1U, Str.find_first_not_of('h'));
320f4a2713aSLionel Sambuc EXPECT_EQ(4U, Str.find_first_not_of("hel"));
321f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
322f4a2713aSLionel Sambuc
323f4a2713aSLionel Sambuc EXPECT_EQ(3U, Str.find_last_not_of('o'));
324f4a2713aSLionel Sambuc EXPECT_EQ(1U, Str.find_last_not_of("lo"));
325f4a2713aSLionel Sambuc EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
326f4a2713aSLionel Sambuc }
327f4a2713aSLionel Sambuc
TEST(StringRefTest,Count)328f4a2713aSLionel Sambuc TEST(StringRefTest, Count) {
329f4a2713aSLionel Sambuc StringRef Str("hello");
330f4a2713aSLionel Sambuc EXPECT_EQ(2U, Str.count('l'));
331f4a2713aSLionel Sambuc EXPECT_EQ(1U, Str.count('o'));
332f4a2713aSLionel Sambuc EXPECT_EQ(0U, Str.count('z'));
333f4a2713aSLionel Sambuc EXPECT_EQ(0U, Str.count("helloworld"));
334f4a2713aSLionel Sambuc EXPECT_EQ(1U, Str.count("hello"));
335f4a2713aSLionel Sambuc EXPECT_EQ(1U, Str.count("ello"));
336f4a2713aSLionel Sambuc EXPECT_EQ(0U, Str.count("zz"));
337f4a2713aSLionel Sambuc }
338f4a2713aSLionel Sambuc
TEST(StringRefTest,EditDistance)339f4a2713aSLionel Sambuc TEST(StringRefTest, EditDistance) {
340f4a2713aSLionel Sambuc StringRef Str("hello");
341f4a2713aSLionel Sambuc EXPECT_EQ(2U, Str.edit_distance("hill"));
342f4a2713aSLionel Sambuc }
343f4a2713aSLionel Sambuc
TEST(StringRefTest,Misc)344f4a2713aSLionel Sambuc TEST(StringRefTest, Misc) {
345f4a2713aSLionel Sambuc std::string Storage;
346f4a2713aSLionel Sambuc raw_string_ostream OS(Storage);
347f4a2713aSLionel Sambuc OS << StringRef("hello");
348f4a2713aSLionel Sambuc EXPECT_EQ("hello", OS.str());
349f4a2713aSLionel Sambuc }
350f4a2713aSLionel Sambuc
TEST(StringRefTest,Hashing)351f4a2713aSLionel Sambuc TEST(StringRefTest, Hashing) {
352f4a2713aSLionel Sambuc EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
353f4a2713aSLionel Sambuc EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
354f4a2713aSLionel Sambuc std::string S = "hello world";
355f4a2713aSLionel Sambuc hash_code H = hash_value(S);
356f4a2713aSLionel Sambuc EXPECT_EQ(H, hash_value(StringRef("hello world")));
357f4a2713aSLionel Sambuc EXPECT_EQ(H, hash_value(StringRef(S)));
358f4a2713aSLionel Sambuc EXPECT_NE(H, hash_value(StringRef("hello worl")));
359f4a2713aSLionel Sambuc EXPECT_EQ(hash_value(std::string("hello worl")),
360f4a2713aSLionel Sambuc hash_value(StringRef("hello worl")));
361f4a2713aSLionel Sambuc EXPECT_NE(H, hash_value(StringRef("hello world ")));
362f4a2713aSLionel Sambuc EXPECT_EQ(hash_value(std::string("hello world ")),
363f4a2713aSLionel Sambuc hash_value(StringRef("hello world ")));
364f4a2713aSLionel Sambuc EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
365f4a2713aSLionel Sambuc EXPECT_NE(hash_value(std::string("ello worl")),
366f4a2713aSLionel Sambuc hash_value(StringRef("hello world").slice(1, -1)));
367f4a2713aSLionel Sambuc }
368f4a2713aSLionel Sambuc
369f4a2713aSLionel Sambuc struct UnsignedPair {
370f4a2713aSLionel Sambuc const char *Str;
371f4a2713aSLionel Sambuc uint64_t Expected;
372f4a2713aSLionel Sambuc } Unsigned[] =
373f4a2713aSLionel Sambuc { {"0", 0}
374f4a2713aSLionel Sambuc , {"255", 255}
375f4a2713aSLionel Sambuc , {"256", 256}
376f4a2713aSLionel Sambuc , {"65535", 65535}
377f4a2713aSLionel Sambuc , {"65536", 65536}
378f4a2713aSLionel Sambuc , {"4294967295", 4294967295ULL}
379f4a2713aSLionel Sambuc , {"4294967296", 4294967296ULL}
380f4a2713aSLionel Sambuc , {"18446744073709551615", 18446744073709551615ULL}
381f4a2713aSLionel Sambuc , {"042", 34}
382f4a2713aSLionel Sambuc , {"0x42", 66}
383f4a2713aSLionel Sambuc , {"0b101010", 42}
384f4a2713aSLionel Sambuc };
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc struct SignedPair {
387f4a2713aSLionel Sambuc const char *Str;
388f4a2713aSLionel Sambuc int64_t Expected;
389f4a2713aSLionel Sambuc } Signed[] =
390f4a2713aSLionel Sambuc { {"0", 0}
391f4a2713aSLionel Sambuc , {"-0", 0}
392f4a2713aSLionel Sambuc , {"127", 127}
393f4a2713aSLionel Sambuc , {"128", 128}
394f4a2713aSLionel Sambuc , {"-128", -128}
395f4a2713aSLionel Sambuc , {"-129", -129}
396f4a2713aSLionel Sambuc , {"32767", 32767}
397f4a2713aSLionel Sambuc , {"32768", 32768}
398f4a2713aSLionel Sambuc , {"-32768", -32768}
399f4a2713aSLionel Sambuc , {"-32769", -32769}
400f4a2713aSLionel Sambuc , {"2147483647", 2147483647LL}
401f4a2713aSLionel Sambuc , {"2147483648", 2147483648LL}
402f4a2713aSLionel Sambuc , {"-2147483648", -2147483648LL}
403f4a2713aSLionel Sambuc , {"-2147483649", -2147483649LL}
404f4a2713aSLionel Sambuc , {"-9223372036854775808", -(9223372036854775807LL) - 1}
405f4a2713aSLionel Sambuc , {"042", 34}
406f4a2713aSLionel Sambuc , {"0x42", 66}
407f4a2713aSLionel Sambuc , {"0b101010", 42}
408f4a2713aSLionel Sambuc , {"-042", -34}
409f4a2713aSLionel Sambuc , {"-0x42", -66}
410f4a2713aSLionel Sambuc , {"-0b101010", -42}
411f4a2713aSLionel Sambuc };
412f4a2713aSLionel Sambuc
TEST(StringRefTest,getAsInteger)413f4a2713aSLionel Sambuc TEST(StringRefTest, getAsInteger) {
414f4a2713aSLionel Sambuc uint8_t U8;
415f4a2713aSLionel Sambuc uint16_t U16;
416f4a2713aSLionel Sambuc uint32_t U32;
417f4a2713aSLionel Sambuc uint64_t U64;
418f4a2713aSLionel Sambuc
419f4a2713aSLionel Sambuc for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
420f4a2713aSLionel Sambuc bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
421f4a2713aSLionel Sambuc if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
422f4a2713aSLionel Sambuc ASSERT_FALSE(U8Success);
423f4a2713aSLionel Sambuc EXPECT_EQ(U8, Unsigned[i].Expected);
424f4a2713aSLionel Sambuc } else {
425f4a2713aSLionel Sambuc ASSERT_TRUE(U8Success);
426f4a2713aSLionel Sambuc }
427f4a2713aSLionel Sambuc bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
428f4a2713aSLionel Sambuc if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
429f4a2713aSLionel Sambuc ASSERT_FALSE(U16Success);
430f4a2713aSLionel Sambuc EXPECT_EQ(U16, Unsigned[i].Expected);
431f4a2713aSLionel Sambuc } else {
432f4a2713aSLionel Sambuc ASSERT_TRUE(U16Success);
433f4a2713aSLionel Sambuc }
434f4a2713aSLionel Sambuc bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
435f4a2713aSLionel Sambuc if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
436f4a2713aSLionel Sambuc ASSERT_FALSE(U32Success);
437f4a2713aSLionel Sambuc EXPECT_EQ(U32, Unsigned[i].Expected);
438f4a2713aSLionel Sambuc } else {
439f4a2713aSLionel Sambuc ASSERT_TRUE(U32Success);
440f4a2713aSLionel Sambuc }
441f4a2713aSLionel Sambuc bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
442f4a2713aSLionel Sambuc if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
443f4a2713aSLionel Sambuc ASSERT_FALSE(U64Success);
444f4a2713aSLionel Sambuc EXPECT_EQ(U64, Unsigned[i].Expected);
445f4a2713aSLionel Sambuc } else {
446f4a2713aSLionel Sambuc ASSERT_TRUE(U64Success);
447f4a2713aSLionel Sambuc }
448f4a2713aSLionel Sambuc }
449f4a2713aSLionel Sambuc
450f4a2713aSLionel Sambuc int8_t S8;
451f4a2713aSLionel Sambuc int16_t S16;
452f4a2713aSLionel Sambuc int32_t S32;
453f4a2713aSLionel Sambuc int64_t S64;
454f4a2713aSLionel Sambuc
455f4a2713aSLionel Sambuc for (size_t i = 0; i < array_lengthof(Signed); ++i) {
456f4a2713aSLionel Sambuc bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
457f4a2713aSLionel Sambuc if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
458f4a2713aSLionel Sambuc ASSERT_FALSE(S8Success);
459f4a2713aSLionel Sambuc EXPECT_EQ(S8, Signed[i].Expected);
460f4a2713aSLionel Sambuc } else {
461f4a2713aSLionel Sambuc ASSERT_TRUE(S8Success);
462f4a2713aSLionel Sambuc }
463f4a2713aSLionel Sambuc bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
464f4a2713aSLionel Sambuc if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
465f4a2713aSLionel Sambuc ASSERT_FALSE(S16Success);
466f4a2713aSLionel Sambuc EXPECT_EQ(S16, Signed[i].Expected);
467f4a2713aSLionel Sambuc } else {
468f4a2713aSLionel Sambuc ASSERT_TRUE(S16Success);
469f4a2713aSLionel Sambuc }
470f4a2713aSLionel Sambuc bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
471f4a2713aSLionel Sambuc if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
472f4a2713aSLionel Sambuc ASSERT_FALSE(S32Success);
473f4a2713aSLionel Sambuc EXPECT_EQ(S32, Signed[i].Expected);
474f4a2713aSLionel Sambuc } else {
475f4a2713aSLionel Sambuc ASSERT_TRUE(S32Success);
476f4a2713aSLionel Sambuc }
477f4a2713aSLionel Sambuc bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
478f4a2713aSLionel Sambuc if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
479f4a2713aSLionel Sambuc ASSERT_FALSE(S64Success);
480f4a2713aSLionel Sambuc EXPECT_EQ(S64, Signed[i].Expected);
481f4a2713aSLionel Sambuc } else {
482f4a2713aSLionel Sambuc ASSERT_TRUE(S64Success);
483f4a2713aSLionel Sambuc }
484f4a2713aSLionel Sambuc }
485f4a2713aSLionel Sambuc }
486f4a2713aSLionel Sambuc
487f4a2713aSLionel Sambuc
488f4a2713aSLionel Sambuc static const char* BadStrings[] = {
489f4a2713aSLionel Sambuc "18446744073709551617" // value just over max
490f4a2713aSLionel Sambuc , "123456789012345678901" // value way too large
491f4a2713aSLionel Sambuc , "4t23v" // illegal decimal characters
492f4a2713aSLionel Sambuc , "0x123W56" // illegal hex characters
493f4a2713aSLionel Sambuc , "0b2" // illegal bin characters
494f4a2713aSLionel Sambuc , "08" // illegal oct characters
495f4a2713aSLionel Sambuc , "0o8" // illegal oct characters
496f4a2713aSLionel Sambuc , "-123" // negative unsigned value
497f4a2713aSLionel Sambuc };
498f4a2713aSLionel Sambuc
499f4a2713aSLionel Sambuc
TEST(StringRefTest,getAsUnsignedIntegerBadStrings)500f4a2713aSLionel Sambuc TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
501f4a2713aSLionel Sambuc unsigned long long U64;
502f4a2713aSLionel Sambuc for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
503f4a2713aSLionel Sambuc bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
504f4a2713aSLionel Sambuc ASSERT_TRUE(IsBadNumber);
505f4a2713aSLionel Sambuc }
506f4a2713aSLionel Sambuc }
507f4a2713aSLionel Sambuc
508f4a2713aSLionel Sambuc static const char *join_input[] = { "a", "b", "c" };
509f4a2713aSLionel Sambuc static const char join_result1[] = "a";
510f4a2713aSLionel Sambuc static const char join_result2[] = "a:b:c";
511f4a2713aSLionel Sambuc static const char join_result3[] = "a::b::c";
512f4a2713aSLionel Sambuc
TEST(StringRefTest,joinStrings)513f4a2713aSLionel Sambuc TEST(StringRefTest, joinStrings) {
514f4a2713aSLionel Sambuc std::vector<StringRef> v1;
515f4a2713aSLionel Sambuc std::vector<std::string> v2;
516f4a2713aSLionel Sambuc for (size_t i = 0; i < array_lengthof(join_input); ++i) {
517f4a2713aSLionel Sambuc v1.push_back(join_input[i]);
518f4a2713aSLionel Sambuc v2.push_back(join_input[i]);
519f4a2713aSLionel Sambuc }
520f4a2713aSLionel Sambuc
521f4a2713aSLionel Sambuc bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
522f4a2713aSLionel Sambuc EXPECT_TRUE(v1_join1);
523f4a2713aSLionel Sambuc bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
524f4a2713aSLionel Sambuc EXPECT_TRUE(v1_join2);
525f4a2713aSLionel Sambuc bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
526f4a2713aSLionel Sambuc EXPECT_TRUE(v1_join3);
527f4a2713aSLionel Sambuc
528f4a2713aSLionel Sambuc bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
529f4a2713aSLionel Sambuc EXPECT_TRUE(v2_join1);
530f4a2713aSLionel Sambuc bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
531f4a2713aSLionel Sambuc EXPECT_TRUE(v2_join2);
532f4a2713aSLionel Sambuc bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
533f4a2713aSLionel Sambuc EXPECT_TRUE(v2_join3);
534f4a2713aSLionel Sambuc }
535f4a2713aSLionel Sambuc
536*0a6a1f1dSLionel Sambuc
TEST(StringRefTest,AllocatorCopy)537*0a6a1f1dSLionel Sambuc TEST(StringRefTest, AllocatorCopy) {
538*0a6a1f1dSLionel Sambuc BumpPtrAllocator Alloc;
539*0a6a1f1dSLionel Sambuc StringRef Str1 = "hello";
540*0a6a1f1dSLionel Sambuc StringRef Str2 = "bye";
541*0a6a1f1dSLionel Sambuc StringRef Str1c = Str1.copy(Alloc);
542*0a6a1f1dSLionel Sambuc StringRef Str2c = Str2.copy(Alloc);
543*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Str1.equals(Str1c));
544*0a6a1f1dSLionel Sambuc EXPECT_NE(Str1.data(), Str1c.data());
545*0a6a1f1dSLionel Sambuc EXPECT_TRUE(Str2.equals(Str2c));
546*0a6a1f1dSLionel Sambuc EXPECT_NE(Str2.data(), Str2c.data());
547*0a6a1f1dSLionel Sambuc }
548*0a6a1f1dSLionel Sambuc
549*0a6a1f1dSLionel Sambuc
550f4a2713aSLionel Sambuc } // end anonymous namespace
551