LCOV - code coverage report
Current view: top level - test/cctest - test-dictionary.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 97 97 100.0 %
Date: 2019-04-17 Functions: 10 10 100.0 %

          Line data    Source code
       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
       4             : // met:
       5             : //
       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.
      15             : //
      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.
      27             : 
      28             : #include "src/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.h"
      34             : #include "src/global-handles.h"
      35             : #include "src/heap/factory.h"
      36             : #include "src/heap/spaces.h"
      37             : #include "src/objects-inl.h"
      38             : #include "src/objects/hash-table-inl.h"
      39             : #include "src/roots.h"
      40             : #include "test/cctest/heap/heap-utils.h"
      41             : 
      42             : namespace v8 {
      43             : namespace internal {
      44             : 
      45             : namespace {
      46             : 
      47             : 
      48             : template<typename HashMap>
      49           5 : static void TestHashMap(Handle<HashMap> table) {
      50             :   Isolate* isolate = CcTest::i_isolate();
      51             :   Factory* factory = isolate->factory();
      52             : 
      53             :   Handle<JSObject> a = factory->NewJSArray(7);
      54             :   Handle<JSObject> b = factory->NewJSArray(11);
      55           5 :   table = HashMap::Put(table, a, b);
      56           5 :   CHECK_EQ(1, table->NumberOfElements());
      57          10 :   CHECK_EQ(table->Lookup(a), *b);
      58             :   // When the key does not exist in the map, Lookup returns the hole.
      59           5 :   ReadOnlyRoots roots(CcTest::heap());
      60          10 :   CHECK_EQ(table->Lookup(b), roots.the_hole_value());
      61             : 
      62             :   // Keys still have to be valid after objects were moved.
      63           5 :   CcTest::CollectGarbage(NEW_SPACE);
      64           5 :   CHECK_EQ(1, table->NumberOfElements());
      65          10 :   CHECK_EQ(table->Lookup(a), *b);
      66          10 :   CHECK_EQ(table->Lookup(b), roots.the_hole_value());
      67             : 
      68             :   // Keys that are overwritten should not change number of elements.
      69           5 :   table = HashMap::Put(table, a, factory->NewJSArray(13));
      70           5 :   CHECK_EQ(1, table->NumberOfElements());
      71          10 :   CHECK_NE(table->Lookup(a), *b);
      72             : 
      73             :   // Keys that have been removed are mapped to the hole.
      74           5 :   bool was_present = false;
      75           5 :   table = HashMap::Remove(isolate, table, a, &was_present);
      76           5 :   CHECK(was_present);
      77           5 :   CHECK_EQ(0, table->NumberOfElements());
      78          10 :   CHECK_EQ(table->Lookup(a), roots.the_hole_value());
      79             : 
      80             :   // Keys should map back to their respective values and also should get
      81             :   // an identity hash code generated.
      82         505 :   for (int i = 0; i < 100; i++) {
      83             :     Handle<JSReceiver> key = factory->NewJSArray(7);
      84             :     Handle<JSObject> value = factory->NewJSArray(11);
      85         500 :     table = HashMap::Put(table, key, value);
      86        1000 :     CHECK_EQ(table->NumberOfElements(), i + 1);
      87         500 :     CHECK_NE(table->FindEntry(isolate, key), HashMap::kNotFound);
      88        1000 :     CHECK_EQ(table->Lookup(key), *value);
      89        1000 :     CHECK(key->GetIdentityHash()->IsSmi());
      90             :   }
      91             : 
      92             :   // Keys never added to the map which already have an identity hash
      93             :   // code should not be found.
      94        1005 :   for (int i = 0; i < 100; i++) {
      95             :     Handle<JSReceiver> key = factory->NewJSArray(7);
      96        1000 :     CHECK(key->GetOrCreateIdentityHash(isolate)->IsSmi());
      97         500 :     CHECK_EQ(table->FindEntry(isolate, key), HashMap::kNotFound);
      98        1000 :     CHECK_EQ(table->Lookup(key), roots.the_hole_value());
      99        1000 :     CHECK(key->GetIdentityHash()->IsSmi());
     100             :   }
     101             : 
     102             :   // Keys that don't have an identity hash should not be found and also
     103             :   // should not get an identity hash code generated.
     104        1005 :   for (int i = 0; i < 100; i++) {
     105             :     Handle<JSReceiver> key = factory->NewJSArray(7);
     106        1000 :     CHECK_EQ(table->Lookup(key), roots.the_hole_value());
     107         500 :     Object identity_hash = key->GetIdentityHash();
     108         500 :     CHECK_EQ(roots.undefined_value(), identity_hash);
     109             :   }
     110           5 : }
     111             : 
     112             : 
     113       26644 : TEST(HashMap) {
     114           5 :   LocalContext context;
     115          10 :   v8::HandleScope scope(context->GetIsolate());
     116             :   Isolate* isolate = CcTest::i_isolate();
     117           5 :   TestHashMap(ObjectHashTable::New(isolate, 23));
     118           5 : }
     119             : 
     120             : template <typename HashSet>
     121           5 : static void TestHashSet(Handle<HashSet> table) {
     122             :   Isolate* isolate = CcTest::i_isolate();
     123             :   Factory* factory = isolate->factory();
     124             : 
     125             :   Handle<JSObject> a = factory->NewJSArray(7);
     126             :   Handle<JSObject> b = factory->NewJSArray(11);
     127           5 :   table = HashSet::Add(isolate, table, a);
     128           5 :   CHECK_EQ(1, table->NumberOfElements());
     129           5 :   CHECK(table->Has(isolate, a));
     130           5 :   CHECK(!table->Has(isolate, b));
     131             : 
     132             :   // Keys still have to be valid after objects were moved.
     133           5 :   CcTest::CollectGarbage(NEW_SPACE);
     134           5 :   CHECK_EQ(1, table->NumberOfElements());
     135           5 :   CHECK(table->Has(isolate, a));
     136           5 :   CHECK(!table->Has(isolate, b));
     137             : 
     138             :   // Keys that are overwritten should not change number of elements.
     139           5 :   table = HashSet::Add(isolate, table, a);
     140           5 :   CHECK_EQ(1, table->NumberOfElements());
     141           5 :   CHECK(table->Has(isolate, a));
     142           5 :   CHECK(!table->Has(isolate, b));
     143             : 
     144             :   // Keys that have been removed are mapped to the hole.
     145             :   // TODO(cbruni): not implemented yet.
     146             :   // bool was_present = false;
     147             :   // table = HashSet::Remove(table, a, &was_present);
     148             :   // CHECK(was_present);
     149             :   // CHECK_EQ(0, table->NumberOfElements());
     150             :   // CHECK(!table->Has(a));
     151             :   // CHECK(!table->Has(b));
     152             : 
     153             :   // Keys should map back to their respective values and also should get
     154             :   // an identity hash code generated.
     155        1005 :   for (int i = 0; i < 100; i++) {
     156             :     Handle<JSReceiver> key = factory->NewJSArray(7);
     157         500 :     table = HashSet::Add(isolate, table, key);
     158        1000 :     CHECK_EQ(table->NumberOfElements(), i + 2);
     159         500 :     CHECK(table->Has(isolate, key));
     160        1000 :     CHECK(key->GetIdentityHash()->IsSmi());
     161             :   }
     162             : 
     163             :   // Keys never added to the map which already have an identity hash
     164             :   // code should not be found.
     165        1005 :   for (int i = 0; i < 100; i++) {
     166             :     Handle<JSReceiver> key = factory->NewJSArray(7);
     167        1000 :     CHECK(key->GetOrCreateIdentityHash(isolate)->IsSmi());
     168         500 :     CHECK(!table->Has(isolate, key));
     169        1000 :     CHECK(key->GetIdentityHash()->IsSmi());
     170             :   }
     171             : 
     172             :   // Keys that don't have an identity hash should not be found and also
     173             :   // should not get an identity hash code generated.
     174        1005 :   for (int i = 0; i < 100; i++) {
     175             :     Handle<JSReceiver> key = factory->NewJSArray(7);
     176         500 :     CHECK(!table->Has(isolate, key));
     177         500 :     Object identity_hash = key->GetIdentityHash();
     178        1000 :     CHECK_EQ(ReadOnlyRoots(CcTest::heap()).undefined_value(), identity_hash);
     179             :   }
     180           5 : }
     181             : 
     182       26644 : TEST(HashSet) {
     183           5 :   LocalContext context;
     184          10 :   v8::HandleScope scope(context->GetIsolate());
     185             :   Isolate* isolate = CcTest::i_isolate();
     186           5 :   TestHashSet(ObjectHashSet::New(isolate, 23));
     187           5 : }
     188             : 
     189             : class ObjectHashTableTest: public ObjectHashTable {
     190             :  public:
     191          10 :   explicit ObjectHashTableTest(ObjectHashTable o) : ObjectHashTable(o) {}
     192             :   ObjectHashTableTest* operator->() { return this; }
     193             : 
     194        1915 :   void insert(int entry, int key, int value) {
     195             :     set(EntryToIndex(entry), Smi::FromInt(key));
     196             :     set(EntryToIndex(entry) + 1, Smi::FromInt(value));
     197        1915 :   }
     198             : 
     199        1915 :   int lookup(int key) {
     200             :     Handle<Object> key_obj(Smi::FromInt(key), CcTest::i_isolate());
     201        3830 :     return Smi::ToInt(Lookup(key_obj));
     202             :   }
     203             : 
     204             :   int capacity() {
     205             :     return Capacity();
     206             :   }
     207             : };
     208             : 
     209             : 
     210       26644 : TEST(HashTableRehash) {
     211           5 :   LocalContext context;
     212             :   Isolate* isolate = CcTest::i_isolate();
     213          10 :   v8::HandleScope scope(context->GetIsolate());
     214             :   // Test almost filled table.
     215             :   {
     216           5 :     Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
     217             :     ObjectHashTableTest t(*table);
     218             :     int capacity = t->capacity();
     219        2555 :     for (int i = 0; i < capacity - 1; i++) {
     220        1275 :       t->insert(i, i * i, i);
     221             :     }
     222           5 :     t->Rehash(ReadOnlyRoots(isolate));
     223        2555 :     for (int i = 0; i < capacity - 1; i++) {
     224        1275 :       CHECK_EQ(i, t->lookup(i * i));
     225             :     }
     226             :   }
     227             :   // Test half-filled table.
     228             :   {
     229           5 :     Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
     230             :     ObjectHashTableTest t(*table);
     231             :     int capacity = t->capacity();
     232        1285 :     for (int i = 0; i < capacity / 2; i++) {
     233         640 :       t->insert(i, i * i, i);
     234             :     }
     235           5 :     t->Rehash(ReadOnlyRoots(isolate));
     236        1285 :     for (int i = 0; i < capacity / 2; i++) {
     237         640 :       CHECK_EQ(i, t->lookup(i * i));
     238             :     }
     239             :   }
     240           5 : }
     241             : 
     242             : 
     243             : #ifdef DEBUG
     244             : template<class HashSet>
     245             : static void TestHashSetCausesGC(Handle<HashSet> table) {
     246             :   Isolate* isolate = CcTest::i_isolate();
     247             :   Factory* factory = isolate->factory();
     248             : 
     249             :   Handle<JSObject> key = factory->NewJSArray(0);
     250             : 
     251             :   // Simulate a full heap so that generating an identity hash code
     252             :   // in subsequent calls will request GC.
     253             :   heap::SimulateFullSpace(CcTest::heap()->new_space());
     254             :   heap::SimulateFullSpace(CcTest::heap()->old_space());
     255             : 
     256             :   // Calling Contains() should not cause GC ever.
     257             :   int gc_count = isolate->heap()->gc_count();
     258             :   CHECK(!table->Contains(key));
     259             :   CHECK(gc_count == isolate->heap()->gc_count());
     260             : 
     261             :   // Calling Remove() will not cause GC in this case.
     262             :   bool was_present = false;
     263             :   table = HashSet::Remove(table, key, &was_present);
     264             :   CHECK(!was_present);
     265             :   CHECK(gc_count == isolate->heap()->gc_count());
     266             : 
     267             :   // Calling Add() should cause GC.
     268             :   table = HashSet::Add(table, key);
     269             :   CHECK(gc_count < isolate->heap()->gc_count());
     270             : }
     271             : #endif
     272             : 
     273             : 
     274             : #ifdef DEBUG
     275             : template <class HashMap>
     276             : static void TestHashMapDoesNotCauseGC(Handle<HashMap> table) {
     277             :   Isolate* isolate = CcTest::i_isolate();
     278             :   Factory* factory = isolate->factory();
     279             : 
     280             :   Handle<JSObject> key = factory->NewJSArray(0);
     281             : 
     282             :   // Even though we simulate a full heap, generating an identity hash
     283             :   // code in subsequent calls will not request GC.
     284             :   heap::SimulateFullSpace(CcTest::heap()->new_space());
     285             :   heap::SimulateFullSpace(CcTest::heap()->old_space());
     286             : 
     287             :   // Calling Lookup() should not cause GC ever.
     288             :   CHECK(table->Lookup(key)->IsTheHole(isolate));
     289             : 
     290             :   // Calling Put() should request GC by returning a failure.
     291             :   int gc_count = isolate->heap()->gc_count();
     292             :   HashMap::Put(table, key, key);
     293             :   CHECK(gc_count == isolate->heap()->gc_count());
     294             : }
     295             : 
     296             : 
     297             : TEST(ObjectHashTableCausesGC) {
     298             :   i::FLAG_stress_compaction = false;
     299             :   LocalContext context;
     300             :   v8::HandleScope scope(context->GetIsolate());
     301             :   Isolate* isolate = CcTest::i_isolate();
     302             :   TestHashMapDoesNotCauseGC(ObjectHashTable::New(isolate, 1));
     303             : }
     304             : #endif
     305             : 
     306       26644 : TEST(MaximumClonedShallowObjectProperties) {
     307             :   // Assert that a NameDictionary with kMaximumClonedShallowObjectProperties is
     308             :   // not in large-object space.
     309             :   const int max_capacity = NameDictionary::ComputeCapacity(
     310             :       ConstructorBuiltins::kMaximumClonedShallowObjectProperties);
     311           5 :   const int max_literal_entry = max_capacity / NameDictionary::kEntrySize;
     312             :   const int max_literal_index = NameDictionary::EntryToIndex(max_literal_entry);
     313           5 :   CHECK_LE(NameDictionary::OffsetOfElementAt(max_literal_index),
     314             :            kMaxRegularHeapObjectSize);
     315           5 : }
     316             : 
     317             : }  // namespace
     318             : 
     319             : }  // namespace internal
     320       79917 : }  // namespace v8

Generated by: LCOV version 1.10