19258b6bc66e09368ada54001f619d53b4fc976d5ager@chromium.org// Copyright 2008 the V8 project authors. All rights reserved. 29a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Redistribution and use in source and binary forms, with or without 39a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// modification, are permitted provided that the following conditions are 49a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// met: 59a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// 69a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// * Redistributions of source code must retain the above copyright 79a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// notice, this list of conditions and the following disclaimer. 89a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// * Redistributions in binary form must reproduce the above 99a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// copyright notice, this list of conditions and the following 109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// disclaimer in the documentation and/or other materials provided 119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// with the distribution. 129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// * Neither the name of Google Inc. nor the names of its 139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// contributors may be used to endorse or promote products derived 149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// from this software without specific prior written permission. 159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// 169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include <stdlib.h> 299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 30196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "src/v8.h" 31196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "test/cctest/cctest.h" 329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 334b0feeef5d01dbc2948080b4f69daa37e1083461machenbach@chromium.org#include "src/hashmap.h" 344b0feeef5d01dbc2948080b4f69daa37e1083461machenbach@chromium.org 359a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comusing namespace v8::internal; 369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstatic bool DefaultMatchFun(void* a, void* b) { 389a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com return a == b; 399a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com} 409a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 419a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 42b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgtypedef uint32_t (*IntKeyHash)(uint32_t key); 43b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 44b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comclass IntSet { 469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com public: 47b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org explicit IntSet(IntKeyHash hash) : hash_(hash), map_(DefaultMatchFun) {} 489a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 499a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com void Insert(int x) { 503a37e9b96c768f6b5b6b09542e1cb1a1ece7a022ager@chromium.org CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value 51b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), hash_(x), true); 529a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(p != NULL); // insert is set! 539a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(reinterpret_cast<void*>(x), p->key); 549a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com // we don't care about p->value 559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 569a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 57b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org void Remove(int x) { 58b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value 59b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org map_.Remove(reinterpret_cast<void*>(x), hash_(x)); 60b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } 61b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 629a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com bool Present(int x) { 63b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org HashMap::Entry* p = 64b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org map_.Lookup(reinterpret_cast<void*>(x), hash_(x), false); 659a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com if (p != NULL) { 669a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(reinterpret_cast<void*>(x), p->key); 679a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 689a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com return p != NULL; 699a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 709a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 719a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com void Clear() { 729a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com map_.Clear(); 739a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 749a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 759a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com uint32_t occupancy() const { 769a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com uint32_t count = 0; 779a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com for (HashMap::Entry* p = map_.Start(); p != NULL; p = map_.Next(p)) { 789a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com count++; 799a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 809a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(map_.occupancy(), static_cast<double>(count)); 819a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com return count; 829a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 839a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 849a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com private: 85b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org IntKeyHash hash_; 869a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com HashMap map_; 879a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}; 889a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 899a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 90b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgstatic uint32_t Hash(uint32_t key) { return 23; } 91b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgstatic uint32_t CollisionHash(uint32_t key) { return key & 0x3; } 92b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 93b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 94b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgvoid TestSet(IntKeyHash hash, int size) { 95b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org IntSet set(hash); 969a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(0, set.occupancy()); 979a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 989a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(1); 999a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(2); 1009a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(3); 1019a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(3, set.occupancy()); 1029a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1039a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(2); 1049a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(3); 1059a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(3, set.occupancy()); 1069a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1079a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(set.Present(1)); 1089a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(set.Present(2)); 1099a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(set.Present(3)); 1109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(!set.Present(4)); 1119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(3, set.occupancy()); 1129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 113b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org set.Remove(1); 114b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(1)); 115b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(2)); 116b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(3)); 117b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(2, set.occupancy()); 118b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 119b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org set.Remove(3); 120b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(1)); 121b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(2)); 122b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(3)); 123b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(1, set.occupancy()); 124b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 1259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Clear(); 1269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(0, set.occupancy()); 1279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com // Insert a long series of values. 1299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com const int start = 453; 1309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com const int factor = 13; 1319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com const int offset = 7; 132b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org const uint32_t n = size; 1339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com int x = start; 1359a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com for (uint32_t i = 0; i < n; i++) { 1369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(i, static_cast<double>(set.occupancy())); 1379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(x); 138b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org x = x * factor + offset; 1399a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 140b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(n, static_cast<double>(set.occupancy())); 1419a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1429a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com // Verify the same sequence of values. 1439a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com x = start; 1449a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com for (uint32_t i = 0; i < n; i++) { 1459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(set.Present(x)); 146b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org x = x * factor + offset; 1479a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 1489a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(n, static_cast<double>(set.occupancy())); 149b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 150b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org // Remove all these values. 151b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org x = start; 152b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org for (uint32_t i = 0; i < n; i++) { 153b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(n - i, static_cast<double>(set.occupancy())); 154b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(x)); 155b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org set.Remove(x); 156b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(x)); 157b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org x = x * factor + offset; 158b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 159b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org // Verify the the expected values are still there. 160b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org int y = start; 161b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org for (uint32_t j = 0; j < n; j++) { 162b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org if (j <= i) { 163b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(y)); 164b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } else { 165b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(y)); 166b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } 167b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org y = y * factor + offset; 168b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } 169b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } 170b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(0, set.occupancy()); 171b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org} 172b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 173b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 174b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgTEST(Set) { 175b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org TestSet(Hash, 100); 176b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org TestSet(CollisionHash, 50); 1779a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com} 178