| Index: tests/SmallAllocatorTest.cpp
|
| diff --git a/tests/SmallAllocatorTest.cpp b/tests/SmallAllocatorTest.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..993fdb8737f3c8440083ba124f3a226cac7333d2
|
| --- /dev/null
|
| +++ b/tests/SmallAllocatorTest.cpp
|
| @@ -0,0 +1,92 @@
|
| +/*
|
| + * Copyright 2014 Google, Inc
|
| + *
|
| + * Use of this source code is governed by a BSD-style license that can be
|
| + * found in the LICENSE file.
|
| + */
|
| +
|
| +#include "SkStackAllocator.h"
|
| +#include "SkTypes.h"
|
| +#include "Test.h"
|
| +
|
| +class CountingClass {
|
| +public:
|
| + CountingClass() {
|
| + kCount++;
|
| + }
|
| +
|
| + ~CountingClass() {
|
| + kCount--;
|
| + }
|
| +
|
| + static int GetCount() { return kCount; }
|
| +
|
| +private:
|
| + static int kCount;
|
| +};
|
| +
|
| +int CountingClass::kCount;
|
| +
|
| +template<uint32_t kMaxObjects, size_t kBytes> void test_allocator(skiatest::Reporter* reporter) {
|
| + {
|
| + SkSmallAllocator<kMaxObjects, kBytes> alloc;
|
| + for (uint32_t i = 0; i < 99; ++i) {
|
| + CountingClass* c = alloc.template createT<CountingClass>();
|
| + if (i >= kMaxObjects) {
|
| + REPORTER_ASSERT(reporter, NULL == c);
|
| + break;
|
| + }
|
| + REPORTER_ASSERT(reporter, c != NULL);
|
| + REPORTER_ASSERT(reporter, CountingClass::GetCount() == static_cast<int>(i+1));
|
| + }
|
| + }
|
| + REPORTER_ASSERT(reporter, CountingClass::GetCount() == 0);
|
| +}
|
| +
|
| +DEF_TEST(SmallAllocator_empty, reporter) {
|
| + // This version of the allocator will never create any objects.
|
| + SkSmallAllocator<0, 10> alloc;
|
| + CountingClass* c = alloc.createT<CountingClass>();
|
| + REPORTER_ASSERT(reporter, NULL == c);
|
| +}
|
| +
|
| +// Tests that ensure that the destructor is called, whether the objects
|
| +// were created in fStorage or on the heap.
|
| +DEF_TEST(SmallAllocator_destructor, reporter) {
|
| + // An equal number of objects as bytes will never require any heap
|
| + // allocations (since sizeof(CountingClass) == 1 and the allocator will
|
| + // stop once it reaches kMaxObjects).
|
| + test_allocator<5, 5>(reporter);
|
| + test_allocator<10, 10>(reporter);
|
| + test_allocator<20, 20>(reporter);
|
| +
|
| + // Allowing less bytes than objects means some will be allocated on the
|
| + // heap.
|
| + test_allocator<50, 20>(reporter);
|
| + test_allocator<100, 20>(reporter);
|
| +}
|
| +
|
| +class Dummy {
|
| +};
|
| +
|
| +class DummyContainer {
|
| +public:
|
| + explicit DummyContainer(Dummy* d)
|
| + :fDummy(d)
|
| + {}
|
| +
|
| + Dummy* getDummy() const { return fDummy; }
|
| +
|
| +private:
|
| + Dummy* fDummy;
|
| +};
|
| +
|
| +// Test that using a createT with a constructor taking a pointer as a
|
| +// parameter works as expected.
|
| +DEF_TEST(SmallAllocator_pointer, reporter) {
|
| + SkSmallAllocator<1, 1> alloc;
|
| + Dummy d;
|
| + DummyContainer* container = alloc.createT<DummyContainer>(&d);
|
| + REPORTER_ASSERT(reporter, container != NULL);
|
| + REPORTER_ASSERT(reporter, container->getDummy() == &d);
|
| +}
|
|
|