aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/ADT/SparseMultiSetTest.cpp
blob: 032990e4bcd657538525c7e5b975b41cdb856a00 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//===------ ADT/SparseSetTest.cpp - SparseSet unit tests -  -----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/SparseMultiSet.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

typedef SparseMultiSet<unsigned> USet;

// Empty set tests.
TEST(SparseMultiSetTest, EmptySet) {
  USet Set;
  EXPECT_TRUE(Set.empty());
  EXPECT_EQ(0u, Set.size());

  Set.setUniverse(10);

  // Lookups on empty set.
  EXPECT_TRUE(Set.find(0) == Set.end());
  EXPECT_TRUE(Set.find(9) == Set.end());

  // Same thing on a const reference.
  const USet &CSet = Set;
  EXPECT_TRUE(CSet.empty());
  EXPECT_EQ(0u, CSet.size());
  EXPECT_TRUE(CSet.find(0) == CSet.end());
  USet::const_iterator I = CSet.find(5);
  EXPECT_TRUE(I == CSet.end());
}

// Single entry set tests.
TEST(SparseMultiSetTest, SingleEntrySet) {
  USet Set;
  Set.setUniverse(10);
  USet::iterator I = Set.insert(5);
  EXPECT_TRUE(I != Set.end());
  EXPECT_TRUE(*I == 5);

  EXPECT_FALSE(Set.empty());
  EXPECT_EQ(1u, Set.size());

  EXPECT_TRUE(Set.find(0) == Set.end());
  EXPECT_TRUE(Set.find(9) == Set.end());

  EXPECT_FALSE(Set.contains(0));
  EXPECT_TRUE(Set.contains(5));

  // Extra insert.
  I = Set.insert(5);
  EXPECT_TRUE(I != Set.end());
  EXPECT_TRUE(I == ++Set.find(5));
  I--;
  EXPECT_TRUE(I == Set.find(5));

  // Erase non-existent element.
  I = Set.find(1);
  EXPECT_TRUE(I == Set.end());
  EXPECT_EQ(2u, Set.size());
  EXPECT_EQ(5u, *Set.find(5));

  // Erase iterator.
  I = Set.find(5);
  EXPECT_TRUE(I != Set.end());
  I = Set.erase(I);
  EXPECT_TRUE(I != Set.end());
  I = Set.erase(I);
  EXPECT_TRUE(I == Set.end());
  EXPECT_TRUE(Set.empty());
}

// Multiple entry set tests.
TEST(SparseMultiSetTest, MultipleEntrySet) {
  USet Set;
  Set.setUniverse(10);

  Set.insert(5);
  Set.insert(5);
  Set.insert(5);
  Set.insert(3);
  Set.insert(2);
  Set.insert(1);
  Set.insert(4);
  EXPECT_EQ(7u, Set.size());

  // Erase last element by key.
  EXPECT_TRUE(Set.erase(Set.find(4)) == Set.end());
  EXPECT_EQ(6u, Set.size());
  EXPECT_FALSE(Set.contains(4));
  EXPECT_TRUE(Set.find(4) == Set.end());

  // Erase first element by key.
  EXPECT_EQ(3u, Set.count(5));
  EXPECT_TRUE(Set.find(5) != Set.end());
  EXPECT_TRUE(Set.erase(Set.find(5)) != Set.end());
  EXPECT_EQ(5u, Set.size());
  EXPECT_EQ(2u, Set.count(5));

  Set.insert(6);
  Set.insert(7);
  EXPECT_EQ(7u, Set.size());

  // Erase tail by iterator.
  EXPECT_TRUE(Set.getTail(6) == Set.getHead(6));
  USet::iterator I = Set.erase(Set.find(6));
  EXPECT_TRUE(I == Set.end());
  EXPECT_EQ(6u, Set.size());

  // Erase tails by iterator.
  EXPECT_EQ(2u, Set.count(5));
  I = Set.getTail(5);
  I = Set.erase(I);
  EXPECT_TRUE(I == Set.end());
  --I;
  EXPECT_EQ(1u, Set.count(5));
  EXPECT_EQ(5u, *I);
  I = Set.erase(I);
  EXPECT_TRUE(I == Set.end());
  EXPECT_EQ(0u, Set.count(5));

  Set.insert(8);
  Set.insert(8);
  Set.insert(8);
  Set.insert(8);
  Set.insert(8);

  // Erase all the 8s
  EXPECT_EQ(5, std::distance(Set.getHead(8), Set.end()));
  Set.eraseAll(8);
  EXPECT_EQ(0, std::distance(Set.getHead(8), Set.end()));

  // Clear and resize the universe.
  Set.clear();
  EXPECT_EQ(0u, Set.size());
  EXPECT_FALSE(Set.contains(3));
  Set.setUniverse(1000);

  // Add more than 256 elements.
  for (unsigned i = 100; i != 800; ++i)
    Set.insert(i);

  for (unsigned i = 0; i != 10; ++i)
    Set.eraseAll(i);

  for (unsigned i = 100; i != 800; ++i)
    EXPECT_EQ(1u, Set.count(i));

  EXPECT_FALSE(Set.contains(99));
  EXPECT_FALSE(Set.contains(800));
  EXPECT_EQ(700u, Set.size());
}

