diff options
| author | Apollon Oikonomopoulos <apoikos@debian.org> | 2016-01-14 00:10:06 +0200 |
|---|---|---|
| committer | Apollon Oikonomopoulos <apollon@skroutz.gr> | 2016-01-14 00:10:06 +0200 |
| commit | 374e1947abcd3e127a2a613aff73ecffdb9199ea (patch) | |
| tree | d83973c3c9802450acd5b5e86fe0d4e8e60a3a1b /src/mongo/base/string_data_test.cpp | |
| parent | 65585c90b12d6523bea75a2aebaae2a2fdf9e641 (diff) | |
Imported Upstream version 2.6.11upstream/2.6.11
Diffstat (limited to 'src/mongo/base/string_data_test.cpp')
| -rw-r--r-- | src/mongo/base/string_data_test.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/mongo/base/string_data_test.cpp b/src/mongo/base/string_data_test.cpp index bbd40011002..dca13e44dba 100644 --- a/src/mongo/base/string_data_test.cpp +++ b/src/mongo/base/string_data_test.cpp @@ -12,9 +12,23 @@ * * 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/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects + * for all of the code used other than as permitted herein. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you do not + * wish to do so, delete this exception statement from your version. If you + * delete this exception statement from all source files in the program, + * then also delete it in the license file. */ +#include <algorithm> #include <string> +#include <vector> #include "mongo/base/string_data.h" #include "mongo/unittest/unittest.h" @@ -123,6 +137,42 @@ namespace { ASSERT_EQUALS( string("foo").find( "" ), StringData("foo").find( "" ) ); } + // Helper function for Test(Hasher, Str1) + template <int SizeofSizeT> + void SDHasher_check(void); + + template <> + void SDHasher_check<4>(void) { + ASSERT_EQUALS(StringData::Hasher()(""), + static_cast<size_t>(0)); + ASSERT_EQUALS(StringData::Hasher()("foo"), + static_cast<size_t>(4138058784ULL)); + ASSERT_EQUALS(StringData::Hasher()("pizza"), + static_cast<size_t>(3587803311ULL)); + ASSERT_EQUALS(StringData::Hasher()("mongo"), + static_cast<size_t>(3724335885ULL)); + ASSERT_EQUALS(StringData::Hasher()("murmur"), + static_cast<size_t>(1945310157ULL)); + } + + template <> + void SDHasher_check<8>(void) { + ASSERT_EQUALS(StringData::Hasher()(""), + static_cast<size_t>(0)); + ASSERT_EQUALS(StringData::Hasher()("foo"), + static_cast<size_t>(16316970633193145697ULL)); + ASSERT_EQUALS(StringData::Hasher()("pizza"), + static_cast<size_t>(12165495155477134356ULL)); + ASSERT_EQUALS(StringData::Hasher()("mongo"), + static_cast<size_t>(2861051452199491487ULL)); + ASSERT_EQUALS(StringData::Hasher()("murmur"), + static_cast<size_t>(18237957392784716687ULL)); + } + + TEST(Hasher, Str1) { + SDHasher_check<sizeof(size_t)>(); + } + TEST(Rfind, Char1) { ASSERT_EQUALS( string::npos, StringData( "foo" ).rfind( 'a' ) ); @@ -224,4 +274,47 @@ namespace { ASSERT(!StringData("abcde").substr(0, 3).endsWith("cde")); } + TEST(ConstIterator, StdCopy) { + std::vector<char> chars; + const char rawData[] = "This is some raw data."; + StringData data(rawData, StringData::LiteralTag()); + + chars.resize(data.size()); + std::copy(data.begin(), data.end(), chars.begin()); + + for (size_t i = 0; i < data.size(); ++i) { + ASSERT_EQUALS(data[i], chars[i]); + } + } + + TEST(ConstIterator, StdReverseCopy) { + std::vector<char> chars; + const char rawData[] = "This is some raw data."; + StringData data(rawData, StringData::LiteralTag()); + + chars.resize(data.size()); + std::reverse_copy(data.begin(), data.end(), chars.begin()); + + const char rawDataExpected[] = ".atad war emos si sihT"; + + for (size_t i = 0; i < data.size(); ++i) { + ASSERT_EQUALS(rawDataExpected[i], chars[i]); + } + } + + TEST(ConstIterator, StdReplaceCopy) { + std::vector<char> chars; + const char rawData[] = "This is some raw data."; + StringData data(rawData, StringData::LiteralTag()); + + chars.resize(data.size()); + std::replace_copy(data.begin(), data.end(), chars.begin(), ' ', '_'); + + const char rawDataExpected[] = "This_is_some_raw_data."; + + for (size_t i = 0; i < data.size(); ++i) { + ASSERT_EQUALS(rawDataExpected[i], chars[i]); + } + } + } // unnamed namespace |
