Add a new split method to StringRef that puts the substrings in a vector.
[oota-llvm.git] / unittests / ADT / StringRefTest.cpp
1 //===- 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
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/raw_ostream.h"
13 using namespace llvm;
14
15 namespace {
16
17 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
18   OS << S;
19   return OS;
20 }
21
22 std::ostream &operator<<(std::ostream &OS,
23                          const std::pair<StringRef, StringRef> &P) {
24   OS << "(" << P.first << ", " << P.second << ")";
25   return OS;
26 }
27
28 TEST(StringRefTest, Construction) {
29   EXPECT_EQ("", StringRef());
30   EXPECT_EQ("hello", StringRef("hello"));
31   EXPECT_EQ("hello", StringRef("hello world", 5));
32   EXPECT_EQ("hello", StringRef(std::string("hello")));
33 }
34
35 TEST(StringRefTest, Iteration) {
36   StringRef S("hello");
37   const char *p = "hello";
38   for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
39     EXPECT_EQ(*it, *p);
40 }
41
42 TEST(StringRefTest, StringOps) {
43   const char *p = "hello";
44   EXPECT_EQ(p, StringRef(p, 0).data());
45   EXPECT_TRUE(StringRef().empty());
46   EXPECT_EQ((size_t) 5, StringRef("hello").size());
47   EXPECT_EQ(-1, StringRef("aab").compare("aad"));
48   EXPECT_EQ( 0, StringRef("aab").compare("aab"));
49   EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
50   EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
51   EXPECT_EQ( 1, StringRef("aab").compare("aa"));
52 }
53
54 TEST(StringRefTest, Operators) {
55   EXPECT_EQ("", StringRef());
56   EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
57   EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
58   EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
59   EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
60   EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
61   EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
62   EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
63   EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
64   EXPECT_EQ(StringRef("aab"), StringRef("aab"));
65   EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
66   EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
67   EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
68   EXPECT_EQ('a', StringRef("aab")[1]);
69 }
70
71 TEST(StringRefTest, Substr) {
72   StringRef Str("hello");
73   EXPECT_EQ("lo", Str.substr(3));
74   EXPECT_EQ("", Str.substr(100));
75   EXPECT_EQ("hello", Str.substr(0, 100));
76   EXPECT_EQ("o", Str.substr(4, 10));
77 }
78
79 TEST(StringRefTest, Slice) {
80   StringRef Str("hello");
81   EXPECT_EQ("l", Str.slice(2, 3));
82   EXPECT_EQ("ell", Str.slice(1, 4));
83   EXPECT_EQ("llo", Str.slice(2, 100));
84   EXPECT_EQ("", Str.slice(2, 1));
85   EXPECT_EQ("", Str.slice(10, 20));
86 }
87
88 TEST(StringRefTest, Split) {
89   StringRef Str("hello");
90   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
91             Str.split('X'));
92   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
93             Str.split('e'));
94   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
95             Str.split('h'));
96   EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
97             Str.split('l'));
98   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
99             Str.split('o'));
100
101   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
102             Str.rsplit('X'));
103   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
104             Str.rsplit('e'));
105   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
106             Str.rsplit('h'));
107   EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
108             Str.rsplit('l'));
109   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
110             Str.rsplit('o'));
111 }
112
113 TEST(StringRefTest, Split2) {
114   std::vector<StringRef> parts;
115   std::vector<StringRef> expected;
116
117   expected.push_back("ab"); expected.push_back("c");
118   StringRef(",ab,,c,").split(parts, ",", -1, false);
119   EXPECT_TRUE(parts == expected);
120
121   expected.clear(); parts.clear();
122   expected.push_back(""); expected.push_back("ab"); expected.push_back("");
123   expected.push_back("c"); expected.push_back("");
124   StringRef(",ab,,c,").split(parts, ",", -1, true);
125   EXPECT_TRUE(parts == expected);
126
127   expected.clear(); parts.clear();
128   expected.push_back("");
129   StringRef("").split(parts, ",", -1, true);
130   EXPECT_TRUE(parts == expected);
131
132   expected.clear(); parts.clear();
133   StringRef("").split(parts, ",", -1, false);
134   EXPECT_TRUE(parts == expected);
135
136   expected.clear(); parts.clear();
137   StringRef(",").split(parts, ",", -1, false);
138   EXPECT_TRUE(parts == expected);
139
140   expected.clear(); parts.clear();
141   expected.push_back(""); expected.push_back("");
142   StringRef(",").split(parts, ",", -1, true);
143   EXPECT_TRUE(parts == expected);
144
145   // Test MaxSplit
146   expected.clear(); parts.clear();
147   expected.push_back("a,,b,c");
148   StringRef("a,,b,c").split(parts, ",", 0, true);
149   EXPECT_TRUE(parts == expected);
150
151   expected.clear(); parts.clear();
152   expected.push_back("a,,b,c");
153   StringRef("a,,b,c").split(parts, ",", 0, false);
154   EXPECT_TRUE(parts == expected);
155
156   expected.clear(); parts.clear();
157   expected.push_back("a"); expected.push_back(",b,c");
158   StringRef("a,,b,c").split(parts, ",", 1, true);
159   EXPECT_TRUE(parts == expected);
160
161   expected.clear(); parts.clear();
162   expected.push_back("a"); expected.push_back(",b,c");
163   StringRef("a,,b,c").split(parts, ",", 1, false);
164   EXPECT_TRUE(parts == expected);
165
166   expected.clear(); parts.clear();
167   expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
168   StringRef("a,,b,c").split(parts, ",", 2, true);
169   EXPECT_TRUE(parts == expected);
170
171   expected.clear(); parts.clear();
172   expected.push_back("a"); expected.push_back("b,c");
173   StringRef("a,,b,c").split(parts, ",", 2, false);
174   EXPECT_TRUE(parts == expected);
175
176   expected.clear(); parts.clear();
177   expected.push_back("a"); expected.push_back(""); expected.push_back("b");
178   expected.push_back("c");
179   StringRef("a,,b,c").split(parts, ",", 3, true);
180   EXPECT_TRUE(parts == expected);
181
182   expected.clear(); parts.clear();
183   expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
184   StringRef("a,,b,c").split(parts, ",", 3, false);
185   EXPECT_TRUE(parts == expected);
186 }
187
188 TEST(StringRefTest, StartsWith) {
189   StringRef Str("hello");
190   EXPECT_TRUE(Str.startswith("he"));
191   EXPECT_FALSE(Str.startswith("helloworld"));
192   EXPECT_FALSE(Str.startswith("hi"));
193 }
194
195 TEST(StringRefTest, Find) {
196   StringRef Str("hello");
197   EXPECT_EQ(2U, Str.find('l'));
198   EXPECT_EQ(StringRef::npos, Str.find('z'));
199   EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
200   EXPECT_EQ(0U, Str.find("hello"));
201   EXPECT_EQ(1U, Str.find("ello"));
202   EXPECT_EQ(StringRef::npos, Str.find("zz"));
203   EXPECT_EQ(2U, Str.find("ll", 2));
204   EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
205
206   EXPECT_EQ(3U, Str.rfind('l'));
207   EXPECT_EQ(StringRef::npos, Str.rfind('z'));
208   EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
209   EXPECT_EQ(0U, Str.rfind("hello"));
210   EXPECT_EQ(1U, Str.rfind("ello"));
211   EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
212
213   EXPECT_EQ(2U, Str.find_first_of('l'));
214   EXPECT_EQ(1U, Str.find_first_of("el"));
215   EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
216
217   EXPECT_EQ(1U, Str.find_first_not_of('h'));
218   EXPECT_EQ(4U, Str.find_first_not_of("hel"));
219   EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
220 }
221
222 TEST(StringRefTest, Count) {
223   StringRef Str("hello");
224   EXPECT_EQ(2U, Str.count('l'));
225   EXPECT_EQ(1U, Str.count('o'));
226   EXPECT_EQ(0U, Str.count('z'));
227   EXPECT_EQ(0U, Str.count("helloworld"));
228   EXPECT_EQ(1U, Str.count("hello"));
229   EXPECT_EQ(1U, Str.count("ello"));
230   EXPECT_EQ(0U, Str.count("zz"));
231 }
232
233 TEST(StringRefTest, Misc) {
234   std::string Storage;
235   raw_string_ostream OS(Storage);
236   OS << StringRef("hello");
237   EXPECT_EQ("hello", OS.str());
238 }
239
240 } // end anonymous namespace