blob: 1a5c4701cd452a725b0b957aa2ac909e18392b87 [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
rmistry@google.comd6bab022013-12-02 13:50:38 +000042 static bool LessThan(const HashElement& entry, const HashKey& key) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000043 return entry.fKey < key.fKey;
44 }
rmistry@google.comd6bab022013-12-02 13:50:38 +000045 static bool Equals(const HashElement& entry, const HashKey& key) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000046 return entry.fKey == key.fKey;
47 }
48
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000049#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +000050 static bool LessThan(const HashElement& a, const HashElement& b) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000051 return a.fKey < b.fKey;
52 }
rmistry@google.comd6bab022013-12-02 13:50:38 +000053 static bool Equals(const HashElement& a, const HashElement& b) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000054 return a.fKey == b.fKey;
55 }
56#endif
57
58protected:
59 int fKey;
60};
61
62////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com67b915d2013-02-04 16:13:32 +000063static void TestHashCache(skiatest::Reporter* reporter) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000064
65 GrTHashTable<HashElement, HashKey, 4> cache;
66
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000067 HashElement negHashElements[10] = {
68 { 0, 0 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000069 { 1, -1 },
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000070 { 2, -2 },
71 { 3, -3 },
72 { 4, -4 },
73 { 5, -5 },
74 { 6, -6 },
75 { 7, -7 },
76 { 8, -8 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000077 { 9, -9 }
78 };
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000079 HashElement posHashElements[10] = {
80 { 0, 0 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000081 { 1, 1 },
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000082 { 2, 2 },
83 { 3, 3 },
84 { 4, 4 },
85 { 5, 5 },
86 { 6, 6 },
87 { 7, 7 },
88 { 8, 8 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000089 { 9, 9 }
90 };
91
92 // add i: -i pairs
93 for (int i = 0; i < 10; ++i) {
94 cache.insert(HashKey(i), &negHashElements[i]);
95 }
96
97 REPORTER_ASSERT(reporter, 10 == cache.count());
98
99 // look for all i's and assert we found the -i's
100 for (int i = 0; i < 10; ++i) {
101 HashElement* found = cache.find(i);
102 REPORTER_ASSERT(reporter, NULL != found && -i == found->fValue);
103 }
104
105 // look for something not in the cache
106 {
107 HashElement* found = cache.find(10);
108 REPORTER_ASSERT(reporter, NULL == found);
109 }
110
111 // add i:i duplicates (so each i will have a positive & negative entry)
112 for (int i = 0; i < 10; ++i) {
113 cache.insert(i, &posHashElements[i]);
114 }
115
116 REPORTER_ASSERT(reporter, 20 == cache.count());
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000117
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000118 // test out the find functor to find all the positive values
119 {
120 GrFindPositivesFunctor findPos;
121
122 HashElement* found = cache.find(0, findPos);
123 REPORTER_ASSERT(reporter, NULL == found);
124
125 for (int i = 1; i < 10; ++i) {
126 found = cache.find(i, findPos);
127
128 REPORTER_ASSERT(reporter, NULL != found && found->fValue > 0);
129 }
130 }
131
132 // make sure finding the positives wasn't a fluke - find the negatives
133 {
134 GrFindNegativesFunctor findNeg;
135
136 HashElement* found = cache.find(0, findNeg);
137 REPORTER_ASSERT(reporter, NULL == found);
138
139 for (int i = 1; i < 10; ++i) {
140 found = cache.find(i, findNeg);
141
142 REPORTER_ASSERT(reporter, NULL != found && found->fValue < 0);
143 }
144 }
145
146 // remove the 0:0 entries
147 {
148 cache.remove(0, &negHashElements[0]);
149 cache.remove(0, &posHashElements[0]);
150 REPORTER_ASSERT(reporter, 18 == cache.count());
151
152 HashElement* found = cache.find(0);
153 REPORTER_ASSERT(reporter, NULL == found);
154 }
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000155}
156
157////////////////////////////////////////////////////////////////////////////////
158#include "TestClassDef.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000159DEFINE_TESTCLASS("HashCache", HashCacheTestClass, TestHashCache)
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000160
161#endif