Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions
SkTBlockListTest.cpp File Reference
#include "include/core/SkTypes.h"
#include "include/private/base/SkAlign.h"
#include "src/base/SkBlockAllocator.h"
#include "src/base/SkTBlockList.h"
#include "tests/Test.h"
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <utility>
#include <vector>

Go to the source code of this file.

Classes

class  TBlockListTestAccess
 

Functions

template<int N>
static void check_allocator_helper (SkTBlockList< C, N > *allocator, int cnt, int popCnt, skiatest::Reporter *reporter)
 
template<int N>
static void check_iterator_helper (SkTBlockList< C, N > *allocator, const std::vector< C * > &expected, skiatest::Reporter *reporter)
 
template<int N>
static void check_allocator (SkTBlockList< C, N > *allocator, int cnt, int popCnt, skiatest::Reporter *reporter)
 
template<int N>
static void run_allocator_test (SkTBlockList< C, N > *allocator, skiatest::Reporter *reporter)
 
template<int N1, int N2>
static void run_concat_test (skiatest::Reporter *reporter, int aCount, int bCount)
 
template<int N1, int N2>
static void run_concat_trivial_test (skiatest::Reporter *reporter, int aCount, int bCount)
 
template<int N>
static void run_reserve_test (skiatest::Reporter *reporter)
 
void run_large_increment_test (skiatest::Reporter *reporter)
 
 DEF_TEST (SkTBlockList, reporter)
 

Function Documentation

◆ check_allocator()

template<int N>
static void check_allocator ( SkTBlockList< C, N > *  allocator,
int  cnt,
int  popCnt,
skiatest::Reporter reporter 
)
static

Definition at line 142 of file SkTBlockListTest.cpp.

143 {
144 enum ItemInitializer : int {
145 kCopyCtor,
146 kMoveCtor,
147 kCopyAssign,
148 kMoveAssign,
149 kEmplace,
150 };
151 static constexpr int kInitCount = (int) kEmplace + 1;
152
153 SkASSERT(allocator);
154 SkASSERT(allocator->empty());
155 std::vector<C*> items;
156 for (int i = 0; i < cnt; ++i) {
157 switch((ItemInitializer) (i % kInitCount)) {
158 case kCopyCtor:
159 allocator->push_back(C(i));
160 break;
161 case kMoveCtor:
162 allocator->push_back(std::move(C(i)));
163 break;
164 case kCopyAssign:
165 allocator->push_back() = C(i);
166 break;
167 case kMoveAssign:
168 allocator->push_back() = std::move(C(i));
169 break;
170 case kEmplace:
171 allocator->emplace_back(i);
172 break;
173 }
174 items.push_back(&allocator->back());
175 }
176 check_iterator_helper(allocator, items, reporter);
177 check_allocator_helper(allocator, cnt, popCnt, reporter);
178 allocator->reset();
179 check_iterator_helper(allocator, {}, reporter);
180 check_allocator_helper(allocator, 0, 0, reporter);
181}
reporter
#define SkASSERT(cond)
Definition SkAssert.h:116
static void check_allocator_helper(SkTBlockList< C, N > *allocator, int cnt, int popCnt, skiatest::Reporter *reporter)
static void check_iterator_helper(SkTBlockList< C, N > *allocator, const std::vector< C * > &expected, skiatest::Reporter *reporter)
Type::kYUV Type::kRGBA() int(0.7 *637)
T & emplace_back(Args &&... args)
bool empty() const

◆ check_allocator_helper()

template<int N>
static void check_allocator_helper ( SkTBlockList< C, N > *  allocator,
int  cnt,
int  popCnt,
skiatest::Reporter reporter 
)
static

Definition at line 68 of file SkTBlockListTest.cpp.

