blob: d61f991bd55d13415c85172d656fa9367619a6e5 [file] [log] [blame]
Misha Brukman8fb520e2009-01-01 02:24:48 +00001//===- llvm/unittest/ADT/DenseMapMap.cpp - DenseMap unit tests --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "gtest/gtest.h"
11#include "llvm/ADT/DenseMap.h"
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000012#include <map>
Misha Brukman8fb520e2009-01-01 02:24:48 +000013
14using namespace llvm;
15
16namespace {
17
Chandler Carruthdd9d38d2012-06-17 09:05:09 +000018uint32_t getTestKey(int i, uint32_t *) { return i; }
19uint32_t getTestValue(int i, uint32_t *) { return 42 + i; }
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000020
Chandler Carruthdd9d38d2012-06-17 09:05:09 +000021uint32_t *getTestKey(int i, uint32_t **) {
22 static uint32_t dummy_arr1[8192];
23 assert(i < 8192 && "Only support 8192 dummy keys.");
24 return &dummy_arr1[i];
Misha Brukman8fb520e2009-01-01 02:24:48 +000025}
Chandler Carruthdd9d38d2012-06-17 09:05:09 +000026uint32_t *getTestValue(int i, uint32_t **) {
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000027 static uint32_t dummy_arr1[8192];
28 assert(i < 8192 && "Only support 8192 dummy keys.");
29 return &dummy_arr1[i];
30}
31
Chandler Carruthdd9d38d2012-06-17 09:05:09 +000032// Test fixture, with helper functions implemented by forwarding to global
33// function overloads selected by component types of the type parameter. This
34// allows all of the map implementations to be tested with shared
35// implementations of helper routines.
36template <typename T>
37class DenseMapTest : public ::testing::Test {
38protected:
39 T Map;
40
41 static typename T::key_type *const dummy_key_ptr;
42 static typename T::mapped_type *const dummy_value_ptr;
43
44 typename T::key_type getKey(int i = 0) {
45 return getTestKey(i, dummy_key_ptr);
46 }
47 typename T::mapped_type getValue(int i = 0) {
48 return getTestValue(i, dummy_value_ptr);
49 }
50};
51
52template <typename T>
53typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = 0;
54template <typename T>
55typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = 0;
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000056
57// Register these types for testing.
58typedef ::testing::Types<DenseMap<uint32_t, uint32_t>,
Chandler Carruthdd9d38d2012-06-17 09:05:09 +000059 DenseMap<uint32_t *, uint32_t *>,
60 SmallDenseMap<uint32_t, uint32_t>,
61 SmallDenseMap<uint32_t *, uint32_t *>
62 > DenseMapTestTypes;
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000063TYPED_TEST_CASE(DenseMapTest, DenseMapTestTypes);
64
65// Empty map tests
66TYPED_TEST(DenseMapTest, EmptyIntMapTest) {
Misha Brukman8fb520e2009-01-01 02:24:48 +000067 // Size tests
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000068 EXPECT_EQ(0u, this->Map.size());
69 EXPECT_TRUE(this->Map.empty());
Misha Brukman8fb520e2009-01-01 02:24:48 +000070
71 // Iterator tests
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000072 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukman8fb520e2009-01-01 02:24:48 +000073
74 // Lookup tests
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000075 EXPECT_FALSE(this->Map.count(this->getKey()));
76 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end());
Chandler Carruth7027ba92012-06-16 03:54:11 +000077#ifndef _MSC_VER
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000078 EXPECT_EQ(typename TypeParam::mapped_type(),
79 this->Map.lookup(this->getKey()));
Chandler Carruth7027ba92012-06-16 03:54:11 +000080#else
81 // MSVC, at least old versions, cannot parse the typename to disambiguate
82 // TypeParam::mapped_type as a type. However, because MSVC doesn't implement
83 // two-phase name lookup, it also doesn't require the typename. Deal with
84 // this mutual incompatibility through specialized code.
85 EXPECT_EQ(TypeParam::mapped_type(),
86 this->Map.lookup(this->getKey()));
87#endif
Misha Brukman8fb520e2009-01-01 02:24:48 +000088}
89
90// Constant map tests
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000091TYPED_TEST(DenseMapTest, ConstEmptyMapTest) {
92 const TypeParam &ConstMap = this->Map;
93 EXPECT_EQ(0u, ConstMap.size());
94 EXPECT_TRUE(ConstMap.empty());
95 EXPECT_TRUE(ConstMap.begin() == ConstMap.end());
Misha Brukman8fb520e2009-01-01 02:24:48 +000096}
97
98// A map with a single entry
Chandler Carruthe5c7bc62012-06-16 01:31:33 +000099TYPED_TEST(DenseMapTest, SingleEntryMapTest) {
100 this->Map[this->getKey()] = this->getValue();
Misha Brukman8fb520e2009-01-01 02:24:48 +0000101
102 // Size tests
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000103 EXPECT_EQ(1u, this->Map.size());
104 EXPECT_FALSE(this->Map.begin() == this->Map.end());
105 EXPECT_FALSE(this->Map.empty());
Misha Brukman8fb520e2009-01-01 02:24:48 +0000106
107 // Iterator tests
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000108 typename TypeParam::iterator it = this->Map.begin();
109 EXPECT_EQ(this->getKey(), it->first);
110 EXPECT_EQ(this->getValue(), it->second);
Misha Brukman8fb520e2009-01-01 02:24:48 +0000111 ++it;
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000112 EXPECT_TRUE(it == this->Map.end());
Misha Brukman8fb520e2009-01-01 02:24:48 +0000113
114 // Lookup tests
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000115 EXPECT_TRUE(this->Map.count(this->getKey()));
116 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin());
117 EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey()));
118 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
Misha Brukman8fb520e2009-01-01 02:24:48 +0000119}
120
121// Test clear() method
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000122TYPED_TEST(DenseMapTest, ClearTest) {
123 this->Map[this->getKey()] = this->getValue();
124 this->Map.clear();
Misha Brukman8fb520e2009-01-01 02:24:48 +0000125
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000126 EXPECT_EQ(0u, this->Map.size());
127 EXPECT_TRUE(this->Map.empty());
128 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukman8fb520e2009-01-01 02:24:48 +0000129}
130
131// Test erase(iterator) method
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000132TYPED_TEST(DenseMapTest, EraseTest) {
133 this->Map[this->getKey()] = this->getValue();
134 this->Map.erase(this->Map.begin());
Misha Brukman8fb520e2009-01-01 02:24:48 +0000135
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000136 EXPECT_EQ(0u, this->Map.size());
137 EXPECT_TRUE(this->Map.empty());
138 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukman8fb520e2009-01-01 02:24:48 +0000139}
140
141// Test erase(value) method
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000142TYPED_TEST(DenseMapTest, EraseTest2) {
143 this->Map[this->getKey()] = this->getValue();
144 this->Map.erase(this->getKey());
Misha Brukman8fb520e2009-01-01 02:24:48 +0000145
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000146 EXPECT_EQ(0u, this->Map.size());
147 EXPECT_TRUE(this->Map.empty());
148 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukman8fb520e2009-01-01 02:24:48 +0000149}
150
151// Test insert() method
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000152TYPED_TEST(DenseMapTest, InsertTest) {
153 this->Map.insert(std::make_pair(this->getKey(), this->getValue()));
154 EXPECT_EQ(1u, this->Map.size());
155 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
Misha Brukman8fb520e2009-01-01 02:24:48 +0000156}
157
158// Test copy constructor method
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000159TYPED_TEST(DenseMapTest, CopyConstructorTest) {
160 this->Map[this->getKey()] = this->getValue();
161 TypeParam copyMap(this->Map);
Misha Brukman8fb520e2009-01-01 02:24:48 +0000162
163 EXPECT_EQ(1u, copyMap.size());
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000164 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
Misha Brukman8fb520e2009-01-01 02:24:48 +0000165}
166
167// Test assignment operator method
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000168TYPED_TEST(DenseMapTest, AssignmentTest) {
169 this->Map[this->getKey()] = this->getValue();
170 TypeParam copyMap = this->Map;
Misha Brukman8fb520e2009-01-01 02:24:48 +0000171
172 EXPECT_EQ(1u, copyMap.size());
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000173 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
Misha Brukman8fb520e2009-01-01 02:24:48 +0000174}
175
176// A more complex iteration test
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000177TYPED_TEST(DenseMapTest, IterationTest) {
Misha Brukman8fb520e2009-01-01 02:24:48 +0000178 bool visited[100];
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000179 std::map<typename TypeParam::key_type, unsigned> visitedIndex;
Misha Brukman8fb520e2009-01-01 02:24:48 +0000180
181 // Insert 100 numbers into the map
182 for (int i = 0; i < 100; ++i) {
183 visited[i] = false;
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000184 visitedIndex[this->getKey(i)] = i;
185
186 this->Map[this->getKey(i)] = this->getValue(i);
Misha Brukman8fb520e2009-01-01 02:24:48 +0000187 }
188
189 // Iterate over all numbers and mark each one found.
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000190 for (typename TypeParam::iterator it = this->Map.begin();
191 it != this->Map.end(); ++it)
192 visited[visitedIndex[it->first]] = true;
Misha Brukman8fb520e2009-01-01 02:24:48 +0000193
194 // Ensure every number was visited.
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000195 for (int i = 0; i < 100; ++i)
Misha Brukmanc870bb52009-01-07 21:13:53 +0000196 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
Misha Brukman8fb520e2009-01-01 02:24:48 +0000197}
198
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000199// const_iterator test
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000200TYPED_TEST(DenseMapTest, ConstIteratorTest) {
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000201 // Check conversion from iterator to const_iterator.
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000202 typename TypeParam::iterator it = this->Map.begin();
203 typename TypeParam::const_iterator cit(it);
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000204 EXPECT_TRUE(it == cit);
205
206 // Check copying of const_iterators.
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000207 typename TypeParam::const_iterator cit2(cit);
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000208 EXPECT_TRUE(cit == cit2);
209}
210
Talinbabd5982012-01-30 06:55:43 +0000211// Key traits that allows lookup with either an unsigned or char* key;
212// In the latter case, "a" == 0, "b" == 1 and so on.
213struct TestDenseMapInfo {
214 static inline unsigned getEmptyKey() { return ~0; }
215 static inline unsigned getTombstoneKey() { return ~0U - 1; }
216 static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
217 static unsigned getHashValue(const char* Val) {
218 return (unsigned)(Val[0] - 'a') * 37U;
219 }
220 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
221 return LHS == RHS;
222 }
223 static bool isEqual(const char* LHS, const unsigned& RHS) {
224 return (unsigned)(LHS[0] - 'a') == RHS;
225 }
226};
227
228// find_as() tests
Chandler Carruthe5c7bc62012-06-16 01:31:33 +0000229TEST(DenseMapCustomTest, FindAsTest) {
Talinbabd5982012-01-30 06:55:43 +0000230 DenseMap<unsigned, unsigned, TestDenseMapInfo> map;
231 map[0] = 1;
232 map[1] = 2;
233 map[2] = 3;
234
235 // Size tests
236 EXPECT_EQ(3u, map.size());
237
238 // Normal lookup tests
239 EXPECT_EQ(1, map.count(1));
240 EXPECT_EQ(1u, map.find(0)->second);
241 EXPECT_EQ(2u, map.find(1)->second);
242 EXPECT_EQ(3u, map.find(2)->second);
243 EXPECT_TRUE(map.find(3) == map.end());
244
245 // find_as() tests
246 EXPECT_EQ(1u, map.find_as("a")->second);
247 EXPECT_EQ(2u, map.find_as("b")->second);
248 EXPECT_EQ(3u, map.find_as("c")->second);
249 EXPECT_TRUE(map.find_as("d") == map.end());
250}
251
Misha Brukman8fb520e2009-01-01 02:24:48 +0000252}