murmur_ut.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "murmur.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. class TMurmurHashTest: public TTestBase {
  4. private:
  5. UNIT_TEST_SUITE(TMurmurHashTest);
  6. UNIT_TEST(TestHash2A32)
  7. UNIT_TEST(TestHash2A64)
  8. UNIT_TEST(TestUnaligned)
  9. UNIT_TEST_SUITE_END();
  10. private:
  11. inline void TestHash2A32() {
  12. ui8 buf[256];
  13. for (size_t i = 0; i < 256; ++i) {
  14. buf[i] = i;
  15. }
  16. Test2A<ui32>(buf, 256, 0, 97, 178525084UL);
  17. Test2A<ui32>(buf, 256, 128, 193, 178525084UL);
  18. Test2A<ui32>(buf, 255, 0, 97, 2459858906UL);
  19. Test2A<ui32>(buf, 255, 128, 193, 2459858906UL);
  20. }
  21. inline void TestHash2A64() {
  22. ui8 buf[256];
  23. for (size_t i = 0; i < 256; ++i) {
  24. buf[i] = i;
  25. }
  26. Test2A<ui64>(buf, 256, 0, 97, ULL(15099340606808450747));
  27. Test2A<ui64>(buf, 256, 128, 193, ULL(15099340606808450747));
  28. Test2A<ui64>(buf, 255, 0, 97, ULL(8331973280124075880));
  29. Test2A<ui64>(buf, 255, 128, 193, ULL(8331973280124075880));
  30. }
  31. inline void TestUnaligned() {
  32. ui8 buf[257];
  33. for (size_t i = 0; i < 256; ++i) {
  34. buf[i + 1] = i;
  35. }
  36. Test2A<ui64>(buf + 1, 256, 0, 97, ULL(15099340606808450747));
  37. Test2A<ui64>(buf + 1, 256, 128, 193, ULL(15099340606808450747));
  38. Test2A<ui64>(buf + 1, 255, 0, 97, ULL(8331973280124075880));
  39. Test2A<ui64>(buf + 1, 255, 128, 193, ULL(8331973280124075880));
  40. }
  41. private:
  42. template <class T>
  43. static inline void Test2A(const ui8* data, size_t len, size_t split1, size_t split2, T expected) {
  44. const T value1 = TMurmurHash2A<T>().Update(data, split1).Update(data + split1, len - split1).Value();
  45. const T value2 = TMurmurHash2A<T>().Update(data, split2).Update(data + split2, len - split2).Value();
  46. UNIT_ASSERT_VALUES_EQUAL(value1, value2);
  47. UNIT_ASSERT_VALUES_EQUAL(value1, expected);
  48. }
  49. };
  50. UNIT_TEST_SUITE_REGISTRATION(TMurmurHashTest);