test-dictionary.cc 8.77 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/v8.h"
29
#include "test/cctest/cctest.h"
30

31 32 33 34
#include "src/api.h"
#include "src/debug.h"
#include "src/execution.h"
#include "src/factory.h"
35
#include "src/global-handles.h"
36 37
#include "src/macro-assembler.h"
#include "src/objects.h"
38 39 40

using namespace v8::internal;

41
namespace {
42

43 44 45

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

49 50
  Handle<JSObject> a = factory->NewJSArray(7);
  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());
56 57

  // Keys still have to be valid after objects were moved.
58
  CcTest::heap()->CollectGarbage(NEW_SPACE);
59
  CHECK_EQ(table->NumberOfElements(), 1);
60 61
  CHECK_EQ(table->Lookup(a), *b);
  CHECK_EQ(table->Lookup(b), CcTest::heap()->the_hole_value());
62 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);
67

68 69 70 71
  // Keys that have been removed are mapped to the hole.
  bool was_present = false;
  table = HashMap::Remove(table, a, &was_present);
  CHECK(was_present);
72
  CHECK_EQ(table->NumberOfElements(), 0);
73
  CHECK_EQ(table->Lookup(a), CcTest::heap()->the_hole_value());
74

75 76
  // Keys should map back to their respective values and also should get
  // 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());
85 86 87 88 89
  }

  // Keys never added to the map which already have an identity hash
  // code should not be found.
  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());
95 96
  }

97 98 99
  // 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++) {
100
    Handle<JSReceiver> key = factory->NewJSArray(7);
101
    CHECK_EQ(table->Lookup(key), CcTest::heap()->the_hole_value());
yangguo@chromium.org's avatar
yangguo@chromium.org committed
102 103
    Object* identity_hash = key->GetIdentityHash();
    CHECK_EQ(identity_hash, CcTest::heap()->undefined_value());
104 105
  }
}
106 107


108 109 110 111
TEST(HashMap) {
  LocalContext context;
  v8::HandleScope scope(context->GetIsolate());
  Isolate* isolate = CcTest::i_isolate();
112
  TestHashMap(ObjectHashTable::New(isolate, 23));
113 114 115 116
  TestHashMap(isolate->factory()->NewOrderedHashMap());
}


117 118 119 120 121 122 123 124
class ObjectHashTableTest: public ObjectHashTable {
 public:
  void insert(int entry, int key, int value) {
    set(EntryToIndex(entry), Smi::FromInt(key));
    set(EntryToIndex(entry) + 1, Smi::FromInt(value));
  }

  int lookup(int key) {
125 126
    Handle<Object> key_obj(Smi::FromInt(key), GetIsolate());
    return Smi::cast(Lookup(key_obj))->value();
127 128 129 130 131 132 133 134 135 136
  }

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


TEST(HashTableRehash) {
  LocalContext context;
137
  Isolate* isolate = CcTest::i_isolate();
138 139 140
  v8::HandleScope scope(context->GetIsolate());
  // Test almost filled table.
  {
141
    Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
142 143 144 145 146
    ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
    int capacity = t->capacity();
    for (int i = 0; i < capacity - 1; i++) {
      t->insert(i, i * i, i);
    }
147
    t->Rehash(handle(Smi::FromInt(0), isolate));
148 149 150 151 152 153
    for (int i = 0; i < capacity - 1; i++) {
      CHECK_EQ(i, t->lookup(i * i));
    }
  }
  // Test half-filled table.
  {
154
    Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
155 156 157 158 159
    ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
    int capacity = t->capacity();
    for (int i = 0; i < capacity / 2; i++) {
      t->insert(i, i * i, i);
    }
160
    t->Rehash(handle(Smi::FromInt(0), isolate));
161 162 163 164 165 166 167
    for (int i = 0; i < capacity / 2; i++) {
      CHECK_EQ(i, t->lookup(i * i));
    }
  }
}


168
#ifdef DEBUG
169 170
template<class HashSet>
static void TestHashSetCausesGC(Handle<HashSet> table) {
171
  Isolate* isolate = CcTest::i_isolate();
172
  Factory* factory = isolate->factory();
173

174
  Handle<JSObject> key = factory->NewJSArray(0);
175 176 177 178 179 180
  v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);

  // Force allocation of hash table backing store for hidden properties.
  key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
  key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
  key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
181 182 183

  // Simulate a full heap so that generating an identity hash code
  // in subsequent calls will request GC.
184 185
  SimulateFullSpace(CcTest::heap()->new_space());
  SimulateFullSpace(CcTest::heap()->old_pointer_space());
186 187

  // Calling Contains() should not cause GC ever.
188
  int gc_count = isolate->heap()->gc_count();
189
  CHECK(!table->Contains(key));
190
  CHECK(gc_count == isolate->heap()->gc_count());
191

192
  // Calling Remove() will not cause GC in this case.
193 194 195
  bool was_present = false;
  table = HashSet::Remove(table, key, &was_present);
  CHECK(!was_present);
196
  CHECK(gc_count == isolate->heap()->gc_count());
197

198
  // Calling Add() should cause GC.
199
  table = HashSet::Add(table, key);
200
  CHECK(gc_count < isolate->heap()->gc_count());
201 202 203
}


204
TEST(ObjectHashSetCausesGC) {
205
  i::FLAG_stress_compaction = false;
206
  LocalContext context;
207 208 209 210 211 212 213 214 215 216
  v8::HandleScope scope(context->GetIsolate());
  Isolate* isolate = CcTest::i_isolate();
  TestHashSetCausesGC(isolate->factory()->NewOrderedHashSet());
}
#endif


#ifdef DEBUG
template<class HashMap>
static void TestHashMapCausesGC(Handle<HashMap> table) {
217
  Isolate* isolate = CcTest::i_isolate();
218
  Factory* factory = isolate->factory();
219

220
  Handle<JSObject> key = factory->NewJSArray(0);
221 222 223 224 225 226
  v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);

  // Force allocation of hash table backing store for hidden properties.
  key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
  key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
  key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
227 228 229

  // Simulate a full heap so that generating an identity hash code
  // in subsequent calls will request GC.
230 231
  SimulateFullSpace(CcTest::heap()->new_space());
  SimulateFullSpace(CcTest::heap()->old_pointer_space());
232 233

  // Calling Lookup() should not cause GC ever.
234
  CHECK(table->Lookup(key)->IsTheHole());
235 236

  // Calling Put() should request GC by returning a failure.
237
  int gc_count = isolate->heap()->gc_count();
238
  HashMap::Put(table, key, key);
239
  CHECK(gc_count < isolate->heap()->gc_count());
240
}
241 242 243 244 245 246 247


TEST(ObjectHashTableCausesGC) {
  i::FLAG_stress_compaction = false;
  LocalContext context;
  v8::HandleScope scope(context->GetIsolate());
  Isolate* isolate = CcTest::i_isolate();
248
  TestHashMapCausesGC(ObjectHashTable::New(isolate, 1));
249 250
  TestHashMapCausesGC(isolate->factory()->NewOrderedHashMap());
}
251
#endif
252 253 254


}