blob: 1b564ac05ac008b73564ef0a68fdfbad207660ba [file] [log] [blame]
Benjamin Kramere1c34e92012-03-06 20:40:02 +00001//===- 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
17using namespace llvm;
18
19// SmallPtrSet swapping test.
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +000020TEST(SmallPtrSetTest, GrowthTest) {
21 int i;
22 int buf[8];
23 for(i=0; i<8; ++i) buf[i]=0;
24
25
26 SmallPtrSet<int *, 4> s;
27 typedef SmallPtrSet<int *, 4>::iterator iter;
28
29 s.insert(&buf[0]);
30 s.insert(&buf[1]);
31 s.insert(&buf[2]);
32 s.insert(&buf[3]);
33 EXPECT_EQ(4U, s.size());
34
35 i = 0;
36 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
37 (**I)++;
38 EXPECT_EQ(4, i);
39 for(i=0; i<8; ++i)
40 EXPECT_EQ(i<4?1:0,buf[i]);
41
42 s.insert(&buf[4]);
43 s.insert(&buf[5]);
44 s.insert(&buf[6]);
45 s.insert(&buf[7]);
46
47 i = 0;
48 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
49 (**I)++;
50 EXPECT_EQ(8, i);
51 s.erase(&buf[4]);
52 s.erase(&buf[5]);
53 s.erase(&buf[6]);
54 s.erase(&buf[7]);
55 EXPECT_EQ(4U, s.size());
56
57 i = 0;
58 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
59 (**I)++;
60 EXPECT_EQ(4, i);
61 for(i=0; i<8; ++i)
62 EXPECT_EQ(i<4?3:1,buf[i]);
63
64 s.clear();
65 for(i=0; i<8; ++i) buf[i]=0;
66 for(i=0; i<128; ++i) s.insert(&buf[i%8]); // test repeated entires
67 EXPECT_EQ(8U, s.size());
68 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
69 (**I)++;
70 for(i=0; i<8; ++i)
71 EXPECT_EQ(1,buf[i]);
72}
73
Chandler Carruth55758e92013-11-20 11:14:33 +000074TEST(SmallPtrSetTest, CopyAndMoveTest) {
75 int buf[8];
76 for (int i = 0; i < 8; ++i)
77 buf[i] = 0;
78
79 SmallPtrSet<int *, 4> s1;
80 s1.insert(&buf[0]);
81 s1.insert(&buf[1]);
82 s1.insert(&buf[2]);
83 s1.insert(&buf[3]);
84 EXPECT_EQ(4U, s1.size());
85 for (int i = 0; i < 8; ++i)
86 if (i < 4)
87 EXPECT_TRUE(s1.count(&buf[i]));
88 else
89 EXPECT_FALSE(s1.count(&buf[i]));
90
91 SmallPtrSet<int *, 4> s2(s1);
92 EXPECT_EQ(4U, s2.size());
93 for (int i = 0; i < 8; ++i)
94 if (i < 4)
95 EXPECT_TRUE(s2.count(&buf[i]));
96 else
97 EXPECT_FALSE(s2.count(&buf[i]));
98
99 s1 = s2;
100 EXPECT_EQ(4U, s1.size());
101 for (int i = 0; i < 8; ++i)
102 if (i < 4)
103 EXPECT_TRUE(s1.count(&buf[i]));
104 else
105 EXPECT_FALSE(s1.count(&buf[i]));
106
107 SmallPtrSet<int *, 4> s3(llvm_move(s1));
108 EXPECT_EQ(4U, s3.size());
109 for (int i = 0; i < 8; ++i)
110 if (i < 4)
111 EXPECT_TRUE(s3.count(&buf[i]));
112 else
113 EXPECT_FALSE(s3.count(&buf[i]));
114
115 s1 = llvm_move(s3);
116 EXPECT_EQ(4U, s1.size());
117 for (int i = 0; i < 8; ++i)
118 if (i < 4)
119 EXPECT_TRUE(s1.count(&buf[i]));
120 else
121 EXPECT_FALSE(s1.count(&buf[i]));
122}
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000123
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000124TEST(SmallPtrSetTest, SwapTest) {
125 int buf[10];
126
127 SmallPtrSet<int *, 2> a;
128 SmallPtrSet<int *, 2> b;
129
130 a.insert(&buf[0]);
131 a.insert(&buf[1]);
132 b.insert(&buf[2]);
133
134 std::swap(a, b);
135
136 EXPECT_EQ(1U, a.size());
137 EXPECT_EQ(2U, b.size());
138 EXPECT_TRUE(a.count(&buf[2]));
139 EXPECT_TRUE(b.count(&buf[0]));
140 EXPECT_TRUE(b.count(&buf[1]));
141
142 b.insert(&buf[3]);
143 std::swap(a, b);
144
145 EXPECT_EQ(3U, a.size());
146 EXPECT_EQ(1U, b.size());
147 EXPECT_TRUE(a.count(&buf[0]));
148 EXPECT_TRUE(a.count(&buf[1]));
149 EXPECT_TRUE(a.count(&buf[3]));
150 EXPECT_TRUE(b.count(&buf[2]));
151
152 std::swap(a, b);
153
154 EXPECT_EQ(1U, a.size());
155 EXPECT_EQ(3U, b.size());
156 EXPECT_TRUE(a.count(&buf[2]));
157 EXPECT_TRUE(b.count(&buf[0]));
158 EXPECT_TRUE(b.count(&buf[1]));
159 EXPECT_TRUE(b.count(&buf[3]));
160
161 a.insert(&buf[4]);
162 a.insert(&buf[5]);
163 a.insert(&buf[6]);
164
165 std::swap(b, a);
166
167 EXPECT_EQ(3U, a.size());
168 EXPECT_EQ(4U, b.size());
169 EXPECT_TRUE(b.count(&buf[2]));
170 EXPECT_TRUE(b.count(&buf[4]));
171 EXPECT_TRUE(b.count(&buf[5]));
172 EXPECT_TRUE(b.count(&buf[6]));
173 EXPECT_TRUE(a.count(&buf[0]));
174 EXPECT_TRUE(a.count(&buf[1]));
175 EXPECT_TRUE(a.count(&buf[3]));
176}