// Test out iterators
TEST(SparseMultiSetTest, Iterators) {
  USet Set;
  Set.setUniverse(100);

  Set.insert(0);
  Set.insert(1);
  Set.insert(2);
  Set.insert(0);
  Set.insert(1);
  Set.insert(0);

  USet::RangePair RangePair = Set.equal_range(0);
  USet::iterator B = RangePair.first;
  USet::iterator E = RangePair.second;

  // Move the iterators around, going to end and coming back.
  EXPECT_EQ(3, std::distance(B, E));
  EXPECT_EQ(B, --(--(--E)));
  EXPECT_EQ(++(++(++E)), Set.end());
  EXPECT_EQ(B, --(--(--E)));
  EXPECT_EQ(++(++(++E)), Set.end());

  // Insert into the tail, and move around again
  Set.insert(0);
  EXPECT_EQ(B, --(--(--(--E))));
  EXPECT_EQ(++(++(++(++E))), Set.end());
  EXPECT_EQ(B, --(--(--(--E))));
  EXPECT_EQ(++(++(++(++E))), Set.end());

  // Erase a tail, and move around again
  USet::iterator Erased = Set.erase(Set.getTail(0));
  EXPECT_EQ(Erased, E);
  EXPECT_EQ(B, --(--(--E)));

  USet Set2;
  Set2.setUniverse(11);
  Set2.insert(3);
  EXPECT_TRUE(!Set2.contains(0));
  EXPECT_TRUE(!Set.contains(3));

  EXPECT_EQ(Set2.getHead(3), Set2.getTail(3));
  EXPECT_EQ(Set2.getHead(0), Set2.getTail(0));
  B = Set2.find(3);
  EXPECT_EQ(Set2.find(3), --(++B));
}

struct Alt {
  unsigned Value;
  explicit Alt(unsigned x) : Value(x) {}
  unsigned getSparseSetIndex() const { return Value - 1000; }
};

TEST(SparseMultiSetTest, AltStructSet) {
  typedef SparseMultiSet<Alt> ASet;
  ASet Set;
  Set.setUniverse(10);
  Set.insert(Alt(1005));

  ASet::iterator I = Set.find(5);
  ASSERT_TRUE(I != Set.end());
  EXPECT_EQ(1005u, I->Value);

  Set.insert(Alt(1006));
  Set.insert(Alt(1006));
  I = Set.erase(Set.find(6));
  ASSERT_TRUE(I != Set.end());
  EXPECT_EQ(1006u, I->Value);
  I = Set.erase(Set.find(6));
  ASSERT_TRUE(I == Set.end());

  EXPECT_TRUE(Set.contains(5));
  EXPECT_FALSE(Set.contains(6));
}
} // namespace