OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "bin/hashmap.h" |
| 6 #include "platform/assert.h" |
| 7 #include "platform/globals.h" |
| 8 #include "vm/unit_test.h" |
| 9 |
| 10 // Default initial size of hashmaps used in these tests. |
| 11 static intptr_t kInitialSize = 8; |
| 12 |
| 13 |
| 14 typedef uint32_t (*IntKeyHash)(uint32_t key); |
| 15 |
| 16 |
| 17 class IntSet { |
| 18 public: |
| 19 explicit IntSet(IntKeyHash hash) |
| 20 : hash_(hash), map_(HashMap::SamePointerValue, kInitialSize) {} |
| 21 |
| 22 void Insert(int x) { |
| 23 EXPECT_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value |
| 24 HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), hash_(x), true); |
| 25 EXPECT(p != NULL); // insert is set! |
| 26 EXPECT_EQ(reinterpret_cast<void*>(x), p->key); |
| 27 // We don't care about p->value. |
| 28 } |
| 29 |
| 30 void Remove(int x) { |
| 31 EXPECT_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value |
| 32 map_.Remove(reinterpret_cast<void*>(x), hash_(x)); |
| 33 } |
| 34 |
| 35 bool Present(int x) { |
| 36 HashMap::Entry* p = |
| 37 map_.Lookup(reinterpret_cast<void*>(x), hash_(x), false); |
| 38 if (p != NULL) { |
| 39 EXPECT_EQ(reinterpret_cast<void*>(x), p->key); |
| 40 } |
| 41 return p != NULL; |
| 42 } |
| 43 |
| 44 void Clear() { |
| 45 map_.Clear(); |
| 46 } |
| 47 |
| 48 uint32_t occupancy() const { |
| 49 uint32_t count = 0; |
| 50 for (HashMap::Entry* p = map_.Start(); p != NULL; p = map_.Next(p)) { |
| 51 count++; |
| 52 } |
| 53 EXPECT_EQ(map_.occupancy_, count); |
| 54 return count; |
| 55 } |
| 56 |
| 57 private: |
| 58 IntKeyHash hash_; |
| 59 HashMap map_; |
| 60 }; |
| 61 |
| 62 |
| 63 static uint32_t WordHash(uint32_t key) { return dart::Utils::WordHash(key); } |
| 64 static uint32_t Hash(uint32_t key) { return 23; } |
| 65 static uint32_t CollisionHash1(uint32_t key) { return key & 0x3; } |
| 66 static uint32_t CollisionHash2(uint32_t key) { return kInitialSize - 1; } |
| 67 static uint32_t CollisionHash3(uint32_t key) { return kInitialSize - 2; } |
| 68 static uint32_t CollisionHash4(uint32_t key) { return kInitialSize - 2; } |
| 69 |
| 70 |
| 71 void TestSet(IntKeyHash hash, int size) { |
| 72 IntSet set(hash); |
| 73 EXPECT_EQ(0u, set.occupancy()); |
| 74 |
| 75 set.Insert(1); |
| 76 set.Insert(2); |
| 77 set.Insert(3); |
| 78 set.Insert(4); |
| 79 EXPECT_EQ(4u, set.occupancy()); |
| 80 |
| 81 set.Insert(2); |
| 82 set.Insert(3); |
| 83 EXPECT_EQ(4u, set.occupancy()); |
| 84 |
| 85 EXPECT(set.Present(1)); |
| 86 EXPECT(set.Present(2)); |
| 87 EXPECT(set.Present(3)); |
| 88 EXPECT(set.Present(4)); |
| 89 EXPECT(!set.Present(5)); |
| 90 EXPECT_EQ(4u, set.occupancy()); |
| 91 |
| 92 set.Remove(1); |
| 93 EXPECT(!set.Present(1)); |
| 94 EXPECT(set.Present(2)); |
| 95 EXPECT(set.Present(3)); |
| 96 EXPECT(set.Present(4)); |
| 97 EXPECT_EQ(3u, set.occupancy()); |
| 98 |
| 99 set.Remove(3); |
| 100 EXPECT(!set.Present(1)); |
| 101 EXPECT(set.Present(2)); |
| 102 EXPECT(!set.Present(3)); |
| 103 EXPECT(set.Present(4)); |
| 104 EXPECT_EQ(2u, set.occupancy()); |
| 105 |
| 106 set.Remove(4); |
| 107 EXPECT(!set.Present(1)); |
| 108 EXPECT(set.Present(2)); |
| 109 EXPECT(!set.Present(3)); |
| 110 EXPECT(!set.Present(4)); |
| 111 EXPECT_EQ(1u, set.occupancy()); |
| 112 |
| 113 set.Clear(); |
| 114 EXPECT_EQ(0u, set.occupancy()); |
| 115 |
| 116 // Insert a long series of values. |
| 117 const int start = 453; |
| 118 const int factor = 13; |
| 119 const int offset = 7; |
| 120 const uint32_t n = size; |
| 121 |
| 122 int x = start; |
| 123 for (uint32_t i = 0; i < n; i++) { |
| 124 EXPECT_EQ(i, set.occupancy()); |
| 125 set.Insert(x); |
| 126 x = x * factor + offset; |
| 127 } |
| 128 EXPECT_EQ(n, set.occupancy()); |
| 129 |
| 130 // Verify the same sequence of values. |
| 131 x = start; |
| 132 for (uint32_t i = 0; i < n; i++) { |
| 133 EXPECT(set.Present(x)); |
| 134 x = x * factor + offset; |
| 135 } |
| 136 EXPECT_EQ(n, set.occupancy()); |
| 137 |
| 138 // Remove all these values. |
| 139 x = start; |
| 140 for (uint32_t i = 0; i < n; i++) { |
| 141 EXPECT_EQ(n - i, set.occupancy()); |
| 142 EXPECT(set.Present(x)); |
| 143 set.Remove(x); |
| 144 EXPECT(!set.Present(x)); |
| 145 x = x * factor + offset; |
| 146 |
| 147 // Verify the the expected values are still there. |
| 148 int y = start; |
| 149 for (uint32_t j = 0; j < n; j++) { |
| 150 if (j <= i) { |
| 151 EXPECT(!set.Present(y)); |
| 152 } else { |
| 153 EXPECT(set.Present(y)); |
| 154 } |
| 155 y = y * factor + offset; |
| 156 } |
| 157 } |
| 158 EXPECT_EQ(0u, set.occupancy()); |
| 159 } |
| 160 |
| 161 |
| 162 UNIT_TEST_CASE(Set) { |
| 163 TestSet(WordHash, 100); |
| 164 TestSet(Hash, 100); |
| 165 TestSet(CollisionHash1, 50); |
| 166 TestSet(CollisionHash2, 50); |
| 167 TestSet(CollisionHash3, 50); |
| 168 TestSet(CollisionHash4, 50); |
| 169 } |
OLD | NEW |