2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 // This is a GPU-backend specific test
11 #include "GrAllocator.h"
15 C() : fID(-1) { ++gInstCnt; }
16 C(int id) : fID(id) { ++gInstCnt; }
26 static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popCnt,
27 skiatest::Reporter* reporter);
29 // Adds cnt items to the allocator, tests the cnts and iterators, pops popCnt items and checks
30 // again. Finally it resets the allocator and checks again.
31 static void check_allocator(GrTAllocator<C>* allocator, int cnt, int popCnt,
32 skiatest::Reporter* reporter) {
34 SkASSERT(allocator->empty());
35 for (int i = 0; i < cnt; ++i) {
36 // Try both variations of push_back().
38 allocator->push_back(C(i));
40 allocator->push_back() = C(i);
43 check_allocator_helper(allocator, cnt, popCnt, reporter);
45 check_allocator_helper(allocator, 0, 0, reporter);
48 // Checks that the allocator has the correct count, etc and that the element IDs are correct.
49 // Then pops popCnt items and checks again.
50 static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popCnt,
51 skiatest::Reporter* reporter) {
52 REPORTER_ASSERT(reporter, (0 == cnt) == allocator->empty());
53 REPORTER_ASSERT(reporter, cnt == allocator->count());
54 REPORTER_ASSERT(reporter, cnt == C::gInstCnt);
56 GrTAllocator<C>::Iter iter(allocator);
57 for (int i = 0; i < cnt; ++i) {
58 REPORTER_ASSERT(reporter, iter.next() && i == iter.get()->fID);
60 REPORTER_ASSERT(reporter, !iter.next());
62 REPORTER_ASSERT(reporter, cnt-1 == allocator->back().fID);
66 for (int i = 0; i < popCnt; ++i) {
67 allocator->pop_back();
69 check_allocator_helper(allocator, cnt - popCnt, 0, reporter);
73 DEF_TEST(GrAllocator, reporter) {
75 // Test combinations of allocators with and without stack storage and with different block
77 SkTArray<GrTAllocator<C>*> allocators;
78 GrTAllocator<C> a1(1);
79 allocators.push_back(&a1);
80 GrTAllocator<C> a2(2);
81 allocators.push_back(&a2);
82 GrTAllocator<C> a5(5);
83 allocators.push_back(&a5);
85 GrSTAllocator<1, C> sa1;
86 allocators.push_back(&a1);
87 GrSTAllocator<3, C> sa3;
88 allocators.push_back(&sa3);
89 GrSTAllocator<4, C> sa4;
90 allocators.push_back(&sa4);
92 for (int i = 0; i < allocators.count(); ++i) {
93 check_allocator(allocators[i], 0, 0, reporter);
94 check_allocator(allocators[i], 1, 1, reporter);
95 check_allocator(allocators[i], 2, 2, reporter);
96 check_allocator(allocators[i], 10, 1, reporter);
97 check_allocator(allocators[i], 10, 5, reporter);
98 check_allocator(allocators[i], 10, 10, reporter);
99 check_allocator(allocators[i], 100, 10, reporter);