OLD | NEW |
(Empty) | |
| 1 #include "Test.h" |
| 2 #include "SkTDynamicHash.h" |
| 3 |
| 4 namespace { |
| 5 |
| 6 struct Entry { |
| 7 int key; |
| 8 float value; |
| 9 |
| 10 static const int& Key(const Entry& entry) { return entry.key; } |
| 11 static uint32_t Hash(const int& key) { return key; } |
| 12 static bool Equal(const Entry& entry, const int& key) { return entry.key ==
key; } |
| 13 }; |
| 14 |
| 15 class Hash : public SkTDynamicHash<Entry, int, Entry::Key, Entry::Hash, Entry::E
qual> { |
| 16 public: |
| 17 Hash() : INHERITED() {} |
| 18 Hash(int capacity) : INHERITED(capacity) {} |
| 19 |
| 20 // Promote protected methods to public for this test. |
| 21 int capacity() const { return this->INHERITED::capacity(); } |
| 22 int countCollisions(const int& key) const { return this->INHERITED::countCol
lisions(key); } |
| 23 |
| 24 private: |
| 25 typedef SkTDynamicHash<Entry, int, Entry::Key, Entry::Hash, Entry::Equal> IN
HERITED; |
| 26 }; |
| 27 |
| 28 } // namespace |
| 29 |
| 30 #define ASSERT(x) REPORTER_ASSERT(reporter, x) |
| 31 |
| 32 static void test_growth(skiatest::Reporter* reporter) { |
| 33 Entry a = { 1, 2.0 }; |
| 34 Entry b = { 2, 3.0 }; |
| 35 Entry c = { 3, 4.0 }; |
| 36 Entry d = { 4, 5.0 }; |
| 37 Entry e = { 5, 6.0 }; |
| 38 |
| 39 Hash hash(0); |
| 40 ASSERT(hash.capacity() == 1); |
| 41 |
| 42 hash.add(&a); |
| 43 ASSERT(hash.capacity() == 2); |
| 44 |
| 45 hash.add(&b); |
| 46 ASSERT(hash.capacity() == 4); |
| 47 |
| 48 hash.add(&c); |
| 49 ASSERT(hash.capacity() == 8); |
| 50 |
| 51 hash.add(&d); |
| 52 ASSERT(hash.capacity() == 8); |
| 53 |
| 54 hash.add(&e); |
| 55 ASSERT(hash.capacity() == 16); |
| 56 |
| 57 ASSERT(hash.count() == 5); |
| 58 } |
| 59 |
| 60 static void test_add(skiatest::Reporter* reporter) { |
| 61 Hash hash; |
| 62 Entry a = { 1, 2.0 }; |
| 63 Entry b = { 2, 3.0 }; |
| 64 Entry c = { 1, 1.0 }; |
| 65 |
| 66 ASSERT(hash.count() == 0); |
| 67 hash.add(&a); |
| 68 ASSERT(hash.count() == 1); |
| 69 hash.add(&b); |
| 70 ASSERT(hash.count() == 2); |
| 71 hash.add(&c); // Overwrites a. |
| 72 ASSERT(hash.count() == 2); |
| 73 |
| 74 // Make sure the hash didn't modify the entries we inserted when overwriting
. |
| 75 ASSERT(a.value == 2.0); |
| 76 ASSERT(b.value == 3.0); |
| 77 ASSERT(c.value == 1.0); |
| 78 } |
| 79 |
| 80 static void test_lookup(skiatest::Reporter* reporter) { |
| 81 Hash hash(4); |
| 82 ASSERT(hash.capacity() == 4); |
| 83 |
| 84 // These collide. |
| 85 Entry a = { 1, 2.0 }; |
| 86 Entry b = { 5, 3.0 }; |
| 87 |
| 88 // Before we insert anything, nothing can collide. |
| 89 ASSERT(hash.countCollisions(1) == 0); |
| 90 ASSERT(hash.countCollisions(5) == 0); |
| 91 ASSERT(hash.countCollisions(9) == 0); |
| 92 |
| 93 // First is easy. |
| 94 hash.add(&a); |
| 95 ASSERT(hash.countCollisions(1) == 0); |
| 96 ASSERT(hash.countCollisions(5) == 1); |
| 97 ASSERT(hash.countCollisions(9) == 1); |
| 98 |
| 99 // Second is one step away. |
| 100 hash.add(&b); |
| 101 ASSERT(hash.countCollisions(1) == 0); |
| 102 ASSERT(hash.countCollisions(5) == 1); |
| 103 ASSERT(hash.countCollisions(9) == 2); |
| 104 |
| 105 // We can find our data right? |
| 106 ASSERT(hash.find(1) != NULL); |
| 107 ASSERT(hash.find(1)->value == 2.0); |
| 108 ASSERT(hash.find(5) != NULL); |
| 109 ASSERT(hash.find(5)->value == 3.0); |
| 110 |
| 111 // These aren't in the hash. |
| 112 ASSERT(hash.find(2) == NULL); |
| 113 ASSERT(hash.find(9) == NULL); |
| 114 } |
| 115 |
| 116 static void test_remove(skiatest::Reporter* reporter) { |
| 117 Hash hash(4); |
| 118 ASSERT(hash.capacity() == 4); |
| 119 |
| 120 // These collide. |
| 121 Entry a = { 1, 2.0 }; |
| 122 Entry b = { 5, 3.0 }; |
| 123 Entry c = { 9, 4.0 }; |
| 124 |
| 125 hash.add(&a); |
| 126 hash.add(&b); |
| 127 hash.remove(1); |
| 128 // a should be marked deleted, and b should still be findable. |
| 129 |
| 130 ASSERT(hash.find(1) == NULL); |
| 131 ASSERT(hash.find(5) != NULL); |
| 132 ASSERT(hash.find(5)->value == 3.0); |
| 133 |
| 134 // This will go in the same slot as 'a' did before. |
| 135 ASSERT(hash.countCollisions(9) == 0); |
| 136 hash.add(&c); |
| 137 ASSERT(hash.find(9) != NULL); |
| 138 ASSERT(hash.find(9)->value == 4.0); |
| 139 ASSERT(hash.find(5) != NULL); |
| 140 ASSERT(hash.find(5)->value == 3.0); |
| 141 } |
| 142 |
| 143 static void test_dynamic_hash(skiatest::Reporter* reporter) { |
| 144 test_growth(reporter); |
| 145 test_add(reporter); |
| 146 test_lookup(reporter); |
| 147 test_remove(reporter); |
| 148 } |
| 149 |
| 150 #include "TestClassDef.h" |
| 151 DEFINE_TESTCLASS("DynamicHash", DynamicHashTestClass, test_dynamic_hash); |
OLD | NEW |