Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2012 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 "SkTObjectPool.h"
      9 #include "SkTObjectPool.h"
     10 #include "Test.h"
     11 
     12 class PoolEntry {
     13 public:
     14 private:
     15     SK_DECLARE_INTERNAL_SLIST_INTERFACE(PoolEntry);
     16 };
     17 
     18 static const int kNumItemsPerBlock = 3;
     19 typedef SkTObjectPool<PoolEntry, kNumItemsPerBlock> ObjectPoolType;
     20 
     21 static bool verifyPool(skiatest::Reporter* reporter,
     22                        const ObjectPoolType& pool,
     23                        const char* stage,
     24                        int available, int blocks) {
     25     if (available != pool.available()) {
     26         ERRORF(reporter, "%s - Pool available is %d not %d",
     27                stage, pool.available(), available);
     28         return false;
     29     }
     30     if (blocks != pool.blocks()) {
     31         ERRORF(reporter, "%s - Pool blocks is %d not %d",
     32                stage, pool.blocks(), blocks);
     33         return false;
     34     }
     35     return true;
     36 }
     37 
     38 static const int kNumToAcquire = kNumItemsPerBlock * 5;
     39 static void testObjectPool(skiatest::Reporter* reporter) {
     40     ObjectPoolType pool;
     41     SkTInternalSList<PoolEntry> used;
     42     verifyPool(reporter, pool, "empty", 0, 0);
     43     for (int index = 0; index < kNumToAcquire; ++index) {
     44         used.push(pool.acquire());
     45         int blocks = (index / kNumItemsPerBlock) + 1;
     46         int available = (blocks * kNumItemsPerBlock) - (index + 1);
     47         if (!verifyPool(reporter, pool, "acquire", available, blocks)) {
     48             return;
     49         }
     50     }
     51     int available = pool.available();
     52     int blocks = pool.blocks();
     53     for (int index = 0; index < kNumToAcquire / 2; ++index) {
     54         pool.release(used.pop());
     55         ++available;
     56         if (!verifyPool(reporter, pool, "release", available, blocks)) {
     57             return;
     58         }
     59     }
     60     available += used.getCount();
     61     pool.releaseAll(&used);
     62     REPORTER_ASSERT(reporter, used.isEmpty());
     63     verifyPool(reporter, pool, "releaseAll", available, blocks);
     64 }
     65 
     66 DEF_TEST(ObjectPool, reporter) {
     67     testObjectPool(reporter);
     68 }
     69