aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/minsketch/ut/count_min_sketch_ut.cpp
blob: a8b0bc46dde3eabceee0bb7e94a8a7f6f68b50c7 (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
#include "count_min_sketch.h"

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

namespace NKikimr {

Y_UNIT_TEST_SUITE(CountMinSketch) {

    Y_UNIT_TEST(CountAndProbe) {
        std::unique_ptr<TCountMinSketch> countMin(TCountMinSketch::Create(256, 8));

        TString str1("foo");
        countMin->Count(str1.data(), str1.size());
        auto probe1 = countMin->Probe(str1.data(), str1.size());

        TString str2("bar");
        for (size_t i = 0; i < 4; ++i) {
            countMin->Count(str2.data(), str2.size());
        }
        auto probe2 = countMin->Probe(str2.data(), str2.size());

        ui32 integer1 = 1234567890U;
        countMin->Count((const char *)&integer1, sizeof(ui32));
        auto probe3 = countMin->Probe((const char *)&integer1, sizeof(ui32));

        ui64 integer2 = 1234567890ULL;
        countMin->Count((const char *)&integer2, sizeof(ui64));
        auto probe4 = countMin->Probe((const char *)&integer2, sizeof(ui64));

        ui64 integer3 = 1234512345ULL;
        auto probe5 = countMin->Probe((const char *)&integer3, sizeof(ui64));

        UNIT_ASSERT_VALUES_EQUAL(probe1, 1);
        UNIT_ASSERT_VALUES_EQUAL(probe2, 4);
        UNIT_ASSERT_VALUES_EQUAL(probe3, 1);
        UNIT_ASSERT_VALUES_EQUAL(probe4, 1);
        UNIT_ASSERT_VALUES_EQUAL(probe5, 0);

        UNIT_ASSERT_VALUES_EQUAL(countMin->GetElementCount(), 7);
    }

    Y_UNIT_TEST(Add) {
        std::unique_ptr<TCountMinSketch> countMinA(TCountMinSketch::Create(256, 8));
        std::unique_ptr<TCountMinSketch> countMinB(TCountMinSketch::Create(256, 8));

        TString str("foo");
        countMinA->Count(str.data(), str.size());

        ui32 integer = 0;
        countMinB->Count((const char *)&integer, sizeof(ui32));

        *countMinA += *countMinB;

        auto probe1 = countMinA->Probe(str.data(), str.size());
        auto probe2 = countMinA->Probe((const char *)&integer, sizeof(ui32));

        UNIT_ASSERT_VALUES_EQUAL(probe1, 1);
        UNIT_ASSERT_VALUES_EQUAL(probe2, 1);
    }

}

} // NKikimr