Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/variations/variations_murmur_hash.h" |
| 6 | |
| 7 | #include <limits> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "build/build_config.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | #include "third_party/smhasher/src/MurmurHash3.h" |
| 13 | |
| 14 | namespace variations { |
| 15 | namespace internal { |
| 16 | |
| 17 | TEST(VariationsMurmurHashTest, StringToLE32) { |
| 18 | EXPECT_EQ(std::vector<uint32_t>(), |
| 19 | VariationsMurmurHash::StringToLE32("")); |
| 20 | EXPECT_EQ(std::vector<uint32_t>({0x00000061}), |
| 21 | VariationsMurmurHash::StringToLE32("a")); |
| 22 | EXPECT_EQ(std::vector<uint32_t>({0x00006261}), |
| 23 | VariationsMurmurHash::StringToLE32("ab")); |
| 24 | EXPECT_EQ(std::vector<uint32_t>({0x00636261}), |
| 25 | VariationsMurmurHash::StringToLE32("abc")); |
| 26 | EXPECT_EQ(std::vector<uint32_t>({0x64636261}), |
| 27 | VariationsMurmurHash::StringToLE32("abcd")); |
| 28 | EXPECT_EQ(std::vector<uint32_t>({0x64636261, 0x00000065}), |
| 29 | VariationsMurmurHash::StringToLE32("abcde")); |
| 30 | EXPECT_EQ(std::vector<uint32_t>({0x64636261, 0x00006665}), |
| 31 | VariationsMurmurHash::StringToLE32("abcdef")); |
| 32 | } |
| 33 | |
| 34 | // The tests inside this #if compare VariationsMurmurHash to the reference |
| 35 | // implementation, MurmurHash3_x86_32, which only works on little-endian. |
| 36 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 37 | |
| 38 | // Compare VariationsMurmurHash::Hash to MurmurHash3_x86_32 for every prefix of |
| 39 | // |data|, from the empty string to all of |data|. |
| 40 | TEST(VariationsMurmurHashTest, Hash) { |
| 41 | // Random bytes generated manually and hard-coded for reproducability |
| 42 | const std::vector<uint32_t> data({ |
| 43 | 2704264845, 2929902289, 1679431515, 1427187834, 1300338468, |
| 44 | 576307953, 1209988079, 1918627109, 3926412991, 74087765}); |
| 45 | |
| 46 | size_t max_size = data.size() * sizeof(uint32_t); |
| 47 | for (size_t size = 0; size <= max_size; size++) { |
| 48 | uint32_t expected; |
| 49 | MurmurHash3_x86_32(data.data(), size, /*seed=*/0, &expected); |
| 50 | EXPECT_EQ(expected, VariationsMurmurHash::Hash(data, size)) |
| 51 | << "size=" << size; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | TEST(VariationsMurmurHashTest, Hash16) { |
| 56 | // Pick some likely edge case values. |
| 57 | constexpr uint32_t max32 = std::numeric_limits<uint32_t>::max(); |
| 58 | uint32_t seeds[] = { |
| 59 | 0, max32 / 2 - 1, max32 - 2, |
| 60 | 1, max32 / 2, max32 - 1, |
| 61 | 2, max32 / 2 + 1, max32}; |
| 62 | |
| 63 | constexpr uint16_t max16 = std::numeric_limits<uint16_t>::max(); |
| 64 | uint16_t data[] = { |
| 65 | 0, max16 / 2 - 1, max16 - 2, |
| 66 | 1, max16 / 2, max16 - 1, |
| 67 | 2, max16 / 2 + 1, max16}; |
| 68 | |
| 69 | for (uint32_t seed : seeds) { |
| 70 | for (uint16_t datum : data) { |
| 71 | uint32_t expected; |
| 72 | MurmurHash3_x86_32(&datum, sizeof(datum), seed, &expected); |
| 73 | EXPECT_EQ(expected, VariationsMurmurHash::Hash16(seed, datum)) |
| 74 | << "seed=" << seed << ", datum=" << datum; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #endif // defined(ARCH_CPU_LITTLE_ENDIAN) |
| 80 | |
| 81 | } // namespace internal |
| 82 | } // namespace variations |