blob: 437a80c6b79c01ca31f5ae8fb1203a313d08261f [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"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000010
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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000062DEF_TEST(HashCache, reporter) {
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000063 GrTHashTable<HashElement, HashKey, 4> cache;
64
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000065 HashElement negHashElements[10] = {
66 { 0, 0 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000067 { 1, -1 },
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000068 { 2, -2 },
69 { 3, -3 },
70 { 4, -4 },
71 { 5, -5 },
72 { 6, -6 },
73 { 7, -7 },
74 { 8, -8 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000075 { 9, -9 }
76 };
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000077 HashElement posHashElements[10] = {
78 { 0, 0 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000079 { 1, 1 },
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +000080 { 2, 2 },
81 { 3, 3 },
82 { 4, 4 },
83 { 5, 5 },
84 { 6, 6 },
85 { 7, 7 },
86 { 8, 8 },
robertphillips@google.com3b57ded2012-09-18 17:16:33 +000087 { 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());
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000115
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000116 // 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 }
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000153}
154
robertphillips@google.com3b57ded2012-09-18 17:16:33 +0000155#endif