aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/small_containers/unittests/compact_set_ut.cpp
blob: 17b96a21954bda1ed9d85c1995f41d984e782596 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
//===- llvm/unittest/ADT/SmallSetTest.cpp ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// CompactSet unit tests.
//
//===----------------------------------------------------------------------===//

#include <library/cpp/yt/small_containers/compact_set.h>

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

#include <string>

namespace NYT {
namespace {

////////////////////////////////////////////////////////////////////////////////

TEST(TCompactSetTest, Insert)
{
    TCompactSet<int, 4> s1;

    for (int i = 0; i < 4; i++)
        s1.insert(i);

    for (int i = 0; i < 4; i++)
        s1.insert(i);

    EXPECT_EQ(4u, s1.size());

    for (int i = 0; i < 4; i++)
        EXPECT_EQ(1u, s1.count(i));

    EXPECT_EQ(0u, s1.count(4));
}

TEST(TCompactSetTest, Grow)
{
    TCompactSet<int, 4> s1;

    for (int i = 0; i < 8; i++)
        s1.insert(i);

    EXPECT_EQ(8u, s1.size());

    for (int i = 0; i < 8; i++)
        EXPECT_EQ(1u, s1.count(i));

    EXPECT_EQ(0u, s1.count(8));
}

TEST(TCompactSetTest, Erase)
{
    TCompactSet<int, 4> s1;

    for (int i = 0; i < 8; i++)
        s1.insert(i);

    EXPECT_EQ(8u, s1.size());

    // Remove elements one by one and check if all other elements are still there.
    for (int i = 0; i < 8; i++) {
        EXPECT_EQ(1u, s1.count(i));
        EXPECT_TRUE(s1.erase(i));
        EXPECT_EQ(0u, s1.count(i));
        EXPECT_EQ(8u - i - 1, s1.size());
        for (int j = i + 1; j < 8; j++)
            EXPECT_EQ(1u, s1.count(j));
    }

    EXPECT_EQ(0u, s1.count(8));
}

TEST(TCompactSetTest, IteratorInt)
{
    TCompactSet<int, 4> s1;

    // Test the 'small' case.
    for (int i = 0; i < 3; i++)
        s1.insert(i);

    std::vector<int> V(s1.begin(), s1.end());
    // Make sure the elements are in the expected order.
    std::sort(V.begin(), V.end());
    for (int i = 0; i < 3; i++)
        EXPECT_EQ(i, V[i]);

    // Test the 'big' case by adding a few more elements to switch to std::set
    // internally.
    for (int i = 3; i < 6; i++)
        s1.insert(i);

    V.assign(s1.begin(), s1.end());
    // Make sure the elements are in the expected order.
    std::sort(V.begin(), V.end());
    for (int i = 0; i < 6; i++)
        EXPECT_EQ(i, V[i]);
}

TEST(TCompactSetTest, IteratorString)
{
    // Test CompactSetIterator for TCompactSet with a type with non-trivial
    // ctors/dtors.
    TCompactSet<std::string, 2> s1;

    s1.insert("str 1");
    s1.insert("str 2");
    s1.insert("str 1");

    std::vector<std::string> V(s1.begin(), s1.end());
    std::sort(V.begin(), V.end());
    EXPECT_EQ(2u, s1.size());
    EXPECT_EQ("str 1", V[0]);
    EXPECT_EQ("str 2", V[1]);

    s1.insert("str 4");
    s1.insert("str 0");
    s1.insert("str 4");

    V.assign(s1.begin(), s1.end());
    // Make sure the elements are in the expected order.
    std::sort(V.begin(), V.end());
    EXPECT_EQ(4u, s1.size());
    EXPECT_EQ("str 0", V[0]);
    EXPECT_EQ("str 1", V[1]);
    EXPECT_EQ("str 2", V[2]);
    EXPECT_EQ("str 4", V[3]);
}

TEST(TCompactSetTest, IteratorIncMoveCopy)
{
    // Test CompactSetIterator for TCompactSet with a type with non-trivial
    // ctors/dtors.
    TCompactSet<std::string, 2> s1;

    s1.insert("str 1");
    s1.insert("str 2");

    auto Iter = s1.begin();
    EXPECT_EQ("str 1", *Iter);
    ++Iter;
    EXPECT_EQ("str 2", *Iter);

    s1.insert("str 4");
    s1.insert("str 0");
    auto Iter2 = s1.begin();
    Iter = std::move(Iter2);
    EXPECT_EQ("str 0", *Iter);
}

// These test weren't taken from llvm.

TEST(TCompactSetTest, Empty)
{
    TCompactSet<int, 10> v;
    EXPECT_TRUE(v.empty());

    auto data = {1, 2, 3, 4, 5};

    v.insert(data.begin(), data.end()); // not crossing size threshold
    v.erase(4);
    v.erase(2);
    v.erase(3);
    v.erase(5);
    v.erase(1);
    EXPECT_TRUE(v.empty());

    auto data2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    v.insert(data2.begin(), data2.end()); // crossing size threshold
    v.erase(7);
    v.erase(3);
    v.erase(1);
    v.erase(10);
    v.erase(9);
    v.erase(0);
    v.erase(2);
    v.erase(6);
    v.erase(4);
    v.erase(5);
    v.erase(8);
    EXPECT_TRUE(v.empty());
}

TEST(TCompactSetTest, ForEach)
{
    TCompactSet<int, 10> v;

    auto data = {10, 9, 3, 4, 1, 5, 6, 8};

    v.insert(data.begin(), data.end());

    std::vector<int> buf(v.begin(), v.end());
    std::vector<int> expected{1, 3, 4, 5, 6, 8, 9, 10};
    EXPECT_EQ(expected, buf);

    auto data2 = {7, 1, 2, 0};
    v.insert(data2.begin(), data2.end());
    buf.assign(v.begin(), v.end());
    expected = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    EXPECT_EQ(expected, buf);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace
} // namespace NYT