OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 | |
13 struct C { | |
14 C() : fID(-1) { ++gCount; } | |
15 C(int id) : fID(id) { ++gCount; } | |
16 ~C() { --gCount; } | |
17 int fID; | |
18 | |
19 static int gCount; | |
20 }; | |
21 | |
robertphillips
2014/09/05 12:31:22
gCount -> gNumInstances, gNumAlloced ?
bsalomon
2014/09/05 13:04:22
Done.
bsalomon
2014/09/05 13:04:22
Done.
| |
22 int C::gCount = 0; | |
23 | |
24 static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popC nt, | |
25 skiatest::Reporter* reporter); | |
26 | |
27 // Adds cnt items to the allocator, tests the cnts and iterators, pops popCnt it ems and checks | |
28 // again. Finally it resets the allocator and checks again. | |
29 static void check_allocator(GrTAllocator<C>* allocator, int cnt, int popCnt, | |
30 skiatest::Reporter* reporter) { | |
31 SkASSERT(NULL != allocator); | |
32 SkASSERT(allocator->empty()); | |
33 for (int i = 0; i < cnt; ++i) { | |
34 // Try both variations of push_back(). | |
35 if (i % 1) { | |
36 allocator->push_back(C(i)); | |
37 } else { | |
38 allocator->push_back() = C(i); | |
39 } | |
40 } | |
41 check_allocator_helper(allocator, cnt, popCnt, reporter); | |
42 allocator->reset(); | |
43 check_allocator_helper(allocator, 0, 0, reporter); | |
44 } | |
45 | |
46 // Checks that the allocator has the correct count, etc and that the element IDs are correct. | |
47 // Then pops popCnt items and checks again. | |
48 static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popC nt, | |
49 skiatest::Reporter* reporter) { | |
50 REPORTER_ASSERT(reporter, (0 == cnt) == allocator->empty()); | |
51 REPORTER_ASSERT(reporter, cnt == allocator->count()); | |
52 REPORTER_ASSERT(reporter, cnt == C::gCount); | |
53 | |
54 GrTAllocator<C>::Iter iter(allocator); | |
55 for (int i = 0; i < cnt; ++i) { | |
56 REPORTER_ASSERT(reporter, iter.next() && i == iter.get()->fID); | |
57 } | |
58 REPORTER_ASSERT(reporter, !iter.next()); | |
59 if (cnt > 0) { | |
60 REPORTER_ASSERT(reporter, cnt-1 == allocator->back().fID); | |
61 } | |
62 | |
63 if (popCnt > 0) { | |
64 for (int i = 0; i < popCnt; ++i) { | |
65 allocator->pop_back(); | |
66 } | |
67 check_allocator_helper(allocator, cnt - popCnt, 0, reporter); | |
68 } | |
69 } | |
70 | |
71 DEF_TEST(GrAllocator, reporter) { | |
72 | |
robertphillips
2014/09/05 12:31:22
statck -> stack ?
bsalomon
2014/09/05 13:04:22
Done.
| |
73 // Test combinations of allocators with and without statck storage and with different block | |
74 // sizes. | |
75 SkTArray<GrTAllocator<C>*> allocators; | |
76 GrTAllocator<C> a1(1); | |
77 allocators.push_back(&a1); | |
78 GrTAllocator<C> a2(2); | |
79 allocators.push_back(&a2); | |
80 GrTAllocator<C> a5(5); | |
81 allocators.push_back(&a5); | |
82 | |
83 GrSTAllocator<1, C> sa1; | |
84 allocators.push_back(&a1); | |
85 GrSTAllocator<3, C> sa3; | |
86 allocators.push_back(&sa3); | |
robertphillips
2014/09/05 12:31:22
Did you intend this to be GrSTAllocator<4 ?
bsalomon
2014/09/05 13:04:22
Done.
| |
87 GrSTAllocator<5, C> sa4; | |
88 allocators.push_back(&sa4); | |
89 | |
90 for (int i = 0; i < allocators.count(); ++i) { | |
91 check_allocator(allocators[i], 0, 0, reporter); | |
92 check_allocator(allocators[i], 1, 1, reporter); | |
93 check_allocator(allocators[i], 2, 2, reporter); | |
94 check_allocator(allocators[i], 10, 1, reporter); | |
95 check_allocator(allocators[i], 10, 5, reporter); | |
96 check_allocator(allocators[i], 10, 10, reporter); | |
97 check_allocator(allocators[i], 100, 10, reporter); | |
98 } | |
99 } | |
100 | |
101 #endif | |
OLD | NEW |