1 //===- llvm/unittest/ADT/TinyPtrVectorTest.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 // TinyPtrVector unit tests.
12 //===----------------------------------------------------------------------===//
14 #include "gtest/gtest.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/TinyPtrVector.h"
19 #include "llvm/Support/type_traits.h"
28 // The world's worst RNG, but it is deterministic and makes it easy to get
29 // *some* shuffling of elements.
30 static ptrdiff_t test_shuffle_rng(ptrdiff_t i) {
31 return (i + i * 33) % i;
33 static ptrdiff_t (*test_shuffle_rng_p)(ptrdiff_t) = &test_shuffle_rng;
35 template <typename VectorT>
36 class TinyPtrVectorTest : public testing::Test {
38 typedef typename VectorT::value_type PtrT;
39 typedef typename remove_pointer<PtrT>::type ValueT;
44 ValueT TestValues[1024];
45 std::vector<PtrT> TestPtrs;
48 for (size_t i = 0, e = array_lengthof(TestValues); i != e; ++i)
49 TestPtrs.push_back(&TestValues[i]);
51 std::random_shuffle(TestPtrs.begin(), TestPtrs.end(), test_shuffle_rng_p);
54 ArrayRef<PtrT> testArray(size_t N) {
55 return makeArrayRef(&TestPtrs[0], N);
58 void appendValues(VectorT &V, ArrayRef<PtrT> Values) {
59 for (size_t i = 0, e = Values.size(); i != e; ++i)
60 V.push_back(Values[i]);
63 void expectValues(const VectorT &V, ArrayRef<PtrT> Values) {
64 EXPECT_EQ(Values.empty(), V.empty());
65 EXPECT_EQ(Values.size(), V.size());
66 for (size_t i = 0, e = Values.size(); i != e; ++i) {
67 EXPECT_EQ(Values[i], V[i]);
68 EXPECT_EQ(Values[i], *llvm::next(V.begin(), i));
70 EXPECT_EQ(V.end(), llvm::next(V.begin(), Values.size()));
74 typedef ::testing::Types<TinyPtrVector<int*>,
75 TinyPtrVector<double*>
76 > TinyPtrVectorTestTypes;
77 TYPED_TEST_CASE(TinyPtrVectorTest, TinyPtrVectorTestTypes);
79 TYPED_TEST(TinyPtrVectorTest, EmptyTest) {
80 this->expectValues(this->V, this->testArray(0));
83 TYPED_TEST(TinyPtrVectorTest, PushPopBack) {
84 this->V.push_back(this->TestPtrs[0]);
85 this->expectValues(this->V, this->testArray(1));
86 this->V.push_back(this->TestPtrs[1]);
87 this->expectValues(this->V, this->testArray(2));
88 this->V.push_back(this->TestPtrs[2]);
89 this->expectValues(this->V, this->testArray(3));
90 this->V.push_back(this->TestPtrs[3]);
91 this->expectValues(this->V, this->testArray(4));
92 this->V.push_back(this->TestPtrs[4]);
93 this->expectValues(this->V, this->testArray(5));
95 // Pop and clobber a few values to keep things interesting.
97 this->expectValues(this->V, this->testArray(4));
99 this->expectValues(this->V, this->testArray(3));
100 this->TestPtrs[3] = &this->TestValues[42];
101 this->TestPtrs[4] = &this->TestValues[43];
102 this->V.push_back(this->TestPtrs[3]);
103 this->expectValues(this->V, this->testArray(4));
104 this->V.push_back(this->TestPtrs[4]);
105 this->expectValues(this->V, this->testArray(5));
108 this->expectValues(this->V, this->testArray(4));
110 this->expectValues(this->V, this->testArray(3));
112 this->expectValues(this->V, this->testArray(2));
114 this->expectValues(this->V, this->testArray(1));
116 this->expectValues(this->V, this->testArray(0));
118 this->appendValues(this->V, this->testArray(42));
119 this->expectValues(this->V, this->testArray(42));
122 TYPED_TEST(TinyPtrVectorTest, ClearTest) {
123 this->expectValues(this->V, this->testArray(0));
125 this->expectValues(this->V, this->testArray(0));
127 this->appendValues(this->V, this->testArray(1));
128 this->expectValues(this->V, this->testArray(1));
130 this->expectValues(this->V, this->testArray(0));
132 this->appendValues(this->V, this->testArray(42));
133 this->expectValues(this->V, this->testArray(42));
135 this->expectValues(this->V, this->testArray(0));
138 TYPED_TEST(TinyPtrVectorTest, CopyAndMoveCtorTest) {
139 this->appendValues(this->V, this->testArray(42));
140 TypeParam Copy(this->V);
141 this->expectValues(Copy, this->testArray(42));
143 // This is a separate copy, and so it shouldn't destroy the original.
145 this->expectValues(Copy, this->testArray(0));
146 this->expectValues(this->V, this->testArray(42));
148 TypeParam Copy2(this->V2);
149 this->appendValues(Copy2, this->testArray(42));
150 this->expectValues(Copy2, this->testArray(42));
151 this->expectValues(this->V2, this->testArray(0));
153 #if LLVM_USE_RVALUE_REFERENCES
154 TypeParam Move(std::move(Copy2));
155 this->expectValues(Move, this->testArray(42));
156 this->expectValues(Copy2, this->testArray(0));
160 TYPED_TEST(TinyPtrVectorTest, EraseTest) {
161 this->appendValues(this->V, this->testArray(1));
162 this->expectValues(this->V, this->testArray(1));
163 this->V.erase(this->V.begin());
164 this->expectValues(this->V, this->testArray(0));
166 this->appendValues(this->V, this->testArray(42));
167 this->expectValues(this->V, this->testArray(42));
168 this->V.erase(this->V.begin());
169 this->TestPtrs.erase(this->TestPtrs.begin());
170 this->expectValues(this->V, this->testArray(41));
171 this->V.erase(llvm::next(this->V.begin(), 1));
172 this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 1));
173 this->expectValues(this->V, this->testArray(40));
174 this->V.erase(llvm::next(this->V.begin(), 2));
175 this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 2));
176 this->expectValues(this->V, this->testArray(39));
177 this->V.erase(llvm::next(this->V.begin(), 5));
178 this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 5));
179 this->expectValues(this->V, this->testArray(38));
180 this->V.erase(llvm::next(this->V.begin(), 13));
181 this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 13));
182 this->expectValues(this->V, this->testArray(37));
184 typename TypeParam::iterator I = this->V.begin();
186 I = this->V.erase(I);
187 } while (I != this->V.end());
188 this->expectValues(this->V, this->testArray(0));