Add a test for assignment operator behavior which was changed in
[oota-llvm.git] / unittests / ADT / SmallPtrSetTest.cpp
1 //===- llvm/unittest/ADT/SmallPtrSetTest.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 // SmallPtrSet unit tests.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "gtest/gtest.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16
17 using namespace llvm;
18
19 TEST(SmallPtrSetTest, Assignment) {
20   int buf[8];
21   for (int i = 0; i < 8; ++i)
22     buf[i] = 0;
23
24   SmallPtrSet<int *, 4> s1;
25   s1.insert(&buf[0]);
26   s1.insert(&buf[1]);
27
28   SmallPtrSet<int *, 4> s2;
29   (s2 = s1).insert(&buf[2]);
30
31   // Self assign as well.
32   (s2 = s2).insert(&buf[3]);
33
34   s1 = s2;
35   EXPECT_EQ(4U, s1.size());
36   for (int i = 0; i < 8; ++i)
37     if (i < 4)
38       EXPECT_TRUE(s1.count(&buf[i]));
39     else
40       EXPECT_FALSE(s1.count(&buf[i]));
41 }
42
43 TEST(SmallPtrSetTest, GrowthTest) {
44   int i;
45   int buf[8];
46   for(i=0; i<8; ++i) buf[i]=0;
47
48
49   SmallPtrSet<int *, 4> s;
50   typedef SmallPtrSet<int *, 4>::iterator iter;
51   
52   s.insert(&buf[0]);
53   s.insert(&buf[1]);
54   s.insert(&buf[2]);
55   s.insert(&buf[3]);
56   EXPECT_EQ(4U, s.size());
57
58   i = 0;
59   for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
60       (**I)++;
61   EXPECT_EQ(4, i);
62   for(i=0; i<8; ++i)
63       EXPECT_EQ(i<4?1:0,buf[i]);
64
65   s.insert(&buf[4]);
66   s.insert(&buf[5]);
67   s.insert(&buf[6]);
68   s.insert(&buf[7]);
69
70   i = 0;
71   for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
72       (**I)++;
73   EXPECT_EQ(8, i);
74   s.erase(&buf[4]);
75   s.erase(&buf[5]);
76   s.erase(&buf[6]);
77   s.erase(&buf[7]);
78   EXPECT_EQ(4U, s.size());
79
80   i = 0;
81   for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
82       (**I)++;
83   EXPECT_EQ(4, i);
84   for(i=0; i<8; ++i)
85       EXPECT_EQ(i<4?3:1,buf[i]);
86
87   s.clear();
88   for(i=0; i<8; ++i) buf[i]=0;
89   for(i=0; i<128; ++i) s.insert(&buf[i%8]); // test repeated entires
90   EXPECT_EQ(8U, s.size());
91   for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
92       (**I)++;
93   for(i=0; i<8; ++i)
94       EXPECT_EQ(1,buf[i]);
95 }
96
97 TEST(SmallPtrSetTest, CopyAndMoveTest) {
98   int buf[8];
99   for (int i = 0; i < 8; ++i)
100     buf[i] = 0;
101
102   SmallPtrSet<int *, 4> s1;
103   s1.insert(&buf[0]);
104   s1.insert(&buf[1]);
105   s1.insert(&buf[2]);
106   s1.insert(&buf[3]);
107   EXPECT_EQ(4U, s1.size());
108   for (int i = 0; i < 8; ++i)
109     if (i < 4)
110       EXPECT_TRUE(s1.count(&buf[i]));
111     else
112       EXPECT_FALSE(s1.count(&buf[i]));
113
114   SmallPtrSet<int *, 4> s2(s1);
115   EXPECT_EQ(4U, s2.size());
116   for (int i = 0; i < 8; ++i)
117     if (i < 4)
118       EXPECT_TRUE(s2.count(&buf[i]));
119     else
120       EXPECT_FALSE(s2.count(&buf[i]));
121
122   s1 = s2;
123   EXPECT_EQ(4U, s1.size());
124   for (int i = 0; i < 8; ++i)
125     if (i < 4)
126       EXPECT_TRUE(s1.count(&buf[i]));
127     else
128       EXPECT_FALSE(s1.count(&buf[i]));
129
130   SmallPtrSet<int *, 4> s3(llvm_move(s1));
131   EXPECT_EQ(4U, s3.size());
132   for (int i = 0; i < 8; ++i)
133     if (i < 4)
134       EXPECT_TRUE(s3.count(&buf[i]));
135     else
136       EXPECT_FALSE(s3.count(&buf[i]));
137
138   // Move assign to the moved-from object.
139   s1 = llvm_move(s3);
140   EXPECT_EQ(4U, s1.size());
141   for (int i = 0; i < 8; ++i)
142     if (i < 4)
143       EXPECT_TRUE(s1.count(&buf[i]));
144     else
145       EXPECT_FALSE(s1.count(&buf[i]));
146 }
147
148 TEST(SmallPtrSetTest, SwapTest) {
149   int buf[10];
150
151   SmallPtrSet<int *, 2> a;
152   SmallPtrSet<int *, 2> b;
153
154   a.insert(&buf[0]);
155   a.insert(&buf[1]);
156   b.insert(&buf[2]);
157
158   std::swap(a, b);
159
160   EXPECT_EQ(1U, a.size());
161   EXPECT_EQ(2U, b.size());
162   EXPECT_TRUE(a.count(&buf[2]));
163   EXPECT_TRUE(b.count(&buf[0]));
164   EXPECT_TRUE(b.count(&buf[1]));
165
166   b.insert(&buf[3]);
167   std::swap(a, b);
168
169   EXPECT_EQ(3U, a.size());
170   EXPECT_EQ(1U, b.size());
171   EXPECT_TRUE(a.count(&buf[0]));
172   EXPECT_TRUE(a.count(&buf[1]));
173   EXPECT_TRUE(a.count(&buf[3]));
174   EXPECT_TRUE(b.count(&buf[2]));
175
176   std::swap(a, b);
177
178   EXPECT_EQ(1U, a.size());
179   EXPECT_EQ(3U, b.size());
180   EXPECT_TRUE(a.count(&buf[2]));
181   EXPECT_TRUE(b.count(&buf[0]));
182   EXPECT_TRUE(b.count(&buf[1]));
183   EXPECT_TRUE(b.count(&buf[3]));
184
185   a.insert(&buf[4]);
186   a.insert(&buf[5]);
187   a.insert(&buf[6]);
188
189   std::swap(b, a);
190
191   EXPECT_EQ(3U, a.size());
192   EXPECT_EQ(4U, b.size());
193   EXPECT_TRUE(b.count(&buf[2]));
194   EXPECT_TRUE(b.count(&buf[4]));
195   EXPECT_TRUE(b.count(&buf[5]));
196   EXPECT_TRUE(b.count(&buf[6]));
197   EXPECT_TRUE(a.count(&buf[0]));
198   EXPECT_TRUE(a.count(&buf[1]));
199   EXPECT_TRUE(a.count(&buf[3]));
200 }