Debian Codegraph @

mfbt/tests/TestSegmentedVector.cpp

raw
1 /* -*- Mode: C++; tab-width: 9; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // This is included first to ensure it doesn't implicitly depend on anything
8 // else.
9 #include "mozilla/SegmentedVector.h"
10
11 #include "mozilla/Alignment.h"
12 #include "mozilla/Assertions.h"
13
14 using mozilla::SegmentedVector;
15
16 // It would be nice if we could use the InfallibleAllocPolicy from mozalloc,
17 // but MFBT cannot use mozalloc.
18 class InfallibleAllocPolicy {
19 public:
20 template <typename T>
21 T* pod_malloc(size_t aNumElems) {
22 if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
23 MOZ_CRASH("TestSegmentedVector.cpp: overflow");
24 }
25 T* rv = static_cast<T*>(malloc(aNumElems * sizeof(T)));
26 if (!rv) {
27 MOZ_CRASH("TestSegmentedVector.cpp: out of memory");
28 }
29 return rv;
30 }
31
32 template <typename T>
33 void free_(T* aPtr, size_t aNumElems = 0) {
34 free(aPtr);
35 }
36 };
37
38 template <typename Vector>
39 void CheckContents(Vector& vector, size_t expectedLength) {
40 MOZ_RELEASE_ASSERT(vector.Length() == expectedLength);
41 size_t n = 0;
42 for (auto iter = vector.Iter(); !iter.Done(); iter.Next()) {
43 MOZ_RELEASE_ASSERT(iter.Get() == int(n));
44 n++;
45 }
46 MOZ_RELEASE_ASSERT(n == expectedLength);
47 }
48
49 // We want to test Append(), which is fallible and marked with
50 // [[nodiscard]]. But we're using an infallible alloc policy, and so
51 // don't really need to check the result. Casting to |void| works with clang
52 // but not GCC, so we instead use this dummy variable which works with both
53 // compilers.
54 static int gDummy;
55
56 // This tests basic segmented vector construction and iteration.
57 void TestBasics() {
58 // A SegmentedVector with a POD element type.
59 typedef SegmentedVector<int, 1024, InfallibleAllocPolicy> MyVector;
60 MyVector v;
61 int i;
62
63 MOZ_RELEASE_ASSERT(v.IsEmpty());
64
65 // Add 100 elements, then check various things.
66 i = 0;
67 for (; i < 100; i++) {
68 gDummy = v.Append(std::move(i));
69 }
70 MOZ_RELEASE_ASSERT(!v.IsEmpty());
71 CheckContents(v, 100);
72
73 // Add another 900 elements, then re-check.
74 for (; i < 1000; i++) {
75 v.InfallibleAppend(std::move(i));
76 }
77 MOZ_RELEASE_ASSERT(!v.IsEmpty());
78 CheckContents(v, 1000);
79
80 // Pop off all of the elements.
81 MOZ_RELEASE_ASSERT(v.Length() == 1000);
82 for (int len = (int)v.Length(); len > 0; len--) {
83 MOZ_RELEASE_ASSERT(v.GetLast() == len - 1);
84 v.PopLast();
85 }
86 MOZ_RELEASE_ASSERT(v.IsEmpty());
87 MOZ_RELEASE_ASSERT(v.Length() == 0);
88
89 // Fill the vector up again to prepare for the clear.
90 for (i = 0; i < 1000; i++) {
91 v.InfallibleAppend(std::move(i));
92 }
93 MOZ_RELEASE_ASSERT(!v.IsEmpty());
94 MOZ_RELEASE_ASSERT(v.Length() == 1000);
95
96 v.Clear();
97 MOZ_RELEASE_ASSERT(v.IsEmpty());
98 MOZ_RELEASE_ASSERT(v.Length() == 0);
99
100 // Fill the vector up to verify PopLastN works.
101 for (i = 0; i < 1000; ++i) {
102 v.InfallibleAppend(std::move(i));
103 }
104 MOZ_RELEASE_ASSERT(!v.IsEmpty());
105 MOZ_RELEASE_ASSERT(v.Length() == 1000);
106
107 // Verify we pop the right amount of elements.
108 v.PopLastN(300);
109 MOZ_RELEASE_ASSERT(v.Length() == 700);
110
111 // Verify the contents are what we expect.
112 CheckContents(v, 700);
113
114 // Verify PopLastN can take larger than .Length() value as an argument.
115 v.PopLastN(1000);
116 MOZ_RELEASE_ASSERT(v.Length() == 0);
117 MOZ_RELEASE_ASSERT(v.IsEmpty());
118
119 // Fill the vector again.
120 for (i = 0; i < 1000; ++i) {
121 v.InfallibleAppend(std::move(i));
122 }
123 MOZ_RELEASE_ASSERT(!v.IsEmpty());
124 MOZ_RELEASE_ASSERT(v.Length() == 1000);
125
126 // Verify that calling PopLastN with Length() empties the vector.
127 v.PopLastN(v.Length());
128 MOZ_RELEASE_ASSERT(v.Length() == 0);
129 MOZ_RELEASE_ASSERT(v.IsEmpty());
130 }
131
132 void TestMoveAndSwap() {
133 typedef SegmentedVector<int, 32, InfallibleAllocPolicy> MyVector;
134 MyVector v;
135
136 for (int i = 0; i < 100; i++) {
137 (void)v.Append(i);
138 }
139 MOZ_RELEASE_ASSERT(!v.IsEmpty());
140 CheckContents(v, 100);
141
142 // Test move constructor.
143 MyVector w(std::move(v));
144 CheckContents(w, 100);
145 MOZ_RELEASE_ASSERT(v.IsEmpty());
146
147 // Test move assignment.
148 v = std::move(w);
149 CheckContents(v, 100);
150 MOZ_RELEASE_ASSERT(w.IsEmpty());
151
152 // Test swap.
153 std::swap(v, w);
154 CheckContents(w, 100);
155 MOZ_RELEASE_ASSERT(v.IsEmpty());
156 }
157
158 static size_t gNumDefaultCtors;
159 static size_t gNumExplicitCtors;
160 static size_t gNumCopyCtors;
161 static size_t gNumMoveCtors;
162 static size_t gNumDtors;
163
164 struct NonPOD {
165 NonPOD() { gNumDefaultCtors++; }
166 explicit NonPOD(int x) { gNumExplicitCtors++; }
167 NonPOD(NonPOD&) { gNumCopyCtors++; }
168 NonPOD(NonPOD&&) { gNumMoveCtors++; }
169 ~NonPOD() { gNumDtors++; }
170 };
171
172 // This tests how segmented vectors with non-POD elements construct and
173 // destruct those elements.
174 void TestConstructorsAndDestructors() {
175 size_t defaultCtorCalls = 0;
176 size_t explicitCtorCalls = 0;
177 size_t copyCtorCalls = 0;
178 size_t moveCtorCalls = 0;
179 size_t dtorCalls = 0;
180
181 {
182 static const size_t segmentSize = 64;
183
184 // A SegmentedVector with a non-POD element type.
185 NonPOD x(1); // explicit constructor called
186 explicitCtorCalls++;
187 SegmentedVector<NonPOD, segmentSize, InfallibleAllocPolicy> v;
188 // default constructor called 0 times
189 MOZ_RELEASE_ASSERT(v.IsEmpty());
190 gDummy = v.Append(x); // copy constructor called
191 copyCtorCalls++;
192 NonPOD y(1); // explicit constructor called
193 explicitCtorCalls++;
194 gDummy = v.Append(std::move(y)); // move constructor called
195 moveCtorCalls++;
196 NonPOD z(1); // explicit constructor called
197 explicitCtorCalls++;
198 v.InfallibleAppend(std::move(z)); // move constructor called
199 moveCtorCalls++;
200 v.PopLast(); // destructor called 1 time
201 dtorCalls++;
202 MOZ_RELEASE_ASSERT(gNumDtors == dtorCalls);
203 v.Clear(); // destructor called 2 times
204 dtorCalls += 2;
205
206 // Test that PopLastN() correctly calls the destructors of all the
207 // elements in the segments it destroys.
208 //
209 // We depend on the size of NonPOD when determining how many things
210 // to push onto the vector. It would be nicer to get this information
211 // from SegmentedVector itself...
212 static_assert(sizeof(NonPOD) == 1, "Fix length calculations!");
213
214 size_t nonFullLastSegmentSize = segmentSize - 1;
215 for (size_t i = 0; i < nonFullLastSegmentSize; ++i) {
216 gDummy = v.Append(x); // copy constructor called
217 copyCtorCalls++;
218 }
219 MOZ_RELEASE_ASSERT(gNumCopyCtors == copyCtorCalls);
220
221 // Pop some of the elements.
222 {
223 size_t partialPopAmount = 5;
224 MOZ_RELEASE_ASSERT(nonFullLastSegmentSize > partialPopAmount);
225 v.PopLastN(partialPopAmount); // destructor called partialPopAmount times
226 dtorCalls += partialPopAmount;
227 MOZ_RELEASE_ASSERT(v.Length() ==
228 nonFullLastSegmentSize - partialPopAmount);
229 MOZ_RELEASE_ASSERT(!v.IsEmpty());
230 MOZ_RELEASE_ASSERT(gNumDtors == dtorCalls);
231 }
232
233 // Pop a full segment.
234 {
235 size_t length = v.Length();
236 v.PopLastN(length);
237 dtorCalls += length;
238 // These two tests *are* semantically different given the underlying
239 // implementation; Length sums up the sizes of the internal segments,
240 // while IsEmpty looks at the sequence of internal segments.
241 MOZ_RELEASE_ASSERT(v.Length() == 0);
242 MOZ_RELEASE_ASSERT(v.IsEmpty());
243 MOZ_RELEASE_ASSERT(gNumDtors == dtorCalls);
244 }
245
246 size_t multipleSegmentsSize = (segmentSize * 3) / 2;
247 for (size_t i = 0; i < multipleSegmentsSize; ++i) {
248 gDummy = v.Append(x); // copy constructor called
249 copyCtorCalls++;
250 }
251 MOZ_RELEASE_ASSERT(gNumCopyCtors == copyCtorCalls);
252
253 // Pop across segment boundaries.
254 {
255 v.PopLastN(segmentSize);
256 dtorCalls += segmentSize;
257 MOZ_RELEASE_ASSERT(v.Length() == (multipleSegmentsSize - segmentSize));
258 MOZ_RELEASE_ASSERT(!v.IsEmpty());
259 MOZ_RELEASE_ASSERT(gNumDtors == dtorCalls);
260 }
261
262 // Clear everything here to make calculations easier.
263 {
264 size_t length = v.Length();
265 v.Clear();
266 dtorCalls += length;
267 MOZ_RELEASE_ASSERT(v.IsEmpty());
268 MOZ_RELEASE_ASSERT(gNumDtors == dtorCalls);
269 }
270
271 MOZ_RELEASE_ASSERT(gNumDefaultCtors == defaultCtorCalls);
272 MOZ_RELEASE_ASSERT(gNumExplicitCtors == explicitCtorCalls);
273 MOZ_RELEASE_ASSERT(gNumCopyCtors == copyCtorCalls);
274 MOZ_RELEASE_ASSERT(gNumMoveCtors == moveCtorCalls);
275 MOZ_RELEASE_ASSERT(gNumDtors == dtorCalls);
276 } // destructor called for x, y, z
277 dtorCalls += 3;
278 MOZ_RELEASE_ASSERT(gNumDtors == dtorCalls);
279 }
280
281 struct A {
282 int mX;
283 int mY;
284 };
285 struct B {
286 int mX;
287 char mY;
288 double mZ;
289 };
290 struct C {
291 A mA;
292 B mB;
293 };
294 struct D {
295 char mBuf[101];
296 };
297 struct E {};
298
299 // This tests that we get the right segment capacities for specified segment
300 // sizes, and that the elements are aligned appropriately.
301 void TestSegmentCapacitiesAndAlignments() {
302 // When SegmentedVector's constructor is passed a size, it asserts that the
303 // vector's segment capacity results in a segment size equal to (or very
304 // close to) the passed size.
305 //
306 // Also, SegmentedVector has a static assertion that elements are
307 // appropriately aligned.
308 SegmentedVector<double, 512> v1(512);
309 SegmentedVector<A, 1024> v2(1024);
310 SegmentedVector<B, 999> v3(999);
311 SegmentedVector<C, 10> v4(10);
312 SegmentedVector<D, 1234> v5(1234);
313 SegmentedVector<E> v6(4096); // 4096 is the default segment size
314 SegmentedVector<mozilla::AlignedElem<16>, 100> v7(100);
315 }
316
317 void TestIterator() {
318 SegmentedVector<int, 4> v;
319
320 auto iter = v.Iter();
321 auto iterFromLast = v.IterFromLast();
322 MOZ_RELEASE_ASSERT(iter.Done());
323 MOZ_RELEASE_ASSERT(iterFromLast.Done());
324
325 gDummy = v.Append(1);
326 iter = v.Iter();
327 iterFromLast = v.IterFromLast();
328 MOZ_RELEASE_ASSERT(!iter.Done());
329 MOZ_RELEASE_ASSERT(!iterFromLast.Done());
330
331 iter.Next();
332 MOZ_RELEASE_ASSERT(iter.Done());
333 iterFromLast.Next();
334 MOZ_RELEASE_ASSERT(iterFromLast.Done());
335
336 iter = v.Iter();
337 iterFromLast = v.IterFromLast();
338 MOZ_RELEASE_ASSERT(!iter.Done());
339 MOZ_RELEASE_ASSERT(!iterFromLast.Done());
340
341 iter.Prev();
342 MOZ_RELEASE_ASSERT(iter.Done());
343 iterFromLast.Prev();
344 MOZ_RELEASE_ASSERT(iterFromLast.Done());
345
346 // Append enough entries to ensure we have at least two segments.
347 gDummy = v.Append(1);
348 gDummy = v.Append(1);
349 gDummy = v.Append(1);
350 gDummy = v.Append(1);
351
352 iter = v.Iter();
353 iterFromLast = v.IterFromLast();
354 MOZ_RELEASE_ASSERT(!iter.Done());
355 MOZ_RELEASE_ASSERT(!iterFromLast.Done());
356
357 iter.Prev();
358 MOZ_RELEASE_ASSERT(iter.Done());
359 iterFromLast.Next();
360 MOZ_RELEASE_ASSERT(iterFromLast.Done());
361
362 iter = v.Iter();
363 iterFromLast = v.IterFromLast();
364 MOZ_RELEASE_ASSERT(!iter.Done());
365 MOZ_RELEASE_ASSERT(!iterFromLast.Done());
366
367 iter.Next();
368 MOZ_RELEASE_ASSERT(!iter.Done());
369 iterFromLast.Prev();
370 MOZ_RELEASE_ASSERT(!iterFromLast.Done());
371
372 iter = v.Iter();
373 iterFromLast = v.IterFromLast();
374 int count = 0;
375 for (; !iter.Done() && !iterFromLast.Done();
376 iter.Next(), iterFromLast.Prev()) {
377 ++count;
378 }
379 MOZ_RELEASE_ASSERT(count == 5);
380
381 // Modify the vector while using the iterator.
382 iterFromLast = v.IterFromLast();
383 gDummy = v.Append(2);
384 gDummy = v.Append(3);
385 gDummy = v.Append(4);
386 iterFromLast.Next();
387 MOZ_RELEASE_ASSERT(!iterFromLast.Done());
388 MOZ_RELEASE_ASSERT(iterFromLast.Get() == 2);
389 iterFromLast.Next();
390 MOZ_RELEASE_ASSERT(iterFromLast.Get() == 3);
391 iterFromLast.Next();
392 MOZ_RELEASE_ASSERT(iterFromLast.Get() == 4);
393 iterFromLast.Next();
394 MOZ_RELEASE_ASSERT(iterFromLast.Done());
395 }
396
397 int main(void) {
398 TestBasics();
399 TestMoveAndSwap();
400 TestConstructorsAndDestructors();
401 TestSegmentCapacitiesAndAlignments();
402 TestIterator();
403
404 return 0;
405 }