1 //===- llvm/unittest/ADT/SmallMapTest.cpp ------------------------------===//
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 // SmallMap unit tests.
12 //===----------------------------------------------------------------------===//
14 #include "gtest/gtest.h"
15 #include "llvm/ADT/SmallMap.h"
20 TEST(SmallMapTest, GeneralTest) {
24 SmallMap<int *, int, 3> a;
25 SmallMap<int *, int, 3> b;
26 SmallMap<int *, int, 3>::iterator found;
27 std::pair<SmallMap<int *, int, 3>::iterator, bool> insRes;
28 SmallMap<int *, int, 3>::const_iterator foundc;
30 a.insert(std::make_pair(&buf[0], 0));
31 insRes = a.insert(std::make_pair(&buf[1], 1));
32 EXPECT_TRUE(insRes.second);
34 // Check insertion, looking up, and data editing in small mode.
35 insRes = a.insert(std::make_pair(&buf[1], 6));
36 EXPECT_FALSE(insRes.second);
37 EXPECT_EQ(insRes.first->second, 1);
38 insRes.first->second = 5;
39 found = a.find(&buf[1]);
40 EXPECT_NE(found, a.end());
41 EXPECT_EQ(found->second, 5);
43 EXPECT_EQ(found->second, 10);
44 // Check "not found" case.
45 found = a.find(&buf[8]);
46 EXPECT_EQ(found, a.end());
48 b.insert(std::make_pair(&buf[2], 2));
54 EXPECT_EQ(1U, a.size());
55 EXPECT_EQ(2U, b.size());
56 EXPECT_TRUE(a.count(&buf[2]));
57 EXPECT_TRUE(b.count(&buf[0]));
58 EXPECT_TRUE(b.count(&buf[1]));
60 insRes = b.insert(std::make_pair(&buf[3], 3));
61 EXPECT_TRUE(insRes.second);
63 // Check insertion, looking up, and data editing in big mode.
64 insRes = b.insert(std::make_pair(&buf[3], 6));
65 EXPECT_FALSE(insRes.second);
66 EXPECT_EQ(insRes.first->second, 3);
67 insRes.first->second = 7;
68 found = b.find(&buf[3]);
69 EXPECT_EQ(found->second, 7);
71 EXPECT_EQ(found->second, 14);
72 // Check constant looking up.
73 foundc = b.find(&buf[3]);
74 EXPECT_EQ(foundc->first, &buf[3]);
75 EXPECT_EQ(foundc->second, 14);
76 // Check not found case.
77 found = b.find(&buf[8]);
78 EXPECT_EQ(found, b.end());
84 EXPECT_EQ(3U, a.size());
85 EXPECT_EQ(1U, b.size());
86 EXPECT_TRUE(a.count(&buf[0]));
87 EXPECT_TRUE(a.count(&buf[1]));
88 EXPECT_TRUE(a.count(&buf[3]));
89 EXPECT_TRUE(b.count(&buf[2]));
90 EXPECT_EQ(b.find(&buf[2])->second, 2);
96 EXPECT_EQ(1U, a.size());
97 EXPECT_EQ(3U, b.size());
98 EXPECT_TRUE(a.count(&buf[2]));
99 EXPECT_TRUE(b.count(&buf[0]));
100 EXPECT_TRUE(b.count(&buf[1]));
101 EXPECT_TRUE(b.count(&buf[3]));
103 a.insert(std::make_pair(&buf[4], 4));
104 a.insert(std::make_pair(&buf[5], 5));
105 a.insert(std::make_pair(&buf[6], 6));
109 EXPECT_EQ(3U, a.size());
110 EXPECT_EQ(4U, b.size());
111 EXPECT_TRUE(b.count(&buf[2]));
112 EXPECT_TRUE(b.count(&buf[4]));
113 EXPECT_TRUE(b.count(&buf[5]));
114 EXPECT_TRUE(b.count(&buf[6]));
115 EXPECT_TRUE(a.count(&buf[0]));
116 EXPECT_TRUE(a.count(&buf[1]));
117 EXPECT_TRUE(a.count(&buf[3]));
119 // Check findAndConstruct
120 SmallMap<int *, int, 3>::value_type Buf7;
121 Buf7 = a.FindAndConstruct(&buf[7]);
122 EXPECT_EQ(Buf7.second, 0);
126 SmallMap<int *, int, 2> c;
127 c.insert(std::make_pair(&buf[0], 0));
128 c.insert(std::make_pair(&buf[1], 1));
130 // For small mode we know that flat array map is used and we know the
133 for (SmallMap<int *, int, 2>::iterator i = c.begin(), e = c.end();
135 EXPECT_TRUE((i->first == &buf[0] && i->second == 0 && ii == 0) ||
136 (i->first == &buf[1] && i->second == 1 && ii == 1));
139 // For big mode DenseMap is used and final order of items is undefined.
140 c.insert(std::make_pair(&buf[2], 2));
141 for (SmallMap<int *, int, 2>::iterator i = c.begin(), e = c.end();
143 EXPECT_TRUE((i->first == &buf[0] && i->second == 0) ||
144 (i->first == &buf[1] && i->second == 1) ||
145 (i->first == &buf[2] && i->second == 2));