test-dictionary.cc 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
#include "src/init/v8.h"
29
#include "test/cctest/cctest.h"
30

31
#include "src/builtins/builtins-constructor.h"
32
#include "src/debug/debug.h"
33
#include "src/execution/execution.h"
34
#include "src/handles/global-handles.h"
35
#include "src/heap/factory.h"
36
#include "src/heap/spaces.h"
37
#include "src/objects/hash-table-inl.h"
38
#include "src/objects/objects-inl.h"
39
#include "src/roots/roots.h"
40
#include "test/cctest/heap/heap-utils.h"
41

42 43
namespace v8 {
namespace internal {
44

45
namespace {
46

47 48 49

template<typename HashMap>
static void TestHashMap(Handle<HashMap> table) {
50
  Isolate* isolate = CcTest::i_isolate();
51
  Factory* factory = isolate->factory();
52

53 54
  Handle<JSObject> a = factory->NewJSArray(7);
  Handle<JSObject> b = factory->NewJSArray(11);
55
  table = HashMap::Put(table, a, b);
56
  CHECK_EQ(1, table->NumberOfElements());
57
  CHECK_EQ(table->Lookup(a), *b);
58
  // When the key does not exist in the map, Lookup returns the hole.
59 60
  ReadOnlyRoots roots(CcTest::heap());
  CHECK_EQ(table->Lookup(b), roots.the_hole_value());
61 62

  // Keys still have to be valid after objects were moved.
63
  CcTest::CollectGarbage(NEW_SPACE);
64
  CHECK_EQ(1, table->NumberOfElements());
65
  CHECK_EQ(table->Lookup(a), *b);
66
  CHECK_EQ(table->Lookup(b), roots.the_hole_value());
67 68

  // Keys that are overwritten should not change number of elements.
69
  table = HashMap::Put(table, a, factory->NewJSArray(13));
70
  CHECK_EQ(1, table->NumberOfElements());
71
  CHECK_NE(table->Lookup(a), *b);
72

73 74
  // Keys that have been removed are mapped to the hole.
  bool was_present = false;
75
  table = HashMap::Remove(isolate, table, a, &was_present);
76
  CHECK(was_present);
77
  CHECK_EQ(0, table->NumberOfElements());
78
  CHECK_EQ(table->Lookup(a), roots.the_hole_value());
79

80 81
  // Keys should map back to their respective values and also should get
  // an identity hash code generated.
82
  for (int i = 0; i < 100; i++) {
83
    Handle<JSReceiver> key = factory->NewJSArray(7);
84
    Handle<JSObject> value = factory->NewJSArray(11);
85
    table = HashMap::Put(table, key, value);
86
    CHECK_EQ(table->NumberOfElements(), i + 1);
87
    CHECK(table->FindEntry(isolate, key).is_found());
88
    CHECK_EQ(table->Lookup(key), *value);
89
    CHECK(key->GetIdentityHash().IsSmi());
90 91 92 93 94
  }

  // Keys never added to the map which already have an identity hash
  // code should not be found.
  for (int i = 0; i < 100; i++) {
95
    Handle<JSReceiver> key = factory->NewJSArray(7);
96
    CHECK(key->GetOrCreateIdentityHash(isolate).IsSmi());
97
    CHECK(table->FindEntry(isolate, key).is_not_found());
98
    CHECK_EQ(table->Lookup(key), roots.the_hole_value());
99
    CHECK(key->GetIdentityHash().IsSmi());
100 101
  }

102 103 104
  // Keys that don't have an identity hash should not be found and also
  // should not get an identity hash code generated.
  for (int i = 0; i < 100; i++) {
105
    Handle<JSReceiver> key = factory->NewJSArray(7);
106
    CHECK_EQ(table->Lookup(key), roots.the_hole_value());
107
    Object identity_hash = key->GetIdentityHash();
108
    CHECK_EQ(roots.undefined_value(), identity_hash);
109 110
  }
}
111 112


113 114 115 116
TEST(HashMap) {
  LocalContext context;
  v8::HandleScope scope(context->GetIsolate());
  Isolate* isolate = CcTest::i_isolate();
117
  TestHashMap(ObjectHashTable::New(isolate, 23));
118 119
}

120 121 122 123 124 125 126
template <typename HashSet>
static void TestHashSet(Handle<HashSet> table) {
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();

  Handle<JSObject> a = factory->NewJSArray(7);
  Handle<JSObject> b = factory->NewJSArray(11);
127
  table = HashSet::Add(isolate, table, a);
128
  CHECK_EQ(1, table->NumberOfElements());
129 130 131 132
  CHECK(table->Has(isolate, a));
  CHECK(!table->Has(isolate, b));

  // Keys still have to be valid after objects were moved.
133
  CcTest::CollectGarbage(NEW_SPACE);
134
  CHECK_EQ(1, table->NumberOfElements());
135 136 137 138
  CHECK(table->Has(isolate, a));
  CHECK(!table->Has(isolate, b));

  // Keys that are overwritten should not change number of elements.
139
  table = HashSet::Add(isolate, table, a);
140
  CHECK_EQ(1, table->NumberOfElements());
141 142 143 144 145 146 147 148
  CHECK(table->Has(isolate, a));
  CHECK(!table->Has(isolate, b));

  // Keys that have been removed are mapped to the hole.
  // TODO(cbruni): not implemented yet.
  // bool was_present = false;
  // table = HashSet::Remove(table, a, &was_present);
  // CHECK(was_present);
149
  // CHECK_EQ(0, table->NumberOfElements());
150 151 152 153 154 155 156
  // CHECK(!table->Has(a));
  // CHECK(!table->Has(b));

  // Keys should map back to their respective values and also should get
  // an identity hash code generated.
  for (int i = 0; i < 100; i++) {
    Handle<JSReceiver> key = factory->NewJSArray(7);
157
    table = HashSet::Add(isolate, table, key);
158 159
    CHECK_EQ(table->NumberOfElements(), i + 2);
    CHECK(table->Has(isolate, key));
160
    CHECK(key->GetIdentityHash().IsSmi());
161 162 163 164 165 166
  }

  // Keys never added to the map which already have an identity hash
  // code should not be found.
  for (int i = 0; i < 100; i++) {
    Handle<JSReceiver> key = factory->NewJSArray(7);
167
    CHECK(key->GetOrCreateIdentityHash(isolate).IsSmi());
168
    CHECK(!table->Has(isolate, key));
169
    CHECK(key->GetIdentityHash().IsSmi());
170 171 172 173 174 175 176
  }

  // Keys that don't have an identity hash should not be found and also
  // should not get an identity hash code generated.
  for (int i = 0; i < 100; i++) {
    Handle<JSReceiver> key = factory->NewJSArray(7);
    CHECK(!table->Has(isolate, key));
177
    Object identity_hash = key->GetIdentityHash();
178
    CHECK_EQ(ReadOnlyRoots(CcTest::heap()).undefined_value(), identity_hash);
179 180 181 182 183 184 185 186 187
  }
}

TEST(HashSet) {
  LocalContext context;
  v8::HandleScope scope(context->GetIsolate());
  Isolate* isolate = CcTest::i_isolate();
  TestHashSet(ObjectHashSet::New(isolate, 23));
}
188

189 190
class ObjectHashTableTest: public ObjectHashTable {
 public:
191 192
  explicit ObjectHashTableTest(ObjectHashTable o) : ObjectHashTable(o) {}

193
  void insert(InternalIndex entry, int key, int value) {
194 195 196 197 198
    set(EntryToIndex(entry), Smi::FromInt(key));
    set(EntryToIndex(entry) + 1, Smi::FromInt(value));
  }

