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 "SkStackAllocator.h" |
| 9 #include "SkTypes.h" |
| 10 #include "Test.h" |
| 11 |
| 12 class CountingClass { |
| 13 public: |
| 14 CountingClass() { |
| 15 kCount++; |
| 16 } |
| 17 |
| 18 ~CountingClass() { |
| 19 kCount--; |
| 20 } |
| 21 |
| 22 static int GetCount() { return kCount; } |
| 23 |
| 24 private: |
| 25 static int kCount; |
| 26 }; |
| 27 |
| 28 int CountingClass::kCount; |
| 29 |
| 30 template<uint32_t kMaxObjects, size_t kBytes> void test_allocator(skiatest::Repo
rter* reporter) { |
| 31 { |
| 32 SkSmallAllocator<kMaxObjects, kBytes> alloc; |
| 33 for (uint32_t i = 0; i < 99; ++i) { |
| 34 CountingClass* c = alloc.template createT<CountingClass>(); |
| 35 if (i >= kMaxObjects) { |
| 36 REPORTER_ASSERT(reporter, NULL == c); |
| 37 break; |
| 38 } |
| 39 REPORTER_ASSERT(reporter, c != NULL); |
| 40 REPORTER_ASSERT(reporter, CountingClass::GetCount() == static_cast<i
nt>(i+1)); |
| 41 } |
| 42 } |
| 43 REPORTER_ASSERT(reporter, CountingClass::GetCount() == 0); |
| 44 } |
| 45 |
| 46 DEF_TEST(SmallAllocator_empty, reporter) { |
| 47 // This version of the allocator will never create any objects. |
| 48 SkSmallAllocator<0, 10> alloc; |
| 49 CountingClass* c = alloc.createT<CountingClass>(); |
| 50 REPORTER_ASSERT(reporter, NULL == c); |
| 51 } |
| 52 |
| 53 // Tests that ensure that the destructor is called, whether the objects |
| 54 // were created in fStorage or on the heap. |
| 55 DEF_TEST(SmallAllocator_destructor, reporter) { |
| 56 // An equal number of objects as bytes will never require any heap |
| 57 // allocations (since sizeof(CountingClass) == 1 and the allocator will |
| 58 // stop once it reaches kMaxObjects). |
| 59 test_allocator<5, 5>(reporter); |
| 60 test_allocator<10, 10>(reporter); |
| 61 test_allocator<20, 20>(reporter); |
| 62 |
| 63 // Allowing less bytes than objects means some will be allocated on the |
| 64 // heap. |
| 65 test_allocator<50, 20>(reporter); |
| 66 test_allocator<100, 20>(reporter); |
| 67 } |
| 68 |
| 69 class Dummy { |
| 70 }; |
| 71 |
| 72 class DummyContainer { |
| 73 public: |
| 74 explicit DummyContainer(Dummy* d) |
| 75 :fDummy(d) |
| 76 {} |
| 77 |
| 78 Dummy* getDummy() const { return fDummy; } |
| 79 |
| 80 private: |
| 81 Dummy* fDummy; |
| 82 }; |
| 83 |
| 84 // Test that using a createT with a constructor taking a pointer as a |
| 85 // parameter works as expected. |
| 86 DEF_TEST(SmallAllocator_pointer, reporter) { |
| 87 SkSmallAllocator<1, 1> alloc; |
| 88 Dummy d; |
| 89 DummyContainer* container = alloc.createT<DummyContainer>(&d); |
| 90 REPORTER_ASSERT(reporter, container != NULL); |
| 91 REPORTER_ASSERT(reporter, container->getDummy() == &d); |
| 92 } |
OLD | NEW |