blob: d881738219b6526b889663bf05b7bf5b25d65f18 [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"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000010#include "SkTDynamicHash.h"
11
12namespace {
13
14struct Entry {
15 int key;
mtklein@google.com0d9f5f72013-08-05 23:08:19 +000016 double value;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000017};
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000018
mtklein@google.com0d9f5f72013-08-05 23:08:19 +000019const int& GetKey(const Entry& entry) { return entry.key; }
20uint32_t GetHash(const int& key) { return key; }
21bool AreEqual(const Entry& entry, const int& key) { return entry.key == key; }
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000022
mtklein@google.com0d9f5f72013-08-05 23:08:19 +000023class Hash : public SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> {
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000024public:
25 Hash() : INHERITED() {}
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000026
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
commit-bot@chromium.org7a4b9fa2014-01-13 18:28:14 +000046 Hash hash;
47 ASSERT(hash.capacity() == 0);
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) {
commit-bot@chromium.org7a4b9fa2014-01-13 18:28:14 +000080 Hash hash;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000081
82 // These collide.
83 Entry a = { 1, 2.0 };
84 Entry b = { 5, 3.0 };
85
86 // Before we insert anything, nothing can collide.
87 ASSERT(hash.countCollisions(1) == 0);
88 ASSERT(hash.countCollisions(5) == 0);
89 ASSERT(hash.countCollisions(9) == 0);
90
91 // First is easy.
92 hash.add(&a);
93 ASSERT(hash.countCollisions(1) == 0);
94 ASSERT(hash.countCollisions(5) == 1);
95 ASSERT(hash.countCollisions(9) == 1);
96
97 // Second is one step away.
98 hash.add(&b);
99 ASSERT(hash.countCollisions(1) == 0);
100 ASSERT(hash.countCollisions(5) == 1);
101 ASSERT(hash.countCollisions(9) == 2);
102
103 // We can find our data right?
104 ASSERT(hash.find(1) != NULL);
105 ASSERT(hash.find(1)->value == 2.0);
106 ASSERT(hash.find(5) != NULL);
107 ASSERT(hash.find(5)->value == 3.0);
108
109 // These aren't in the hash.
110 ASSERT(hash.find(2) == NULL);
111 ASSERT(hash.find(9) == NULL);
112}
113
114static void test_remove(skiatest::Reporter* reporter) {
commit-bot@chromium.org7a4b9fa2014-01-13 18:28:14 +0000115 Hash hash;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000116
117 // These collide.
118 Entry a = { 1, 2.0 };
119 Entry b = { 5, 3.0 };
120 Entry c = { 9, 4.0 };
121
122 hash.add(&a);
123 hash.add(&b);
124 hash.remove(1);
125 // a should be marked deleted, and b should still be findable.
126
127 ASSERT(hash.find(1) == NULL);
128 ASSERT(hash.find(5) != NULL);
129 ASSERT(hash.find(5)->value == 3.0);
130
131 // This will go in the same slot as 'a' did before.
132 ASSERT(hash.countCollisions(9) == 0);
133 hash.add(&c);
134 ASSERT(hash.find(9) != NULL);
135 ASSERT(hash.find(9)->value == 4.0);
136 ASSERT(hash.find(5) != NULL);
137 ASSERT(hash.find(5)->value == 3.0);
138}
139
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000140DEF_TEST(DynamicHash, reporter) {
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000141 test_growth(reporter);
142 test_add(reporter);
143 test_lookup(reporter);
144 test_remove(reporter);
145}