| OLD | NEW |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include <stdlib.h> | 28 #include <stdlib.h> |
| 29 | 29 |
| 30 #include "src/v8.h" | 30 #include "src/v8.h" |
| 31 #include "test/cctest/cctest.h" | 31 #include "test/cctest/cctest.h" |
| 32 | 32 |
| 33 #include "src/hashmap.h" | 33 #include "src/base/hashmap.h" |
| 34 | 34 |
| 35 using namespace v8::internal; | 35 using namespace v8::internal; |
| 36 | 36 |
| 37 static bool DefaultMatchFun(void* a, void* b) { | 37 static bool DefaultMatchFun(void* a, void* b) { |
| 38 return a == b; | 38 return a == b; |
| 39 } | 39 } |
| 40 | 40 |
| 41 | 41 |
| 42 typedef uint32_t (*IntKeyHash)(uint32_t key); | 42 typedef uint32_t (*IntKeyHash)(uint32_t key); |
| 43 | 43 |
| 44 | 44 |
| 45 class IntSet { | 45 class IntSet { |
| 46 public: | 46 public: |
| 47 explicit IntSet(IntKeyHash hash) : hash_(hash), map_(DefaultMatchFun) {} | 47 explicit IntSet(IntKeyHash hash) : hash_(hash), map_(DefaultMatchFun) {} |
| 48 | 48 |
| 49 void Insert(int x) { | 49 void Insert(int x) { |
| 50 CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value | 50 CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value |
| 51 HashMap::Entry* p = | 51 v8::base::HashMap::Entry* p = |
| 52 map_.LookupOrInsert(reinterpret_cast<void*>(x), hash_(x)); | 52 map_.LookupOrInsert(reinterpret_cast<void*>(x), hash_(x)); |
| 53 CHECK(p != NULL); // insert is set! | 53 CHECK(p != NULL); // insert is set! |
| 54 CHECK_EQ(reinterpret_cast<void*>(x), p->key); | 54 CHECK_EQ(reinterpret_cast<void*>(x), p->key); |
| 55 // we don't care about p->value | 55 // we don't care about p->value |
| 56 } | 56 } |
| 57 | 57 |
| 58 void Remove(int x) { | 58 void Remove(int x) { |
| 59 CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value | 59 CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value |
| 60 map_.Remove(reinterpret_cast<void*>(x), hash_(x)); | 60 map_.Remove(reinterpret_cast<void*>(x), hash_(x)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 bool Present(int x) { | 63 bool Present(int x) { |
| 64 HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), hash_(x)); | 64 v8::base::HashMap::Entry* p = |
| 65 map_.Lookup(reinterpret_cast<void*>(x), hash_(x)); |
| 65 if (p != NULL) { | 66 if (p != NULL) { |
| 66 CHECK_EQ(reinterpret_cast<void*>(x), p->key); | 67 CHECK_EQ(reinterpret_cast<void*>(x), p->key); |
| 67 } | 68 } |
| 68 return p != NULL; | 69 return p != NULL; |
| 69 } | 70 } |
| 70 | 71 |
| 71 void Clear() { | 72 void Clear() { |
| 72 map_.Clear(); | 73 map_.Clear(); |
| 73 } | 74 } |
| 74 | 75 |
| 75 uint32_t occupancy() const { | 76 uint32_t occupancy() const { |
| 76 uint32_t count = 0; | 77 uint32_t count = 0; |
| 77 for (HashMap::Entry* p = map_.Start(); p != NULL; p = map_.Next(p)) { | 78 for (v8::base::HashMap::Entry* p = map_.Start(); p != NULL; |
| 79 p = map_.Next(p)) { |
| 78 count++; | 80 count++; |
| 79 } | 81 } |
| 80 CHECK_EQ(map_.occupancy(), static_cast<double>(count)); | 82 CHECK_EQ(map_.occupancy(), static_cast<double>(count)); |
| 81 return count; | 83 return count; |
| 82 } | 84 } |
| 83 | 85 |
| 84 private: | 86 private: |
| 85 IntKeyHash hash_; | 87 IntKeyHash hash_; |
| 86 HashMap map_; | 88 v8::base::HashMap map_; |
| 87 }; | 89 }; |
| 88 | 90 |
| 89 | 91 |
| 90 static uint32_t Hash(uint32_t key) { return 23; } | 92 static uint32_t Hash(uint32_t key) { return 23; } |
| 91 static uint32_t CollisionHash(uint32_t key) { return key & 0x3; } | 93 static uint32_t CollisionHash(uint32_t key) { return key & 0x3; } |
| 92 | 94 |
| 93 | 95 |
| 94 void TestSet(IntKeyHash hash, int size) { | 96 void TestSet(IntKeyHash hash, int size) { |
| 95 IntSet set(hash); | 97 IntSet set(hash); |
| 96 CHECK_EQ(0u, set.occupancy()); | 98 CHECK_EQ(0u, set.occupancy()); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } | 170 } |
| 169 } | 171 } |
| 170 CHECK_EQ(0u, set.occupancy()); | 172 CHECK_EQ(0u, set.occupancy()); |
| 171 } | 173 } |
| 172 | 174 |
| 173 | 175 |
| 174 TEST(HashSet) { | 176 TEST(HashSet) { |
| 175 TestSet(Hash, 100); | 177 TestSet(Hash, 100); |
| 176 TestSet(CollisionHash, 50); | 178 TestSet(CollisionHash, 50); |
| 177 } | 179 } |
| OLD | NEW |