summaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/concurrent_hash/concurrent_hash_ut.cpp
blob: 6427adf92c690d7cb62e6a98d22486fba1f3a5fc (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "concurrent_hash.h"

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

#include <util/generic/noncopyable.h>
#include <util/generic/ptr.h>
#include <util/generic/string.h>
#include <util/string/cast.h>

TEST(TConcurrentHashTest, TEmptyGetTest) {
    TConcurrentHashMap<TString, ui32> h;

    EXPECT_FALSE(h.Has("key"));

    ui32 res = 100;
    EXPECT_FALSE(h.Get("key", res));
    EXPECT_EQ(res, 100);

    // Can't check h.Get("key") here because it has Y_ABORT_UNLESS inside o_O
}

TEST(TConcurrentHashTest, TInsertTest) {
    TConcurrentHashMap<TString, ui32> h;

    h.Insert("key1", 1);
    h.Insert("key2", 2);

    EXPECT_EQ(h.Get("key1"), 1);
    EXPECT_EQ(h.Get("key2"), 2);

    ui32 res = 100;
    EXPECT_TRUE(h.Has("key1"));
    EXPECT_TRUE(h.Get("key1", res));
    EXPECT_EQ(res, 1);

    EXPECT_TRUE(h.Has("key2"));
    EXPECT_TRUE(h.Get("key2", res));
    EXPECT_EQ(res, 2);

    EXPECT_FALSE(h.Has("key3"));
    EXPECT_FALSE(h.Get("key3", res));
    EXPECT_EQ(res, 2);
}

TEST(TConcurrentHashTest, TInsertIfAbsentTest) {
    TConcurrentHashMap<TString, ui32> h;

    ui32 res;
    EXPECT_FALSE(h.Has("key"));
    EXPECT_FALSE(h.Get("key", res));

    EXPECT_EQ(h.InsertIfAbsent("key", 1), static_cast<ui32>(1));
    EXPECT_EQ(h.Get("key"), 1);

    EXPECT_EQ(h.InsertIfAbsent("key", 2), static_cast<ui32>(1));
    EXPECT_EQ(h.Get("key"), 1);
}

TEST(TConcurrentHashTest, TInsertIfAbsentTestFunc) {
    TConcurrentHashMap<TString, ui32> h;

    bool initialized = false;
    auto f = [&initialized]() {
        initialized = true;
        return static_cast<ui32>(1);
    };

    ui32 res = 0;
    EXPECT_FALSE(h.Get("key", res));
    EXPECT_EQ(res, 0);

    EXPECT_EQ(h.InsertIfAbsentWithInit("key", f), 1);
    EXPECT_EQ(h.Get("key"), 1);
    EXPECT_TRUE(initialized);

    initialized = false;
    EXPECT_EQ(h.InsertIfAbsentWithInit("key", f), 1);
    EXPECT_EQ(h.Get("key"), 1);
    EXPECT_FALSE(initialized);

    h.Insert("key", 2);
    EXPECT_EQ(h.InsertIfAbsentWithInit("key", f), 2);
    EXPECT_EQ(h.Get("key"), 2);
    EXPECT_FALSE(initialized);
}


TEST(TConcurrentHashTest, TEmplaceIfAbsentTest) {
    struct TBadConstructor{};

    // InsertIfAbsent cannot be uses for noncopyable and nonmovable types (e.g. atomics or structs with atomic members)
    struct TFoo : public TNonCopyable {
        explicit TFoo(int value)
            : Value(value)
        {}

        explicit TFoo(TBadConstructor) {
            ythrow yexception{} << "THis constructor must not be called";
        }

        int Value;
    };

    TConcurrentHashMap<TString, TFoo> h;

    EXPECT_FALSE(h.Has("key"));

    EXPECT_EQ(h.EmplaceIfAbsent("key", 123).Value, 123);
    EXPECT_TRUE(h.Has("key"));

    // If the key already exists, the value must not be constructed
    EXPECT_EQ(h.EmplaceIfAbsent("key", TBadConstructor{}).Value, 123);
}

TEST(TConcurrentHashTest, TRemoveTest) {
    TConcurrentHashMap<TString, ui32> h;

    EXPECT_FALSE(h.Has("key1"));
    EXPECT_FALSE(h.Has("key2"));
    EXPECT_FALSE(h.Has("key3"));

    h.Insert("key1", 1);
    EXPECT_TRUE(h.Has("key1"));
    EXPECT_FALSE(h.Has("key2"));
    EXPECT_FALSE(h.Has("key3"));
    EXPECT_EQ(h.Get("key1"), 1);

    h.Remove("key1");
    EXPECT_FALSE(h.Has("key1"));
    EXPECT_FALSE(h.Has("key2"));
    EXPECT_FALSE(h.Has("key3"));

    h.Insert("key1", 1);
    h.Insert("key2", 2);

    EXPECT_TRUE(h.Has("key1"));
    EXPECT_TRUE(h.Has("key2"));
    EXPECT_FALSE(h.Has("key3"));
    EXPECT_EQ(h.Get("key1"), 1);
    EXPECT_EQ(h.Get("key2"), 2);

    h.Remove("key2");
    EXPECT_TRUE(h.Has("key1"));
    EXPECT_FALSE(h.Has("key2"));
    EXPECT_FALSE(h.Has("key3"));
    EXPECT_EQ(h.Get("key1"), 1);
}

TEST(TConcurrentHashTest, TTryRemoveTest) {
    TConcurrentHashMap<TString, ui32> h;

    EXPECT_FALSE(h.Has("key"));

    ui32 res;
    EXPECT_FALSE(h.TryRemove("key", res));

    h.Insert("key", 1);
    EXPECT_TRUE(h.Has("key"));
    EXPECT_TRUE(h.TryRemove("key", res));
    EXPECT_EQ(res, 1);
    EXPECT_FALSE(h.TryRemove("key", res));
}

TEST(TConcurrentHashTest, TExchangeTest) {
    struct TValue: TThrRefBase {
        TValue(int v)
            : Value(v)
        {
        }

        int Value;
    };

    using TValuePtr = TIntrusivePtr<TValue>;

    TConcurrentHashMap<int, TValuePtr> h;

    TValuePtr v = MakeIntrusive<TValue>(123);
    h.Exchange(1, v);
    EXPECT_EQ(v, nullptr);

    v = MakeIntrusive<TValue>(456);
    h.Exchange(1, v);
    EXPECT_EQ(v->RefCount(), 1);
    EXPECT_EQ(v->Value, 123);
    EXPECT_EQ(h.Get(1)->Value, 456);
}

TEST(TConcurrentHashTest, TGetBucketTest) {
    TConcurrentHashMap<TString, ui32> h;

    for (int i = 0; i < 100; ++i) {
        TString key = ToString(i);
        auto& bucket1 = h.GetBucketForKey(key);
        auto& bucket2 = h.GetBucketForKey(TStringBuf(key));
        EXPECT_EQ(&bucket1, &bucket2);
    }
}