blob: f502dbae70287142449b8c1cd8ae5a438f6f354a [file] [log] [blame]
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +00001/*
2 * Copyright 2013 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
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +00008#include "Test.h"
9#include "SkTDynamicHash.h"
10
11namespace {
12
13struct Entry {
14 int key;
mtklein@google.com0d9f5f72013-08-05 23:08:19 +000015 double value;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000016};
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000017
mtklein@google.com0d9f5f72013-08-05 23:08:19 +000018const int& GetKey(const Entry& entry) { return entry.key; }
19uint32_t GetHash(const int& key) { return key; }
20bool AreEqual(const Entry& entry, const int& key) { return entry.key == key; }
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000021
mtklein@google.com0d9f5f72013-08-05 23:08:19 +000022class Hash : public SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> {
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000023public:
24 Hash() : INHERITED() {}
25 Hash(int capacity) : INHERITED(capacity) {}
26
27 // Promote protected methods to public for this test.
28 int capacity() const { return this->INHERITED::capacity(); }
29 int countCollisions(const int& key) const { return this->INHERITED::countCollisions(key); }
30
31private:
mtklein@google.com0d9f5f72013-08-05 23:08:19 +000032 typedef SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> INHERITED;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000033};
34
35} // namespace
36
37#define ASSERT(x) REPORTER_ASSERT(reporter, x)
38
39static void test_growth(skiatest::Reporter* reporter) {
40 Entry a = { 1, 2.0 };
41 Entry b = { 2, 3.0 };
42 Entry c = { 3, 4.0 };
43 Entry d = { 4, 5.0 };
44 Entry e = { 5, 6.0 };
45
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000046 Hash hash(4);
47 ASSERT(hash.capacity() == 4);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000048
49 hash.add(&a);
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000050 ASSERT(hash.capacity() == 4);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000051
52 hash.add(&b);
53 ASSERT(hash.capacity() == 4);
54
55 hash.add(&c);
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000056 ASSERT(hash.capacity() == 4);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000057
58 hash.add(&d);
59 ASSERT(hash.capacity() == 8);
60
61 hash.add(&e);
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000062 ASSERT(hash.capacity() == 8);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000063
64 ASSERT(hash.count() == 5);
65}
66
67static void test_add(skiatest::Reporter* reporter) {
68 Hash hash;
69 Entry a = { 1, 2.0 };
70 Entry b = { 2, 3.0 };
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000071
72 ASSERT(hash.count() == 0);
73 hash.add(&a);
74 ASSERT(hash.count() == 1);
75 hash.add(&b);
76 ASSERT(hash.count() == 2);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000077}
78
79static void test_lookup(skiatest::Reporter* reporter) {
80 Hash hash(4);
81 ASSERT(hash.capacity() == 4);
82
83 // These collide.
84 Entry a = { 1, 2.0 };
85 Entry b = { 5, 3.0 };
86
87 // Before we insert anything, nothing can collide.
88 ASSERT(hash.countCollisions(1) == 0);
89 ASSERT(hash.countCollisions(5) == 0);
90 ASSERT(hash.countCollisions(9) == 0);
91
92 // First is easy.
93 hash.add(&a);
94 ASSERT(hash.countCollisions(1) == 0);
95 ASSERT(hash.countCollisions(5) == 1);
96 ASSERT(hash.countCollisions(9) == 1);
97
98 // Second is one step away.
99 hash.add(&b);
100 ASSERT(hash.countCollisions(1) == 0);
101 ASSERT(hash.countCollisions(5) == 1);
102 ASSERT(hash.countCollisions(9) == 2);
103
104 // We can find our data right?
105 ASSERT(hash.find(1) != NULL);
106 ASSERT(hash.find(1)->value == 2.0);
107 ASSERT(hash.find(5) != NULL);
108 ASSERT(hash.find(5)->value == 3.0);
109
110 // These aren't in the hash.
111 ASSERT(hash.find(2) == NULL);
112 ASSERT(hash.find(9) == NULL);
113}
114
115static void test_remove(skiatest::Reporter* reporter) {
116 Hash hash(4);
117 ASSERT(hash.capacity() == 4);
118
119 // These collide.
120 Entry a = { 1, 2.0 };
121 Entry b = { 5, 3.0 };
122 Entry c = { 9, 4.0 };
123
124 hash.add(&a);
125 hash.add(&b);
126 hash.remove(1);
127 // a should be marked deleted, and b should still be findable.
128
129 ASSERT(hash.find(1) == NULL);
130 ASSERT(hash.find(5) != NULL);
131 ASSERT(hash.find(5)->value == 3.0);
132
133 // This will go in the same slot as 'a' did before.
134 ASSERT(hash.countCollisions(9) == 0);
135 hash.add(&c);
136 ASSERT(hash.find(9) != NULL);
137 ASSERT(hash.find(9)->value == 4.0);
138 ASSERT(hash.find(5) != NULL);
139 ASSERT(hash.find(5)->value == 3.0);
140}
141
142static void test_dynamic_hash(skiatest::Reporter* reporter) {
143 test_growth(reporter);
144 test_add(reporter);
145 test_lookup(reporter);
146 test_remove(reporter);
147}
148
149#include "TestClassDef.h"
150DEFINE_TESTCLASS("DynamicHash", DynamicHashTestClass, test_dynamic_hash);