blob: 4202270beb94655f1d922282feca31e870e37aee [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() {}
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000025
26 // Promote protected methods to public for this test.
27 int capacity() const { return this->INHERITED::capacity(); }
28 int countCollisions(const int& key) const { return this->INHERITED::countCollisions(key); }
29
30private:
mtklein@google.com0d9f5f72013-08-05 23:08:19 +000031 typedef SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> INHERITED;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000032};
33
34} // namespace
35
36#define ASSERT(x) REPORTER_ASSERT(reporter, x)
37
38static void test_growth(skiatest::Reporter* reporter) {
39 Entry a = { 1, 2.0 };
40 Entry b = { 2, 3.0 };
41 Entry c = { 3, 4.0 };
42 Entry d = { 4, 5.0 };
43 Entry e = { 5, 6.0 };
44
commit-bot@chromium.org7a4b9fa2014-01-13 18:28:14 +000045 Hash hash;
46 ASSERT(hash.capacity() == 0);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000047
48 hash.add(&a);
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000049 ASSERT(hash.capacity() == 4);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000050
51 hash.add(&b);
52 ASSERT(hash.capacity() == 4);
53
54 hash.add(&c);
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000055 ASSERT(hash.capacity() == 4);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000056
57 hash.add(&d);
58 ASSERT(hash.capacity() == 8);
59
60 hash.add(&e);
mtklein@google.comc9ab2b72013-08-12 14:51:25 +000061 ASSERT(hash.capacity() == 8);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000062
63 ASSERT(hash.count() == 5);
64}
65
66static void test_add(skiatest::Reporter* reporter) {
67 Hash hash;
68 Entry a = { 1, 2.0 };
69 Entry b = { 2, 3.0 };
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000070
71 ASSERT(hash.count() == 0);
72 hash.add(&a);
73 ASSERT(hash.count() == 1);
74 hash.add(&b);
75 ASSERT(hash.count() == 2);
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000076}
77
78static void test_lookup(skiatest::Reporter* reporter) {
commit-bot@chromium.org7a4b9fa2014-01-13 18:28:14 +000079 Hash hash;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +000080
81 // These collide.
82 Entry a = { 1, 2.0 };
83 Entry b = { 5, 3.0 };
84
85 // Before we insert anything, nothing can collide.
86 ASSERT(hash.countCollisions(1) == 0);
87 ASSERT(hash.countCollisions(5) == 0);
88 ASSERT(hash.countCollisions(9) == 0);
89
90 // First is easy.
91 hash.add(&a);
92 ASSERT(hash.countCollisions(1) == 0);
93 ASSERT(hash.countCollisions(5) == 1);
94 ASSERT(hash.countCollisions(9) == 1);
95
96 // Second is one step away.
97 hash.add(&b);
98 ASSERT(hash.countCollisions(1) == 0);
99 ASSERT(hash.countCollisions(5) == 1);
100 ASSERT(hash.countCollisions(9) == 2);
101
102 // We can find our data right?
103 ASSERT(hash.find(1) != NULL);
104 ASSERT(hash.find(1)->value == 2.0);
105 ASSERT(hash.find(5) != NULL);
106 ASSERT(hash.find(5)->value == 3.0);
107
108 // These aren't in the hash.
109 ASSERT(hash.find(2) == NULL);
110 ASSERT(hash.find(9) == NULL);
111}
112
113static void test_remove(skiatest::Reporter* reporter) {
commit-bot@chromium.org7a4b9fa2014-01-13 18:28:14 +0000114 Hash hash;
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000115
116 // These collide.
117 Entry a = { 1, 2.0 };
118 Entry b = { 5, 3.0 };
119 Entry c = { 9, 4.0 };
120
121 hash.add(&a);
122 hash.add(&b);
123 hash.remove(1);
124 // a should be marked deleted, and b should still be findable.
125
126 ASSERT(hash.find(1) == NULL);
127 ASSERT(hash.find(5) != NULL);
128 ASSERT(hash.find(5)->value == 3.0);
129
130 // This will go in the same slot as 'a' did before.
131 ASSERT(hash.countCollisions(9) == 0);
132 hash.add(&c);
133 ASSERT(hash.find(9) != NULL);
134 ASSERT(hash.find(9)->value == 4.0);
135 ASSERT(hash.find(5) != NULL);
136 ASSERT(hash.find(5)->value == 3.0);
137}
138
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000139DEF_TEST(DynamicHash, reporter) {
commit-bot@chromium.orgf916f9e2013-08-05 22:31:20 +0000140 test_growth(reporter);
141 test_add(reporter);
142 test_lookup(reporter);
143 test_remove(reporter);
144}