69 {
70 REPORTER_ASSERT(reporter, (0 == cnt) == allocator->empty());
71 REPORTER_ASSERT(reporter, cnt == allocator->count());
72 REPORTER_ASSERT(reporter, cnt == C::gInstCnt);
73
74 int i = 0;
75 for (const C& c : allocator->items()) {
76 REPORTER_ASSERT(reporter, i == c.fID);
77 REPORTER_ASSERT(reporter, allocator->item(i).fID == i);
78 ++i;
79 }
80 REPORTER_ASSERT(reporter, i == cnt);
81
82 if (cnt > 0) {
83 REPORTER_ASSERT(reporter, cnt-1 == allocator->back().fID);
84 }
85
86 if (popCnt > 0) {
87 for (i = 0; i < popCnt; ++i) {
88 allocator->pop_back();
89 }
90 check_allocator_helper(allocator, cnt - popCnt, 0, reporter);
91 }
92}
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
T & item(int i)
int count() const

◆ check_iterator_helper()

template<int N>
static void check_iterator_helper ( SkTBlockList< C, N > *  allocator,
const std::vector< C * > &  expected,
skiatest::Reporter reporter 
)
static

Definition at line 95 of file SkTBlockListTest.cpp.

97 {
98 const SkTBlockList<C, N>* cAlloc = allocator;
99 REPORTER_ASSERT(reporter, (size_t) allocator->count() == expected.size());
100 // Forward+const
101 int i = 0;
102 for (const C& c : cAlloc->items()) {
103 REPORTER_ASSERT(reporter, (uintptr_t) &c == (uintptr_t) expected[i]);
104 ++i;
105 }
106 REPORTER_ASSERT(reporter, (size_t) i == expected.size());
107
108 // Forward+non-const
109 i = 0;
110 for (C& c : allocator->items()) {
111 REPORTER_ASSERT(reporter, (uintptr_t) &c == (uintptr_t) expected[i]);
112 ++i;
113 }
114 REPORTER_ASSERT(reporter, (size_t) i == expected.size());
115
116 // Reverse+const
117 i = (int) expected.size() - 1;
118 for (const C& c : cAlloc->ritems()) {
119 REPORTER_ASSERT(reporter, (uintptr_t) &c == (uintptr_t) expected[i]);
120 --i;
121 }
122 REPORTER_ASSERT(reporter, i == -1);
123
124 // Reverse+non-const
125 i = (int) expected.size() - 1;
126 for (C& c : allocator->ritems()) {
127 REPORTER_ASSERT(reporter, (uintptr_t) &c == (uintptr_t) expected[i]);
128 --i;
129 }
130 REPORTER_ASSERT(reporter, i == -1);
131
132 // Also test random access
133 for (i = 0; i < allocator->count(); ++i) {
134 REPORTER_ASSERT(reporter, (uintptr_t) &allocator->item(i) == (uintptr_t) expected[i]);
135 REPORTER_ASSERT(reporter, (uintptr_t) &cAlloc->item(i) == (uintptr_t) expected[i]);
136 }
137}

◆ DEF_TEST()

DEF_TEST ( SkTBlockList  ,
reporter   
)

Definition at line 329 of file SkTBlockListTest.cpp.

329 {
330 // Test combinations of allocators with and without stack storage and with different block sizes
331 SkTBlockList<C> a1(1);
333
334 SkTBlockList<C> a2(2);
336
337 SkTBlockList<C> a5(5);
339
342
345
348
349 run_reserve_test<1>(reporter);
350 run_reserve_test<2>(reporter);
351 run_reserve_test<3>(reporter);
352 run_reserve_test<4>(reporter);
353 run_reserve_test<5>(reporter);
354
355 run_concat_test<1, 1>(reporter, 10, 10);
356 run_concat_test<5, 1>(reporter, 50, 10);
357 run_concat_test<1, 5>(reporter, 10, 50);
358 run_concat_test<5, 5>(reporter, 100, 100);
359
360 run_concat_trivial_test<1, 1>(reporter, 10, 10);
361 run_concat_trivial_test<5, 1>(reporter, 50, 10);
362 run_concat_trivial_test<1, 5>(reporter, 10, 50);
363 run_concat_trivial_test<5, 5>(reporter, 100, 100);
364
366}
void run_large_increment_test(skiatest::Reporter *reporter)
static void run_allocator_test(SkTBlockList< C, N > *allocator, skiatest::Reporter *reporter)

