commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 1 | /* |
| 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.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkTDynamicHash.h" |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | struct Entry { |
| 14 | int key; |
mtklein@google.com | 0d9f5f7 | 2013-08-05 23:08:19 +0000 | [diff] [blame] | 15 | double value; |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 16 | }; |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 17 | |
mtklein@google.com | 0d9f5f7 | 2013-08-05 23:08:19 +0000 | [diff] [blame] | 18 | const int& GetKey(const Entry& entry) { return entry.key; } |
| 19 | uint32_t GetHash(const int& key) { return key; } |
| 20 | bool AreEqual(const Entry& entry, const int& key) { return entry.key == key; } |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 21 | |
mtklein@google.com | 0d9f5f7 | 2013-08-05 23:08:19 +0000 | [diff] [blame] | 22 | class Hash : public SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> { |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 23 | public: |
| 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 | |
| 31 | private: |
mtklein@google.com | 0d9f5f7 | 2013-08-05 23:08:19 +0000 | [diff] [blame] | 32 | typedef SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> INHERITED; |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | #define ASSERT(x) REPORTER_ASSERT(reporter, x) |
| 38 | |
| 39 | static 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.com | c9ab2b7 | 2013-08-12 14:51:25 +0000 | [diff] [blame] | 46 | Hash hash(4); |
| 47 | ASSERT(hash.capacity() == 4); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 48 | |
| 49 | hash.add(&a); |
mtklein@google.com | c9ab2b7 | 2013-08-12 14:51:25 +0000 | [diff] [blame] | 50 | ASSERT(hash.capacity() == 4); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 51 | |
| 52 | hash.add(&b); |
| 53 | ASSERT(hash.capacity() == 4); |
| 54 | |
| 55 | hash.add(&c); |
mtklein@google.com | c9ab2b7 | 2013-08-12 14:51:25 +0000 | [diff] [blame] | 56 | ASSERT(hash.capacity() == 4); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 57 | |
| 58 | hash.add(&d); |
| 59 | ASSERT(hash.capacity() == 8); |
| 60 | |
| 61 | hash.add(&e); |
mtklein@google.com | c9ab2b7 | 2013-08-12 14:51:25 +0000 | [diff] [blame] | 62 | ASSERT(hash.capacity() == 8); |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 63 | |
| 64 | ASSERT(hash.count() == 5); |
| 65 | } |
| 66 | |
| 67 | static void test_add(skiatest::Reporter* reporter) { |
| 68 | Hash hash; |
| 69 | Entry a = { 1, 2.0 }; |
| 70 | Entry b = { 2, 3.0 }; |
commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 71 | |
| 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.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static 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 | |
| 115 | static 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 | |
| 142 | static 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" |
| 150 | DEFINE_TESTCLASS("DynamicHash", DynamicHashTestClass, test_dynamic_hash); |