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 
      9 #include "Test.h"
     10 
     11 // This is a GR test
     12 #if SK_SUPPORT_GPU
     13 #include "GrTHashCache.h"
     14 
     15 struct HashElement {
     16     int     fKey;
     17     int     fValue;
     18 };
     19 
     20 class GrFindPositivesFunctor {
     21 public:
     22     // only return elements with positive values
     23     bool operator()(const HashElement* elem) const {
     24         return elem->fValue > 0;
     25     }
     26 };
     27 
     28 class GrFindNegativesFunctor {
     29 public:
     30     // only return elements with negative values
     31     bool operator()(const HashElement* elem) const {
     32         return elem->fValue < 0;
     33     }
     34 };
     35 
     36 class HashKey {
     37 public:
     38     HashKey(int key) : fKey(key) {}
     39 
     40     uint32_t getHash() const { return fKey; }
     41 
     42     static bool LT(const HashElement& entry, const HashKey& key) {
     43         return entry.fKey < key.fKey;
     44     }
     45     static bool EQ(const HashElement& entry, const HashKey& key) {
     46         return entry.fKey == key.fKey;
     47     }
     48 
     49 #if GR_DEBUG
     50     static uint32_t GetHash(const HashElement& entry) {
     51         return entry.fKey;
     52     }
     53     static bool LT(const HashElement& a, const HashElement& b) {
     54         return a.fKey < b.fKey;
     55     }
     56     static bool EQ(const HashElement& a, const HashElement& b) {
     57         return a.fKey == b.fKey;
     58     }
     59 #endif
     60 
     61 protected:
     62     int fKey;
     63 };
     64 
     65 ////////////////////////////////////////////////////////////////////////////////
     66 static void TestHashCache(skiatest::Reporter* reporter) {
     67 
     68     GrTHashTable<HashElement, HashKey, 4> cache;
     69 
     70     HashElement negHashElements[10] = {
     71         { 0,  0 },
     72         { 1, -1 },
     73         { 2, -2 },
     74         { 3, -3 },
     75         { 4, -4 },
     76         { 5, -5 },
     77         { 6, -6 },
     78         { 7, -7 },
     79         { 8, -8 },
     80         { 9, -9 }
     81     };
     82     HashElement posHashElements[10] = {
     83         { 0, 0 },
     84         { 1, 1 },
     85         { 2, 2 },
     86         { 3, 3 },
     87         { 4, 4 },
     88         { 5, 5 },
     89         { 6, 6 },
     90         { 7, 7 },
     91         { 8, 8 },
     92         { 9, 9 }
     93     };
     94 
     95     // add i: -i pairs
     96     for (int i = 0; i < 10; ++i) {
     97         cache.insert(HashKey(i), &negHashElements[i]);
     98     }
     99 
    100     REPORTER_ASSERT(reporter, 10 == cache.count());
    101 
    102     // look for all i's and assert we found the -i's
    103     for (int i = 0; i < 10; ++i) {
    104         HashElement* found = cache.find(i);
    105         REPORTER_ASSERT(reporter, NULL != found && -i == found->fValue);
    106     }
    107 
    108     // look for something not in the cache
    109     {
    110         HashElement* found = cache.find(10);
    111         REPORTER_ASSERT(reporter, NULL == found);
    112     }
    113 
    114     // add i:i duplicates (so each i will have a positive & negative entry)
    115     for (int i = 0; i < 10; ++i) {
    116         cache.insert(i, &posHashElements[i]);
    117     }
    118 
    119     REPORTER_ASSERT(reporter, 20 == cache.count());
    120 
    121     // test out the find functor to find all the positive values
    122     {
    123         GrFindPositivesFunctor findPos;
    124 
    125         HashElement* found = cache.find(0, findPos);
    126         REPORTER_ASSERT(reporter, NULL == found);
    127 
    128         for (int i = 1; i < 10; ++i) {
    129             found = cache.find(i, findPos);
    130 
    131             REPORTER_ASSERT(reporter, NULL != found && found->fValue > 0);
    132         }
    133     }
    134 
    135     // make sure finding the positives wasn't a fluke - find the negatives
    136     {
    137         GrFindNegativesFunctor findNeg;
    138 
    139         HashElement* found = cache.find(0, findNeg);
    140         REPORTER_ASSERT(reporter, NULL == found);
    141 
    142         for (int i = 1; i < 10; ++i) {
    143             found = cache.find(i, findNeg);
    144 
    145             REPORTER_ASSERT(reporter, NULL != found && found->fValue < 0);
    146         }
    147     }
    148 
    149     // remove the 0:0 entries
    150     {
    151         cache.remove(0, &negHashElements[0]);
    152         cache.remove(0, &posHashElements[0]);
    153         REPORTER_ASSERT(reporter, 18 == cache.count());
    154 
    155         HashElement* found = cache.find(0);
    156         REPORTER_ASSERT(reporter, NULL == found);
    157     }
    158 
    159     // remove all
    160     {
    161         cache.removeAll();
    162         REPORTER_ASSERT(reporter, 0 == cache.count());
    163     }
    164 }
    165 
    166 ////////////////////////////////////////////////////////////////////////////////
    167 #include "TestClassDef.h"
    168 DEFINE_TESTCLASS("HashCache", HashCacheTestClass, TestHashCache)
    169 
    170 #endif
    171