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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 // Keys that don't have an identity hash should not be found and also | 94 // Keys that don't have an identity hash should not be found and also |
95 // should not get an identity hash code generated. | 95 // should not get an identity hash code generated. |
96 for (int i = 0; i < 100; i++) { | 96 for (int i = 0; i < 100; i++) { |
97 Handle<JSReceiver> key = factory->NewJSArray(7); | 97 Handle<JSReceiver> key = factory->NewJSArray(7); |
98 CHECK_EQ(table->Lookup(*key), HEAP->the_hole_value()); | 98 CHECK_EQ(table->Lookup(*key), HEAP->the_hole_value()); |
99 CHECK_EQ(key->GetIdentityHash(OMIT_CREATION), HEAP->undefined_value()); | 99 CHECK_EQ(key->GetIdentityHash(OMIT_CREATION), HEAP->undefined_value()); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 | 103 |
| 104 class ObjectHashTableTest: public ObjectHashTable { |
| 105 public: |
| 106 void insert(int entry, int key, int value) { |
| 107 set(EntryToIndex(entry), Smi::FromInt(key)); |
| 108 set(EntryToIndex(entry) + 1, Smi::FromInt(value)); |
| 109 } |
| 110 |
| 111 int lookup(int key) { |
| 112 return Smi::cast(Lookup(Smi::FromInt(key)))->value(); |
| 113 } |
| 114 |
| 115 int capacity() { |
| 116 return Capacity(); |
| 117 } |
| 118 }; |
| 119 |
| 120 |
| 121 TEST(HashTableRehash) { |
| 122 LocalContext context; |
| 123 Isolate* isolate = Isolate::Current(); |
| 124 Factory* factory = isolate->factory(); |
| 125 v8::HandleScope scope(context->GetIsolate()); |
| 126 // Test almost filled table. |
| 127 { |
| 128 Handle<ObjectHashTable> table = factory->NewObjectHashTable(100); |
| 129 ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table); |
| 130 int capacity = t->capacity(); |
| 131 for (int i = 0; i < capacity - 1; i++) { |
| 132 t->insert(i, i * i, i); |
| 133 } |
| 134 t->Rehash(Smi::FromInt(0)); |
| 135 for (int i = 0; i < capacity - 1; i++) { |
| 136 CHECK_EQ(i, t->lookup(i * i)); |
| 137 } |
| 138 } |
| 139 // Test half-filled table. |
| 140 { |
| 141 Handle<ObjectHashTable> table = factory->NewObjectHashTable(100); |
| 142 ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table); |
| 143 int capacity = t->capacity(); |
| 144 for (int i = 0; i < capacity / 2; i++) { |
| 145 t->insert(i, i * i, i); |
| 146 } |
| 147 t->Rehash(Smi::FromInt(0)); |
| 148 for (int i = 0; i < capacity / 2; i++) { |
| 149 CHECK_EQ(i, t->lookup(i * i)); |
| 150 } |
| 151 } |
| 152 } |
| 153 |
| 154 |
104 #ifdef DEBUG | 155 #ifdef DEBUG |
105 TEST(ObjectHashSetCausesGC) { | 156 TEST(ObjectHashSetCausesGC) { |
106 i::FLAG_stress_compaction = false; | 157 i::FLAG_stress_compaction = false; |
107 LocalContext context; | 158 LocalContext context; |
108 Isolate* isolate = Isolate::Current(); | 159 Isolate* isolate = Isolate::Current(); |
109 Factory* factory = isolate->factory(); | 160 Factory* factory = isolate->factory(); |
110 v8::HandleScope scope(context->GetIsolate()); | 161 v8::HandleScope scope(context->GetIsolate()); |
111 Handle<ObjectHashSet> table = factory->NewObjectHashSet(1); | 162 Handle<ObjectHashSet> table = factory->NewObjectHashSet(1); |
112 Handle<JSObject> key = factory->NewJSArray(0); | 163 Handle<JSObject> key = factory->NewJSArray(0); |
113 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key); | 164 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 SimulateFullSpace(HEAP->new_space()); | 206 SimulateFullSpace(HEAP->new_space()); |
156 SimulateFullSpace(HEAP->old_pointer_space()); | 207 SimulateFullSpace(HEAP->old_pointer_space()); |
157 | 208 |
158 // Calling Lookup() should not cause GC ever. | 209 // Calling Lookup() should not cause GC ever. |
159 CHECK(table->Lookup(*key)->IsTheHole()); | 210 CHECK(table->Lookup(*key)->IsTheHole()); |
160 | 211 |
161 // Calling Put() should request GC by returning a failure. | 212 // Calling Put() should request GC by returning a failure. |
162 CHECK(table->Put(*key, *key)->IsRetryAfterGC()); | 213 CHECK(table->Put(*key, *key)->IsRetryAfterGC()); |
163 } | 214 } |
164 #endif | 215 #endif |
OLD | NEW |