diff options
author | Alberto Lerner <alerner@10gen.com> | 2012-11-14 15:56:30 -0500 |
---|---|---|
committer | Alberto Lerner <alerner@10gen.com> | 2012-11-15 13:56:02 -0500 |
commit | 28fec164384288a10815c34855dd7e867fbfef4a (patch) | |
tree | 92b9f49b397471f4cf947e8f4a92c39d706e8a81 /src/mongo/base/string_data_test.cpp | |
parent | 3266ccff2ba64391f574bc3f35c11f132642bd8a (diff) | |
download | mongo-28fec164384288a10815c34855dd7e867fbfef4a.tar.gz |
SERVER-7172 Added comparison support in StringData.
Diffstat (limited to 'src/mongo/base/string_data_test.cpp')
-rw-r--r-- | src/mongo/base/string_data_test.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/mongo/base/string_data_test.cpp b/src/mongo/base/string_data_test.cpp new file mode 100644 index 00000000000..4932d2b2b55 --- /dev/null +++ b/src/mongo/base/string_data_test.cpp @@ -0,0 +1,92 @@ +/** + * Copyright (C) 2012 10gen Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <string> + +#include "mongo/base/string_data.h" +#include "mongo/unittest/unittest.h" + +namespace { + + using mongo::StringData; + using std::string; + + TEST(Construction, FromStdString) { + std::string base("aaa"); + StringData strData(base); + ASSERT_EQUALS(strData.size(), base.size()); + ASSERT_EQUALS(strData.toString(), base); + } + + TEST(Construction, FromCString) { + std::string base("aaa"); + StringData strData(base.c_str()); + ASSERT_EQUALS(strData.size(), base.size()); + ASSERT_EQUALS(strData.toString(), base); + } + + TEST(Construction, FromLiteral) { + StringData strData("ccc", StringData::LiteralTag()); + ASSERT_EQUALS(strData.size(), 3U); + ASSERT_EQUALS(strData.toString(), string("ccc")); + } + + TEST(Comparison, BothEmpty) { + StringData empty(""); + ASSERT_TRUE(empty == empty); + ASSERT_FALSE(empty != empty); + ASSERT_FALSE(empty > empty); + ASSERT_TRUE(empty >= empty); + ASSERT_FALSE(empty < empty); + ASSERT_TRUE(empty <= empty); + } + + TEST(Comparison, BothNonEmptyOnSize) { + StringData a("a"); + StringData aa("aa"); + ASSERT_FALSE(a == aa); + ASSERT_TRUE(a != aa); + ASSERT_FALSE(a > aa); + ASSERT_FALSE(a >= aa); + ASSERT_TRUE(a >= a); + ASSERT_TRUE(a < aa); + ASSERT_TRUE(a <= aa); + ASSERT_TRUE(a <= a); + } + + TEST(Comparison, BothNonEmptyOnContent) { + StringData a("a"); + StringData b("b"); + ASSERT_FALSE(a == b); + ASSERT_TRUE(a != b); + ASSERT_FALSE(a > b); + ASSERT_FALSE(a >= b); + ASSERT_TRUE(a < b); + ASSERT_TRUE(a <= b); + } + + TEST(Comparison, MixedEmptyAndNot) { + StringData empty(""); + StringData a("a"); + ASSERT_FALSE(a == empty); + ASSERT_TRUE(a != empty); + ASSERT_TRUE(a > empty); + ASSERT_TRUE(a >= empty); + ASSERT_FALSE(a < empty); + ASSERT_FALSE(a <= empty); + } + +} // unnamed namespace |