◆ run_allocator_test()

template<int N>
static void run_allocator_test ( SkTBlockList< C, N > *  allocator,
skiatest::Reporter reporter 
)
static

Definition at line 184 of file SkTBlockListTest.cpp.

184 {
185 check_allocator(allocator, 0, 0, reporter);
186 check_allocator(allocator, 1, 1, reporter);
187 check_allocator(allocator, 2, 2, reporter);
188 check_allocator(allocator, 10, 1, reporter);
189 check_allocator(allocator, 10, 5, reporter);
190 check_allocator(allocator, 10, 10, reporter);
191 check_allocator(allocator, 100, 10, reporter);
192}
static void check_allocator(SkTBlockList< C, N > *allocator, int cnt, int popCnt, skiatest::Reporter *reporter)

◆ run_concat_test()

template<int N1, int N2>
static void run_concat_test ( skiatest::Reporter reporter,
int  aCount,
int  bCount 
)
static

Definition at line 195 of file SkTBlockListTest.cpp.

195 {
196
199
200 for (int i = 0; i < aCount; ++i) {
201 listA.emplace_back(i);
202 }
203 for (int i = 0; i < bCount; ++i) {
204 listB.emplace_back(aCount + i);
205 }
206
207 REPORTER_ASSERT(reporter, listA.count() == aCount && listB.count() == bCount);
208 REPORTER_ASSERT(reporter, C::gInstCnt == aCount + bCount);
209
210 // Concatenate B into A and verify.
211 listA.concat(std::move(listB));
212 REPORTER_ASSERT(reporter, listA.count() == aCount + bCount);
213 // SkTBlockList guarantees the moved list is empty, but clang-tidy doesn't know about it;
214 // in practice we won't really be using moved lists so this won't pollute our main code base
215 // with lots of warning disables.
216 REPORTER_ASSERT(reporter, listB.count() == 0); // NOLINT(bugprone-use-after-move)
217 REPORTER_ASSERT(reporter, C::gInstCnt == aCount + bCount);
218
219 int i = 0;
220 for (const C& item : listA.items()) {
221 // By construction of A and B originally, the concatenated id sequence is continuous
222 REPORTER_ASSERT(reporter, i == item.fID);
223 i++;
224 }
225 REPORTER_ASSERT(reporter, i == (aCount + bCount));
226}
void concat(SkTBlockList< T, SI > &&other)

◆ run_concat_trivial_test()

template<int N1, int N2>
static void run_concat_trivial_test ( skiatest::Reporter reporter,
int  aCount,
int  bCount 
)
static

Definition at line 229 of file SkTBlockListTest.cpp.

229 {
230 static_assert(std::is_trivially_copyable<D>::value);
231
232 // This is similar to run_concat_test(), except since D is trivial we can't verify the instant
233 // counts that are tracked via ctor/dtor.
236
237 for (int i = 0; i < aCount; ++i) {
238 listA.push_back({i});
239 }
240 for (int i = 0; i < bCount; ++i) {
241 listB.push_back({aCount + i});
242 }
243
244 REPORTER_ASSERT(reporter, listA.count() == aCount && listB.count() == bCount);
245 // Concatenate B into A and verify.
246 listA.concat(std::move(listB));
247 REPORTER_ASSERT(reporter, listA.count() == aCount + bCount);
248 REPORTER_ASSERT(reporter, listB.count() == 0); // NOLINT(bugprone-use-after-move): see above
249
250 int i = 0;
251 for (const D& item : listA.items()) {
252 // By construction of A and B originally, the concatenated id sequence is continuous
253 REPORTER_ASSERT(reporter, i == item.fID);
254 i++;
255 }
256 REPORTER_ASSERT(reporter, i == (aCount + bCount));
257}

