blob: 93877cb13c709ffb495659c8d26a51bbdcd6db8b [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
robertphillips@google.com3b57ded2012-09-18 17:16:33 +00008#include "Test.h"
9
10// This is a GR test
11#if SK_SUPPORT_GPU
mtklein@google.com4c2af742013-10-21 21:04:06 +000012#include "GrTHashTable.h"
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000013
14struct HashElement {
15 int fKey;
16 int fValue;
17};
18
19class GrFindPositivesFunctor {
20public:
21 // only return elements with positive values
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000022 bool operator()(const HashElement* elem) const {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000023 return elem->fValue > 0;
24 }
25};
26
27class GrFindNegativesFunctor {
28public:
29 // only return elements with negative values
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000030 bool operator()(const HashElement* elem) const {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000031 return elem->fValue < 0;
32 }
33};
34
35class HashKey {
36public:
37 HashKey(int key) : fKey(key) {}
38
39 uint32_t getHash() const { return fKey; }
40
rmistry@google.comd6bab022013-12-02 13:50:38 +000041 static bool LessThan(const HashElement& entry, const HashKey& key) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000042 return entry.fKey < key.fKey;
43 }
rmistry@google.comd6bab022013-12-02 13:50:38 +000044 static bool Equals(const HashElement& entry, const HashKey& key) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000045 return entry.fKey == key.fKey;
46 }
47
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000048#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +000049 static bool LessThan(const HashElement& a, const HashElement& b) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000050 return a.fKey < b.fKey;
51 }
rmistry@google.comd6bab022013-12-02 13:50:38 +000052 static bool Equals(const HashElement& a, const HashElement& b) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000053 return a.fKey == b.fKey;
54 }
55#endif
56
57protected:
58 int fKey;
59};
60
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000061DEF_TEST(HashCache, reporter) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000062 GrTHashTable<HashElement, HashKey, 4> cache;
63
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000064 HashElement negHashElements[10] = {
65 { 0, 0 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000066 { 1, -1 },
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000067 { 2, -2 },
68 { 3, -3 },
69 { 4, -4 },
70 { 5, -5 },
71 { 6, -6 },
72 { 7, -7 },
73 { 8, -8 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000074 { 9, -9 }
75 };
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000076 HashElement posHashElements[10] = {
77 { 0, 0 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000078 { 1, 1 },
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000079 { 2, 2 },
80 { 3, 3 },
81 { 4, 4 },
82 { 5, 5 },
83 { 6, 6 },
84 { 7, 7 },
85 { 8, 8 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000086 { 9, 9 }
87 };
88
89 // add i: -i pairs
90 for (int i = 0; i < 10; ++i) {
91 cache.insert(HashKey(i), &negHashElements[i]);
92 }
93
94 REPORTER_ASSERT(reporter, 10 == cache.count());
95
96 // look for all i's and assert we found the -i's
97 for (int i = 0; i < 10; ++i) {
98 HashElement* found = cache.find(i);
99 REPORTER_ASSERT(reporter, NULL != found && -i == found->fValue);
100 }
101
102 // look for something not in the cache
103 {
104 HashElement* found = cache.find(10);
105 REPORTER_ASSERT(reporter, NULL == found);
106 }
107
108 // add i:i duplicates (so each i will have a positive & negative entry)
109 for (int i = 0; i < 10; ++i) {
110 cache.insert(i, &posHashElements[i]);
111 }
112
113 REPORTER_ASSERT(reporter, 20 == cache.count());
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000114
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000115 // test out the find functor to find all the positive values
116 {
117 GrFindPositivesFunctor findPos;
118
119 HashElement* found = cache.find(0, findPos);
120 REPORTER_ASSERT(reporter, NULL == found);
121
122 for (int i = 1; i < 10; ++i) {
123 found = cache.find(i, findPos);
124
125 REPORTER_ASSERT(reporter, NULL != found && found->fValue > 0);
126 }
127 }
128
129 // make sure finding the positives wasn't a fluke - find the negatives
130 {
131 GrFindNegativesFunctor findNeg;
132
133 HashElement* found = cache.find(0, findNeg);
134 REPORTER_ASSERT(reporter, NULL == found);
135
136 for (int i = 1; i < 10; ++i) {
137 found = cache.find(i, findNeg);
138
139 REPORTER_ASSERT(reporter, NULL != found && found->fValue < 0);
140 }
141 }
142
143 // remove the 0:0 entries
144 {
145 cache.remove(0, &negHashElements[0]);
146 cache.remove(0, &posHashElements[0]);
147 REPORTER_ASSERT(reporter, 18 == cache.count());
148
149 HashElement* found = cache.find(0);
150 REPORTER_ASSERT(reporter, NULL == found);
151 }
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000152}
153
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000154#endif