1 // Copyright 2011 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.
29 #include "test/cctest/cctest.h"
32 #include "src/debug/debug.h"
33 #include "src/execution.h"
34 #include "src/factory.h"
35 #include "src/global-handles.h"
36 #include "src/macro-assembler.h"
37 #include "src/objects.h"
39 using namespace v8::internal;
44 template<typename HashMap>
45 static void TestHashMap(Handle<HashMap> table) {
46 Isolate* isolate = CcTest::i_isolate();
47 Factory* factory = isolate->factory();
49 Handle<JSObject> a = factory->NewJSArray(7);
50 Handle<JSObject> b = factory->NewJSArray(11);
51 table = HashMap::Put(table, a, b);
52 CHECK_EQ(table->NumberOfElements(), 1);
53 CHECK_EQ(table->Lookup(a), *b);
54 // When the key does not exist in the map, Lookup returns the hole.
55 CHECK_EQ(table->Lookup(b), CcTest::heap()->the_hole_value());
57 // Keys still have to be valid after objects were moved.
58 CcTest::heap()->CollectGarbage(NEW_SPACE);
59 CHECK_EQ(table->NumberOfElements(), 1);
60 CHECK_EQ(table->Lookup(a), *b);
61 CHECK_EQ(table->Lookup(b), CcTest::heap()->the_hole_value());
63 // Keys that are overwritten should not change number of elements.
64 table = HashMap::Put(table, a, factory->NewJSArray(13));
65 CHECK_EQ(table->NumberOfElements(), 1);
66 CHECK_NE(table->Lookup(a), *b);
68 // Keys that have been removed are mapped to the hole.
69 bool was_present = false;
70 table = HashMap::Remove(table, a, &was_present);
72 CHECK_EQ(table->NumberOfElements(), 0);
73 CHECK_EQ(table->Lookup(a), CcTest::heap()->the_hole_value());
75 // Keys should map back to their respective values and also should get
76 // an identity hash code generated.
77 for (int i = 0; i < 100; i++) {
78 Handle<JSReceiver> key = factory->NewJSArray(7);
79 Handle<JSObject> value = factory->NewJSArray(11);
80 table = HashMap::Put(table, key, value);
81 CHECK_EQ(table->NumberOfElements(), i + 1);
82 CHECK_NE(table->FindEntry(key), HashMap::kNotFound);
83 CHECK_EQ(table->Lookup(key), *value);
84 CHECK(key->GetIdentityHash()->IsSmi());
87 // Keys never added to the map which already have an identity hash
88 // code should not be found.
89 for (int i = 0; i < 100; i++) {
90 Handle<JSReceiver> key = factory->NewJSArray(7);
91 CHECK(JSReceiver::GetOrCreateIdentityHash(key)->IsSmi());
92 CHECK_EQ(table->FindEntry(key), HashMap::kNotFound);
93 CHECK_EQ(table->Lookup(key), CcTest::heap()->the_hole_value());
94 CHECK(key->GetIdentityHash()->IsSmi());
97 // Keys that don't have an identity hash should not be found and also
98 // should not get an identity hash code generated.
99 for (int i = 0; i < 100; i++) {
100 Handle<JSReceiver> key = factory->NewJSArray(7);
101 CHECK_EQ(table->Lookup(key), CcTest::heap()->the_hole_value());
102 Object* identity_hash = key->GetIdentityHash();
103 CHECK_EQ(identity_hash, CcTest::heap()->undefined_value());
109 LocalContext context;
110 v8::HandleScope scope(context->GetIsolate());
111 Isolate* isolate = CcTest::i_isolate();
112 TestHashMap(ObjectHashTable::New(isolate, 23));
116 class ObjectHashTableTest: public ObjectHashTable {
118 void insert(int entry, int key, int value) {
119 set(EntryToIndex(entry), Smi::FromInt(key));
120 set(EntryToIndex(entry) + 1, Smi::FromInt(value));
123 int lookup(int key) {
124 Handle<Object> key_obj(Smi::FromInt(key), GetIsolate());
125 return Smi::cast(Lookup(key_obj))->value();
134 TEST(HashTableRehash) {
135 LocalContext context;
136 Isolate* isolate = CcTest::i_isolate();
137 v8::HandleScope scope(context->GetIsolate());
138 // Test almost filled table.
140 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
141 ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
142 int capacity = t->capacity();
143 for (int i = 0; i < capacity - 1; i++) {
144 t->insert(i, i * i, i);
146 t->Rehash(handle(Smi::FromInt(0), isolate));
147 for (int i = 0; i < capacity - 1; i++) {
148 CHECK_EQ(i, t->lookup(i * i));
151 // Test half-filled table.
153 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
154 ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
155 int capacity = t->capacity();
156 for (int i = 0; i < capacity / 2; i++) {
157 t->insert(i, i * i, i);
159 t->Rehash(handle(Smi::FromInt(0), isolate));
160 for (int i = 0; i < capacity / 2; i++) {
161 CHECK_EQ(i, t->lookup(i * i));
168 template<class HashSet>
169 static void TestHashSetCausesGC(Handle<HashSet> table) {
170 Isolate* isolate = CcTest::i_isolate();
171 Factory* factory = isolate->factory();
173 Handle<JSObject> key = factory->NewJSArray(0);
174 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);
176 // Force allocation of hash table backing store for hidden properties.
177 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
178 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
179 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
181 // Simulate a full heap so that generating an identity hash code
182 // in subsequent calls will request GC.
183 SimulateFullSpace(CcTest::heap()->new_space());
184 SimulateFullSpace(CcTest::heap()->old_space());
186 // Calling Contains() should not cause GC ever.
187 int gc_count = isolate->heap()->gc_count();
188 CHECK(!table->Contains(key));
189 CHECK(gc_count == isolate->heap()->gc_count());
191 // Calling Remove() will not cause GC in this case.
192 bool was_present = false;
193 table = HashSet::Remove(table, key, &was_present);
195 CHECK(gc_count == isolate->heap()->gc_count());
197 // Calling Add() should cause GC.
198 table = HashSet::Add(table, key);
199 CHECK(gc_count < isolate->heap()->gc_count());
205 template<class HashMap>
206 static void TestHashMapCausesGC(Handle<HashMap> table) {
207 Isolate* isolate = CcTest::i_isolate();
208 Factory* factory = isolate->factory();
210 Handle<JSObject> key = factory->NewJSArray(0);
211 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);
213 // Force allocation of hash table backing store for hidden properties.
214 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
215 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
216 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
218 // Simulate a full heap so that generating an identity hash code
219 // in subsequent calls will request GC.
220 SimulateFullSpace(CcTest::heap()->new_space());
221 SimulateFullSpace(CcTest::heap()->old_space());
223 // Calling Lookup() should not cause GC ever.
224 CHECK(table->Lookup(key)->IsTheHole());
226 // Calling Put() should request GC by returning a failure.
227 int gc_count = isolate->heap()->gc_count();
228 HashMap::Put(table, key, key);
229 CHECK(gc_count < isolate->heap()->gc_count());
233 TEST(ObjectHashTableCausesGC) {
234 i::FLAG_stress_compaction = false;
235 LocalContext context;
236 v8::HandleScope scope(context->GetIsolate());
237 Isolate* isolate = CcTest::i_isolate();
238 TestHashMapCausesGC(ObjectHashTable::New(isolate, 1));