xref: /aosp_15_r20/external/llvm/unittests/ADT/TinyPtrVectorTest.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- llvm/unittest/ADT/TinyPtrVectorTest.cpp ----------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // TinyPtrVector unit tests.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/TinyPtrVector.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/ArrayRef.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/type_traits.h"
18*9880d681SAndroid Build Coastguard Worker #include "gtest/gtest.h"
19*9880d681SAndroid Build Coastguard Worker #include <algorithm>
20*9880d681SAndroid Build Coastguard Worker #include <vector>
21*9880d681SAndroid Build Coastguard Worker 
22*9880d681SAndroid Build Coastguard Worker using namespace llvm;
23*9880d681SAndroid Build Coastguard Worker 
24*9880d681SAndroid Build Coastguard Worker namespace {
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker // The world's worst RNG, but it is deterministic and makes it easy to get
27*9880d681SAndroid Build Coastguard Worker // *some* shuffling of elements.
test_shuffle_rng(ptrdiff_t i)28*9880d681SAndroid Build Coastguard Worker static ptrdiff_t test_shuffle_rng(ptrdiff_t i) {
29*9880d681SAndroid Build Coastguard Worker   return (i + i * 33) % i;
30*9880d681SAndroid Build Coastguard Worker }
31*9880d681SAndroid Build Coastguard Worker static ptrdiff_t (*test_shuffle_rng_p)(ptrdiff_t) = &test_shuffle_rng;
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker template <typename VectorT>
34*9880d681SAndroid Build Coastguard Worker class TinyPtrVectorTest : public testing::Test {
35*9880d681SAndroid Build Coastguard Worker protected:
36*9880d681SAndroid Build Coastguard Worker   typedef typename VectorT::value_type PtrT;
37*9880d681SAndroid Build Coastguard Worker   typedef typename std::remove_pointer<PtrT>::type ValueT;
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker   VectorT V;
40*9880d681SAndroid Build Coastguard Worker   VectorT V2;
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker   ValueT TestValues[1024];
43*9880d681SAndroid Build Coastguard Worker   std::vector<PtrT> TestPtrs;
44*9880d681SAndroid Build Coastguard Worker 
TinyPtrVectorTest()45*9880d681SAndroid Build Coastguard Worker   TinyPtrVectorTest() {
46*9880d681SAndroid Build Coastguard Worker     for (size_t i = 0, e = array_lengthof(TestValues); i != e; ++i)
47*9880d681SAndroid Build Coastguard Worker       TestPtrs.push_back(&TestValues[i]);
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker     std::random_shuffle(TestPtrs.begin(), TestPtrs.end(), test_shuffle_rng_p);
50*9880d681SAndroid Build Coastguard Worker   }
51*9880d681SAndroid Build Coastguard Worker 
testArray(size_t N)52*9880d681SAndroid Build Coastguard Worker   ArrayRef<PtrT> testArray(size_t N) {
53*9880d681SAndroid Build Coastguard Worker     return makeArrayRef(&TestPtrs[0], N);
54*9880d681SAndroid Build Coastguard Worker   }
55*9880d681SAndroid Build Coastguard Worker 
appendValues(VectorT & V,ArrayRef<PtrT> Values)56*9880d681SAndroid Build Coastguard Worker   void appendValues(VectorT &V, ArrayRef<PtrT> Values) {
57*9880d681SAndroid Build Coastguard Worker     for (size_t i = 0, e = Values.size(); i != e; ++i)
58*9880d681SAndroid Build Coastguard Worker       V.push_back(Values[i]);
59*9880d681SAndroid Build Coastguard Worker   }
60*9880d681SAndroid Build Coastguard Worker 
setVectors(ArrayRef<PtrT> Values1,ArrayRef<PtrT> Values2)61*9880d681SAndroid Build Coastguard Worker   void setVectors(ArrayRef<PtrT> Values1, ArrayRef<PtrT> Values2) {
62*9880d681SAndroid Build Coastguard Worker     V.clear();
63*9880d681SAndroid Build Coastguard Worker     appendValues(V, Values1);
64*9880d681SAndroid Build Coastguard Worker     V2.clear();
65*9880d681SAndroid Build Coastguard Worker     appendValues(V2, Values2);
66*9880d681SAndroid Build Coastguard Worker   }
67*9880d681SAndroid Build Coastguard Worker 
expectValues(const VectorT & V,ArrayRef<PtrT> Values)68*9880d681SAndroid Build Coastguard Worker   void expectValues(const VectorT &V, ArrayRef<PtrT> Values) {
69*9880d681SAndroid Build Coastguard Worker     EXPECT_EQ(Values.empty(), V.empty());
70*9880d681SAndroid Build Coastguard Worker     EXPECT_EQ(Values.size(), V.size());
71*9880d681SAndroid Build Coastguard Worker     for (size_t i = 0, e = Values.size(); i != e; ++i) {
72*9880d681SAndroid Build Coastguard Worker       EXPECT_EQ(Values[i], V[i]);
73*9880d681SAndroid Build Coastguard Worker       EXPECT_EQ(Values[i], *std::next(V.begin(), i));
74*9880d681SAndroid Build Coastguard Worker     }
75*9880d681SAndroid Build Coastguard Worker     EXPECT_EQ(V.end(), std::next(V.begin(), Values.size()));
76*9880d681SAndroid Build Coastguard Worker   }
77*9880d681SAndroid Build Coastguard Worker };
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker typedef ::testing::Types<TinyPtrVector<int*>,
80*9880d681SAndroid Build Coastguard Worker                          TinyPtrVector<double*>
81*9880d681SAndroid Build Coastguard Worker                          > TinyPtrVectorTestTypes;
82*9880d681SAndroid Build Coastguard Worker TYPED_TEST_CASE(TinyPtrVectorTest, TinyPtrVectorTestTypes);
83*9880d681SAndroid Build Coastguard Worker 
TYPED_TEST(TinyPtrVectorTest,EmptyTest)84*9880d681SAndroid Build Coastguard Worker TYPED_TEST(TinyPtrVectorTest, EmptyTest) {
85*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
86*9880d681SAndroid Build Coastguard Worker }
87*9880d681SAndroid Build Coastguard Worker 
TYPED_TEST(TinyPtrVectorTest,PushPopBack)88*9880d681SAndroid Build Coastguard Worker TYPED_TEST(TinyPtrVectorTest, PushPopBack) {
89*9880d681SAndroid Build Coastguard Worker   this->V.push_back(this->TestPtrs[0]);
90*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
91*9880d681SAndroid Build Coastguard Worker   this->V.push_back(this->TestPtrs[1]);
92*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
93*9880d681SAndroid Build Coastguard Worker   this->V.push_back(this->TestPtrs[2]);
94*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(3));
95*9880d681SAndroid Build Coastguard Worker   this->V.push_back(this->TestPtrs[3]);
96*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(4));
97*9880d681SAndroid Build Coastguard Worker   this->V.push_back(this->TestPtrs[4]);
98*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(5));
99*9880d681SAndroid Build Coastguard Worker 
100*9880d681SAndroid Build Coastguard Worker   // Pop and clobber a few values to keep things interesting.
101*9880d681SAndroid Build Coastguard Worker   this->V.pop_back();
102*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(4));
103*9880d681SAndroid Build Coastguard Worker   this->V.pop_back();
104*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(3));
105*9880d681SAndroid Build Coastguard Worker   this->TestPtrs[3] = &this->TestValues[42];
106*9880d681SAndroid Build Coastguard Worker   this->TestPtrs[4] = &this->TestValues[43];
107*9880d681SAndroid Build Coastguard Worker   this->V.push_back(this->TestPtrs[3]);
108*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(4));
109*9880d681SAndroid Build Coastguard Worker   this->V.push_back(this->TestPtrs[4]);
110*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(5));
111*9880d681SAndroid Build Coastguard Worker 
112*9880d681SAndroid Build Coastguard Worker   this->V.pop_back();
113*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(4));
114*9880d681SAndroid Build Coastguard Worker   this->V.pop_back();
115*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(3));
116*9880d681SAndroid Build Coastguard Worker   this->V.pop_back();
117*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
118*9880d681SAndroid Build Coastguard Worker   this->V.pop_back();
119*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
120*9880d681SAndroid Build Coastguard Worker   this->V.pop_back();
121*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
122*9880d681SAndroid Build Coastguard Worker 
123*9880d681SAndroid Build Coastguard Worker   this->appendValues(this->V, this->testArray(42));
124*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
125*9880d681SAndroid Build Coastguard Worker }
126*9880d681SAndroid Build Coastguard Worker 
TYPED_TEST(TinyPtrVectorTest,ClearTest)127*9880d681SAndroid Build Coastguard Worker TYPED_TEST(TinyPtrVectorTest, ClearTest) {
128*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
129*9880d681SAndroid Build Coastguard Worker   this->V.clear();
130*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker   this->appendValues(this->V, this->testArray(1));
133*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
134*9880d681SAndroid Build Coastguard Worker   this->V.clear();
135*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
136*9880d681SAndroid Build Coastguard Worker 
137*9880d681SAndroid Build Coastguard Worker   this->appendValues(this->V, this->testArray(42));
138*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
139*9880d681SAndroid Build Coastguard Worker   this->V.clear();
140*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
141*9880d681SAndroid Build Coastguard Worker }
142*9880d681SAndroid Build Coastguard Worker 
TYPED_TEST(TinyPtrVectorTest,CopyAndMoveCtorTest)143*9880d681SAndroid Build Coastguard Worker TYPED_TEST(TinyPtrVectorTest, CopyAndMoveCtorTest) {
144*9880d681SAndroid Build Coastguard Worker   this->appendValues(this->V, this->testArray(42));
145*9880d681SAndroid Build Coastguard Worker   TypeParam Copy(this->V);
146*9880d681SAndroid Build Coastguard Worker   this->expectValues(Copy, this->testArray(42));
147*9880d681SAndroid Build Coastguard Worker 
148*9880d681SAndroid Build Coastguard Worker   // This is a separate copy, and so it shouldn't destroy the original.
149*9880d681SAndroid Build Coastguard Worker   Copy.clear();
150*9880d681SAndroid Build Coastguard Worker   this->expectValues(Copy, this->testArray(0));
151*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
152*9880d681SAndroid Build Coastguard Worker 
153*9880d681SAndroid Build Coastguard Worker   TypeParam Copy2(this->V2);
154*9880d681SAndroid Build Coastguard Worker   this->appendValues(Copy2, this->testArray(42));
155*9880d681SAndroid Build Coastguard Worker   this->expectValues(Copy2, this->testArray(42));
156*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(0));
157*9880d681SAndroid Build Coastguard Worker 
158*9880d681SAndroid Build Coastguard Worker   TypeParam Move(std::move(Copy2));
159*9880d681SAndroid Build Coastguard Worker   this->expectValues(Move, this->testArray(42));
160*9880d681SAndroid Build Coastguard Worker   this->expectValues(Copy2, this->testArray(0));
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker 
TYPED_TEST(TinyPtrVectorTest,CopyAndMoveTest)163*9880d681SAndroid Build Coastguard Worker TYPED_TEST(TinyPtrVectorTest, CopyAndMoveTest) {
164*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
165*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
166*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(0));
167*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
168*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
169*9880d681SAndroid Build Coastguard Worker 
170*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(1), this->testArray(0));
171*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
172*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
173*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(0));
174*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(1), this->testArray(0));
175*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
176*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
177*9880d681SAndroid Build Coastguard Worker 
178*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(2), this->testArray(0));
179*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
180*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
181*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(0));
182*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(2), this->testArray(0));
183*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
184*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
185*9880d681SAndroid Build Coastguard Worker 
186*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(42), this->testArray(0));
187*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
188*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
189*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(0));
190*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(42), this->testArray(0));
191*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
192*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
193*9880d681SAndroid Build Coastguard Worker 
194*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(0), this->testArray(1));
195*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
196*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
197*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(1));
198*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(0), this->testArray(1));
199*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
200*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
201*9880d681SAndroid Build Coastguard Worker 
202*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(0), this->testArray(2));
203*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
204*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
205*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(2));
206*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(0), this->testArray(2));
207*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
208*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
209*9880d681SAndroid Build Coastguard Worker 
210*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(0), this->testArray(42));
211*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
212*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
213*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(42));
214*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(0), this->testArray(42));
215*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
216*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
217*9880d681SAndroid Build Coastguard Worker 
218*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(1), this->testArray(1));
219*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
220*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
221*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(1));
222*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
223*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
224*9880d681SAndroid Build Coastguard Worker 
225*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(1), this->testArray(2));
226*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
227*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
228*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(2));
229*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(1), this->testArray(2));
230*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
231*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
232*9880d681SAndroid Build Coastguard Worker 
233*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(1), this->testArray(42));
234*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
235*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
236*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(42));
237*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(1), this->testArray(42));
238*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
239*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
240*9880d681SAndroid Build Coastguard Worker 
241*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(2), this->testArray(1));
242*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
243*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
244*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(1));
245*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(2), this->testArray(1));
246*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
247*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
248*9880d681SAndroid Build Coastguard Worker 
249*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(2), this->testArray(2));
250*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
251*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
252*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(2));
253*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(2), this->testArray(2));
254*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
255*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
256*9880d681SAndroid Build Coastguard Worker 
257*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(2), this->testArray(42));
258*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
259*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
260*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(42));
261*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(2), this->testArray(42));
262*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
263*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
264*9880d681SAndroid Build Coastguard Worker 
265*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(42), this->testArray(1));
266*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
267*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
268*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(1));
269*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(42), this->testArray(1));
270*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
271*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
272*9880d681SAndroid Build Coastguard Worker 
273*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(42), this->testArray(2));
274*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
275*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
276*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(2));
277*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(42), this->testArray(2));
278*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
279*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
280*9880d681SAndroid Build Coastguard Worker 
281*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(42), this->testArray(42));
282*9880d681SAndroid Build Coastguard Worker   this->V = this->V2;
283*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
284*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V2, this->testArray(42));
285*9880d681SAndroid Build Coastguard Worker   this->setVectors(this->testArray(42), this->testArray(42));
286*9880d681SAndroid Build Coastguard Worker   this->V = std::move(this->V2);
287*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
288*9880d681SAndroid Build Coastguard Worker }
289*9880d681SAndroid Build Coastguard Worker 
TYPED_TEST(TinyPtrVectorTest,EraseTest)290*9880d681SAndroid Build Coastguard Worker TYPED_TEST(TinyPtrVectorTest, EraseTest) {
291*9880d681SAndroid Build Coastguard Worker   this->appendValues(this->V, this->testArray(1));
292*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
293*9880d681SAndroid Build Coastguard Worker   this->V.erase(this->V.begin());
294*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
295*9880d681SAndroid Build Coastguard Worker 
296*9880d681SAndroid Build Coastguard Worker   this->appendValues(this->V, this->testArray(42));
297*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
298*9880d681SAndroid Build Coastguard Worker   this->V.erase(this->V.begin());
299*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(this->TestPtrs.begin());
300*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(41));
301*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 1));
302*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 1));
303*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(40));
304*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 2));
305*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 2));
306*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(39));
307*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 5));
308*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 5));
309*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(38));
310*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 13));
311*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 13));
312*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(37));
313*9880d681SAndroid Build Coastguard Worker 
314*9880d681SAndroid Build Coastguard Worker   typename TypeParam::iterator I = this->V.begin();
315*9880d681SAndroid Build Coastguard Worker   do {
316*9880d681SAndroid Build Coastguard Worker     I = this->V.erase(I);
317*9880d681SAndroid Build Coastguard Worker   } while (I != this->V.end());
318*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
319*9880d681SAndroid Build Coastguard Worker }
320*9880d681SAndroid Build Coastguard Worker 
TYPED_TEST(TinyPtrVectorTest,EraseRangeTest)321*9880d681SAndroid Build Coastguard Worker TYPED_TEST(TinyPtrVectorTest, EraseRangeTest) {
322*9880d681SAndroid Build Coastguard Worker   this->appendValues(this->V, this->testArray(1));
323*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
324*9880d681SAndroid Build Coastguard Worker   this->V.erase(this->V.begin(), this->V.begin());
325*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
326*9880d681SAndroid Build Coastguard Worker   this->V.erase(this->V.end(), this->V.end());
327*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
328*9880d681SAndroid Build Coastguard Worker   this->V.erase(this->V.begin(), this->V.end());
329*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
330*9880d681SAndroid Build Coastguard Worker 
331*9880d681SAndroid Build Coastguard Worker   this->appendValues(this->V, this->testArray(42));
332*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
333*9880d681SAndroid Build Coastguard Worker   this->V.erase(this->V.begin(), std::next(this->V.begin(), 1));
334*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(this->TestPtrs.begin(),
335*9880d681SAndroid Build Coastguard Worker                        std::next(this->TestPtrs.begin(), 1));
336*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(41));
337*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 1), std::next(this->V.begin(), 2));
338*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 1),
339*9880d681SAndroid Build Coastguard Worker                        std::next(this->TestPtrs.begin(), 2));
340*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(40));
341*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 2), std::next(this->V.begin(), 4));
342*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 2),
343*9880d681SAndroid Build Coastguard Worker                        std::next(this->TestPtrs.begin(), 4));
344*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(38));
345*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 5), std::next(this->V.begin(), 10));
346*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 5),
347*9880d681SAndroid Build Coastguard Worker                        std::next(this->TestPtrs.begin(), 10));
348*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(33));
349*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 13), std::next(this->V.begin(), 26));
350*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.erase(std::next(this->TestPtrs.begin(), 13),
351*9880d681SAndroid Build Coastguard Worker                        std::next(this->TestPtrs.begin(), 26));
352*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(20));
353*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 7), this->V.end());
354*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(7));
355*9880d681SAndroid Build Coastguard Worker   this->V.erase(this->V.begin(), this->V.end());
356*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
357*9880d681SAndroid Build Coastguard Worker }
358*9880d681SAndroid Build Coastguard Worker 
TYPED_TEST(TinyPtrVectorTest,Insert)359*9880d681SAndroid Build Coastguard Worker TYPED_TEST(TinyPtrVectorTest, Insert) {
360*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.end(), this->TestPtrs[0]);
361*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
362*9880d681SAndroid Build Coastguard Worker   this->V.clear();
363*9880d681SAndroid Build Coastguard Worker   this->appendValues(this->V, this->testArray(4));
364*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(4));
365*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.end(), this->TestPtrs[4]);
366*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(5));
367*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.begin(), this->TestPtrs[42]);
368*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.insert(this->TestPtrs.begin(), this->TestPtrs[42]);
369*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(6));
370*9880d681SAndroid Build Coastguard Worker   this->V.insert(std::next(this->V.begin(), 3), this->TestPtrs[43]);
371*9880d681SAndroid Build Coastguard Worker   this->TestPtrs.insert(std::next(this->TestPtrs.begin(), 3),
372*9880d681SAndroid Build Coastguard Worker                         this->TestPtrs[43]);
373*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(7));
374*9880d681SAndroid Build Coastguard Worker }
375*9880d681SAndroid Build Coastguard Worker 
TYPED_TEST(TinyPtrVectorTest,InsertRange)376*9880d681SAndroid Build Coastguard Worker TYPED_TEST(TinyPtrVectorTest, InsertRange) {
377*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.end(), this->TestPtrs.begin(), this->TestPtrs.begin());
378*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
379*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.begin(), this->TestPtrs.begin(),
380*9880d681SAndroid Build Coastguard Worker                  this->TestPtrs.begin());
381*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
382*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.end(), this->TestPtrs.end(), this->TestPtrs.end());
383*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(0));
384*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.end(), this->TestPtrs.begin(),
385*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin()));
386*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(1));
387*9880d681SAndroid Build Coastguard Worker   this->V.clear();
388*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.end(), this->TestPtrs.begin(),
389*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 2));
390*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(2));
391*9880d681SAndroid Build Coastguard Worker   this->V.clear();
392*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.end(), this->TestPtrs.begin(),
393*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 42));
394*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(42));
395*9880d681SAndroid Build Coastguard Worker   this->V.clear();
396*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.end(),
397*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 5),
398*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 13));
399*9880d681SAndroid Build Coastguard Worker   this->V.insert(this->V.begin(),
400*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 0),
401*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 3));
402*9880d681SAndroid Build Coastguard Worker   this->V.insert(std::next(this->V.begin(), 2),
403*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 2),
404*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 4));
405*9880d681SAndroid Build Coastguard Worker   this->V.erase(std::next(this->V.begin(), 4));
406*9880d681SAndroid Build Coastguard Worker   this->V.insert(std::next(this->V.begin(), 4),
407*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 4),
408*9880d681SAndroid Build Coastguard Worker                  std::next(this->TestPtrs.begin(), 5));
409*9880d681SAndroid Build Coastguard Worker   this->expectValues(this->V, this->testArray(13));
410*9880d681SAndroid Build Coastguard Worker }
411*9880d681SAndroid Build Coastguard Worker 
412*9880d681SAndroid Build Coastguard Worker }
413*9880d681SAndroid Build Coastguard Worker 
TEST(TinyPtrVectorTest,SingleEltCtorTest)414*9880d681SAndroid Build Coastguard Worker TEST(TinyPtrVectorTest, SingleEltCtorTest) {
415*9880d681SAndroid Build Coastguard Worker   int v = 55;
416*9880d681SAndroid Build Coastguard Worker   TinyPtrVector<int *> V(&v);
417*9880d681SAndroid Build Coastguard Worker 
418*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(V.size() == 1);
419*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(V.empty());
420*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(V.front() == &v);
421*9880d681SAndroid Build Coastguard Worker }
422*9880d681SAndroid Build Coastguard Worker 
TEST(TinyPtrVectorTest,ArrayRefCtorTest)423*9880d681SAndroid Build Coastguard Worker TEST(TinyPtrVectorTest, ArrayRefCtorTest) {
424*9880d681SAndroid Build Coastguard Worker   int data_array[128];
425*9880d681SAndroid Build Coastguard Worker   std::vector<int *> data;
426*9880d681SAndroid Build Coastguard Worker 
427*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = 128; i != e; ++i) {
428*9880d681SAndroid Build Coastguard Worker     data_array[i] = 324 - int(i);
429*9880d681SAndroid Build Coastguard Worker     data.push_back(&data_array[i]);
430*9880d681SAndroid Build Coastguard Worker   }
431*9880d681SAndroid Build Coastguard Worker 
432*9880d681SAndroid Build Coastguard Worker   TinyPtrVector<int *> V(data);
433*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(V.size() == 128);
434*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(V.empty());
435*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = 128; i != e; ++i) {
436*9880d681SAndroid Build Coastguard Worker     EXPECT_TRUE(V[i] == data[i]);
437*9880d681SAndroid Build Coastguard Worker   }
438*9880d681SAndroid Build Coastguard Worker }
439*9880d681SAndroid Build Coastguard Worker 
TEST(TinyPtrVectorTest,MutableArrayRefTest)440*9880d681SAndroid Build Coastguard Worker TEST(TinyPtrVectorTest, MutableArrayRefTest) {
441*9880d681SAndroid Build Coastguard Worker   int data_array[128];
442*9880d681SAndroid Build Coastguard Worker   std::vector<int *> data;
443*9880d681SAndroid Build Coastguard Worker 
444*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = 128; i != e; ++i) {
445*9880d681SAndroid Build Coastguard Worker     data_array[i] = 324 - int(i);
446*9880d681SAndroid Build Coastguard Worker     data.push_back(&data_array[i]);
447*9880d681SAndroid Build Coastguard Worker   }
448*9880d681SAndroid Build Coastguard Worker 
449*9880d681SAndroid Build Coastguard Worker   TinyPtrVector<int *> V(data);
450*9880d681SAndroid Build Coastguard Worker   EXPECT_TRUE(V.size() == 128);
451*9880d681SAndroid Build Coastguard Worker   EXPECT_FALSE(V.empty());
452*9880d681SAndroid Build Coastguard Worker 
453*9880d681SAndroid Build Coastguard Worker   MutableArrayRef<int *> mut_array = V;
454*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = 128; i != e; ++i) {
455*9880d681SAndroid Build Coastguard Worker     EXPECT_TRUE(mut_array[i] == data[i]);
456*9880d681SAndroid Build Coastguard Worker     mut_array[i] = 324 + mut_array[i];
457*9880d681SAndroid Build Coastguard Worker     EXPECT_TRUE(mut_array[i] == (324 + data[i]));
458*9880d681SAndroid Build Coastguard Worker   }
459*9880d681SAndroid Build Coastguard Worker }
460