bsalomon | a1ae66d | 2014-09-05 06:13:43 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrAllocator.h" |
| 9 | #include "tests/Test.h" |
bsalomon | a1ae66d | 2014-09-05 06:13:43 -0700 | [diff] [blame] | 10 | |
| 11 | namespace { |
| 12 | struct C { |
| 13 | C() : fID(-1) { ++gInstCnt; } |
| 14 | C(int id) : fID(id) { ++gInstCnt; } |
| 15 | ~C() { --gInstCnt; } |
| 16 | int fID; |
| 17 | |
| 18 | static int gInstCnt; |
| 19 | }; |
| 20 | |
| 21 | int C::gInstCnt = 0; |
| 22 | } |
| 23 | |
| 24 | static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popCnt, |
| 25 | skiatest::Reporter* reporter); |
| 26 | |
| 27 | // Adds cnt items to the allocator, tests the cnts and iterators, pops popCnt items 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) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 31 | SkASSERT(allocator); |
bsalomon | a1ae66d | 2014-09-05 06:13:43 -0700 | [diff] [blame] | 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 popCnt, |
| 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::gInstCnt); |
| 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 | |
| 73 | // Test combinations of allocators with and without stack 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); |
| 87 | GrSTAllocator<4, 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 | } |