| OLD | NEW |
| 1 #include "Test.h" | 1 #include "Test.h" |
| 2 #include "SkTDynamicHash.h" | 2 #include "SkTDynamicHash.h" |
| 3 | 3 |
| 4 namespace { | 4 namespace { |
| 5 | 5 |
| 6 struct Entry { | 6 struct Entry { |
| 7 int key; | 7 int key; |
| 8 double value; | 8 double value; |
| 9 }; | 9 }; |
| 10 const int& GetKey(const Entry& entry) { return entry.key; } | 10 const int& GetKey(const Entry& entry) { return entry.key; } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 #define ASSERT(x) REPORTER_ASSERT(reporter, x) | 30 #define ASSERT(x) REPORTER_ASSERT(reporter, x) |
| 31 | 31 |
| 32 static void test_growth(skiatest::Reporter* reporter) { | 32 static void test_growth(skiatest::Reporter* reporter) { |
| 33 Entry a = { 1, 2.0 }; | 33 Entry a = { 1, 2.0 }; |
| 34 Entry b = { 2, 3.0 }; | 34 Entry b = { 2, 3.0 }; |
| 35 Entry c = { 3, 4.0 }; | 35 Entry c = { 3, 4.0 }; |
| 36 Entry d = { 4, 5.0 }; | 36 Entry d = { 4, 5.0 }; |
| 37 Entry e = { 5, 6.0 }; | 37 Entry e = { 5, 6.0 }; |
| 38 | 38 |
| 39 Hash hash(0); | 39 Hash hash(4); |
| 40 ASSERT(hash.capacity() == 1); | 40 ASSERT(hash.capacity() == 4); |
| 41 | 41 |
| 42 hash.add(&a); | 42 hash.add(&a); |
| 43 ASSERT(hash.capacity() == 2); | 43 ASSERT(hash.capacity() == 4); |
| 44 | 44 |
| 45 hash.add(&b); | 45 hash.add(&b); |
| 46 ASSERT(hash.capacity() == 4); | 46 ASSERT(hash.capacity() == 4); |
| 47 | 47 |
| 48 hash.add(&c); | 48 hash.add(&c); |
| 49 ASSERT(hash.capacity() == 8); | 49 ASSERT(hash.capacity() == 4); |
| 50 | 50 |
| 51 hash.add(&d); | 51 hash.add(&d); |
| 52 ASSERT(hash.capacity() == 8); | 52 ASSERT(hash.capacity() == 8); |
| 53 | 53 |
| 54 hash.add(&e); | 54 hash.add(&e); |
| 55 ASSERT(hash.capacity() == 16); | 55 ASSERT(hash.capacity() == 8); |
| 56 | 56 |
| 57 ASSERT(hash.count() == 5); | 57 ASSERT(hash.count() == 5); |
| 58 } | 58 } |
| 59 | 59 |
| 60 static void test_add(skiatest::Reporter* reporter) { | 60 static void test_add(skiatest::Reporter* reporter) { |
| 61 Hash hash; | 61 Hash hash; |
| 62 Entry a = { 1, 2.0 }; | 62 Entry a = { 1, 2.0 }; |
| 63 Entry b = { 2, 3.0 }; | 63 Entry b = { 2, 3.0 }; |
| 64 Entry c = { 1, 1.0 }; | |
| 65 | 64 |
| 66 ASSERT(hash.count() == 0); | 65 ASSERT(hash.count() == 0); |
| 67 hash.add(&a); | 66 hash.add(&a); |
| 68 ASSERT(hash.count() == 1); | 67 ASSERT(hash.count() == 1); |
| 69 hash.add(&b); | 68 hash.add(&b); |
| 70 ASSERT(hash.count() == 2); | 69 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 } | 70 } |
| 79 | 71 |
| 80 static void test_lookup(skiatest::Reporter* reporter) { | 72 static void test_lookup(skiatest::Reporter* reporter) { |
| 81 Hash hash(4); | 73 Hash hash(4); |
| 82 ASSERT(hash.capacity() == 4); | 74 ASSERT(hash.capacity() == 4); |
| 83 | 75 |
| 84 // These collide. | 76 // These collide. |
| 85 Entry a = { 1, 2.0 }; | 77 Entry a = { 1, 2.0 }; |
| 86 Entry b = { 5, 3.0 }; | 78 Entry b = { 5, 3.0 }; |
| 87 | 79 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 134 |
| 143 static void test_dynamic_hash(skiatest::Reporter* reporter) { | 135 static void test_dynamic_hash(skiatest::Reporter* reporter) { |
| 144 test_growth(reporter); | 136 test_growth(reporter); |
| 145 test_add(reporter); | 137 test_add(reporter); |
| 146 test_lookup(reporter); | 138 test_lookup(reporter); |
| 147 test_remove(reporter); | 139 test_remove(reporter); |
| 148 } | 140 } |
| 149 | 141 |
| 150 #include "TestClassDef.h" | 142 #include "TestClassDef.h" |
| 151 DEFINE_TESTCLASS("DynamicHash", DynamicHashTestClass, test_dynamic_hash); | 143 DEFINE_TESTCLASS("DynamicHash", DynamicHashTestClass, test_dynamic_hash); |
| OLD | NEW |