~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/mongo/base/string_data_test.cpp

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *    Copyright (C) 2012 10gen Inc.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 */
 
16
 
 
17
#include <string>
 
18
 
 
19
#include "mongo/base/string_data.h"
 
20
#include "mongo/unittest/unittest.h"
 
21
 
 
22
namespace {
 
23
 
 
24
    using mongo::StringData;
 
25
    using std::string;
 
26
 
 
27
    TEST(Construction, Empty) {
 
28
        StringData strData;
 
29
        ASSERT_EQUALS(strData.size(), 0U);
 
30
        ASSERT_TRUE(strData.rawData() == NULL);
 
31
    }
 
32
 
 
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);
 
38
    }
 
39
 
 
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);
 
45
    }
 
46
 
 
47
    TEST(Construction, FromNullCString) {
 
48
        char* c = NULL;
 
49
        StringData strData(c);
 
50
        ASSERT_EQUALS(strData.size(), 0U);
 
51
        ASSERT_TRUE(strData.rawData() == NULL);
 
52
    }
 
53
 
 
54
    TEST(Construction, FromLiteral) {
 
55
        StringData strData("ccc", StringData::LiteralTag());
 
56
        ASSERT_EQUALS(strData.size(), 3U);
 
57
        ASSERT_EQUALS(strData.toString(), string("ccc"));
 
58
    }
 
59
 
 
60
    TEST(Comparison, BothEmpty) {
 
61
        StringData empty("");
 
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);
 
68
    }
 
69
 
 
70
    TEST(Comparison, BothNonEmptyOnSize) {
 
71
        StringData a("a");
 
72
        StringData aa("aa");
 
73
        ASSERT_FALSE(a == aa);
 
74
        ASSERT_TRUE(a != aa);
 
75
        ASSERT_FALSE(a > aa);
 
76
        ASSERT_FALSE(a >= aa);
 
77
        ASSERT_TRUE(a >= a);
 
78
        ASSERT_TRUE(a < aa);
 
79
        ASSERT_TRUE(a <= aa);
 
80
        ASSERT_TRUE(a <= a);
 
81
    }
 
82
 
 
83
    TEST(Comparison, BothNonEmptyOnContent) {
 
84
        StringData a("a");
 
85
        StringData b("b");
 
86
        ASSERT_FALSE(a == b);
 
87
        ASSERT_TRUE(a != b);
 
88
        ASSERT_FALSE(a > b);
 
89
        ASSERT_FALSE(a >= b);
 
90
        ASSERT_TRUE(a < b);
 
91
        ASSERT_TRUE(a <= b);
 
92
    }
 
93
 
 
94
    TEST(Comparison, MixedEmptyAndNot) {
 
95
        StringData empty("");
 
96
        StringData a("a");
 
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);
 
103
    }
 
104
 
 
105
    TEST(Find, Char1) {
 
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' ) );
 
109
    }
 
110
 
 
111
    TEST(Find, Str1 ) {
 
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" ) );
 
116
 
 
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" ) );
 
122
 
 
123
        ASSERT_EQUALS( string("foo").find( "" ), StringData("foo").find( "" ) );
 
124
    }
 
125
 
 
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));
 
130
    }
 
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));
 
134
    }
 
135
 
 
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) );
 
140
 
 
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) );
 
144
 
 
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 );
 
158
 
 
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);
 
166
    }
 
167
 
 
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" ) );
 
176
    }
 
177
 
 
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"));
 
192
    }
 
193
 
 
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"));
 
209
    }
 
210
 
 
211
} // unnamed namespace