  int lookup(int key) {
199
    Handle<Object> key_obj(Smi::FromInt(key), CcTest::i_isolate());
jgruber's avatar
jgruber committed
200
    return Smi::ToInt(Lookup(key_obj));
201 202 203 204 205 206 207 208 209 210
  }

  int capacity() {
    return Capacity();
  }
};


TEST(HashTableRehash) {
  LocalContext context;
211
  Isolate* isolate = CcTest::i_isolate();
212 213 214
  v8::HandleScope scope(context->GetIsolate());
  // Test almost filled table.
  {
215
    Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
216
    ObjectHashTableTest t(*table);
217
    int capacity = t.capacity();
218
    for (int i = 0; i < capacity - 1; i++) {
219
      t.insert(InternalIndex(i), i * i, i);
220
    }
221
    t.Rehash(isolate);
222
    for (int i = 0; i < capacity - 1; i++) {
223
      CHECK_EQ(i, t.lookup(i * i));
224 225 226 227
    }
  }
  // Test half-filled table.
  {
228
    Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
229
    ObjectHashTableTest t(*table);
230
    int capacity = t.capacity();
231
    for (int i = 0; i < capacity / 2; i++) {
232
      t.insert(InternalIndex(i), i * i, i);
233
    }
234
    t.Rehash(isolate);
235
    for (int i = 0; i < capacity / 2; i++) {
236
      CHECK_EQ(i, t.lookup(i * i));
237 238 239 240 241
    }
  }
}


242
#ifdef DEBUG
243 244
template<class HashSet>
static void TestHashSetCausesGC(Handle<HashSet> table) {
245
  Isolate* isolate = CcTest::i_isolate();
246
  Factory* factory = isolate->factory();
247

248
  Handle<JSObject> key = factory->NewJSArray(0);
249 250 251

  // Simulate a full heap so that generating an identity hash code
  // in subsequent calls will request GC.
252 253
  heap::SimulateFullSpace(CcTest::heap()->new_space());
  heap::SimulateFullSpace(CcTest::heap()->old_space());
254 255

  // Calling Contains() should not cause GC ever.
256
  int gc_count = isolate->heap()->gc_count();
257
  CHECK(!table->Contains(key));
258
  CHECK(gc_count == isolate->heap()->gc_count());
259

260
  // Calling Remove() will not cause GC in this case.
261 262 263
  bool was_present = false;
  table = HashSet::Remove(table, key, &was_present);
  CHECK(!was_present);
264
  CHECK(gc_count == isolate->heap()->gc_count());
265

266
  // Calling Add() should cause GC.
267
  table = HashSet::Add(table, key);
268
  CHECK(gc_count < isolate->heap()->gc_count());
269
}
270 271 272 273
#endif


#ifdef DEBUG
274 275
template <class HashMap>
static void TestHashMapDoesNotCauseGC(Handle<HashMap> table) {
276
  Isolate* isolate = CcTest::i_isolate();
277
  Factory* factory = isolate->factory();
278

279
  Handle<JSObject> key = factory->NewJSArray(0);
280

281 282
  // Even though we simulate a full heap, generating an identity hash
  // code in subsequent calls will not request GC.
283 284 285
  if (!FLAG_single_generation) {
    heap::SimulateFullSpace(CcTest::heap()->new_space());
  }
286
  heap::SimulateFullSpace(CcTest::heap()->old_space());
287 288

  // Calling Lookup() should not cause GC ever.
289
  CHECK(table->Lookup(key).IsTheHole(isolate));
290 291

  // Calling Put() should request GC by returning a failure.
292
  int gc_count = isolate->heap()->gc_count();
293
  HashMap::Put(table, key, key);
294
  CHECK(gc_count == isolate->heap()->gc_count());
295
}
296 297 298 299


TEST(ObjectHashTableCausesGC) {
  i::FLAG_stress_compaction = false;
300 301
  // For SimulateFullSpace in TestHashMapDoesNotCauseGC.
  i::FLAG_stress_concurrent_allocation = false;
302 303 304
  LocalContext context;
  v8::HandleScope scope(context->GetIsolate());
  Isolate* isolate = CcTest::i_isolate();
305
  TestHashMapDoesNotCauseGC(ObjectHashTable::New(isolate, 1));
306
}
307
#endif
308

309 310 311 312 313
TEST(MaximumClonedShallowObjectProperties) {
  // Assert that a NameDictionary with kMaximumClonedShallowObjectProperties is
  // not in large-object space.
  const int max_capacity = NameDictionary::ComputeCapacity(
      ConstructorBuiltins::kMaximumClonedShallowObjectProperties);
314 315
  const InternalIndex max_literal_entry(max_capacity /
                                        NameDictionary::kEntrySize);
316 317 318 319 320
  const int max_literal_index = NameDictionary::EntryToIndex(max_literal_entry);
  CHECK_LE(NameDictionary::OffsetOfElementAt(max_literal_index),
           kMaxRegularHeapObjectSize);
}

321
}  // namespace
322 323 324

}  // namespace internal
}  // namespace v8