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"
18 class DenseMapTest
: public testing::Test
{
20 DenseMap
<uint32_t, uint32_t> uintMap
;
21 DenseMap
<uint32_t *, uint32_t *> uintPtrMap
;
26 TEST_F(DenseMapTest
, EmptyIntMapTest
) {
28 EXPECT_EQ(0u, uintMap
.size());
29 EXPECT_TRUE(uintMap
.empty());
32 EXPECT_TRUE(uintMap
.begin() == uintMap
.end());
35 EXPECT_FALSE(uintMap
.count(0u));
36 EXPECT_TRUE(uintMap
.find(0u) == uintMap
.end());
37 EXPECT_EQ(0u, uintMap
.lookup(0u));
40 // Empty map tests for pointer map
41 TEST_F(DenseMapTest
, EmptyPtrMapTest
) {
43 EXPECT_EQ(0u, uintPtrMap
.size());
44 EXPECT_TRUE(uintPtrMap
.empty());
47 EXPECT_TRUE(uintPtrMap
.begin() == uintPtrMap
.end());
50 EXPECT_FALSE(uintPtrMap
.count(&dummyInt
));
51 EXPECT_TRUE(uintPtrMap
.find(&dummyInt
) == uintPtrMap
.begin());
52 EXPECT_EQ(0, uintPtrMap
.lookup(&dummyInt
));
56 TEST_F(DenseMapTest
, ConstEmptyMapTest
) {
57 const DenseMap
<uint32_t, uint32_t> & constUintMap
= uintMap
;
58 const DenseMap
<uint32_t *, uint32_t *> & constUintPtrMap
= uintPtrMap
;
59 EXPECT_EQ(0u, constUintMap
.size());
60 EXPECT_EQ(0u, constUintPtrMap
.size());
61 EXPECT_TRUE(constUintMap
.empty());
62 EXPECT_TRUE(constUintPtrMap
.empty());
63 EXPECT_TRUE(constUintMap
.begin() == constUintMap
.end());
64 EXPECT_TRUE(constUintPtrMap
.begin() == constUintPtrMap
.end());
67 // A map with a single entry
68 TEST_F(DenseMapTest
, SingleEntryMapTest
) {
72 EXPECT_EQ(1u, uintMap
.size());
73 EXPECT_FALSE(uintMap
.begin() == uintMap
.end());
74 EXPECT_FALSE(uintMap
.empty());
77 DenseMap
<uint32_t, uint32_t>::iterator it
= uintMap
.begin();
78 EXPECT_EQ(0u, it
->first
);
79 EXPECT_EQ(1u, it
->second
);
81 EXPECT_TRUE(it
== uintMap
.end());
84 EXPECT_TRUE(uintMap
.count(0u));
85 EXPECT_TRUE(uintMap
.find(0u) == uintMap
.begin());
86 EXPECT_EQ(1u, uintMap
.lookup(0u));
87 EXPECT_EQ(1u, uintMap
[0]);
90 // Test clear() method
91 TEST_F(DenseMapTest
, ClearTest
) {
95 EXPECT_EQ(0u, uintMap
.size());
96 EXPECT_TRUE(uintMap
.empty());
97 EXPECT_TRUE(uintMap
.begin() == uintMap
.end());
100 // Test erase(iterator) method
101 TEST_F(DenseMapTest
, EraseTest
) {
103 uintMap
.erase(uintMap
.begin());
105 EXPECT_EQ(0u, uintMap
.size());
106 EXPECT_TRUE(uintMap
.empty());
107 EXPECT_TRUE(uintMap
.begin() == uintMap
.end());
110 // Test erase(value) method
111 TEST_F(DenseMapTest
, EraseTest2
) {
115 EXPECT_EQ(0u, uintMap
.size());
116 EXPECT_TRUE(uintMap
.empty());
117 EXPECT_TRUE(uintMap
.begin() == uintMap
.end());
120 // Test insert() method
121 TEST_F(DenseMapTest
, InsertTest
) {
122 uintMap
.insert(std::make_pair(0u, 1u));
123 EXPECT_EQ(1u, uintMap
.size());
124 EXPECT_EQ(1u, uintMap
[0]);
127 // Test copy constructor method
128 TEST_F(DenseMapTest
, CopyConstructorTest
) {
130 DenseMap
<uint32_t, uint32_t> copyMap(uintMap
);
132 EXPECT_EQ(1u, copyMap
.size());
133 EXPECT_EQ(1u, copyMap
[0]);
136 // Test assignment operator method
137 TEST_F(DenseMapTest
, AssignmentTest
) {
139 DenseMap
<uint32_t, uint32_t> copyMap
= uintMap
;
141 EXPECT_EQ(1u, copyMap
.size());
142 EXPECT_EQ(1u, copyMap
[0]);
145 // A more complex iteration test
146 TEST_F(DenseMapTest
, IterationTest
) {
149 // Insert 100 numbers into the map
150 for (int i
= 0; i
< 100; ++i
) {
155 // Iterate over all numbers and mark each one found.
156 for (DenseMap
<uint32_t, uint32_t>::iterator it
= uintMap
.begin();
157 it
!= uintMap
.end(); ++it
) {
158 visited
[it
->first
] = true;
161 // Ensure every number was visited.
162 for (int i
= 0; i
< 100; ++i
) {
163 ASSERT_TRUE(visited
[i
]) << "Entry #" << i
<< " was never visited";