blob: c02a7ca9af407891f1907ba93f1019389885f544 [file] [log] [blame]
bsalomona1ae66d2014-09-05 06:13:43 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Test.h"
9// This is a GPU-backend specific test
10#if SK_SUPPORT_GPU
11#include "GrAllocator.h"
12
13namespace {
14struct C {
15 C() : fID(-1) { ++gInstCnt; }
16 C(int id) : fID(id) { ++gInstCnt; }
17 ~C() { --gInstCnt; }
18 int fID;
19
20 static int gInstCnt;
21};
22
23int C::gInstCnt = 0;
24}
25
26static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popCnt,
27 skiatest::Reporter* reporter);
28
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.
31static void check_allocator(GrTAllocator<C>* allocator, int cnt, int popCnt,
32 skiatest::Reporter* reporter) {
bsalomon49f085d2014-09-05 13:34:00 -070033 SkASSERT(allocator);
bsalomona1ae66d2014-09-05 06:13:43 -070034 SkASSERT(allocator->empty());
35 for (int i = 0; i < cnt; ++i) {
36 // Try both variations of push_back().
37 if (i % 1) {
38 allocator->push_back(C(i));
39 } else {
40 allocator->push_back() = C(i);
41 }
42 }
43 check_allocator_helper(allocator, cnt, popCnt, reporter);
44 allocator->reset();
45 check_allocator_helper(allocator, 0, 0, reporter);
46}
47
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.
50static 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);
55
56 GrTAllocator<C>::Iter iter(allocator);
57 for (int i = 0; i < cnt; ++i) {
58 REPORTER_ASSERT(reporter, iter.next() && i == iter.get()->fID);
59 }
60 REPORTER_ASSERT(reporter, !iter.next());
61 if (cnt > 0) {
62 REPORTER_ASSERT(reporter, cnt-1 == allocator->back().fID);
63 }
64
65 if (popCnt > 0) {
66 for (int i = 0; i < popCnt; ++i) {
67 allocator->pop_back();
68 }
69 check_allocator_helper(allocator, cnt - popCnt, 0, reporter);
70 }
71}
72
73DEF_TEST(GrAllocator, reporter) {
74
75 // Test combinations of allocators with and without stack storage and with different block
76 // sizes.
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);
84
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);
91
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);
100 }
101}
102
103#endif