murmur_ut.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "murmur.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. class TMurmurHashTest: public TTestBase {
  4. UNIT_TEST_SUITE(TMurmurHashTest);
  5. UNIT_TEST(TestHash32)
  6. UNIT_TEST(TestUnalignedHash32)
  7. UNIT_TEST(TestHash64)
  8. UNIT_TEST(TestUnalignedHash64)
  9. UNIT_TEST(TestWrapperBiggerTypes)
  10. UNIT_TEST_SUITE_END();
  11. private:
  12. inline void TestHash32() {
  13. ui8 buf[256];
  14. for (size_t i = 0; i < 256; ++i) {
  15. buf[i] = i;
  16. }
  17. Test<ui32>(buf, 256, 2373126550UL);
  18. TestWrapper<ui8, ui32>({buf, buf + 256}, 2373126550UL);
  19. Test<ui32>(buf, 255, 3301607533UL);
  20. Test<ui32>(buf, 254, 2547410121UL);
  21. Test<ui32>(buf, 253, 80030810UL);
  22. }
  23. inline void TestUnalignedHash32() {
  24. ui8 buf[257];
  25. ui8* unalignedBuf = buf + 1;
  26. for (size_t i = 0; i < 256; ++i) {
  27. unalignedBuf[i] = i;
  28. }
  29. Test<ui32>(unalignedBuf, 256, 2373126550UL);
  30. }
  31. inline void TestHash64() {
  32. ui8 buf[256];
  33. for (size_t i = 0; i < 256; ++i) {
  34. buf[i] = i;
  35. }
  36. Test<ui64>(buf, 256, ULL(12604435678857905857));
  37. TestWrapper<ui8, ui64>({buf, buf + 256}, ULL(12604435678857905857));
  38. Test<ui64>(buf, 255, ULL(1708835094528446095));
  39. Test<ui64>(buf, 254, ULL(5077937678736514994));
  40. Test<ui64>(buf, 253, ULL(11553864555081396353));
  41. }
  42. inline void TestUnalignedHash64() {
  43. ui8 buf[257];
  44. ui8* unalignedBuf = buf + 1;
  45. for (size_t i = 0; i < 256; ++i) {
  46. unalignedBuf[i] = i;
  47. }
  48. Test<ui64>(unalignedBuf, 256, ULL(12604435678857905857));
  49. }
  50. inline void TestWrapperBiggerTypes() {
  51. ui32 buf[] = {24, 42};
  52. TestWrapper<ui32, ui32>({buf, buf + 2}, MurmurHash<ui32>(buf, sizeof(ui32) * 2));
  53. TestWrapper<ui32, ui64>({buf, buf + 2}, MurmurHash<ui64>(buf, sizeof(ui32) * 2));
  54. }
  55. private:
  56. template <class T>
  57. inline void Test(const void* data, size_t len, T expected) {
  58. UNIT_ASSERT_STRINGS_EQUAL(ToString(MurmurHash<T>(data, len)), ToString(expected));
  59. }
  60. template <class E, class T>
  61. inline void TestWrapper(const TArrayRef<E>& array, T expected) {
  62. auto val = TMurmurHash<T>()(array);
  63. UNIT_ASSERT_EQUAL(val, expected);
  64. }
  65. };
  66. UNIT_TEST_SUITE_REGISTRATION(TMurmurHashTest);