variations_murmur_hash_unittest.cc 2.9 KB

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