1 //===- llvm/unittest/ADT/SmallStringTest.cpp ------------------------------===//
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 // SmallString unit tests.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SmallString.h"
15 #include "gtest/gtest.h"
25 class SmallStringTest : public testing::Test {
27 typedef SmallString<40> StringType;
31 void assertEmpty(StringType & v) {
33 EXPECT_EQ(0u, v.size());
34 EXPECT_TRUE(v.empty());
36 EXPECT_TRUE(v.begin() == v.end());
41 TEST_F(SmallStringTest, EmptyStringTest) {
42 SCOPED_TRACE("EmptyStringTest");
43 assertEmpty(theString);
44 EXPECT_TRUE(theString.rbegin() == theString.rend());
47 TEST_F(SmallStringTest, AssignRepeated) {
48 theString.assign(3, 'a');
49 EXPECT_EQ(3u, theString.size());
50 EXPECT_STREQ("aaa", theString.c_str());
53 TEST_F(SmallStringTest, AssignIterPair) {
54 StringRef abc = "abc";
55 theString.assign(abc.begin(), abc.end());
56 EXPECT_EQ(3u, theString.size());
57 EXPECT_STREQ("abc", theString.c_str());
60 TEST_F(SmallStringTest, AssignStringRef) {
61 StringRef abc = "abc";
62 theString.assign(abc);
63 EXPECT_EQ(3u, theString.size());
64 EXPECT_STREQ("abc", theString.c_str());
67 TEST_F(SmallStringTest, AssignSmallVector) {
68 StringRef abc = "abc";
69 SmallVector<char, 10> abcVec(abc.begin(), abc.end());
70 theString.assign(abcVec);
71 EXPECT_EQ(3u, theString.size());
72 EXPECT_STREQ("abc", theString.c_str());
75 TEST_F(SmallStringTest, AppendIterPair) {
76 StringRef abc = "abc";
77 theString.append(abc.begin(), abc.end());
78 theString.append(abc.begin(), abc.end());
79 EXPECT_EQ(6u, theString.size());
80 EXPECT_STREQ("abcabc", theString.c_str());
83 TEST_F(SmallStringTest, AppendStringRef) {
84 StringRef abc = "abc";
85 theString.append(abc);
86 theString.append(abc);
87 EXPECT_EQ(6u, theString.size());
88 EXPECT_STREQ("abcabc", theString.c_str());
91 TEST_F(SmallStringTest, AppendSmallVector) {
92 StringRef abc = "abc";
93 SmallVector<char, 10> abcVec(abc.begin(), abc.end());
94 theString.append(abcVec);
95 theString.append(abcVec);
96 EXPECT_EQ(6u, theString.size());
97 EXPECT_STREQ("abcabc", theString.c_str());
100 TEST_F(SmallStringTest, Substr) {
102 EXPECT_EQ("lo", theString.substr(3));
103 EXPECT_EQ("", theString.substr(100));
104 EXPECT_EQ("hello", theString.substr(0, 100));
105 EXPECT_EQ("o", theString.substr(4, 10));
108 TEST_F(SmallStringTest, Slice) {
110 EXPECT_EQ("l", theString.slice(2, 3));
111 EXPECT_EQ("ell", theString.slice(1, 4));
112 EXPECT_EQ("llo", theString.slice(2, 100));
113 EXPECT_EQ("", theString.slice(2, 1));
114 EXPECT_EQ("", theString.slice(10, 20));
117 TEST_F(SmallStringTest, Find) {
119 EXPECT_EQ(2U, theString.find('l'));
120 EXPECT_EQ(StringRef::npos, theString.find('z'));
121 EXPECT_EQ(StringRef::npos, theString.find("helloworld"));
122 EXPECT_EQ(0U, theString.find("hello"));
123 EXPECT_EQ(1U, theString.find("ello"));
124 EXPECT_EQ(StringRef::npos, theString.find("zz"));
125 EXPECT_EQ(2U, theString.find("ll", 2));
126 EXPECT_EQ(StringRef::npos, theString.find("ll", 3));
127 EXPECT_EQ(0U, theString.find(""));
129 EXPECT_EQ(3U, theString.rfind('l'));
130 EXPECT_EQ(StringRef::npos, theString.rfind('z'));
131 EXPECT_EQ(StringRef::npos, theString.rfind("helloworld"));
132 EXPECT_EQ(0U, theString.rfind("hello"));
133 EXPECT_EQ(1U, theString.rfind("ello"));
134 EXPECT_EQ(StringRef::npos, theString.rfind("zz"));
136 EXPECT_EQ(2U, theString.find_first_of('l'));
137 EXPECT_EQ(1U, theString.find_first_of("el"));
138 EXPECT_EQ(StringRef::npos, theString.find_first_of("xyz"));
140 EXPECT_EQ(1U, theString.find_first_not_of('h'));
141 EXPECT_EQ(4U, theString.find_first_not_of("hel"));
142 EXPECT_EQ(StringRef::npos, theString.find_first_not_of("hello"));
144 theString = "hellx xello hell ello world foo bar hello";
145 EXPECT_EQ(36U, theString.find("hello"));
146 EXPECT_EQ(28U, theString.find("foo"));
147 EXPECT_EQ(12U, theString.find("hell", 2));
148 EXPECT_EQ(0U, theString.find(""));
151 TEST_F(SmallStringTest, Count) {
153 EXPECT_EQ(2U, theString.count('l'));
154 EXPECT_EQ(1U, theString.count('o'));
155 EXPECT_EQ(0U, theString.count('z'));
156 EXPECT_EQ(0U, theString.count("helloworld"));
157 EXPECT_EQ(1U, theString.count("hello"));
158 EXPECT_EQ(1U, theString.count("ello"));
159 EXPECT_EQ(0U, theString.count("zz"));
162 TEST(StringRefTest, Comparisons) {
163 EXPECT_EQ(-1, SmallString<10>("aab").compare("aad"));
164 EXPECT_EQ( 0, SmallString<10>("aab").compare("aab"));
165 EXPECT_EQ( 1, SmallString<10>("aab").compare("aaa"));
166 EXPECT_EQ(-1, SmallString<10>("aab").compare("aabb"));
167 EXPECT_EQ( 1, SmallString<10>("aab").compare("aa"));
168 EXPECT_EQ( 1, SmallString<10>("\xFF").compare("\1"));
170 EXPECT_EQ(-1, SmallString<10>("AaB").compare_lower("aAd"));
171 EXPECT_EQ( 0, SmallString<10>("AaB").compare_lower("aab"));
172 EXPECT_EQ( 1, SmallString<10>("AaB").compare_lower("AAA"));
173 EXPECT_EQ(-1, SmallString<10>("AaB").compare_lower("aaBb"));
174 EXPECT_EQ( 1, SmallString<10>("AaB").compare_lower("aA"));
175 EXPECT_EQ( 1, SmallString<10>("\xFF").compare_lower("\1"));
177 EXPECT_EQ(-1, SmallString<10>("aab").compare_numeric("aad"));
178 EXPECT_EQ( 0, SmallString<10>("aab").compare_numeric("aab"));
179 EXPECT_EQ( 1, SmallString<10>("aab").compare_numeric("aaa"));
180 EXPECT_EQ(-1, SmallString<10>("aab").compare_numeric("aabb"));
181 EXPECT_EQ( 1, SmallString<10>("aab").compare_numeric("aa"));
182 EXPECT_EQ(-1, SmallString<10>("1").compare_numeric("10"));
183 EXPECT_EQ( 0, SmallString<10>("10").compare_numeric("10"));
184 EXPECT_EQ( 0, SmallString<10>("10a").compare_numeric("10a"));
185 EXPECT_EQ( 1, SmallString<10>("2").compare_numeric("1"));
186 EXPECT_EQ( 0, SmallString<10>("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
187 EXPECT_EQ( 1, SmallString<10>("\xFF").compare_numeric("\1"));
188 EXPECT_EQ( 1, SmallString<10>("V16").compare_numeric("V1_q0"));
189 EXPECT_EQ(-1, SmallString<10>("V1_q0").compare_numeric("V16"));
190 EXPECT_EQ(-1, SmallString<10>("V8_q0").compare_numeric("V16"));
191 EXPECT_EQ( 1, SmallString<10>("V16").compare_numeric("V8_q0"));
192 EXPECT_EQ(-1, SmallString<10>("V1_q0").compare_numeric("V8_q0"));
193 EXPECT_EQ( 1, SmallString<10>("V8_q0").compare_numeric("V1_q0"));