• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Test.h"
9 #include "TestClassDef.h"
10 
11 // This is a GR test
12 #if SK_SUPPORT_GPU
13 #include "GrTHashTable.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
operator ()(const HashElement * elem) const23     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
operator ()(const HashElement * elem) const31     bool operator()(const HashElement* elem) const {
32         return elem->fValue < 0;
33     }
34 };
35 
36 class HashKey {
37 public:
HashKey(int key)38     HashKey(int key) : fKey(key) {}
39 
getHash() const40     uint32_t getHash() const { return fKey; }
41 
LessThan(const HashElement & entry,const HashKey & key)42     static bool LessThan(const HashElement& entry, const HashKey& key) {
43         return entry.fKey < key.fKey;
44     }
Equals(const HashElement & entry,const HashKey & key)45     static bool Equals(const HashElement& entry, const HashKey& key) {
46         return entry.fKey == key.fKey;
47     }
48 
49 #ifdef SK_DEBUG
LessThan(const HashElement & a,const HashElement & b)50     static bool LessThan(const HashElement& a, const HashElement& b) {
51         return a.fKey < b.fKey;
52     }
Equals(const HashElement & a,const HashElement & b)53     static bool Equals(const HashElement& a, const HashElement& b) {
54         return a.fKey == b.fKey;
55     }
56 #endif
57 
58 protected:
59     int fKey;
60 };
61 
DEF_TEST(HashCache,reporter)62 DEF_TEST(HashCache, reporter) {
63     GrTHashTable<HashElement, HashKey, 4> cache;
64 
65     HashElement negHashElements[10] = {
66         { 0,  0 },
67         { 1, -1 },
68         { 2, -2 },
69         { 3, -3 },
70         { 4, -4 },
71         { 5, -5 },
72         { 6, -6 },
73         { 7, -7 },
74         { 8, -8 },
75         { 9, -9 }
76     };
77     HashElement posHashElements[10] = {
78         { 0, 0 },
79         { 1, 1 },
80         { 2, 2 },
81         { 3, 3 },
82         { 4, 4 },
83         { 5, 5 },
84         { 6, 6 },
85         { 7, 7 },
86         { 8, 8 },
87         { 9, 9 }
88     };
89 
90     // add i: -i pairs
91     for (int i = 0; i < 10; ++i) {
92         cache.insert(HashKey(i), &negHashElements[i]);
93     }
94 
95     REPORTER_ASSERT(reporter, 10 == cache.count());
96 
97     // look for all i's and assert we found the -i's
98     for (int i = 0; i < 10; ++i) {
99         HashElement* found = cache.find(i);
100         REPORTER_ASSERT(reporter, NULL != found && -i == found->fValue);
101     }
102 
103     // look for something not in the cache
104     {
105         HashElement* found = cache.find(10);
106         REPORTER_ASSERT(reporter, NULL == found);
107     }
108 
109     // add i:i duplicates (so each i will have a positive & negative entry)
110     for (int i = 0; i < 10; ++i) {
111         cache.insert(i, &posHashElements[i]);
112     }
113 
114     REPORTER_ASSERT(reporter, 20 == cache.count());
115 
116     // test out the find functor to find all the positive values
117     {
118         GrFindPositivesFunctor findPos;
119 
120         HashElement* found = cache.find(0, findPos);
121         REPORTER_ASSERT(reporter, NULL == found);
122 
123         for (int i = 1; i < 10; ++i) {
124             found = cache.find(i, findPos);
125 
126             REPORTER_ASSERT(reporter, NULL != found && found->fValue > 0);
127         }
128     }
129 
130     // make sure finding the positives wasn't a fluke - find the negatives
131     {
132         GrFindNegativesFunctor findNeg;
133 
134         HashElement* found = cache.find(0, findNeg);
135         REPORTER_ASSERT(reporter, NULL == found);
136 
137         for (int i = 1; i < 10; ++i) {
138             found = cache.find(i, findNeg);
139 
140             REPORTER_ASSERT(reporter, NULL != found && found->fValue < 0);
141         }
142     }
143 
144     // remove the 0:0 entries
145     {
146         cache.remove(0, &negHashElements[0]);
147         cache.remove(0, &posHashElements[0]);
148         REPORTER_ASSERT(reporter, 18 == cache.count());
149 
150         HashElement* found = cache.find(0);
151         REPORTER_ASSERT(reporter, NULL == found);
152     }
153 }
154 
155 #endif
156