DenseMapTest.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. //===- 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. #include "gtest/gtest.h"
  10. #include "llvm/ADT/DenseMap.h"
  11. #include <map>
  12. #include <set>
  13. using namespace llvm;
  14. namespace {
  15. uint32_t getTestKey(int i, uint32_t *) { return i; }
  16. uint32_t getTestValue(int i, uint32_t *) { return 42 + i; }
  17. uint32_t *getTestKey(int i, uint32_t **) {
  18. static uint32_t dummy_arr1[8192];
  19. assert(i < 8192 && "Only support 8192 dummy keys.");
  20. return &dummy_arr1[i];
  21. }
  22. uint32_t *getTestValue(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];
  26. }
  27. /// \brief A test class that tries to check that construction and destruction
  28. /// occur correctly.
  29. class CtorTester {
  30. static std::set<CtorTester *> Constructed;
  31. int Value;
  32. public:
  33. explicit CtorTester(int Value = 0) : Value(Value) {
  34. EXPECT_TRUE(Constructed.insert(this).second);
  35. }
  36. CtorTester(uint32_t Value) : Value(Value) {
  37. EXPECT_TRUE(Constructed.insert(this).second);
  38. }
  39. CtorTester(const CtorTester &Arg) : Value(Arg.Value) {
  40. EXPECT_TRUE(Constructed.insert(this).second);
  41. }
  42. CtorTester &operator=(const CtorTester &) = default;
  43. ~CtorTester() {
  44. EXPECT_EQ(1u, Constructed.erase(this));
  45. }
  46. operator uint32_t() const { return Value; }
  47. int getValue() const { return Value; }
  48. bool operator==(const CtorTester &RHS) const { return Value == RHS.Value; }
  49. };
  50. std::set<CtorTester *> CtorTester::Constructed;
  51. struct CtorTesterMapInfo {
  52. static inline CtorTester getEmptyKey() { return CtorTester(-1); }
  53. static inline CtorTester getTombstoneKey() { return CtorTester(-2); }
  54. static unsigned getHashValue(const CtorTester &Val) {
  55. return Val.getValue() * 37u;
  56. }
  57. static bool isEqual(const CtorTester &LHS, const CtorTester &RHS) {
  58. return LHS == RHS;
  59. }
  60. };
  61. CtorTester getTestKey(int i, CtorTester *) { return CtorTester(i); }
  62. CtorTester getTestValue(int i, CtorTester *) { return CtorTester(42 + i); }
  63. // Test fixture, with helper functions implemented by forwarding to global
  64. // function overloads selected by component types of the type parameter. This
  65. // allows all of the map implementations to be tested with shared
  66. // implementations of helper routines.
  67. template <typename T>
  68. class DenseMapTest : public ::testing::Test {
  69. protected:
  70. T Map;
  71. static typename T::key_type *const dummy_key_ptr;
  72. static typename T::mapped_type *const dummy_value_ptr;
  73. typename T::key_type getKey(int i = 0) {
  74. return getTestKey(i, dummy_key_ptr);
  75. }
  76. typename T::mapped_type getValue(int i = 0) {
  77. return getTestValue(i, dummy_value_ptr);
  78. }
  79. };
  80. template <typename T>
  81. typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = nullptr;
  82. template <typename T>
  83. typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = nullptr;
  84. // Register these types for testing.
  85. typedef ::testing::Types<DenseMap<uint32_t, uint32_t>,
  86. DenseMap<uint32_t *, uint32_t *>,
  87. DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>,
  88. SmallDenseMap<uint32_t, uint32_t>,
  89. SmallDenseMap<uint32_t *, uint32_t *>,
  90. SmallDenseMap<CtorTester, CtorTester, 4,
  91. CtorTesterMapInfo>
  92. > DenseMapTestTypes;
  93. TYPED_TEST_CASE(DenseMapTest, DenseMapTestTypes);
  94. // Empty map tests
  95. TYPED_TEST(DenseMapTest, EmptyIntMapTest) {
  96. // Size tests
  97. EXPECT_EQ(0u, this->Map.size());
  98. EXPECT_TRUE(this->Map.empty());
  99. // Iterator tests
  100. EXPECT_TRUE(this->Map.begin() == this->Map.end());
  101. // Lookup tests
  102. EXPECT_FALSE(this->Map.count(this->getKey()));
  103. EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end());
  104. #if !defined(_MSC_VER) || defined(__clang__)
  105. EXPECT_EQ(typename TypeParam::mapped_type(),
  106. this->Map.lookup(this->getKey()));
  107. #else
  108. // MSVC, at least old versions, cannot parse the typename to disambiguate
  109. // TypeParam::mapped_type as a type. However, because MSVC doesn't implement
  110. // two-phase name lookup, it also doesn't require the typename. Deal with
  111. // this mutual incompatibility through specialized code.
  112. EXPECT_EQ(TypeParam::mapped_type(),
  113. this->Map.lookup(this->getKey()));
  114. #endif
  115. }
  116. // Constant map tests
  117. TYPED_TEST(DenseMapTest, ConstEmptyMapTest) {
  118. const TypeParam &ConstMap = this->Map;
  119. EXPECT_EQ(0u, ConstMap.size());
  120. EXPECT_TRUE(ConstMap.empty());
  121. EXPECT_TRUE(ConstMap.begin() == ConstMap.end());
  122. }
  123. // A map with a single entry
  124. TYPED_TEST(DenseMapTest, SingleEntryMapTest) {
  125. this->Map[this->getKey()] = this->getValue();
  126. // Size tests
  127. EXPECT_EQ(1u, this->Map.size());
  128. EXPECT_FALSE(this->Map.begin() == this->Map.end());
  129. EXPECT_FALSE(this->Map.empty());
  130. // Iterator tests
  131. typename TypeParam::iterator it = this->Map.begin();
  132. EXPECT_EQ(this->getKey(), it->first);
  133. EXPECT_EQ(this->getValue(), it->second);
  134. ++it;
  135. EXPECT_TRUE(it == this->Map.end());
  136. // Lookup tests
  137. EXPECT_TRUE(this->Map.count(this->getKey()));
  138. EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin());
  139. EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey()));
  140. EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
  141. }
  142. // Test clear() method
  143. TYPED_TEST(DenseMapTest, ClearTest) {
  144. this->Map[this->getKey()] = this->getValue();
  145. this->Map.clear();
  146. EXPECT_EQ(0u, this->Map.size());
  147. EXPECT_TRUE(this->Map.empty());
  148. EXPECT_TRUE(this->Map.begin() == this->Map.end());
  149. }
  150. // Test erase(iterator) method
  151. TYPED_TEST(DenseMapTest, EraseTest) {
  152. this->Map[this->getKey()] = this->getValue();
  153. this->Map.erase(this->Map.begin());
  154. EXPECT_EQ(0u, this->Map.size());
  155. EXPECT_TRUE(this->Map.empty());
  156. EXPECT_TRUE(this->Map.begin() == this->Map.end());
  157. }
  158. // Test erase(value) method
  159. TYPED_TEST(DenseMapTest, EraseTest2) {
  160. this->Map[this->getKey()] = this->getValue();
  161. this->Map.erase(this->getKey());
  162. EXPECT_EQ(0u, this->Map.size());
  163. EXPECT_TRUE(this->Map.empty());
  164. EXPECT_TRUE(this->Map.begin() == this->Map.end());
  165. }
  166. // Test insert() method
  167. TYPED_TEST(DenseMapTest, InsertTest) {
  168. this->Map.insert(std::make_pair(this->getKey(), this->getValue()));
  169. EXPECT_EQ(1u, this->Map.size());
  170. EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
  171. }
  172. // Test copy constructor method
  173. TYPED_TEST(DenseMapTest, CopyConstructorTest) {
  174. this->Map[this->getKey()] = this->getValue();
  175. TypeParam copyMap(this->Map);
  176. EXPECT_EQ(1u, copyMap.size());
  177. EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
  178. }
  179. // Test copy constructor method where SmallDenseMap isn't small.
  180. TYPED_TEST(DenseMapTest, CopyConstructorNotSmallTest) {
  181. for (int Key = 0; Key < 5; ++Key)
  182. this->Map[this->getKey(Key)] = this->getValue(Key);
  183. TypeParam copyMap(this->Map);
  184. EXPECT_EQ(5u, copyMap.size());
  185. for (int Key = 0; Key < 5; ++Key)
  186. EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]);
  187. }
  188. // Test copying from a default-constructed map.
  189. TYPED_TEST(DenseMapTest, CopyConstructorFromDefaultTest) {
  190. TypeParam copyMap(this->Map);
  191. EXPECT_TRUE(copyMap.empty());
  192. }
  193. // Test copying from an empty map where SmallDenseMap isn't small.
  194. TYPED_TEST(DenseMapTest, CopyConstructorFromEmptyTest) {
  195. for (int Key = 0; Key < 5; ++Key)
  196. this->Map[this->getKey(Key)] = this->getValue(Key);
  197. this->Map.clear();
  198. TypeParam copyMap(this->Map);
  199. EXPECT_TRUE(copyMap.empty());
  200. }
  201. // Test assignment operator method
  202. TYPED_TEST(DenseMapTest, AssignmentTest) {
  203. this->Map[this->getKey()] = this->getValue();
  204. TypeParam copyMap = this->Map;
  205. EXPECT_EQ(1u, copyMap.size());
  206. EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
  207. // test self-assignment.
  208. copyMap = copyMap;
  209. EXPECT_EQ(1u, copyMap.size());
  210. EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
  211. }
  212. // Test swap method
  213. TYPED_TEST(DenseMapTest, SwapTest) {
  214. this->Map[this->getKey()] = this->getValue();
  215. TypeParam otherMap;
  216. this->Map.swap(otherMap);
  217. EXPECT_EQ(0u, this->Map.size());
  218. EXPECT_TRUE(this->Map.empty());
  219. EXPECT_EQ(1u, otherMap.size());
  220. EXPECT_EQ(this->getValue(), otherMap[this->getKey()]);
  221. this->Map.swap(otherMap);
  222. EXPECT_EQ(0u, otherMap.size());
  223. EXPECT_TRUE(otherMap.empty());
  224. EXPECT_EQ(1u, this->Map.size());
  225. EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
  226. // Make this more interesting by inserting 100 numbers into the map.
  227. for (int i = 0; i < 100; ++i)
  228. this->Map[this->getKey(i)] = this->getValue(i);
  229. this->Map.swap(otherMap);
  230. EXPECT_EQ(0u, this->Map.size());
  231. EXPECT_TRUE(this->Map.empty());
  232. EXPECT_EQ(100u, otherMap.size());
  233. for (int i = 0; i < 100; ++i)
  234. EXPECT_EQ(this->getValue(i), otherMap[this->getKey(i)]);
  235. this->Map.swap(otherMap);
  236. EXPECT_EQ(0u, otherMap.size());
  237. EXPECT_TRUE(otherMap.empty());
  238. EXPECT_EQ(100u, this->Map.size());
  239. for (int i = 0; i < 100; ++i)
  240. EXPECT_EQ(this->getValue(i), this->Map[this->getKey(i)]);
  241. }
  242. // A more complex iteration test
  243. TYPED_TEST(DenseMapTest, IterationTest) {
  244. bool visited[100];
  245. std::map<typename TypeParam::key_type, unsigned> visitedIndex;
  246. // Insert 100 numbers into the map
  247. for (int i = 0; i < 100; ++i) {
  248. visited[i] = false;
  249. visitedIndex[this->getKey(i)] = i;
  250. this->Map[this->getKey(i)] = this->getValue(i);
  251. }
  252. // Iterate over all numbers and mark each one found.
  253. for (typename TypeParam::iterator it = this->Map.begin();
  254. it != this->Map.end(); ++it)
  255. visited[visitedIndex[it->first]] = true;
  256. // Ensure every number was visited.
  257. for (int i = 0; i < 100; ++i)
  258. ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
  259. }
  260. // const_iterator test
  261. TYPED_TEST(DenseMapTest, ConstIteratorTest) {
  262. // Check conversion from iterator to const_iterator.
  263. typename TypeParam::iterator it = this->Map.begin();
  264. typename TypeParam::const_iterator cit(it);
  265. EXPECT_TRUE(it == cit);
  266. // Check copying of const_iterators.
  267. typename TypeParam::const_iterator cit2(cit);
  268. EXPECT_TRUE(cit == cit2);
  269. }
  270. // Make sure DenseMap works with StringRef keys.
  271. TEST(DenseMapCustomTest, StringRefTest) {
  272. DenseMap<StringRef, int> M;
  273. M["a"] = 1;
  274. M["b"] = 2;
  275. M["c"] = 3;
  276. EXPECT_EQ(3u, M.size());
  277. EXPECT_EQ(1, M.lookup("a"));
  278. EXPECT_EQ(2, M.lookup("b"));
  279. EXPECT_EQ(3, M.lookup("c"));
  280. EXPECT_EQ(0, M.lookup("q"));
  281. // Test the empty string, spelled various ways.
  282. EXPECT_EQ(0, M.lookup(""));
  283. EXPECT_EQ(0, M.lookup(StringRef()));
  284. EXPECT_EQ(0, M.lookup(StringRef("a", 0)));
  285. M[""] = 42;
  286. EXPECT_EQ(42, M.lookup(""));
  287. EXPECT_EQ(42, M.lookup(StringRef()));
  288. EXPECT_EQ(42, M.lookup(StringRef("a", 0)));
  289. }
  290. // Key traits that allows lookup with either an unsigned or char* key;
  291. // In the latter case, "a" == 0, "b" == 1 and so on.
  292. struct TestDenseMapInfo {
  293. static inline unsigned getEmptyKey() { return ~0; }
  294. static inline unsigned getTombstoneKey() { return ~0U - 1; }
  295. static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
  296. static unsigned getHashValue(const char* Val) {
  297. return (unsigned)(Val[0] - 'a') * 37U;
  298. }
  299. static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  300. return LHS == RHS;
  301. }
  302. static bool isEqual(const char* LHS, const unsigned& RHS) {
  303. return (unsigned)(LHS[0] - 'a') == RHS;
  304. }
  305. };
  306. // find_as() tests
  307. TEST(DenseMapCustomTest, FindAsTest) {
  308. DenseMap<unsigned, unsigned, TestDenseMapInfo> map;
  309. map[0] = 1;
  310. map[1] = 2;
  311. map[2] = 3;
  312. // Size tests
  313. EXPECT_EQ(3u, map.size());
  314. // Normal lookup tests
  315. EXPECT_EQ(1u, map.count(1));
  316. EXPECT_EQ(1u, map.find(0)->second);
  317. EXPECT_EQ(2u, map.find(1)->second);
  318. EXPECT_EQ(3u, map.find(2)->second);
  319. EXPECT_TRUE(map.find(3) == map.end());
  320. // find_as() tests
  321. EXPECT_EQ(1u, map.find_as("a")->second);
  322. EXPECT_EQ(2u, map.find_as("b")->second);
  323. EXPECT_EQ(3u, map.find_as("c")->second);
  324. EXPECT_TRUE(map.find_as("d") == map.end());
  325. }
  326. struct ContiguousDenseMapInfo {
  327. static inline unsigned getEmptyKey() { return ~0; }
  328. static inline unsigned getTombstoneKey() { return ~0U - 1; }
  329. static unsigned getHashValue(const unsigned& Val) { return Val; }
  330. static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  331. return LHS == RHS;
  332. }
  333. };
  334. // Test that filling a small dense map with exactly the number of elements in
  335. // the map grows to have enough space for an empty bucket.
  336. TEST(DenseMapCustomTest, SmallDenseMapGrowTest) {
  337. SmallDenseMap<unsigned, unsigned, 32, ContiguousDenseMapInfo> map;
  338. // Add some number of elements, then delete a few to leave us some tombstones.
  339. // If we just filled the map with 32 elements we'd grow because of not enough
  340. // tombstones which masks the issue here.
  341. for (unsigned i = 0; i < 20; ++i)
  342. map[i] = i + 1;
  343. for (unsigned i = 0; i < 10; ++i)
  344. map.erase(i);
  345. for (unsigned i = 20; i < 32; ++i)
  346. map[i] = i + 1;
  347. // Size tests
  348. EXPECT_EQ(22u, map.size());
  349. // Try to find an element which doesn't exist. There was a bug in
  350. // SmallDenseMap which led to a map with num elements == small capacity not
  351. // having an empty bucket any more. Finding an element not in the map would
  352. // therefore never terminate.
  353. EXPECT_TRUE(map.find(32) == map.end());
  354. }
  355. }