◆ run_large_increment_test()

void run_large_increment_test ( skiatest::Reporter reporter)

Definition at line 315 of file SkTBlockListTest.cpp.

315 {
316 static constexpr size_t kIncrementMax = std::numeric_limits<uint16_t>::max();
317 // Pick an item count such that count*sizeof(C)/max align exceeds uint16_t max.
318 int itemCount = (int) (sizeof(C) * kIncrementMax / TBlockListTestAccess::kAddressAlign) + 1;
319 SkTBlockList<C> largeIncrement(itemCount);
320 // Trigger a scratch block allocation, which given default fixed growth policy, will be one
321 // block increment.
322 largeIncrement.reserve(10);
323 size_t scratchSize = TBlockListTestAccess::ScratchBlockSize(largeIncrement);
324 // SkBlockAllocator aligns large blocks to 4k
325 size_t expected = SkAlignTo(kIncrementMax * TBlockListTestAccess::kAddressAlign, (1 << 12));
326 REPORTER_ASSERT(reporter, scratchSize == expected);
327}
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition SkAlign.h:33
static constexpr size_t kAddressAlign
static size_t ScratchBlockSize(SkTBlockList< C, N > &list)

◆ run_reserve_test()

template<int N>
static void run_reserve_test ( skiatest::Reporter reporter)
static

Definition at line 260 of file SkTBlockListTest.cpp.

260 {
261 constexpr int kItemsPerBlock = N + 4; // Make this a number > 1, even if N starting items == 1
262
263 SkTBlockList<C, N> list(kItemsPerBlock);
264 size_t initialSize = TBlockListTestAccess::TotalSize(list);
265 // Should be able to add N instances of T w/o changing size from initialSize
266 for (int i = 0; i < N; ++i) {
267 list.push_back(C(i));
268 }
270
271 // Reserve room for 2*kItemsPerBlock items
272 list.reserve(2 * kItemsPerBlock);
273 REPORTER_ASSERT(reporter, list.count() == N); // count shouldn't change though
274
275 size_t reservedSize = TBlockListTestAccess::TotalSize(list);
276 REPORTER_ASSERT(reporter, reservedSize >= initialSize + 2 * kItemsPerBlock * sizeof(C));
277 for (int i = 0; i < 2 * kItemsPerBlock; ++i) {
278 list.push_back(C(i));
279 }
281
282 // Make the next block partially fully (N > 0 but < kItemsPerBlock)
283 for (int i = 0; i < N; ++i) {
284 list.push_back(C(i));
285 }
286
287 // Reserve room again for 2*kItemsPerBlock, but reserve should automatically take account of the
288 // (kItemsPerBlock-N) that are still available in the active block
289 list.reserve(2 * kItemsPerBlock);
290 int extraReservedCount = kItemsPerBlock + N;
291 // Because SkTBlockList normally allocates blocks in fixed sizes, and extraReservedCount >
292 // items-per-block, it will always use that size and not that of the growth policy.
294 extraReservedCount * sizeof(C));
295
296 reservedSize = TBlockListTestAccess::TotalSize(list);
297 for (int i = 0; i < 2 * kItemsPerBlock; ++i) {
298 list.push_back(C(i));
299 }
301
302 // If we reserve a count < items-per-block, it will use the fixed size from the growth policy.
303 list.reserve(2);
305 kItemsPerBlock * sizeof(C));
306
307 // Ensure the reservations didn't initialize any more D's than anticipated
308 int expectedInstanceCount = 2 * (N + 2 * kItemsPerBlock);
309 REPORTER_ASSERT(reporter, expectedInstanceCount == C::gInstCnt);
310
311 list.reset();
312 REPORTER_ASSERT(reporter, 0 == C::gInstCnt);
313}
#define N
Definition beziers.cpp:19
static size_t TotalSize(SkTBlockList< C, N > &list)