First implementation of:
[oota-llvm.git] / unittests / ADT / SmallMapTest.cpp
1 //===- llvm/unittest/ADT/SmallMapTest.cpp ------------------------------===//
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 //
10 // SmallMap unit tests.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "gtest/gtest.h"
15 #include "llvm/ADT/SmallMap.h"
16
17 using namespace llvm;
18
19 // SmallMap test.
20 TEST(SmallMapTest, GeneralTest) {
21
22   int buf[10];
23
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;
29
30   a.insert(std::make_pair(&buf[0], 0));
31   insRes = a.insert(std::make_pair(&buf[1], 1));
32   EXPECT_TRUE(insRes.second);
33
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);
42   a[&buf[1]] = 10;
43   EXPECT_EQ(found->second, 10);
44   // Check "not found" case.
45   found = a.find(&buf[8]);
46   EXPECT_EQ(found, a.end());
47   
48   // Check increment for small mode.
49   found = a.begin();
50   ++found;
51   EXPECT_EQ(found->second, 10);
52
53   b.insert(std::make_pair(&buf[2], 2));
54
55   std::swap(a, b);
56   a.swap(b);
57   std::swap(a, b);
58
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]));
64
65   insRes = b.insert(std::make_pair(&buf[3], 3));
66   EXPECT_TRUE(insRes.second);
67
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);
75   b[&buf[3]] = 14;
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());
84   
85   // Check increment for big mode.
86   found = b.find(&buf[1]);
87   ++found;
88   EXPECT_EQ(found->second, 14);
89   
90   std::swap(a, b);
91   a.swap(b);
92   std::swap(a, b);
93
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);
101
102   std::swap(a, b);
103   a.swap(b);
104   std::swap(a, b);
105
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]));
112
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));
116
117   std::swap(b, a);
118
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]));
128   
129   // Check findAndConstruct
130   SmallMap<int *, int, 3>::value_type Buf7;
131   Buf7 = a.FindAndConstruct(&buf[7]);
132   EXPECT_EQ(Buf7.second, 0);
133 }