OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 } | 106 } |
107 | 107 |
108 | 108 |
109 TEST(HashMap) { | 109 TEST(HashMap) { |
110 LocalContext context; | 110 LocalContext context; |
111 v8::HandleScope scope(context->GetIsolate()); | 111 v8::HandleScope scope(context->GetIsolate()); |
112 Isolate* isolate = CcTest::i_isolate(); | 112 Isolate* isolate = CcTest::i_isolate(); |
113 TestHashMap(ObjectHashTable::New(isolate, 23)); | 113 TestHashMap(ObjectHashTable::New(isolate, 23)); |
114 } | 114 } |
115 | 115 |
| 116 template <typename HashSet> |
| 117 static void TestHashSet(Handle<HashSet> table) { |
| 118 Isolate* isolate = CcTest::i_isolate(); |
| 119 Factory* factory = isolate->factory(); |
| 120 |
| 121 Handle<JSObject> a = factory->NewJSArray(7); |
| 122 Handle<JSObject> b = factory->NewJSArray(11); |
| 123 table = HashSet::Add(table, a); |
| 124 CHECK_EQ(table->NumberOfElements(), 1); |
| 125 CHECK(table->Has(isolate, a)); |
| 126 CHECK(!table->Has(isolate, b)); |
| 127 |
| 128 // Keys still have to be valid after objects were moved. |
| 129 CcTest::heap()->CollectGarbage(NEW_SPACE); |
| 130 CHECK_EQ(table->NumberOfElements(), 1); |
| 131 CHECK(table->Has(isolate, a)); |
| 132 CHECK(!table->Has(isolate, b)); |
| 133 |
| 134 // Keys that are overwritten should not change number of elements. |
| 135 table = HashSet::Add(table, a); |
| 136 CHECK_EQ(table->NumberOfElements(), 1); |
| 137 CHECK(table->Has(isolate, a)); |
| 138 CHECK(!table->Has(isolate, b)); |
| 139 |
| 140 // Keys that have been removed are mapped to the hole. |
| 141 // TODO(cbruni): not implemented yet. |
| 142 // bool was_present = false; |
| 143 // table = HashSet::Remove(table, a, &was_present); |
| 144 // CHECK(was_present); |
| 145 // CHECK_EQ(table->NumberOfElements(), 0); |
| 146 // CHECK(!table->Has(a)); |
| 147 // CHECK(!table->Has(b)); |
| 148 |
| 149 // Keys should map back to their respective values and also should get |
| 150 // an identity hash code generated. |
| 151 for (int i = 0; i < 100; i++) { |
| 152 Handle<JSReceiver> key = factory->NewJSArray(7); |
| 153 table = HashSet::Add(table, key); |
| 154 CHECK_EQ(table->NumberOfElements(), i + 2); |
| 155 CHECK(table->Has(isolate, key)); |
| 156 CHECK(JSReceiver::GetIdentityHash(isolate, key)->IsSmi()); |
| 157 } |
| 158 |
| 159 // Keys never added to the map which already have an identity hash |
| 160 // code should not be found. |
| 161 for (int i = 0; i < 100; i++) { |
| 162 Handle<JSReceiver> key = factory->NewJSArray(7); |
| 163 CHECK(JSReceiver::GetOrCreateIdentityHash(isolate, key)->IsSmi()); |
| 164 CHECK(!table->Has(isolate, key)); |
| 165 CHECK(JSReceiver::GetIdentityHash(isolate, key)->IsSmi()); |
| 166 } |
| 167 |
| 168 // Keys that don't have an identity hash should not be found and also |
| 169 // should not get an identity hash code generated. |
| 170 for (int i = 0; i < 100; i++) { |
| 171 Handle<JSReceiver> key = factory->NewJSArray(7); |
| 172 CHECK(!table->Has(isolate, key)); |
| 173 Object* identity_hash = JSReceiver::GetIdentityHash(isolate, key); |
| 174 CHECK_EQ(CcTest::heap()->undefined_value(), identity_hash); |
| 175 } |
| 176 } |
| 177 |
| 178 TEST(HashSet) { |
| 179 LocalContext context; |
| 180 v8::HandleScope scope(context->GetIsolate()); |
| 181 Isolate* isolate = CcTest::i_isolate(); |
| 182 TestHashSet(ObjectHashSet::New(isolate, 23)); |
| 183 } |
116 | 184 |
117 class ObjectHashTableTest: public ObjectHashTable { | 185 class ObjectHashTableTest: public ObjectHashTable { |
118 public: | 186 public: |
119 void insert(int entry, int key, int value) { | 187 void insert(int entry, int key, int value) { |
120 set(EntryToIndex(entry), Smi::FromInt(key)); | 188 set(EntryToIndex(entry), Smi::FromInt(key)); |
121 set(EntryToIndex(entry) + 1, Smi::FromInt(value)); | 189 set(EntryToIndex(entry) + 1, Smi::FromInt(value)); |
122 } | 190 } |
123 | 191 |
124 int lookup(int key) { | 192 int lookup(int key) { |
125 Handle<Object> key_obj(Smi::FromInt(key), GetIsolate()); | 193 Handle<Object> key_obj(Smi::FromInt(key), GetIsolate()); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 dict->SetRequiresCopyOnCapacityChange(); | 304 dict->SetRequiresCopyOnCapacityChange(); |
237 Handle<Name> key = isolate->factory()->InternalizeString( | 305 Handle<Name> key = isolate->factory()->InternalizeString( |
238 v8::Utils::OpenHandle(*v8_str("key"))); | 306 v8::Utils::OpenHandle(*v8_str("key"))); |
239 Handle<Object> value = handle(Smi::FromInt(0), isolate); | 307 Handle<Object> value = handle(Smi::FromInt(0), isolate); |
240 Handle<NameDictionary> new_dict = | 308 Handle<NameDictionary> new_dict = |
241 NameDictionary::Add(dict, key, value, PropertyDetails::Empty()); | 309 NameDictionary::Add(dict, key, value, PropertyDetails::Empty()); |
242 CHECK_NE(*dict, *new_dict); | 310 CHECK_NE(*dict, *new_dict); |
243 } | 311 } |
244 | 312 |
245 } // namespace | 313 } // namespace |
OLD | NEW |