aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/encode/buffered/string_pool_ut.cpp
blob: 1b360f5cdcd5931e1cc64a8720079f2a58a20a05 (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
#include "string_pool.h"

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

using namespace NMonitoring;

Y_UNIT_TEST_SUITE(TStringPoolTest) { 
    Y_UNIT_TEST(PutIfAbsent) { 
        TStringPoolBuilder strPool;
        strPool.SetSorted(true);

        auto* h1 = strPool.PutIfAbsent("one");
        auto* h2 = strPool.PutIfAbsent("two");
        auto* h3 = strPool.PutIfAbsent("two");
        UNIT_ASSERT(h1 != h2);
        UNIT_ASSERT(h2 == h3);

        UNIT_ASSERT_VALUES_EQUAL(h1->Frequency, 1);
        UNIT_ASSERT_VALUES_EQUAL(h1->Index, 0);

        UNIT_ASSERT_VALUES_EQUAL(h2->Frequency, 2);
        UNIT_ASSERT_VALUES_EQUAL(h2->Index, 1);

        UNIT_ASSERT_VALUES_EQUAL(strPool.BytesSize(), 6);
        UNIT_ASSERT_VALUES_EQUAL(strPool.Count(), 2);
    }

    Y_UNIT_TEST(SortByFrequency) { 
        TStringPoolBuilder strPool;
        strPool.SetSorted(true);

        auto* h1 = strPool.PutIfAbsent("one");
        auto* h2 = strPool.PutIfAbsent("two");
        auto* h3 = strPool.PutIfAbsent("two");
        UNIT_ASSERT(h1 != h2);
        UNIT_ASSERT(h2 == h3);

        strPool.Build();

        UNIT_ASSERT_VALUES_EQUAL(h1->Frequency, 1);
        UNIT_ASSERT_VALUES_EQUAL(h1->Index, 1);

        UNIT_ASSERT_VALUES_EQUAL(h2->Frequency, 2);
        UNIT_ASSERT_VALUES_EQUAL(h2->Index, 0);

        UNIT_ASSERT_VALUES_EQUAL(strPool.BytesSize(), 6);
        UNIT_ASSERT_VALUES_EQUAL(strPool.Count(), 2);
    }

    Y_UNIT_TEST(ForEach) { 
        TStringPoolBuilder strPool;
        strPool.SetSorted(true);

        strPool.PutIfAbsent("one");
        strPool.PutIfAbsent("two");
        strPool.PutIfAbsent("two");
        strPool.PutIfAbsent("three");
        strPool.PutIfAbsent("three");
        strPool.PutIfAbsent("three");

        UNIT_ASSERT_VALUES_EQUAL(strPool.BytesSize(), 11);
        UNIT_ASSERT_VALUES_EQUAL(strPool.Count(), 3);

        strPool.Build();

        TVector<TString> strings;
        TVector<ui32> indexes;
        TVector<ui32> frequences;
        strPool.ForEach([&](TStringBuf str, ui32 index, ui32 freq) {
            strings.emplace_back(str);
            indexes.push_back(index);
            frequences.push_back(freq);
        });

        TVector<TString> expectedStrings = {"three", "two", "one"};
        UNIT_ASSERT_EQUAL(strings, expectedStrings);

        TVector<ui32> expectedIndexes = {0, 1, 2};
        UNIT_ASSERT_EQUAL(indexes, expectedIndexes);

        TVector<ui32> expectedFrequences = {3, 2, 1};
        UNIT_ASSERT_EQUAL(frequences, expectedFrequences);
    }
}