summaryrefslogtreecommitdiff
path: root/src/mongo/base/string_data_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/base/string_data_test.cpp')
-rw-r--r--src/mongo/base/string_data_test.cpp93
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