aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/ObjectPoolTest.cpp
blob: 404448ee7ceced3f3a5e4a574246230db7e117a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkTObjectPool.h"
#include "SkTObjectPool.h"
#include "Test.h"

class PoolEntry {
public:
private:
    SK_DECLARE_INTERNAL_SLIST_INTERFACE(PoolEntry);
};

static const int kNumItemsPerBlock = 3;
typedef SkTObjectPool<PoolEntry, kNumItemsPerBlock> ObjectPoolType;

static bool verifyPool(skiatest::Reporter* reporter,
                       const ObjectPoolType& pool,
                       const char* stage,
                       int available, int blocks) {
    if (available != pool.available()) {
        ERRORF(reporter, "%s - Pool available is %d not %d",
               stage, pool.available(), available);
        return false;
    }
    if (blocks != pool.blocks()) {
        ERRORF(reporter, "%s - Pool blocks is %d not %d",
               stage, pool.blocks(), blocks);
        return false;
    }
    return true;
}

static const int kNumToAcquire = kNumItemsPerBlock * 5;
static void testObjectPool(skiatest::Reporter* reporter) {
    ObjectPoolType pool;
    SkTInternalSList<PoolEntry> used;
    verifyPool(reporter, pool, "empty", 0, 0);
    for (int index = 0; index < kNumToAcquire; ++index) {
        used.push(pool.acquire());
        int blocks = (index / kNumItemsPerBlock) + 1;
        int available = (blocks * kNumItemsPerBlock) - (index + 1);
        if (!verifyPool(reporter, pool, "acquire", available, blocks)) {
            return;
        }
    }
    int available = pool.available();
    int blocks = pool.blocks();
    for (int index = 0; index < kNumToAcquire / 2; ++index) {
        pool.release(used.pop());
        ++available;
        if (!verifyPool(reporter, pool, "release", available, blocks)) {
            return;
        }
    }
    available += used.getCount();
    pool.releaseAll(&used);
    REPORTER_ASSERT(reporter, used.isEmpty());
    verifyPool(reporter, pool, "releaseAll", available, blocks);
}

DEF_TEST(ObjectPool, reporter) {
    testObjectPool(reporter);
}