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 // Check increment for small mode.
51 EXPECT_EQ(found->second, 10);
53 b.insert(std::make_pair(&buf[2], 2));
59 EXPECT_EQ(1U, a.size());
60 EXPECT_EQ(2U, b.size());
61 EXPECT_TRUE(a.count(&buf[2]));
62 EXPECT_TRUE(b.count(&buf[0]));
63 EXPECT_TRUE(b.count(&buf[1]));
65 insRes = b.insert(std::make_pair(&buf[3], 3));
66 EXPECT_TRUE(insRes.second);
68 // Check insertion, looking up, and data editing in big mode.
69 insRes = b.insert(std::make_pair(&buf[3], 6));
70 EXPECT_FALSE(insRes.second);
71 EXPECT_EQ(insRes.first->second, 3);
72 insRes.first->second = 7;
73 found = b.find(&buf[3]);
74 EXPECT_EQ(found->second, 7);
76 EXPECT_EQ(found->second, 14);
77 // Check constant looking up.
78 foundc = b.find(&buf[3]);
79 EXPECT_EQ(foundc->first, &buf[3]);
80 EXPECT_EQ(foundc->second, 14);
81 // Check not found case.
82 found = b.find(&buf[8]);
83 EXPECT_EQ(found, b.end());
85 // Check increment for big mode.
86 found = b.find(&buf[1]);
88 EXPECT_EQ(found->second, 14);
94 EXPECT_EQ(3U, a.size());
95 EXPECT_EQ(1U, b.size());
96 EXPECT_TRUE(a.count(&buf[0]));
97 EXPECT_TRUE(a.count(&buf[1]));
98 EXPECT_TRUE(a.count(&buf[3]));
99 EXPECT_TRUE(b.count(&buf[2]));
100 EXPECT_EQ(b.find(&buf[2])->second, 2);
106 EXPECT_EQ(1U, a.size());
107 EXPECT_EQ(3U, b.size());
108 EXPECT_TRUE(a.count(&buf[2]));
109 EXPECT_TRUE(b.count(&buf[0]));
110 EXPECT_TRUE(b.count(&buf[1]));
111 EXPECT_TRUE(b.count(&buf[3]));
113 a.insert(std::make_pair(&buf[4], 4));
114 a.insert(std::make_pair(&buf[5], 5));
115 a.insert(std::make_pair(&buf[6], 6));
119 EXPECT_EQ(3U, a.size());
120 EXPECT_EQ(4U, b.size());
121 EXPECT_TRUE(b.count(&buf[2]));
122 EXPECT_TRUE(b.count(&buf[4]));
123 EXPECT_TRUE(b.count(&buf[5]));
124 EXPECT_TRUE(b.count(&buf[6]));
125 EXPECT_TRUE(a.count(&buf[0]));
126 EXPECT_TRUE(a.count(&buf[1]));
127 EXPECT_TRUE(a.count(&buf[3]));
129 // Check findAndConstruct
130 SmallMap<int *, int, 3>::value_type Buf7;
131 Buf7 = a.FindAndConstruct(&buf[7]);
132 EXPECT_EQ(Buf7.second, 0);