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 309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "v8.h" 319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "hashmap.h" 329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "cctest.h" 339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comusing namespace v8::internal; 359a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comstatic bool DefaultMatchFun(void* a, void* b) { 379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com return a == b; 389a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com} 399a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 409a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 41b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgtypedef uint32_t (*IntKeyHash)(uint32_t key); 42b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 43b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 449a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comclass IntSet { 459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com public: 46b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org explicit IntSet(IntKeyHash hash) : hash_(hash), map_(DefaultMatchFun) {} 479a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 489a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com void Insert(int x) { 493a37e9b96c768f6b5b6b09542e1cb1a1ece7a022ager@chromium.org CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value 50b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), hash_(x), true); 519a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(p != NULL); // insert is set! 529a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(reinterpret_cast<void*>(x), p->key); 539a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com // we don't care about p->value 549a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 56b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org void Remove(int x) { 57b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value 58b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org map_.Remove(reinterpret_cast<void*>(x), hash_(x)); 59b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } 60b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 619a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com bool Present(int x) { 62b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org HashMap::Entry* p = 63b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org map_.Lookup(reinterpret_cast<void*>(x), hash_(x), false); 649a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com if (p != NULL) { 659a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(reinterpret_cast<void*>(x), p->key); 669a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 679a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com return p != NULL; 689a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 699a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 709a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com void Clear() { 719a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com map_.Clear(); 729a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 739a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 749a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com uint32_t occupancy() const { 759a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com uint32_t count = 0; 769a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com for (HashMap::Entry* p = map_.Start(); p != NULL; p = map_.Next(p)) { 779a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com count++; 789a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 799a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(map_.occupancy(), static_cast<double>(count)); 809a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com return count; 819a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 829a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 839a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com private: 84b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org IntKeyHash hash_; 859a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com HashMap map_; 869a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}; 879a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 889a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 89b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgstatic uint32_t Hash(uint32_t key) { return 23; } 90b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgstatic uint32_t CollisionHash(uint32_t key) { return key & 0x3; } 91b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 92b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 93b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgvoid TestSet(IntKeyHash hash, int size) { 94b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org IntSet set(hash); 959a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(0, set.occupancy()); 969a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 979a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(1); 989a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(2); 999a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(3); 1009a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(3, set.occupancy()); 1019a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1029a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(2); 1039a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(3); 1049a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(3, set.occupancy()); 1059a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1069a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(set.Present(1)); 1079a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(set.Present(2)); 1089a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(set.Present(3)); 1099a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(!set.Present(4)); 1109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(3, set.occupancy()); 1119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 112b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org set.Remove(1); 113b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(1)); 114b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(2)); 115b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(3)); 116b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(2, set.occupancy()); 117b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 118b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org set.Remove(3); 119b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(1)); 120b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(2)); 121b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(3)); 122b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(1, set.occupancy()); 123b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 1249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Clear(); 1259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(0, set.occupancy()); 1269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com // Insert a long series of values. 1289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com const int start = 453; 1299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com const int factor = 13; 1309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com const int offset = 7; 131b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org const uint32_t n = size; 1329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com int x = start; 1349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com for (uint32_t i = 0; i < n; i++) { 1359a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(i, static_cast<double>(set.occupancy())); 1369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com set.Insert(x); 137b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org x = x * factor + offset; 1389a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 139b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(n, static_cast<double>(set.occupancy())); 1409a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com 1419a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com // Verify the same sequence of values. 1429a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com x = start; 1439a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com for (uint32_t i = 0; i < n; i++) { 1449a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK(set.Present(x)); 145b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org x = x * factor + offset; 1469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com } 1479a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com CHECK_EQ(n, static_cast<double>(set.occupancy())); 148b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 149b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org // Remove all these values. 150b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org x = start; 151b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org for (uint32_t i = 0; i < n; i++) { 152b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(n - i, static_cast<double>(set.occupancy())); 153b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(x)); 154b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org set.Remove(x); 155b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(x)); 156b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org x = x * factor + offset; 157b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 158b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org // Verify the the expected values are still there. 159b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org int y = start; 160b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org for (uint32_t j = 0; j < n; j++) { 161b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org if (j <= i) { 162b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(!set.Present(y)); 163b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } else { 164b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK(set.Present(y)); 165b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } 166b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org y = y * factor + offset; 167b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } 168b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org } 169b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org CHECK_EQ(0, set.occupancy()); 170b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org} 171b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 172b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org 173b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.orgTEST(Set) { 174b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org TestSet(Hash, 100); 175b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org TestSet(CollisionHash, 50); 1769a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com} 177