| 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" |
| tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
| commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 10 | #include "SkTDynamicHash.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | struct Entry { |
| 15 | int key; |
| mtklein@google.com | 0d9f5f7 | 2013-08-05 23:08:19 +0000 | [diff] [blame] | 16 | double value; |
| commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 17 | }; |
| commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 18 | |
| mtklein@google.com | 0d9f5f7 | 2013-08-05 23:08:19 +0000 | [diff] [blame] | 19 | const int& GetKey(const Entry& entry) { return entry.key; } |
| 20 | uint32_t GetHash(const int& key) { return key; } |
| 21 | 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] | 22 | |
| mtklein@google.com | 0d9f5f7 | 2013-08-05 23:08:19 +0000 | [diff] [blame] | 23 | class Hash : public SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> { |
| commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 24 | public: |
| 25 | Hash() : INHERITED() {} |
| commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 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 | |
| commit-bot@chromium.org | 7a4b9fa | 2014-01-13 18:28:14 +0000 | [diff] [blame^] | 46 | Hash hash; |
| 47 | ASSERT(hash.capacity() == 0); |
| 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) { |
| commit-bot@chromium.org | 7a4b9fa | 2014-01-13 18:28:14 +0000 | [diff] [blame^] | 80 | Hash hash; |
| commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 81 | |
| 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 | |
| 114 | static void test_remove(skiatest::Reporter* reporter) { |
| commit-bot@chromium.org | 7a4b9fa | 2014-01-13 18:28:14 +0000 | [diff] [blame^] | 115 | Hash hash; |
| commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 116 | |
| 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 140 | DEF_TEST(DynamicHash, reporter) { |
| commit-bot@chromium.org | f916f9e | 2013-08-05 22:31:20 +0000 | [diff] [blame] | 141 | test_growth(reporter); |
| 142 | test_add(reporter); |
| 143 | test_lookup(reporter); |
| 144 | test_remove(reporter); |
| 145 | } |