Add an empty key for DebugLoc so that you can store an empty DebugLoc in a
[llvm/stm8.git] / unittests / ADT / StringRefTest.cpp
blob5731e4abaf1525055a2ddc6ba98b08b141aa097c
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 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
16 namespace llvm {
18 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
19 OS << S.str();
20 return OS;
23 std::ostream &operator<<(std::ostream &OS,
24 const std::pair<StringRef, StringRef> &P) {
25 OS << "(" << P.first << ", " << P.second << ")";
26 return OS;
31 namespace {
32 TEST(StringRefTest, Construction) {
33 EXPECT_EQ("", StringRef());
34 EXPECT_EQ("hello", StringRef("hello"));
35 EXPECT_EQ("hello", StringRef("hello world", 5));
36 EXPECT_EQ("hello", StringRef(std::string("hello")));
39 TEST(StringRefTest, Iteration) {
40 StringRef S("hello");
41 const char *p = "hello";
42 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
43 EXPECT_EQ(*it, *p);
46 TEST(StringRefTest, StringOps) {
47 const char *p = "hello";
48 EXPECT_EQ(p, StringRef(p, 0).data());
49 EXPECT_TRUE(StringRef().empty());
50 EXPECT_EQ((size_t) 5, StringRef("hello").size());
51 EXPECT_EQ(-1, StringRef("aab").compare("aad"));
52 EXPECT_EQ( 0, StringRef("aab").compare("aab"));
53 EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
54 EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
55 EXPECT_EQ( 1, StringRef("aab").compare("aa"));
56 EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
58 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
59 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
60 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
61 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
62 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
63 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
65 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
66 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
67 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
68 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
69 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
70 EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
71 EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
72 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
73 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
74 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
75 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
78 TEST(StringRefTest, Operators) {
79 EXPECT_EQ("", StringRef());
80 EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
81 EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
82 EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
83 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
84 EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
85 EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
86 EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
87 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
88 EXPECT_EQ(StringRef("aab"), StringRef("aab"));
89 EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
90 EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
91 EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
92 EXPECT_EQ('a', StringRef("aab")[1]);
95 TEST(StringRefTest, Substr) {
96 StringRef Str("hello");
97 EXPECT_EQ("lo", Str.substr(3));
98 EXPECT_EQ("", Str.substr(100));
99 EXPECT_EQ("hello", Str.substr(0, 100));
100 EXPECT_EQ("o", Str.substr(4, 10));
103 TEST(StringRefTest, Slice) {
104 StringRef Str("hello");
105 EXPECT_EQ("l", Str.slice(2, 3));
106 EXPECT_EQ("ell", Str.slice(1, 4));
107 EXPECT_EQ("llo", Str.slice(2, 100));
108 EXPECT_EQ("", Str.slice(2, 1));
109 EXPECT_EQ("", Str.slice(10, 20));
112 TEST(StringRefTest, Split) {
113 StringRef Str("hello");
114 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
115 Str.split('X'));
116 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
117 Str.split('e'));
118 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
119 Str.split('h'));
120 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
121 Str.split('l'));
122 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
123 Str.split('o'));
125 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
126 Str.rsplit('X'));
127 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
128 Str.rsplit('e'));
129 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
130 Str.rsplit('h'));
131 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
132 Str.rsplit('l'));
133 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
134 Str.rsplit('o'));
137 TEST(StringRefTest, Split2) {
138 SmallVector<StringRef, 5> parts;
139 SmallVector<StringRef, 5> expected;
141 expected.push_back("ab"); expected.push_back("c");
142 StringRef(",ab,,c,").split(parts, ",", -1, false);
143 EXPECT_TRUE(parts == expected);
145 expected.clear(); parts.clear();
146 expected.push_back(""); expected.push_back("ab"); expected.push_back("");
147 expected.push_back("c"); expected.push_back("");
148 StringRef(",ab,,c,").split(parts, ",", -1, true);
149 EXPECT_TRUE(parts == expected);
151 expected.clear(); parts.clear();
152 expected.push_back("");
153 StringRef("").split(parts, ",", -1, true);
154 EXPECT_TRUE(parts == expected);
156 expected.clear(); parts.clear();
157 StringRef("").split(parts, ",", -1, false);
158 EXPECT_TRUE(parts == expected);
160 expected.clear(); parts.clear();
161 StringRef(",").split(parts, ",", -1, false);
162 EXPECT_TRUE(parts == expected);
164 expected.clear(); parts.clear();
165 expected.push_back(""); expected.push_back("");
166 StringRef(",").split(parts, ",", -1, true);
167 EXPECT_TRUE(parts == expected);
169 expected.clear(); parts.clear();
170 expected.push_back("a"); expected.push_back("b");
171 StringRef("a,b").split(parts, ",", -1, true);
172 EXPECT_TRUE(parts == expected);
174 // Test MaxSplit
175 expected.clear(); parts.clear();
176 expected.push_back("a,,b,c");
177 StringRef("a,,b,c").split(parts, ",", 0, true);
178 EXPECT_TRUE(parts == expected);
180 expected.clear(); parts.clear();
181 expected.push_back("a,,b,c");
182 StringRef("a,,b,c").split(parts, ",", 0, false);
183 EXPECT_TRUE(parts == expected);
185 expected.clear(); parts.clear();
186 expected.push_back("a"); expected.push_back(",b,c");
187 StringRef("a,,b,c").split(parts, ",", 1, true);
188 EXPECT_TRUE(parts == expected);
190 expected.clear(); parts.clear();
191 expected.push_back("a"); expected.push_back(",b,c");
192 StringRef("a,,b,c").split(parts, ",", 1, false);
193 EXPECT_TRUE(parts == expected);
195 expected.clear(); parts.clear();
196 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
197 StringRef("a,,b,c").split(parts, ",", 2, true);
198 EXPECT_TRUE(parts == expected);
200 expected.clear(); parts.clear();
201 expected.push_back("a"); expected.push_back("b,c");
202 StringRef("a,,b,c").split(parts, ",", 2, false);
203 EXPECT_TRUE(parts == expected);
205 expected.clear(); parts.clear();
206 expected.push_back("a"); expected.push_back(""); expected.push_back("b");
207 expected.push_back("c");
208 StringRef("a,,b,c").split(parts, ",", 3, true);
209 EXPECT_TRUE(parts == expected);
211 expected.clear(); parts.clear();
212 expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
213 StringRef("a,,b,c").split(parts, ",", 3, false);
214 EXPECT_TRUE(parts == expected);
217 TEST(StringRefTest, StartsWith) {
218 StringRef Str("hello");
219 EXPECT_TRUE(Str.startswith("he"));
220 EXPECT_FALSE(Str.startswith("helloworld"));
221 EXPECT_FALSE(Str.startswith("hi"));
224 TEST(StringRefTest, EndsWith) {
225 StringRef Str("hello");
226 EXPECT_TRUE(Str.endswith("lo"));
227 EXPECT_FALSE(Str.endswith("helloworld"));
228 EXPECT_FALSE(Str.endswith("worldhello"));
229 EXPECT_FALSE(Str.endswith("so"));
232 TEST(StringRefTest, Find) {
233 StringRef Str("hello");
234 EXPECT_EQ(2U, Str.find('l'));
235 EXPECT_EQ(StringRef::npos, Str.find('z'));
236 EXPECT_EQ(StringRef::npos, Str.find("helloworld"));
237 EXPECT_EQ(0U, Str.find("hello"));
238 EXPECT_EQ(1U, Str.find("ello"));
239 EXPECT_EQ(StringRef::npos, Str.find("zz"));
240 EXPECT_EQ(2U, Str.find("ll", 2));
241 EXPECT_EQ(StringRef::npos, Str.find("ll", 3));
243 EXPECT_EQ(3U, Str.rfind('l'));
244 EXPECT_EQ(StringRef::npos, Str.rfind('z'));
245 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
246 EXPECT_EQ(0U, Str.rfind("hello"));
247 EXPECT_EQ(1U, Str.rfind("ello"));
248 EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
250 EXPECT_EQ(2U, Str.find_first_of('l'));
251 EXPECT_EQ(1U, Str.find_first_of("el"));
252 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
254 EXPECT_EQ(1U, Str.find_first_not_of('h'));
255 EXPECT_EQ(4U, Str.find_first_not_of("hel"));
256 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
259 TEST(StringRefTest, Count) {
260 StringRef Str("hello");
261 EXPECT_EQ(2U, Str.count('l'));
262 EXPECT_EQ(1U, Str.count('o'));
263 EXPECT_EQ(0U, Str.count('z'));
264 EXPECT_EQ(0U, Str.count("helloworld"));
265 EXPECT_EQ(1U, Str.count("hello"));
266 EXPECT_EQ(1U, Str.count("ello"));
267 EXPECT_EQ(0U, Str.count("zz"));
270 TEST(StringRefTest, EditDistance) {
271 StringRef Str("hello");
272 EXPECT_EQ(2U, Str.edit_distance("hill"));
275 TEST(StringRefTest, Misc) {
276 std::string Storage;
277 raw_string_ostream OS(Storage);
278 OS << StringRef("hello");
279 EXPECT_EQ("hello", OS.str());
282 } // end anonymous namespace