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
|
//===- llvm/unittest/ADT/SmallMapTest.cpp ------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// SmallMap unit tests.
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "llvm/ADT/SmallMap.h"
using namespace llvm;
// SmallMap test.
TEST(SmallMapTest, GeneralTest) {
int buf[10];
SmallMap<int *, int, 3> a;
SmallMap<int *, int, 3> b;
SmallMap<int *, int, 3>::iterator found;
std::pair<SmallMap<int *, int, 3>::iterator, bool> insRes;
SmallMap<int *, int, 3>::const_iterator foundc;
a.insert(std::make_pair(&buf[0], 0));
insRes = a.insert(std::make_pair(&buf[1], 1));
EXPECT_TRUE(insRes.second);
// Check insertion, looking up, and data editing in small mode.
insRes = a.insert(std::make_pair(&buf[1], 6));
EXPECT_FALSE(insRes.second);
EXPECT_EQ(insRes.first->second, 1);
insRes.first->second = 5;
found = a.find(&buf[1]);
EXPECT_NE(found, a.end());
EXPECT_EQ(found->second, 5);
a[&buf[1]] = 10;
EXPECT_EQ(found->second, 10);
// Check "not found" case.
found = a.find(&buf[8]);
EXPECT_EQ(found, a.end());
b.insert(std::make_pair(&buf[2], 2));
std::swap(a, b);
a.swap(b);
std::swap(a, b);
EXPECT_EQ(1U, a.size());
EXPECT_EQ(2U, b.size());
EXPECT_TRUE(a.count(&buf[2]));
EXPECT_TRUE(b.count(&buf[0]));
EXPECT_TRUE(b.count(&buf[1]));
insRes = b.insert(std::make_pair(&buf[3], 3));
EXPECT_TRUE(insRes.second);
// Check insertion, looking up, and data editing in big mode.
insRes = b.insert(std::make_pair(&buf[3], 6));
EXPECT_FALSE(insRes.second);
EXPECT_EQ(insRes.first->second, 3);
insRes.first->second = 7;
found = b.find(&buf[3]);
EXPECT_EQ(found->second, 7);
b[&buf[3]] = 14;
EXPECT_EQ(found->second, 14);
// Check constant looking up.
foundc = b.find(&buf[3]);
EXPECT_EQ(foundc->first, &buf[3]);
EXPECT_EQ(foundc->second, 14);
// Check not found case.
found = b.find(&buf[8]);
EXPECT_EQ(found, b.end());
std::swap(a, b);
a.swap(b);
std::swap(a, b);
EXPECT_EQ(3U, a.size());
EXPECT_EQ(1U, b.size());
EXPECT_TRUE(a.count(&buf[0]));
EXPECT_TRUE(a.count(&buf[1]));
EXPECT_TRUE(a.count(&buf[3]));
EXPECT_TRUE(b.count(&buf[2]));
EXPECT_EQ(b.find(&buf[2])->second, 2);
std::swap(a, b);
a.swap(b);
std::swap(a, b);
EXPECT_EQ(1U, a.size());
EXPECT_EQ(3U, b.size());
EXPECT_TRUE(a.count(&buf[2]));
EXPECT_TRUE(b.count(&buf[0]));
EXPECT_TRUE(b.count(&buf[1]));
EXPECT_TRUE(b.count(&buf[3]));
a.insert(std::make_pair(&buf[4], 4));
a.insert(std::make_pair(&buf[5], 5));
a.insert(std::make_pair(&buf[6], 6));
std::swap(b, a);
EXPECT_EQ(3U, a.size());
EXPECT_EQ(4U, b.size());
EXPECT_TRUE(b.count(&buf[2]));
EXPECT_TRUE(b.count(&buf[4]));
EXPECT_TRUE(b.count(&buf[5]));
EXPECT_TRUE(b.count(&buf[6]));
EXPECT_TRUE(a.count(&buf[0]));
EXPECT_TRUE(a.count(&buf[1]));
EXPECT_TRUE(a.count(&buf[3]));
// Check findAndConstruct
SmallMap<int *, int, 3>::value_type Buf7;
Buf7 = a.FindAndConstruct(&buf[7]);
EXPECT_EQ(Buf7.second, 0);
// Check increments
SmallMap<int *, int, 2> c;
c.insert(std::make_pair(&buf[0], 0));
c.insert(std::make_pair(&buf[1], 1));
// For small mode we know that flat array map is used and we know the
// order of items.
unsigned ii = 0;
for (SmallMap<int *, int, 2>::iterator i = c.begin(), e = c.end();
i != e; ++i, ++ii) {
EXPECT_TRUE((i->first == &buf[0] && i->second == 0 && ii == 0) ||
(i->first == &buf[1] && i->second == 1 && ii == 1));
}
// For big mode DenseMap is used and final order of items is undefined.
c.insert(std::make_pair(&buf[2], 2));
for (SmallMap<int *, int, 2>::iterator i = c.begin(), e = c.end();
i != e; ++i) {
EXPECT_TRUE((i->first == &buf[0] && i->second == 0) ||
(i->first == &buf[1] && i->second == 1) ||
(i->first == &buf[2] && i->second == 2));
}
// Check that iteration only visits elements that actually exist.
SmallMap<int, int, 8> d;
d[0] = 2;
d[1] = 3;
int counts[2] = { 0, 0 };
for (SmallMap<int, int, 8>::iterator I = d.begin(), E = d.end(); I != E;
++I) {
EXPECT_TRUE(I->first == 0 || I->first == 1);
EXPECT_TRUE(I->second == 2 || I->second == 3);
EXPECT_EQ(I->second, I->first + 2);
++counts[I->first];
}
EXPECT_EQ(counts[0], 1);
EXPECT_EQ(counts[1], 1);
}
|