1 //===- llvm/unittest/ADT/SmallPtrSetTest.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 // SmallPtrSet unit tests.
12 //===----------------------------------------------------------------------===//
14 #include "gtest/gtest.h"
15 #include "llvm/ADT/SmallPtrSet.h"
19 TEST(SmallPtrSetTest, Assignment) {
21 for (int i = 0; i < 8; ++i)
24 SmallPtrSet<int *, 4> s1;
28 SmallPtrSet<int *, 4> s2;
29 (s2 = s1).insert(&buf[2]);
31 // Self assign as well.
32 (s2 = s2).insert(&buf[3]);
35 EXPECT_EQ(4U, s1.size());
36 for (int i = 0; i < 8; ++i)
38 EXPECT_TRUE(s1.count(&buf[i]));
40 EXPECT_FALSE(s1.count(&buf[i]));
43 TEST(SmallPtrSetTest, GrowthTest) {
46 for(i=0; i<8; ++i) buf[i]=0;
49 SmallPtrSet<int *, 4> s;
50 typedef SmallPtrSet<int *, 4>::iterator iter;
56 EXPECT_EQ(4U, s.size());
59 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
63 EXPECT_EQ(i<4?1:0,buf[i]);
71 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
78 EXPECT_EQ(4U, s.size());
81 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
85 EXPECT_EQ(i<4?3:1,buf[i]);
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)
97 TEST(SmallPtrSetTest, CopyAndMoveTest) {
99 for (int i = 0; i < 8; ++i)
102 SmallPtrSet<int *, 4> s1;
107 EXPECT_EQ(4U, s1.size());
108 for (int i = 0; i < 8; ++i)
110 EXPECT_TRUE(s1.count(&buf[i]));
112 EXPECT_FALSE(s1.count(&buf[i]));
114 SmallPtrSet<int *, 4> s2(s1);
115 EXPECT_EQ(4U, s2.size());
116 for (int i = 0; i < 8; ++i)
118 EXPECT_TRUE(s2.count(&buf[i]));
120 EXPECT_FALSE(s2.count(&buf[i]));
123 EXPECT_EQ(4U, s1.size());
124 EXPECT_EQ(4U, s2.size());
125 for (int i = 0; i < 8; ++i)
127 EXPECT_TRUE(s1.count(&buf[i]));
129 EXPECT_FALSE(s1.count(&buf[i]));
131 SmallPtrSet<int *, 4> s3(std::move(s1));
132 EXPECT_EQ(4U, s3.size());
133 EXPECT_TRUE(s1.empty());
134 for (int i = 0; i < 8; ++i)
136 EXPECT_TRUE(s3.count(&buf[i]));
138 EXPECT_FALSE(s3.count(&buf[i]));
140 // Move assign into the moved-from object. Also test move of a non-small
147 EXPECT_EQ(8U, s1.size());
148 EXPECT_TRUE(s3.empty());
149 for (int i = 0; i < 8; ++i)
150 EXPECT_TRUE(s1.count(&buf[i]));
152 // Copy assign into a moved-from object.
154 EXPECT_EQ(8U, s3.size());
155 EXPECT_EQ(8U, s1.size());
156 for (int i = 0; i < 8; ++i)
157 EXPECT_TRUE(s3.count(&buf[i]));
160 TEST(SmallPtrSetTest, SwapTest) {
163 SmallPtrSet<int *, 2> a;
164 SmallPtrSet<int *, 2> b;
172 EXPECT_EQ(1U, a.size());
173 EXPECT_EQ(2U, b.size());
174 EXPECT_TRUE(a.count(&buf[2]));
175 EXPECT_TRUE(b.count(&buf[0]));
176 EXPECT_TRUE(b.count(&buf[1]));
181 EXPECT_EQ(3U, a.size());
182 EXPECT_EQ(1U, b.size());
183 EXPECT_TRUE(a.count(&buf[0]));
184 EXPECT_TRUE(a.count(&buf[1]));
185 EXPECT_TRUE(a.count(&buf[3]));
186 EXPECT_TRUE(b.count(&buf[2]));
190 EXPECT_EQ(1U, a.size());
191 EXPECT_EQ(3U, b.size());
192 EXPECT_TRUE(a.count(&buf[2]));
193 EXPECT_TRUE(b.count(&buf[0]));
194 EXPECT_TRUE(b.count(&buf[1]));
195 EXPECT_TRUE(b.count(&buf[3]));
203 EXPECT_EQ(3U, a.size());
204 EXPECT_EQ(4U, b.size());
205 EXPECT_TRUE(b.count(&buf[2]));
206 EXPECT_TRUE(b.count(&buf[4]));
207 EXPECT_TRUE(b.count(&buf[5]));
208 EXPECT_TRUE(b.count(&buf[6]));
209 EXPECT_TRUE(a.count(&buf[0]));
210 EXPECT_TRUE(a.count(&buf[1]));
211 EXPECT_TRUE(a.count(&buf[3]));