aboutsummaryrefslogtreecommitdiffstats
path: root/util/digest/murmur_ut.cpp
blob: 29287668bc4394258e9aaf1423e7b944251dd102 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "murmur.h"

#include <library/cpp/testing/unittest/registar.h>

class TMurmurHashTest: public TTestBase {
    UNIT_TEST_SUITE(TMurmurHashTest);
    UNIT_TEST(TestHash32)
    UNIT_TEST(TestUnalignedHash32)
    UNIT_TEST(TestHash64)
    UNIT_TEST(TestUnalignedHash64)
    UNIT_TEST(TestWrapperBiggerTypes)
    UNIT_TEST_SUITE_END();

private:
    inline void TestHash32() {
        ui8 buf[256];

        for (size_t i = 0; i < 256; ++i) {
            buf[i] = i;
        }

        Test<ui32>(buf, 256, 2373126550UL);
        TestWrapper<ui8, ui32>({buf, buf + 256}, 2373126550UL);
        Test<ui32>(buf, 255, 3301607533UL);
        Test<ui32>(buf, 254, 2547410121UL);
        Test<ui32>(buf, 253, 80030810UL);
    }

    inline void TestUnalignedHash32() {
        ui8 buf[257];
        ui8* unalignedBuf = buf + 1;

        for (size_t i = 0; i < 256; ++i) {
            unalignedBuf[i] = i;
        }

        Test<ui32>(unalignedBuf, 256, 2373126550UL);
    }

    inline void TestHash64() {
        ui8 buf[256];

        for (size_t i = 0; i < 256; ++i) {
            buf[i] = i;
        }

        Test<ui64>(buf, 256, ULL(12604435678857905857));
        TestWrapper<ui8, ui64>({buf, buf + 256}, ULL(12604435678857905857));
        Test<ui64>(buf, 255, ULL(1708835094528446095));
        Test<ui64>(buf, 254, ULL(5077937678736514994));
        Test<ui64>(buf, 253, ULL(11553864555081396353));
    }

    inline void TestUnalignedHash64() {
        ui8 buf[257];
        ui8* unalignedBuf = buf + 1;

        for (size_t i = 0; i < 256; ++i) {
            unalignedBuf[i] = i;
        }

        Test<ui64>(unalignedBuf, 256, ULL(12604435678857905857));
    }

    inline void TestWrapperBiggerTypes() {
        ui32 buf[] = {24, 42};
        TestWrapper<ui32, ui32>({buf, buf + 2}, MurmurHash<ui32>(buf, sizeof(ui32) * 2));
        TestWrapper<ui32, ui64>({buf, buf + 2}, MurmurHash<ui64>(buf, sizeof(ui32) * 2));
    }

private:
    template <class T>
    inline void Test(const void* data, size_t len, T expected) {
        UNIT_ASSERT_STRINGS_EQUAL(ToString(MurmurHash<T>(data, len)), ToString(expected));
    }

    template <class E, class T>
    inline void TestWrapper(const TArrayRef<E>& array, T expected) {
        auto val = TMurmurHash<T>()(array);
        UNIT_ASSERT_EQUAL(val, expected);
    }
};

UNIT_TEST_SUITE_REGISTRATION(TMurmurHashTest);