1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
31 #include "test/cctest/cctest.h"
33 #include "src/hashmap.h"
35 using namespace v8::internal;
37 static bool DefaultMatchFun(void* a, void* b) {
42 typedef uint32_t (*IntKeyHash)(uint32_t key);
47 explicit IntSet(IntKeyHash hash) : hash_(hash), map_(DefaultMatchFun) {}
50 CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value
51 HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), hash_(x), true);
52 CHECK(p != NULL); // insert is set!
53 CHECK_EQ(reinterpret_cast<void*>(x), p->key);
54 // we don't care about p->value
58 CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value
59 map_.Remove(reinterpret_cast<void*>(x), hash_(x));
64 map_.Lookup(reinterpret_cast<void*>(x), hash_(x), false);
66 CHECK_EQ(reinterpret_cast<void*>(x), p->key);
75 uint32_t occupancy() const {
77 for (HashMap::Entry* p = map_.Start(); p != NULL; p = map_.Next(p)) {
80 CHECK_EQ(map_.occupancy(), static_cast<double>(count));
90 static uint32_t Hash(uint32_t key) { return 23; }
91 static uint32_t CollisionHash(uint32_t key) { return key & 0x3; }
94 void TestSet(IntKeyHash hash, int size) {
96 CHECK_EQ(0, set.occupancy());
101 CHECK_EQ(3, set.occupancy());
105 CHECK_EQ(3, set.occupancy());
107 CHECK(set.Present(1));
108 CHECK(set.Present(2));
109 CHECK(set.Present(3));
110 CHECK(!set.Present(4));
111 CHECK_EQ(3, set.occupancy());
114 CHECK(!set.Present(1));
115 CHECK(set.Present(2));
116 CHECK(set.Present(3));
117 CHECK_EQ(2, set.occupancy());
120 CHECK(!set.Present(1));
121 CHECK(set.Present(2));
122 CHECK(!set.Present(3));
123 CHECK_EQ(1, set.occupancy());
126 CHECK_EQ(0, set.occupancy());
128 // Insert a long series of values.
129 const int start = 453;
130 const int factor = 13;
131 const int offset = 7;
132 const uint32_t n = size;
135 for (uint32_t i = 0; i < n; i++) {
136 CHECK_EQ(i, static_cast<double>(set.occupancy()));
138 x = x * factor + offset;
140 CHECK_EQ(n, static_cast<double>(set.occupancy()));
142 // Verify the same sequence of values.
144 for (uint32_t i = 0; i < n; i++) {
145 CHECK(set.Present(x));
146 x = x * factor + offset;
148 CHECK_EQ(n, static_cast<double>(set.occupancy()));
150 // Remove all these values.
152 for (uint32_t i = 0; i < n; i++) {
153 CHECK_EQ(n - i, static_cast<double>(set.occupancy()));
154 CHECK(set.Present(x));
156 CHECK(!set.Present(x));
157 x = x * factor + offset;
159 // Verify the the expected values are still there.
161 for (uint32_t j = 0; j < n; j++) {
163 CHECK(!set.Present(y));
165 CHECK(set.Present(y));
167 y = y * factor + offset;
170 CHECK_EQ(0, set.occupancy());
176 TestSet(CollisionHash, 50);