1 // Copyright 2014 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 <stdlib.h> 29 30 #include "src/v8.h" 31 32 #include "src/factory.h" 33 #include "test/cctest/cctest.h" 34 35 namespace { 36 37 using namespace v8::internal; 38 39 40 TEST(Set) { 41 i::FLAG_harmony_collections = true; 42 43 LocalContext context; 44 Isolate* isolate = CcTest::i_isolate(); 45 Factory* factory = isolate->factory(); 46 HandleScope scope(isolate); 47 Handle<OrderedHashSet> ordered_set = factory->NewOrderedHashSet(); 48 CHECK_EQ(2, ordered_set->NumberOfBuckets()); 49 CHECK_EQ(0, ordered_set->NumberOfElements()); 50 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); 51 52 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); 53 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); 54 CHECK(!ordered_set->Contains(obj)); 55 ordered_set = OrderedHashSet::Add(ordered_set, obj); 56 CHECK_EQ(1, ordered_set->NumberOfElements()); 57 CHECK(ordered_set->Contains(obj)); 58 bool was_present = false; 59 ordered_set = OrderedHashSet::Remove(ordered_set, obj, &was_present); 60 CHECK(was_present); 61 CHECK_EQ(0, ordered_set->NumberOfElements()); 62 CHECK(!ordered_set->Contains(obj)); 63 64 // Removing a not-present object should set was_present to false. 65 ordered_set = OrderedHashSet::Remove(ordered_set, obj, &was_present); 66 CHECK(!was_present); 67 68 // Test for collisions/chaining 69 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); 70 ordered_set = OrderedHashSet::Add(ordered_set, obj1); 71 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); 72 ordered_set = OrderedHashSet::Add(ordered_set, obj2); 73 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); 74 ordered_set = OrderedHashSet::Add(ordered_set, obj3); 75 CHECK_EQ(3, ordered_set->NumberOfElements()); 76 CHECK(ordered_set->Contains(obj1)); 77 CHECK(ordered_set->Contains(obj2)); 78 CHECK(ordered_set->Contains(obj3)); 79 80 // Test growth 81 ordered_set = OrderedHashSet::Add(ordered_set, obj); 82 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); 83 ordered_set = OrderedHashSet::Add(ordered_set, obj4); 84 CHECK(ordered_set->Contains(obj)); 85 CHECK(ordered_set->Contains(obj1)); 86 CHECK(ordered_set->Contains(obj2)); 87 CHECK(ordered_set->Contains(obj3)); 88 CHECK(ordered_set->Contains(obj4)); 89 CHECK_EQ(5, ordered_set->NumberOfElements()); 90 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); 91 CHECK_EQ(4, ordered_set->NumberOfBuckets()); 92 93 // Test shrinking 94 ordered_set = OrderedHashSet::Remove(ordered_set, obj, &was_present); 95 CHECK(was_present); 96 ordered_set = OrderedHashSet::Remove(ordered_set, obj1, &was_present); 97 CHECK(was_present); 98 ordered_set = OrderedHashSet::Remove(ordered_set, obj2, &was_present); 99 CHECK(was_present); 100 ordered_set = OrderedHashSet::Remove(ordered_set, obj3, &was_present); 101 CHECK(was_present); 102 CHECK_EQ(1, ordered_set->NumberOfElements()); 103 CHECK_EQ(2, ordered_set->NumberOfBuckets()); 104 } 105 106 107 TEST(Map) { 108 i::FLAG_harmony_collections = true; 109 110 LocalContext context; 111 Isolate* isolate = CcTest::i_isolate(); 112 Factory* factory = isolate->factory(); 113 HandleScope scope(isolate); 114 Handle<OrderedHashMap> ordered_map = factory->NewOrderedHashMap(); 115 CHECK_EQ(2, ordered_map->NumberOfBuckets()); 116 CHECK_EQ(0, ordered_map->NumberOfElements()); 117 CHECK_EQ(0, ordered_map->NumberOfDeletedElements()); 118 119 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); 120 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); 121 Handle<JSObject> val = factory->NewJSObjectFromMap(map); 122 CHECK(ordered_map->Lookup(obj)->IsTheHole()); 123 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); 124 CHECK_EQ(1, ordered_map->NumberOfElements()); 125 CHECK(ordered_map->Lookup(obj)->SameValue(*val)); 126 bool was_present = false; 127 ordered_map = OrderedHashMap::Remove(ordered_map, obj, &was_present); 128 CHECK(was_present); 129 CHECK_EQ(0, ordered_map->NumberOfElements()); 130 CHECK(ordered_map->Lookup(obj)->IsTheHole()); 131 132 // Test for collisions/chaining 133 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); 134 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); 135 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); 136 Handle<JSObject> val1 = factory->NewJSObjectFromMap(map); 137 Handle<JSObject> val2 = factory->NewJSObjectFromMap(map); 138 Handle<JSObject> val3 = factory->NewJSObjectFromMap(map); 139 ordered_map = OrderedHashMap::Put(ordered_map, obj1, val1); 140 ordered_map = OrderedHashMap::Put(ordered_map, obj2, val2); 141 ordered_map = OrderedHashMap::Put(ordered_map, obj3, val3); 142 CHECK_EQ(3, ordered_map->NumberOfElements()); 143 CHECK(ordered_map->Lookup(obj1)->SameValue(*val1)); 144 CHECK(ordered_map->Lookup(obj2)->SameValue(*val2)); 145 CHECK(ordered_map->Lookup(obj3)->SameValue(*val3)); 146 147 // Test growth 148 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); 149 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); 150 Handle<JSObject> val4 = factory->NewJSObjectFromMap(map); 151 ordered_map = OrderedHashMap::Put(ordered_map, obj4, val4); 152 CHECK(ordered_map->Lookup(obj)->SameValue(*val)); 153 CHECK(ordered_map->Lookup(obj1)->SameValue(*val1)); 154 CHECK(ordered_map->Lookup(obj2)->SameValue(*val2)); 155 CHECK(ordered_map->Lookup(obj3)->SameValue(*val3)); 156 CHECK(ordered_map->Lookup(obj4)->SameValue(*val4)); 157 CHECK_EQ(5, ordered_map->NumberOfElements()); 158 CHECK_EQ(4, ordered_map->NumberOfBuckets()); 159 160 // Test shrinking 161 ordered_map = OrderedHashMap::Remove(ordered_map, obj, &was_present); 162 CHECK(was_present); 163 ordered_map = OrderedHashMap::Remove(ordered_map, obj1, &was_present); 164 CHECK(was_present); 165 ordered_map = OrderedHashMap::Remove(ordered_map, obj2, &was_present); 166 CHECK(was_present); 167 ordered_map = OrderedHashMap::Remove(ordered_map, obj3, &was_present); 168 CHECK(was_present); 169 CHECK_EQ(1, ordered_map->NumberOfElements()); 170 CHECK_EQ(2, ordered_map->NumberOfBuckets()); 171 } 172 173 174 } 175