1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #include <algorithm>
32 #include <string>
33 #include <vector>
34 
35 #include "mongo/base/simple_string_data_comparator.h"
36 #include "mongo/base/string_data.h"
37 #include "mongo/unittest/unittest.h"
38 
39 namespace mongo {
40 
41 using std::string;
42 
TEST(Construction,Empty)43 TEST(Construction, Empty) {
44     StringData strData;
45     ASSERT_EQUALS(strData.size(), 0U);
46     ASSERT_TRUE(strData.rawData() == NULL);
47 }
48 
TEST(Construction,FromStdString)49 TEST(Construction, FromStdString) {
50     std::string base("aaa");
51     StringData strData(base);
52     ASSERT_EQUALS(strData.size(), base.size());
53     ASSERT_EQUALS(strData.toString(), base);
54 }
55 
TEST(Construction,FromCString)56 TEST(Construction, FromCString) {
57     std::string base("aaa");
58     StringData strData(base.c_str());
59     ASSERT_EQUALS(strData.size(), base.size());
60     ASSERT_EQUALS(strData.toString(), base);
61 }
62 
TEST(Construction,FromNullCString)63 TEST(Construction, FromNullCString) {
64     char* c = NULL;
65     StringData strData(c);
66     ASSERT_EQUALS(strData.size(), 0U);
67     ASSERT_TRUE(strData.rawData() == NULL);
68 }
69 
TEST(Construction,FromUserDefinedLiteral)70 TEST(Construction, FromUserDefinedLiteral) {
71     const auto strData = "cc\0c"_sd;
72     ASSERT_EQUALS(strData.size(), 4U);
73     ASSERT_EQUALS(strData.toString(), string("cc\0c", 4));
74 }
75 
TEST(Construction,FromUserDefinedRawLiteral)76 TEST(Construction, FromUserDefinedRawLiteral) {
77     const auto strData = R"("")"_sd;
78     ASSERT_EQUALS(strData.size(), 2U);
79     ASSERT_EQUALS(strData.toString(), string("\"\"", 2));
80 }
81 
TEST(Construction,FromEmptyUserDefinedLiteral)82 TEST(Construction, FromEmptyUserDefinedLiteral) {
83     const auto strData = ""_sd;
84     ASSERT_EQUALS(strData.size(), 0U);
85     ASSERT_EQUALS(strData.toString(), string(""));
86 }
87 
TEST(Comparison,BothEmpty)88 TEST(Comparison, BothEmpty) {
89     StringData empty("");
90     ASSERT_TRUE(empty == empty);
91     ASSERT_FALSE(empty != empty);
92     ASSERT_FALSE(empty > empty);
93     ASSERT_TRUE(empty >= empty);
94     ASSERT_FALSE(empty < empty);
95     ASSERT_TRUE(empty <= empty);
96 }
97 
TEST(Comparison,BothNonEmptyOnSize)98 TEST(Comparison, BothNonEmptyOnSize) {
99     StringData a("a");
100     StringData aa("aa");
101     ASSERT_FALSE(a == aa);
102     ASSERT_TRUE(a != aa);
103     ASSERT_FALSE(a > aa);
104     ASSERT_FALSE(a >= aa);
105     ASSERT_TRUE(a >= a);
106     ASSERT_TRUE(a < aa);
107     ASSERT_TRUE(a <= aa);
108     ASSERT_TRUE(a <= a);
109 }
110 
TEST(Comparison,BothNonEmptyOnContent)111 TEST(Comparison, BothNonEmptyOnContent) {
112     StringData a("a");
113     StringData b("b");
114     ASSERT_FALSE(a == b);
115     ASSERT_TRUE(a != b);
116     ASSERT_FALSE(a > b);
117     ASSERT_FALSE(a >= b);
118     ASSERT_TRUE(a < b);
119     ASSERT_TRUE(a <= b);
120 }
121 
TEST(Comparison,MixedEmptyAndNot)122 TEST(Comparison, MixedEmptyAndNot) {
123     StringData empty("");
124     StringData a("a");
125     ASSERT_FALSE(a == empty);
126     ASSERT_TRUE(a != empty);
127     ASSERT_TRUE(a > empty);
128     ASSERT_TRUE(a >= empty);
129     ASSERT_FALSE(a < empty);
130     ASSERT_FALSE(a <= empty);
131 }
132 
TEST(Find,Char1)133 TEST(Find, Char1) {
134     ASSERT_EQUALS(string::npos, StringData("foo").find('a'));
135     ASSERT_EQUALS(0U, StringData("foo").find('f'));
136     ASSERT_EQUALS(1U, StringData("foo").find('o'));
137 }
138 
TEST(Find,Str1)139 TEST(Find, Str1) {
140     ASSERT_EQUALS(string::npos, StringData("foo").find("asdsadasda"));
141     ASSERT_EQUALS(string::npos, StringData("foo").find("a"));
142     ASSERT_EQUALS(string::npos, StringData("foo").find("food"));
143     ASSERT_EQUALS(string::npos, StringData("foo").find("ooo"));
144 
145     ASSERT_EQUALS(0U, StringData("foo").find("f"));
146     ASSERT_EQUALS(0U, StringData("foo").find("fo"));
147     ASSERT_EQUALS(0U, StringData("foo").find("foo"));
148     ASSERT_EQUALS(1U, StringData("foo").find("o"));
149     ASSERT_EQUALS(1U, StringData("foo").find("oo"));
150 
151     ASSERT_EQUALS(string("foo").find(""), StringData("foo").find(""));
152 }
153 
154 // Helper function for Test(Hasher, Str1)
155 template <int SizeofSizeT>
156 void SDHasher_check(void);
157 
158 template <>
SDHasher_check(void)159 void SDHasher_check<4>(void) {
160     const auto& strCmp = SimpleStringDataComparator::kInstance;
161     ASSERT_EQUALS(strCmp.hash(""), static_cast<size_t>(0));
162     ASSERT_EQUALS(strCmp.hash("foo"), static_cast<size_t>(4138058784ULL));
163     ASSERT_EQUALS(strCmp.hash("pizza"), static_cast<size_t>(3587803311ULL));
164     ASSERT_EQUALS(strCmp.hash("mongo"), static_cast<size_t>(3724335885ULL));
165     ASSERT_EQUALS(strCmp.hash("murmur"), static_cast<size_t>(1945310157ULL));
166 }
167 
168 template <>
SDHasher_check(void)169 void SDHasher_check<8>(void) {
170     const auto& strCmp = SimpleStringDataComparator::kInstance;
171     ASSERT_EQUALS(strCmp.hash(""), static_cast<size_t>(0));
172     ASSERT_EQUALS(strCmp.hash("foo"), static_cast<size_t>(16316970633193145697ULL));
173     ASSERT_EQUALS(strCmp.hash("pizza"), static_cast<size_t>(12165495155477134356ULL));
174     ASSERT_EQUALS(strCmp.hash("mongo"), static_cast<size_t>(2861051452199491487ULL));
175     ASSERT_EQUALS(strCmp.hash("murmur"), static_cast<size_t>(18237957392784716687ULL));
176 }
177 
TEST(Hasher,Str1)178 TEST(Hasher, Str1) {
179     SDHasher_check<sizeof(size_t)>();
180 }
181 
TEST(Rfind,Char1)182 TEST(Rfind, Char1) {
183     ASSERT_EQUALS(string::npos, StringData("foo").rfind('a'));
184 
185     ASSERT_EQUALS(0U, StringData("foo").rfind('f'));
186     ASSERT_EQUALS(0U, StringData("foo").rfind('f', 3));
187     ASSERT_EQUALS(0U, StringData("foo").rfind('f', 2));
188     ASSERT_EQUALS(0U, StringData("foo").rfind('f', 1));
189     ASSERT_EQUALS(string::npos, StringData("foo", 0).rfind('f'));
190 
191     ASSERT_EQUALS(2U, StringData("foo").rfind('o'));
192     ASSERT_EQUALS(2U, StringData("foo", 3).rfind('o'));
193     ASSERT_EQUALS(1U, StringData("foo", 2).rfind('o'));
194     ASSERT_EQUALS(string::npos, StringData("foo", 1).rfind('o'));
195     ASSERT_EQUALS(string::npos, StringData("foo", 0).rfind('o'));
196 }
197 
198 // this is to verify we match std::string
SUBSTR_TEST_HELP(StringData big,StringData small,size_t start,size_t len)199 void SUBSTR_TEST_HELP(StringData big, StringData small, size_t start, size_t len) {
200     ASSERT_EQUALS(small.toString(), big.toString().substr(start, len));
201     ASSERT_EQUALS(small, StringData(big).substr(start, len));
202 }
SUBSTR_TEST_HELP(StringData big,StringData small,size_t start)203 void SUBSTR_TEST_HELP(StringData big, StringData small, size_t start) {
204     ASSERT_EQUALS(small.toString(), big.toString().substr(start));
205     ASSERT_EQUALS(small, StringData(big).substr(start));
206 }
207 
208 // [12] is number of args to substr
209 #define SUBSTR_1_TEST_HELP(big, small, start)                                              \
210     ASSERT_EQUALS(StringData(small).toString(), StringData(big).toString().substr(start)); \
211     ASSERT_EQUALS(StringData(small), StringData(big).substr(start));
212 
213 #define SUBSTR_2_TEST_HELP(big, small, start, len)                                              \
214     ASSERT_EQUALS(StringData(small).toString(), StringData(big).toString().substr(start, len)); \
215     ASSERT_EQUALS(StringData(small), StringData(big).substr(start, len));
216 
TEST(Substr,Simple1)217 TEST(Substr, Simple1) {
218     SUBSTR_1_TEST_HELP("abcde", "abcde", 0);
219     SUBSTR_2_TEST_HELP("abcde", "abcde", 0, 10);
220     SUBSTR_2_TEST_HELP("abcde", "abcde", 0, 5);
221     SUBSTR_2_TEST_HELP("abcde", "abc", 0, 3);
222     SUBSTR_1_TEST_HELP("abcde", "cde", 2);
223     SUBSTR_2_TEST_HELP("abcde", "cde", 2, 5);
224     SUBSTR_2_TEST_HELP("abcde", "cde", 2, 3);
225     SUBSTR_2_TEST_HELP("abcde", "cd", 2, 2);
226     SUBSTR_2_TEST_HELP("abcde", "cd", 2, 2);
227     SUBSTR_1_TEST_HELP("abcde", "", 5);
228     SUBSTR_2_TEST_HELP("abcde", "", 5, 0);
229     SUBSTR_2_TEST_HELP("abcde", "", 5, 10);
230 
231     // make sure we don't blow past the end of the StringData
232     SUBSTR_1_TEST_HELP(StringData("abcdeXXX", 5), "abcde", 0);
233     SUBSTR_2_TEST_HELP(StringData("abcdeXXX", 5), "abcde", 0, 10);
234     SUBSTR_1_TEST_HELP(StringData("abcdeXXX", 5), "de", 3);
235     SUBSTR_2_TEST_HELP(StringData("abcdeXXX", 5), "de", 3, 7);
236     SUBSTR_1_TEST_HELP(StringData("abcdeXXX", 5), "", 5);
237     SUBSTR_2_TEST_HELP(StringData("abcdeXXX", 5), "", 5, 1);
238 }
239 
TEST(equalCaseInsensitiveTest,Simple1)240 TEST(equalCaseInsensitiveTest, Simple1) {
241     ASSERT(StringData("abc").equalCaseInsensitive("abc"));
242     ASSERT(StringData("abc").equalCaseInsensitive("ABC"));
243     ASSERT(StringData("ABC").equalCaseInsensitive("abc"));
244     ASSERT(StringData("ABC").equalCaseInsensitive("ABC"));
245     ASSERT(StringData("ABC").equalCaseInsensitive("AbC"));
246     ASSERT(!StringData("ABC").equalCaseInsensitive("AbCd"));
247     ASSERT(!StringData("ABC").equalCaseInsensitive("AdC"));
248 }
249 
TEST(StartsWith,Simple)250 TEST(StartsWith, Simple) {
251     ASSERT(StringData("").startsWith(""));
252     ASSERT(!StringData("").startsWith("x"));
253     ASSERT(StringData("abcde").startsWith(""));
254     ASSERT(StringData("abcde").startsWith("a"));
255     ASSERT(StringData("abcde").startsWith("ab"));
256     ASSERT(StringData("abcde").startsWith("abc"));
257     ASSERT(StringData("abcde").startsWith("abcd"));
258     ASSERT(StringData("abcde").startsWith("abcde"));
259     ASSERT(!StringData("abcde").startsWith("abcdef"));
260     ASSERT(!StringData("abcde").startsWith("abdce"));
261     ASSERT(StringData("abcde").startsWith(StringData("abcdeXXXX").substr(0, 4)));
262     ASSERT(!StringData("abcde").startsWith(StringData("abdef").substr(0, 4)));
263     ASSERT(!StringData("abcde").substr(0, 3).startsWith("abcd"));
264 }
265 
TEST(EndsWith,Simple)266 TEST(EndsWith, Simple) {
267     // ASSERT(StringData("").endsWith(""));
268     ASSERT(!StringData("").endsWith("x"));
269     // ASSERT(StringData("abcde").endsWith(""));
270     ASSERT(StringData("abcde").endsWith(StringData("e", 0)));
271     ASSERT(StringData("abcde").endsWith("e"));
272     ASSERT(StringData("abcde").endsWith("de"));
273     ASSERT(StringData("abcde").endsWith("cde"));
274     ASSERT(StringData("abcde").endsWith("bcde"));
275     ASSERT(StringData("abcde").endsWith("abcde"));
276     ASSERT(!StringData("abcde").endsWith("0abcde"));
277     ASSERT(!StringData("abcde").endsWith("abdce"));
278     ASSERT(StringData("abcde").endsWith(StringData("bcdef").substr(0, 4)));
279     ASSERT(!StringData("abcde").endsWith(StringData("bcde", 3)));
280     ASSERT(!StringData("abcde").substr(0, 3).endsWith("cde"));
281 }
282 
TEST(ConstIterator,StdCopy)283 TEST(ConstIterator, StdCopy) {
284     std::vector<char> chars;
285     auto data = "This is some raw data."_sd;
286 
287     chars.resize(data.size());
288     std::copy(data.begin(), data.end(), chars.begin());
289 
290     for (size_t i = 0; i < data.size(); ++i) {
291         ASSERT_EQUALS(data[i], chars[i]);
292     }
293 }
294 
TEST(ConstIterator,StdReverseCopy)295 TEST(ConstIterator, StdReverseCopy) {
296     std::vector<char> chars;
297     auto data = "This is some raw data."_sd;
298 
299     chars.resize(data.size());
300     std::reverse_copy(data.begin(), data.end(), chars.begin());
301 
302     const char rawDataExpected[] = ".atad war emos si sihT";
303 
304     for (size_t i = 0; i < data.size(); ++i) {
305         ASSERT_EQUALS(rawDataExpected[i], chars[i]);
306     }
307 }
308 
TEST(ConstIterator,StdReplaceCopy)309 TEST(ConstIterator, StdReplaceCopy) {
310     std::vector<char> chars;
311     auto data = "This is some raw data."_sd;
312 
313     chars.resize(data.size());
314     std::replace_copy(data.begin(), data.end(), chars.begin(), ' ', '_');
315 
316     const char rawDataExpected[] = "This_is_some_raw_data.";
317 
318     for (size_t i = 0; i < data.size(); ++i) {
319         ASSERT_EQUALS(rawDataExpected[i], chars[i]);
320     }
321 }
322 
323 }  // namespace mongo
324