/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include "folly/Hash.h"
-#include "folly/MapUtil.h"
+#include <folly/Hash.h>
+#include <folly/MapUtil.h>
#include <gtest/gtest.h>
#include <stdint.h>
#include <unordered_map>
TEST(Hash, Fnv32) {
const char* s1 = "hello, world!";
- const uint32_t s1_res = 3180823791ul;
+ const uint32_t s1_res = 3605494790UL;
EXPECT_EQ(fnv32(s1), s1_res);
EXPECT_EQ(fnv32(s1), fnv32_buf(s1, strlen(s1)));
const char* s2 = "monkeys! m0nk3yz! ev3ry \\/\\/here~~~~";
- const uint32_t s2_res = 194407565ul;
+ const uint32_t s2_res = 1270448334UL;
EXPECT_EQ(fnv32(s2), s2_res);
EXPECT_EQ(fnv32(s2), fnv32_buf(s2, strlen(s2)));
const char* s3 = "";
- const uint32_t s3_res = 216613626ul;
+ const uint32_t s3_res = 2166136261UL;
EXPECT_EQ(fnv32(s3), s3_res);
EXPECT_EQ(fnv32(s3), fnv32_buf(s3, strlen(s3)));
}
m[t] = "bar";
EXPECT_EQ("bar", m[t]);
}
+
+namespace {
+template <class T>
+size_t hash_vector(const std::vector<T>& v) {
+ return hash_range(v.begin(), v.end());
+}
+}
+
+TEST(Hash, hash_range) {
+ EXPECT_EQ(hash_vector<int32_t>({1, 2}), hash_vector<int16_t>({1, 2}));
+ EXPECT_NE(hash_vector<int>({2, 1}), hash_vector<int>({1, 2}));
+ EXPECT_EQ(hash_vector<int>({}), hash_vector<float>({}));
+}
+
+TEST(Hash, std_tuple_different_hash) {
+ typedef std::tuple<int64_t, std::string, int32_t> tuple3;
+ tuple3 t1(42, "foo", 1);
+ tuple3 t2(9, "bar", 3);
+ tuple3 t3(42, "foo", 3);
+
+ EXPECT_NE(std::hash<tuple3>()(t1),
+ std::hash<tuple3>()(t2));
+ EXPECT_NE(std::hash<tuple3>()(t1),
+ std::hash<tuple3>()(t3));
+}