1 //===- llvm/unittest/ADT/DenseMapMap.cpp - DenseMap unit tests --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/DenseMap.h"
19 uint32_t getTestKey(int i, uint32_t *) { return i; }
20 uint32_t getTestValue(int i, uint32_t *) { return 42 + i; }
22 uint32_t *getTestKey(int i, uint32_t **) {
23 static uint32_t dummy_arr1[8192];
24 assert(i < 8192 && "Only support 8192 dummy keys.");
25 return &dummy_arr1[i];
27 uint32_t *getTestValue(int i, uint32_t **) {
28 static uint32_t dummy_arr1[8192];
29 assert(i < 8192 && "Only support 8192 dummy keys.");
30 return &dummy_arr1[i];
33 /// \brief A test class that tries to check that construction and destruction
36 static std::set<CtorTester *> Constructed;
40 explicit CtorTester(int Value = 0) : Value(Value) {
41 EXPECT_TRUE(Constructed.insert(this).second);
43 CtorTester(uint32_t Value) : Value(Value) {
44 EXPECT_TRUE(Constructed.insert(this).second);
46 CtorTester(const CtorTester &Arg) : Value(Arg.Value) {
47 EXPECT_TRUE(Constructed.insert(this).second);
50 EXPECT_EQ(1u, Constructed.erase(this));
52 operator uint32_t() const { return Value; }
54 int getValue() const { return Value; }
55 bool operator==(const CtorTester &RHS) const { return Value == RHS.Value; }
58 std::set<CtorTester *> CtorTester::Constructed;
60 struct CtorTesterMapInfo {
61 static inline CtorTester getEmptyKey() { return CtorTester(-1); }
62 static inline CtorTester getTombstoneKey() { return CtorTester(-2); }
63 static unsigned getHashValue(const CtorTester &Val) {
64 return Val.getValue() * 37u;
66 static bool isEqual(const CtorTester &LHS, const CtorTester &RHS) {
71 CtorTester getTestKey(int i, CtorTester *) { return CtorTester(i); }
72 CtorTester getTestValue(int i, CtorTester *) { return CtorTester(42 + i); }
74 // Test fixture, with helper functions implemented by forwarding to global
75 // function overloads selected by component types of the type parameter. This
76 // allows all of the map implementations to be tested with shared
77 // implementations of helper routines.
79 class DenseMapTest : public ::testing::Test {
83 static typename T::key_type *const dummy_key_ptr;
84 static typename T::mapped_type *const dummy_value_ptr;
86 typename T::key_type getKey(int i = 0) {
87 return getTestKey(i, dummy_key_ptr);
89 typename T::mapped_type getValue(int i = 0) {
90 return getTestValue(i, dummy_value_ptr);
95 typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = 0;
97 typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = 0;
99 // Register these types for testing.
100 typedef ::testing::Types<DenseMap<uint32_t, uint32_t>,
101 DenseMap<uint32_t *, uint32_t *>,
102 DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>,
103 SmallDenseMap<uint32_t, uint32_t>,
104 SmallDenseMap<uint32_t *, uint32_t *>,
105 SmallDenseMap<CtorTester, CtorTester, 4,
108 TYPED_TEST_CASE(DenseMapTest, DenseMapTestTypes);
111 TYPED_TEST(DenseMapTest, EmptyIntMapTest) {
113 EXPECT_EQ(0u, this->Map.size());
114 EXPECT_TRUE(this->Map.empty());
117 EXPECT_TRUE(this->Map.begin() == this->Map.end());
120 EXPECT_FALSE(this->Map.count(this->getKey()));
121 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end());
123 EXPECT_EQ(typename TypeParam::mapped_type(),
124 this->Map.lookup(this->getKey()));
126 // MSVC, at least old versions, cannot parse the typename to disambiguate
127 // TypeParam::mapped_type as a type. However, because MSVC doesn't implement
128 // two-phase name lookup, it also doesn't require the typename. Deal with
129 // this mutual incompatibility through specialized code.
130 EXPECT_EQ(TypeParam::mapped_type(),
131 this->Map.lookup(this->getKey()));
135 // Constant map tests
136 TYPED_TEST(DenseMapTest, ConstEmptyMapTest) {
137 const TypeParam &ConstMap = this->Map;
138 EXPECT_EQ(0u, ConstMap.size());
139 EXPECT_TRUE(ConstMap.empty());
140 EXPECT_TRUE(ConstMap.begin() == ConstMap.end());
143 // A map with a single entry
144 TYPED_TEST(DenseMapTest, SingleEntryMapTest) {
145 this->Map[this->getKey()] = this->getValue();
148 EXPECT_EQ(1u, this->Map.size());
149 EXPECT_FALSE(this->Map.begin() == this->Map.end());
150 EXPECT_FALSE(this->Map.empty());
153 typename TypeParam::iterator it = this->Map.begin();
154 EXPECT_EQ(this->getKey(), it->first);
155 EXPECT_EQ(this->getValue(), it->second);
157 EXPECT_TRUE(it == this->Map.end());
160 EXPECT_TRUE(this->Map.count(this->getKey()));
161 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin());
162 EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey()));
163 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
166 // Test clear() method
167 TYPED_TEST(DenseMapTest, ClearTest) {
168 this->Map[this->getKey()] = this->getValue();
171 EXPECT_EQ(0u, this->Map.size());
172 EXPECT_TRUE(this->Map.empty());
173 EXPECT_TRUE(this->Map.begin() == this->Map.end());
176 // Test erase(iterator) method
177 TYPED_TEST(DenseMapTest, EraseTest) {
178 this->Map[this->getKey()] = this->getValue();
179 this->Map.erase(this->Map.begin());
181 EXPECT_EQ(0u, this->Map.size());
182 EXPECT_TRUE(this->Map.empty());
183 EXPECT_TRUE(this->Map.begin() == this->Map.end());
186 // Test erase(value) method
187 TYPED_TEST(DenseMapTest, EraseTest2) {
188 this->Map[this->getKey()] = this->getValue();
189 this->Map.erase(this->getKey());
191 EXPECT_EQ(0u, this->Map.size());
192 EXPECT_TRUE(this->Map.empty());
193 EXPECT_TRUE(this->Map.begin() == this->Map.end());
196 // Test insert() method
197 TYPED_TEST(DenseMapTest, InsertTest) {
198 this->Map.insert(std::make_pair(this->getKey(), this->getValue()));
199 EXPECT_EQ(1u, this->Map.size());
200 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
203 // Test copy constructor method
204 TYPED_TEST(DenseMapTest, CopyConstructorTest) {
205 this->Map[this->getKey()] = this->getValue();
206 TypeParam copyMap(this->Map);
208 EXPECT_EQ(1u, copyMap.size());
209 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
212 // Test assignment operator method
213 TYPED_TEST(DenseMapTest, AssignmentTest) {
214 this->Map[this->getKey()] = this->getValue();
215 TypeParam copyMap = this->Map;
217 EXPECT_EQ(1u, copyMap.size());
218 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
221 // A more complex iteration test
222 TYPED_TEST(DenseMapTest, IterationTest) {
224 std::map<typename TypeParam::key_type, unsigned> visitedIndex;
226 // Insert 100 numbers into the map
227 for (int i = 0; i < 100; ++i) {
229 visitedIndex[this->getKey(i)] = i;
231 this->Map[this->getKey(i)] = this->getValue(i);
234 // Iterate over all numbers and mark each one found.
235 for (typename TypeParam::iterator it = this->Map.begin();
236 it != this->Map.end(); ++it)
237 visited[visitedIndex[it->first]] = true;
239 // Ensure every number was visited.
240 for (int i = 0; i < 100; ++i)
241 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
244 // const_iterator test
245 TYPED_TEST(DenseMapTest, ConstIteratorTest) {
246 // Check conversion from iterator to const_iterator.
247 typename TypeParam::iterator it = this->Map.begin();
248 typename TypeParam::const_iterator cit(it);
249 EXPECT_TRUE(it == cit);
251 // Check copying of const_iterators.
252 typename TypeParam::const_iterator cit2(cit);
253 EXPECT_TRUE(cit == cit2);
256 // Key traits that allows lookup with either an unsigned or char* key;
257 // In the latter case, "a" == 0, "b" == 1 and so on.
258 struct TestDenseMapInfo {
259 static inline unsigned getEmptyKey() { return ~0; }
260 static inline unsigned getTombstoneKey() { return ~0U - 1; }
261 static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
262 static unsigned getHashValue(const char* Val) {
263 return (unsigned)(Val[0] - 'a') * 37U;
265 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
268 static bool isEqual(const char* LHS, const unsigned& RHS) {
269 return (unsigned)(LHS[0] - 'a') == RHS;
274 TEST(DenseMapCustomTest, FindAsTest) {
275 DenseMap<unsigned, unsigned, TestDenseMapInfo> map;
281 EXPECT_EQ(3u, map.size());
283 // Normal lookup tests
284 EXPECT_EQ(1, map.count(1));
285 EXPECT_EQ(1u, map.find(0)->second);
286 EXPECT_EQ(2u, map.find(1)->second);
287 EXPECT_EQ(3u, map.find(2)->second);
288 EXPECT_TRUE(map.find(3) == map.end());
291 EXPECT_EQ(1u, map.find_as("a")->second);
292 EXPECT_EQ(2u, map.find_as("b")->second);
293 EXPECT_EQ(3u, map.find_as("c")->second);
294 EXPECT_TRUE(map.find_as("d") == map.end());