blob: bd28c44197e7523ed29130dcb62d9a0bfa4ce2e4 [file] [log] [blame]
robertphillips@google.com3b57ded2012-09-18 17:16:33 +00001/*
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
mtklein@google.com4c2af742013-10-21 21:04:06 +000013#include "GrTHashTable.h"
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000014
15struct HashElement {
16 int fKey;
17 int fValue;
18};
19
20class GrFindPositivesFunctor {
21public:
22 // only return elements with positive values
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000023 bool operator()(const HashElement* elem) const {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000024 return elem->fValue > 0;
25 }
26};
27
28class GrFindNegativesFunctor {
29public:
30 // only return elements with negative values
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000031 bool operator()(const HashElement* elem) const {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000032 return elem->fValue < 0;
33 }
34};
35
36class HashKey {
37public:
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
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000049#ifdef SK_DEBUG
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000050 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
61protected:
62 int fKey;
63};
64
65////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com67b915d2013-02-04 16:13:32 +000066static void TestHashCache(skiatest::Reporter* reporter) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000067
68 GrTHashTable<HashElement, HashKey, 4> cache;
69
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000070 HashElement negHashElements[10] = {
71 { 0, 0 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000072 { 1, -1 },
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000073 { 2, -2 },
74 { 3, -3 },
75 { 4, -4 },
76 { 5, -5 },
77 { 6, -6 },
78 { 7, -7 },
79 { 8, -8 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000080 { 9, -9 }
81 };
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000082 HashElement posHashElements[10] = {
83 { 0, 0 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000084 { 1, 1 },
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000085 { 2, 2 },
86 { 3, 3 },
87 { 4, 4 },
88 { 5, 5 },
89 { 6, 6 },
90 { 7, 7 },
91 { 8, 8 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000092 { 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());
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000120
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000121 // 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 }
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000158}
159
160////////////////////////////////////////////////////////////////////////////////
161#include "TestClassDef.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000162DEFINE_TESTCLASS("HashCache", HashCacheTestClass, TestHashCache)
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000163
164#endif