OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Test.h" | 8 #include "Test.h" |
9 #include "TestClassDef.h" | 9 #include "TestClassDef.h" |
10 #include "SkTDynamicHash.h" | 10 #include "SkTDynamicHash.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 struct Entry { | 14 struct Entry { |
15 int key; | 15 int key; |
16 double value; | 16 double value; |
17 }; | 17 }; |
18 | 18 |
19 const int& GetKey(const Entry& entry) { return entry.key; } | 19 const int& GetKey(const Entry& entry) { return entry.key; } |
20 uint32_t GetHash(const int& key) { return key; } | 20 uint32_t GetHash(const int& key) { return key; } |
21 bool AreEqual(const Entry& entry, const int& key) { return entry.key == key; } | 21 bool AreEqual(const Entry& entry, const int& key) { return entry.key == key; } |
22 | 22 |
23 class Hash : public SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> { | 23 class Hash : public SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> { |
24 public: | 24 public: |
25 Hash() : INHERITED() {} | 25 Hash() : INHERITED() {} |
26 Hash(int capacity) : INHERITED(capacity) {} | |
27 | 26 |
28 // Promote protected methods to public for this test. | 27 // Promote protected methods to public for this test. |
29 int capacity() const { return this->INHERITED::capacity(); } | 28 int capacity() const { return this->INHERITED::capacity(); } |
30 int countCollisions(const int& key) const { return this->INHERITED::countCol
lisions(key); } | 29 int countCollisions(const int& key) const { return this->INHERITED::countCol
lisions(key); } |
31 | 30 |
32 private: | 31 private: |
33 typedef SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> INHERITED; | 32 typedef SkTDynamicHash<Entry, int, GetKey, GetHash, AreEqual> INHERITED; |
34 }; | 33 }; |
35 | 34 |
36 } // namespace | 35 } // namespace |
37 | 36 |
38 #define ASSERT(x) REPORTER_ASSERT(reporter, x) | 37 #define ASSERT(x) REPORTER_ASSERT(reporter, x) |
39 | 38 |
40 static void test_growth(skiatest::Reporter* reporter) { | 39 static void test_growth(skiatest::Reporter* reporter) { |
41 Entry a = { 1, 2.0 }; | 40 Entry a = { 1, 2.0 }; |
42 Entry b = { 2, 3.0 }; | 41 Entry b = { 2, 3.0 }; |
43 Entry c = { 3, 4.0 }; | 42 Entry c = { 3, 4.0 }; |
44 Entry d = { 4, 5.0 }; | 43 Entry d = { 4, 5.0 }; |
45 Entry e = { 5, 6.0 }; | 44 Entry e = { 5, 6.0 }; |
46 | 45 |
47 Hash hash(4); | 46 Hash hash; |
48 ASSERT(hash.capacity() == 4); | 47 ASSERT(hash.capacity() == 0); |
49 | 48 |
50 hash.add(&a); | 49 hash.add(&a); |
51 ASSERT(hash.capacity() == 4); | 50 ASSERT(hash.capacity() == 4); |
52 | 51 |
53 hash.add(&b); | 52 hash.add(&b); |
54 ASSERT(hash.capacity() == 4); | 53 ASSERT(hash.capacity() == 4); |
55 | 54 |
56 hash.add(&c); | 55 hash.add(&c); |
57 ASSERT(hash.capacity() == 4); | 56 ASSERT(hash.capacity() == 4); |
58 | 57 |
(...skipping 12 matching lines...) Expand all Loading... |
71 Entry b = { 2, 3.0 }; | 70 Entry b = { 2, 3.0 }; |
72 | 71 |
73 ASSERT(hash.count() == 0); | 72 ASSERT(hash.count() == 0); |
74 hash.add(&a); | 73 hash.add(&a); |
75 ASSERT(hash.count() == 1); | 74 ASSERT(hash.count() == 1); |
76 hash.add(&b); | 75 hash.add(&b); |
77 ASSERT(hash.count() == 2); | 76 ASSERT(hash.count() == 2); |
78 } | 77 } |
79 | 78 |
80 static void test_lookup(skiatest::Reporter* reporter) { | 79 static void test_lookup(skiatest::Reporter* reporter) { |
81 Hash hash(4); | 80 Hash hash; |
82 ASSERT(hash.capacity() == 4); | |
83 | 81 |
84 // These collide. | 82 // These collide. |
85 Entry a = { 1, 2.0 }; | 83 Entry a = { 1, 2.0 }; |
86 Entry b = { 5, 3.0 }; | 84 Entry b = { 5, 3.0 }; |
87 | 85 |
88 // Before we insert anything, nothing can collide. | 86 // Before we insert anything, nothing can collide. |
89 ASSERT(hash.countCollisions(1) == 0); | 87 ASSERT(hash.countCollisions(1) == 0); |
90 ASSERT(hash.countCollisions(5) == 0); | 88 ASSERT(hash.countCollisions(5) == 0); |
91 ASSERT(hash.countCollisions(9) == 0); | 89 ASSERT(hash.countCollisions(9) == 0); |
92 | 90 |
(...skipping 14 matching lines...) Expand all Loading... |
107 ASSERT(hash.find(1)->value == 2.0); | 105 ASSERT(hash.find(1)->value == 2.0); |
108 ASSERT(hash.find(5) != NULL); | 106 ASSERT(hash.find(5) != NULL); |
109 ASSERT(hash.find(5)->value == 3.0); | 107 ASSERT(hash.find(5)->value == 3.0); |
110 | 108 |
111 // These aren't in the hash. | 109 // These aren't in the hash. |
112 ASSERT(hash.find(2) == NULL); | 110 ASSERT(hash.find(2) == NULL); |
113 ASSERT(hash.find(9) == NULL); | 111 ASSERT(hash.find(9) == NULL); |
114 } | 112 } |
115 | 113 |
116 static void test_remove(skiatest::Reporter* reporter) { | 114 static void test_remove(skiatest::Reporter* reporter) { |
117 Hash hash(4); | 115 Hash hash; |
118 ASSERT(hash.capacity() == 4); | |
119 | 116 |
120 // These collide. | 117 // These collide. |
121 Entry a = { 1, 2.0 }; | 118 Entry a = { 1, 2.0 }; |
122 Entry b = { 5, 3.0 }; | 119 Entry b = { 5, 3.0 }; |
123 Entry c = { 9, 4.0 }; | 120 Entry c = { 9, 4.0 }; |
124 | 121 |
125 hash.add(&a); | 122 hash.add(&a); |
126 hash.add(&b); | 123 hash.add(&b); |
127 hash.remove(1); | 124 hash.remove(1); |
128 // a should be marked deleted, and b should still be findable. | 125 // a should be marked deleted, and b should still be findable. |
(...skipping 10 matching lines...) Expand all Loading... |
139 ASSERT(hash.find(5) != NULL); | 136 ASSERT(hash.find(5) != NULL); |
140 ASSERT(hash.find(5)->value == 3.0); | 137 ASSERT(hash.find(5)->value == 3.0); |
141 } | 138 } |
142 | 139 |
143 DEF_TEST(DynamicHash, reporter) { | 140 DEF_TEST(DynamicHash, reporter) { |
144 test_growth(reporter); | 141 test_growth(reporter); |
145 test_add(reporter); | 142 test_add(reporter); |
146 test_lookup(reporter); | 143 test_lookup(reporter); |
147 test_remove(reporter); | 144 test_remove(reporter); |
148 } | 145 } |
OLD | NEW |