1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/raw_ostream.h"
17 std::ostream
&operator<<(std::ostream
&OS
, const StringRef
&S
) {
22 std::ostream
&operator<<(std::ostream
&OS
,
23 const std::pair
<StringRef
, StringRef
> &P
) {
24 OS
<< "(" << P
.first
<< ", " << P
.second
<< ")";
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")));
35 TEST(StringRefTest
, Iteration
) {
37 const char *p
= "hello";
38 for (const char *it
= S
.begin(), *ie
= S
.end(); it
!= ie
; ++it
, ++p
)
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"));
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]);
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));
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));
88 TEST(StringRefTest
, Split
) {
89 StringRef
Str("hello");
90 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
92 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
94 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
96 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
98 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
101 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
103 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
105 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
107 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
109 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
113 TEST(StringRefTest
, StartsWith
) {
114 StringRef
Str("hello");
115 EXPECT_TRUE(Str
.startswith("he"));
116 EXPECT_FALSE(Str
.startswith("helloworld"));
117 EXPECT_FALSE(Str
.startswith("hi"));
120 TEST(StringRefTest
, Find
) {
121 StringRef
Str("hello");
122 EXPECT_EQ(2U, Str
.find('l'));
123 EXPECT_EQ(StringRef::npos
, Str
.find('z'));
124 EXPECT_EQ(StringRef::npos
, Str
.find("helloworld"));
125 EXPECT_EQ(0U, Str
.find("hello"));
126 EXPECT_EQ(1U, Str
.find("ello"));
127 EXPECT_EQ(StringRef::npos
, Str
.find("zz"));
129 EXPECT_EQ(3U, Str
.rfind('l'));
130 EXPECT_EQ(StringRef::npos
, Str
.rfind('z'));
131 EXPECT_EQ(StringRef::npos
, Str
.rfind("helloworld"));
132 EXPECT_EQ(0U, Str
.rfind("hello"));
133 EXPECT_EQ(1U, Str
.rfind("ello"));
134 EXPECT_EQ(StringRef::npos
, Str
.rfind("zz"));
137 TEST(StringRefTest
, Count
) {
138 StringRef
Str("hello");
139 EXPECT_EQ(2U, Str
.count('l'));
140 EXPECT_EQ(1U, Str
.count('o'));
141 EXPECT_EQ(0U, Str
.count('z'));
142 EXPECT_EQ(0U, Str
.count("helloworld"));
143 EXPECT_EQ(1U, Str
.count("hello"));
144 EXPECT_EQ(1U, Str
.count("ello"));
145 EXPECT_EQ(0U, Str
.count("zz"));
148 TEST(StringRefTest
, Misc
) {
150 raw_string_ostream
OS(Storage
);
151 OS
<< StringRef("hello");
152 EXPECT_EQ("hello", OS
.str());
155 } // end anonymous namespace