2
* Copyright (C) 2012 10gen Inc.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Affero General Public License, version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Affero General Public License for more details.
13
* You should have received a copy of the GNU Affero General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include "mongo/base/string_data.h"
20
#include "mongo/unittest/unittest.h"
24
using mongo::StringData;
27
TEST(Construction, Empty) {
29
ASSERT_EQUALS(strData.size(), 0U);
30
ASSERT_TRUE(strData.rawData() == NULL);
33
TEST(Construction, FromStdString) {
34
std::string base("aaa");
35
StringData strData(base);
36
ASSERT_EQUALS(strData.size(), base.size());
37
ASSERT_EQUALS(strData.toString(), base);
40
TEST(Construction, FromCString) {
41
std::string base("aaa");
42
StringData strData(base.c_str());
43
ASSERT_EQUALS(strData.size(), base.size());
44
ASSERT_EQUALS(strData.toString(), base);
47
TEST(Construction, FromNullCString) {
49
StringData strData(c);
50
ASSERT_EQUALS(strData.size(), 0U);
51
ASSERT_TRUE(strData.rawData() == NULL);
54
TEST(Construction, FromLiteral) {
55
StringData strData("ccc", StringData::LiteralTag());
56
ASSERT_EQUALS(strData.size(), 3U);
57
ASSERT_EQUALS(strData.toString(), string("ccc"));
60
TEST(Comparison, BothEmpty) {
62
ASSERT_TRUE(empty == empty);
63
ASSERT_FALSE(empty != empty);
64
ASSERT_FALSE(empty > empty);
65
ASSERT_TRUE(empty >= empty);
66
ASSERT_FALSE(empty < empty);
67
ASSERT_TRUE(empty <= empty);
70
TEST(Comparison, BothNonEmptyOnSize) {
73
ASSERT_FALSE(a == aa);
76
ASSERT_FALSE(a >= aa);
83
TEST(Comparison, BothNonEmptyOnContent) {
94
TEST(Comparison, MixedEmptyAndNot) {
97
ASSERT_FALSE(a == empty);
98
ASSERT_TRUE(a != empty);
99
ASSERT_TRUE(a > empty);
100
ASSERT_TRUE(a >= empty);
101
ASSERT_FALSE(a < empty);
102
ASSERT_FALSE(a <= empty);
106
ASSERT_EQUALS( string::npos, StringData( "foo" ).find( 'a' ) );
107
ASSERT_EQUALS( 0U, StringData( "foo" ).find( 'f' ) );
108
ASSERT_EQUALS( 1U, StringData( "foo" ).find( 'o' ) );
112
ASSERT_EQUALS( string::npos, StringData( "foo" ).find( "asdsadasda" ) );
113
ASSERT_EQUALS( string::npos, StringData( "foo" ).find( "a" ) );
114
ASSERT_EQUALS( string::npos, StringData( "foo" ).find( "food" ) );
115
ASSERT_EQUALS( string::npos, StringData( "foo" ).find( "ooo" ) );
117
ASSERT_EQUALS( 0U, StringData( "foo" ).find( "f" ) );
118
ASSERT_EQUALS( 0U, StringData( "foo" ).find( "fo" ) );
119
ASSERT_EQUALS( 0U, StringData( "foo" ).find( "foo" ) );
120
ASSERT_EQUALS( 1U, StringData( "foo" ).find( "o" ) );
121
ASSERT_EQUALS( 1U, StringData( "foo" ).find( "oo" ) );
123
ASSERT_EQUALS( string("foo").find( "" ), StringData("foo").find( "" ) );
126
// this is to verify we match std::string
127
void SUBSTR_TEST_HELP(StringData big, StringData small, size_t start, size_t len) {
128
ASSERT_EQUALS(small.toString(), big.toString().substr(start, len));
129
ASSERT_EQUALS(small, StringData(big).substr(start, len));
131
void SUBSTR_TEST_HELP(StringData big, StringData small, size_t start) {
132
ASSERT_EQUALS(small.toString(), big.toString().substr(start));
133
ASSERT_EQUALS(small, StringData(big).substr(start));
136
// [12] is number of args to substr
137
#define SUBSTR_1_TEST_HELP(big,small,start) \
138
ASSERT_EQUALS( StringData(small).toString(), StringData(big).toString().substr(start) ); \
139
ASSERT_EQUALS( StringData(small), StringData(big).substr(start) );
141
#define SUBSTR_2_TEST_HELP(big,small,start,len) \
142
ASSERT_EQUALS( StringData(small).toString(), StringData(big).toString().substr(start, len) ); \
143
ASSERT_EQUALS( StringData(small), StringData(big).substr(start, len) );
145
TEST(Substr, Simple1 ) {
146
SUBSTR_1_TEST_HELP( "abcde", "abcde", 0 );
147
SUBSTR_2_TEST_HELP( "abcde", "abcde", 0, 10 );
148
SUBSTR_2_TEST_HELP( "abcde", "abcde", 0, 5 );
149
SUBSTR_2_TEST_HELP( "abcde", "abc", 0, 3 );
150
SUBSTR_1_TEST_HELP( "abcde", "cde", 2 );
151
SUBSTR_2_TEST_HELP( "abcde", "cde", 2, 5 );
152
SUBSTR_2_TEST_HELP( "abcde", "cde", 2, 3 );
153
SUBSTR_2_TEST_HELP( "abcde", "cd", 2, 2 );
154
SUBSTR_2_TEST_HELP( "abcde", "cd", 2, 2 );
155
SUBSTR_1_TEST_HELP( "abcde", "", 5 );
156
SUBSTR_2_TEST_HELP( "abcde", "", 5, 0 );
157
SUBSTR_2_TEST_HELP( "abcde", "", 5, 10 );
159
// make sure we don't blow past the end of the StringData
160
SUBSTR_1_TEST_HELP( StringData("abcdeXXX", 5), "abcde", 0);
161
SUBSTR_2_TEST_HELP( StringData("abcdeXXX", 5), "abcde", 0, 10);
162
SUBSTR_1_TEST_HELP( StringData("abcdeXXX", 5), "de", 3);
163
SUBSTR_2_TEST_HELP( StringData("abcdeXXX", 5), "de", 3, 7);
164
SUBSTR_1_TEST_HELP( StringData("abcdeXXX", 5), "", 5);
165
SUBSTR_2_TEST_HELP( StringData("abcdeXXX", 5), "", 5, 1);
168
TEST( equalCaseInsensitiveTest, Simple1 ) {
169
ASSERT( StringData( "abc" ).equalCaseInsensitive( "abc" ) );
170
ASSERT( StringData( "abc" ).equalCaseInsensitive( "ABC" ) );
171
ASSERT( StringData( "ABC" ).equalCaseInsensitive( "abc" ) );
172
ASSERT( StringData( "ABC" ).equalCaseInsensitive( "ABC" ) );
173
ASSERT( StringData( "ABC" ).equalCaseInsensitive( "AbC" ) );
174
ASSERT( !StringData( "ABC" ).equalCaseInsensitive( "AbCd" ) );
175
ASSERT( !StringData( "ABC" ).equalCaseInsensitive( "AdC" ) );
178
TEST(StartsWith, Simple) {
179
ASSERT(StringData("").startsWith(""));
180
ASSERT(!StringData("").startsWith("x"));
181
ASSERT(StringData("abcde").startsWith(""));
182
ASSERT(StringData("abcde").startsWith("a"));
183
ASSERT(StringData("abcde").startsWith("ab"));
184
ASSERT(StringData("abcde").startsWith("abc"));
185
ASSERT(StringData("abcde").startsWith("abcd"));
186
ASSERT(StringData("abcde").startsWith("abcde"));
187
ASSERT(!StringData("abcde").startsWith("abcdef"));
188
ASSERT(!StringData("abcde").startsWith("abdce"));
189
ASSERT(StringData("abcde").startsWith(StringData("abcdeXXXX").substr(0, 4)));
190
ASSERT(!StringData("abcde").startsWith(StringData("abdef").substr(0, 4)));
191
ASSERT(!StringData("abcde").substr(0, 3).startsWith("abcd"));
194
TEST(EndsWith, Simple) {
195
//ASSERT(StringData("").endsWith(""));
196
ASSERT(!StringData("").endsWith("x"));
197
//ASSERT(StringData("abcde").endsWith(""));
198
ASSERT(StringData("abcde").endsWith(StringData("e", 0)));
199
ASSERT(StringData("abcde").endsWith("e"));
200
ASSERT(StringData("abcde").endsWith("de"));
201
ASSERT(StringData("abcde").endsWith("cde"));
202
ASSERT(StringData("abcde").endsWith("bcde"));
203
ASSERT(StringData("abcde").endsWith("abcde"));
204
ASSERT(!StringData("abcde").endsWith("0abcde"));
205
ASSERT(!StringData("abcde").endsWith("abdce"));
206
ASSERT(StringData("abcde").endsWith(StringData("bcdef").substr(0, 4)));
207
ASSERT(!StringData("abcde").endsWith(StringData("bcde", 3)));
208
ASSERT(!StringData("abcde").substr(0, 3).endsWith("cde"));
211
} // unnamed namespace