OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 18 matching lines...) Expand all Loading... |
29 | 29 |
30 #include "v8.h" | 30 #include "v8.h" |
31 | 31 |
32 #include "cctest.h" | 32 #include "cctest.h" |
33 #include "factory.h" | 33 #include "factory.h" |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 using namespace v8::internal; | 37 using namespace v8::internal; |
38 | 38 |
39 | |
40 void CheckIterResultObject(Factory* factory, | |
41 Handle<JSObject> result, | |
42 Handle<Object> value, | |
43 bool done) { | |
44 CHECK(GetProperty(result, "value").ToHandleChecked()->SameValue(*value)); | |
45 CHECK(GetProperty(result, "done").ToHandleChecked()->IsBoolean()); | |
46 CHECK_EQ(GetProperty(result, "done").ToHandleChecked()->BooleanValue(), done); | |
47 } | |
48 | |
49 | |
50 TEST(Set) { | 39 TEST(Set) { |
51 i::FLAG_harmony_collections = true; | |
52 | |
53 LocalContext context; | 40 LocalContext context; |
54 Isolate* isolate = CcTest::i_isolate(); | 41 Isolate* isolate = CcTest::i_isolate(); |
55 Factory* factory = isolate->factory(); | 42 Factory* factory = isolate->factory(); |
56 HandleScope scope(isolate); | 43 HandleScope scope(isolate); |
57 Handle<OrderedHashSet> ordered_set = factory->NewOrderedHashSet(); | 44 Handle<OrderedHashSet> ordered_set = factory->NewOrderedHashSet(); |
58 CHECK_EQ(2, ordered_set->NumberOfBuckets()); | 45 CHECK_EQ(2, ordered_set->NumberOfBuckets()); |
59 CHECK_EQ(0, ordered_set->NumberOfElements()); | 46 CHECK_EQ(0, ordered_set->NumberOfElements()); |
60 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); | 47 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); |
61 | 48 |
62 Handle<JSSetIterator> value_iterator = | |
63 JSSetIterator::Create(ordered_set, JSSetIterator::kKindValues); | |
64 Handle<JSSetIterator> value_iterator_2 = | |
65 JSSetIterator::Create(ordered_set, JSSetIterator::kKindValues); | |
66 | |
67 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 49 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
68 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); | 50 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); |
69 CHECK(!ordered_set->Contains(*obj)); | 51 CHECK(!ordered_set->Contains(*obj)); |
70 ordered_set = OrderedHashSet::Add(ordered_set, obj); | 52 ordered_set = OrderedHashSet::Add(ordered_set, obj); |
71 CHECK_EQ(1, ordered_set->NumberOfElements()); | 53 CHECK_EQ(1, ordered_set->NumberOfElements()); |
72 CHECK(ordered_set->Contains(*obj)); | 54 CHECK(ordered_set->Contains(*obj)); |
73 ordered_set = OrderedHashSet::Remove(ordered_set, obj); | 55 ordered_set = OrderedHashSet::Remove(ordered_set, obj); |
74 CHECK_EQ(0, ordered_set->NumberOfElements()); | 56 CHECK_EQ(0, ordered_set->NumberOfElements()); |
75 CHECK(!ordered_set->Contains(*obj)); | 57 CHECK(!ordered_set->Contains(*obj)); |
76 | 58 |
77 // Test for collisions/chaining | 59 // Test for collisions/chaining |
78 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); | 60 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); |
79 ordered_set = OrderedHashSet::Add(ordered_set, obj1); | 61 ordered_set = OrderedHashSet::Add(ordered_set, obj1); |
80 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); | 62 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); |
81 ordered_set = OrderedHashSet::Add(ordered_set, obj2); | 63 ordered_set = OrderedHashSet::Add(ordered_set, obj2); |
82 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); | 64 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); |
83 ordered_set = OrderedHashSet::Add(ordered_set, obj3); | 65 ordered_set = OrderedHashSet::Add(ordered_set, obj3); |
84 CHECK_EQ(3, ordered_set->NumberOfElements()); | 66 CHECK_EQ(3, ordered_set->NumberOfElements()); |
85 CHECK(ordered_set->Contains(*obj1)); | 67 CHECK(ordered_set->Contains(*obj1)); |
86 CHECK(ordered_set->Contains(*obj2)); | 68 CHECK(ordered_set->Contains(*obj2)); |
87 CHECK(ordered_set->Contains(*obj3)); | 69 CHECK(ordered_set->Contains(*obj3)); |
88 | 70 |
89 // Test iteration | |
90 CheckIterResultObject( | |
91 factory, JSSetIterator::Next(value_iterator), obj1, false); | |
92 CheckIterResultObject( | |
93 factory, JSSetIterator::Next(value_iterator), obj2, false); | |
94 CheckIterResultObject( | |
95 factory, JSSetIterator::Next(value_iterator), obj3, false); | |
96 CheckIterResultObject(factory, | |
97 JSSetIterator::Next(value_iterator), | |
98 factory->undefined_value(), | |
99 true); | |
100 | |
101 // Test growth | 71 // Test growth |
102 ordered_set = OrderedHashSet::Add(ordered_set, obj); | 72 ordered_set = OrderedHashSet::Add(ordered_set, obj); |
103 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); | 73 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); |
104 ordered_set = OrderedHashSet::Add(ordered_set, obj4); | 74 ordered_set = OrderedHashSet::Add(ordered_set, obj4); |
105 CHECK(ordered_set->Contains(*obj)); | 75 CHECK(ordered_set->Contains(*obj)); |
106 CHECK(ordered_set->Contains(*obj1)); | 76 CHECK(ordered_set->Contains(*obj1)); |
107 CHECK(ordered_set->Contains(*obj2)); | 77 CHECK(ordered_set->Contains(*obj2)); |
108 CHECK(ordered_set->Contains(*obj3)); | 78 CHECK(ordered_set->Contains(*obj3)); |
109 CHECK(ordered_set->Contains(*obj4)); | 79 CHECK(ordered_set->Contains(*obj4)); |
110 CHECK_EQ(5, ordered_set->NumberOfElements()); | 80 CHECK_EQ(5, ordered_set->NumberOfElements()); |
111 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); | 81 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); |
112 CHECK_EQ(4, ordered_set->NumberOfBuckets()); | 82 CHECK_EQ(4, ordered_set->NumberOfBuckets()); |
113 | 83 |
114 // Test iteration after growth | |
115 CheckIterResultObject( | |
116 factory, JSSetIterator::Next(value_iterator_2), obj1, false); | |
117 CheckIterResultObject( | |
118 factory, JSSetIterator::Next(value_iterator_2), obj2, false); | |
119 CheckIterResultObject( | |
120 factory, JSSetIterator::Next(value_iterator_2), obj3, false); | |
121 CheckIterResultObject( | |
122 factory, JSSetIterator::Next(value_iterator_2), obj, false); | |
123 CheckIterResultObject( | |
124 factory, JSSetIterator::Next(value_iterator_2), obj4, false); | |
125 CheckIterResultObject(factory, | |
126 JSSetIterator::Next(value_iterator_2), | |
127 factory->undefined_value(), | |
128 true); | |
129 | |
130 // Test shrinking | 84 // Test shrinking |
131 ordered_set = OrderedHashSet::Remove(ordered_set, obj); | 85 ordered_set = OrderedHashSet::Remove(ordered_set, obj); |
132 ordered_set = OrderedHashSet::Remove(ordered_set, obj1); | 86 ordered_set = OrderedHashSet::Remove(ordered_set, obj1); |
133 ordered_set = OrderedHashSet::Remove(ordered_set, obj2); | 87 ordered_set = OrderedHashSet::Remove(ordered_set, obj2); |
134 ordered_set = OrderedHashSet::Remove(ordered_set, obj3); | 88 ordered_set = OrderedHashSet::Remove(ordered_set, obj3); |
135 CHECK_EQ(1, ordered_set->NumberOfElements()); | 89 CHECK_EQ(1, ordered_set->NumberOfElements()); |
136 CHECK_EQ(2, ordered_set->NumberOfBuckets()); | 90 CHECK_EQ(2, ordered_set->NumberOfBuckets()); |
137 } | 91 } |
138 | 92 |
139 | 93 |
140 TEST(Map) { | 94 TEST(Map) { |
141 i::FLAG_harmony_collections = true; | |
142 | |
143 LocalContext context; | 95 LocalContext context; |
144 Isolate* isolate = CcTest::i_isolate(); | 96 Isolate* isolate = CcTest::i_isolate(); |
145 Factory* factory = isolate->factory(); | 97 Factory* factory = isolate->factory(); |
146 HandleScope scope(isolate); | 98 HandleScope scope(isolate); |
147 Handle<OrderedHashMap> ordered_map = factory->NewOrderedHashMap(); | 99 Handle<OrderedHashMap> ordered_map = factory->NewOrderedHashMap(); |
148 CHECK_EQ(2, ordered_map->NumberOfBuckets()); | 100 CHECK_EQ(2, ordered_map->NumberOfBuckets()); |
149 CHECK_EQ(0, ordered_map->NumberOfElements()); | 101 CHECK_EQ(0, ordered_map->NumberOfElements()); |
150 CHECK_EQ(0, ordered_map->NumberOfDeletedElements()); | 102 CHECK_EQ(0, ordered_map->NumberOfDeletedElements()); |
151 | 103 |
152 Handle<JSMapIterator> value_iterator = | |
153 JSMapIterator::Create(ordered_map, JSMapIterator::kKindValues); | |
154 Handle<JSMapIterator> key_iterator = | |
155 JSMapIterator::Create(ordered_map, JSMapIterator::kKindKeys); | |
156 | |
157 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 104 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
158 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); | 105 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); |
159 Handle<JSObject> val = factory->NewJSObjectFromMap(map); | 106 Handle<JSObject> val = factory->NewJSObjectFromMap(map); |
160 CHECK(ordered_map->Lookup(*obj)->IsTheHole()); | 107 CHECK(ordered_map->Lookup(*obj)->IsTheHole()); |
161 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); | 108 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); |
162 CHECK_EQ(1, ordered_map->NumberOfElements()); | 109 CHECK_EQ(1, ordered_map->NumberOfElements()); |
163 CHECK(ordered_map->Lookup(*obj)->SameValue(*val)); | 110 CHECK(ordered_map->Lookup(*obj)->SameValue(*val)); |
164 ordered_map = OrderedHashMap::Put( | 111 ordered_map = OrderedHashMap::Put( |
165 ordered_map, obj, factory->the_hole_value()); | 112 ordered_map, obj, factory->the_hole_value()); |
166 CHECK_EQ(0, ordered_map->NumberOfElements()); | 113 CHECK_EQ(0, ordered_map->NumberOfElements()); |
167 CHECK(ordered_map->Lookup(*obj)->IsTheHole()); | 114 CHECK(ordered_map->Lookup(*obj)->IsTheHole()); |
168 | 115 |
169 // Test for collisions/chaining | 116 // Test for collisions/chaining |
170 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); | 117 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); |
171 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); | 118 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); |
172 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); | 119 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); |
173 Handle<JSObject> val1 = factory->NewJSObjectFromMap(map); | 120 Handle<JSObject> val1 = factory->NewJSObjectFromMap(map); |
174 Handle<JSObject> val2 = factory->NewJSObjectFromMap(map); | 121 Handle<JSObject> val2 = factory->NewJSObjectFromMap(map); |
175 Handle<JSObject> val3 = factory->NewJSObjectFromMap(map); | 122 Handle<JSObject> val3 = factory->NewJSObjectFromMap(map); |
176 ordered_map = OrderedHashMap::Put(ordered_map, obj1, val1); | 123 ordered_map = OrderedHashMap::Put(ordered_map, obj1, val1); |
177 ordered_map = OrderedHashMap::Put(ordered_map, obj2, val2); | 124 ordered_map = OrderedHashMap::Put(ordered_map, obj2, val2); |
178 ordered_map = OrderedHashMap::Put(ordered_map, obj3, val3); | 125 ordered_map = OrderedHashMap::Put(ordered_map, obj3, val3); |
179 CHECK_EQ(3, ordered_map->NumberOfElements()); | 126 CHECK_EQ(3, ordered_map->NumberOfElements()); |
180 CHECK(ordered_map->Lookup(*obj1)->SameValue(*val1)); | 127 CHECK(ordered_map->Lookup(*obj1)->SameValue(*val1)); |
181 CHECK(ordered_map->Lookup(*obj2)->SameValue(*val2)); | 128 CHECK(ordered_map->Lookup(*obj2)->SameValue(*val2)); |
182 CHECK(ordered_map->Lookup(*obj3)->SameValue(*val3)); | 129 CHECK(ordered_map->Lookup(*obj3)->SameValue(*val3)); |
183 | 130 |
184 // Test iteration | |
185 CheckIterResultObject( | |
186 factory, JSMapIterator::Next(value_iterator), val1, false); | |
187 CheckIterResultObject( | |
188 factory, JSMapIterator::Next(value_iterator), val2, false); | |
189 CheckIterResultObject( | |
190 factory, JSMapIterator::Next(value_iterator), val3, false); | |
191 CheckIterResultObject(factory, | |
192 JSMapIterator::Next(value_iterator), | |
193 factory->undefined_value(), | |
194 true); | |
195 | |
196 // Test growth | 131 // Test growth |
197 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); | 132 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); |
198 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); | 133 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); |
199 Handle<JSObject> val4 = factory->NewJSObjectFromMap(map); | 134 Handle<JSObject> val4 = factory->NewJSObjectFromMap(map); |
200 ordered_map = OrderedHashMap::Put(ordered_map, obj4, val4); | 135 ordered_map = OrderedHashMap::Put(ordered_map, obj4, val4); |
201 CHECK(ordered_map->Lookup(*obj)->SameValue(*val)); | 136 CHECK(ordered_map->Lookup(*obj)->SameValue(*val)); |
202 CHECK(ordered_map->Lookup(*obj1)->SameValue(*val1)); | 137 CHECK(ordered_map->Lookup(*obj1)->SameValue(*val1)); |
203 CHECK(ordered_map->Lookup(*obj2)->SameValue(*val2)); | 138 CHECK(ordered_map->Lookup(*obj2)->SameValue(*val2)); |
204 CHECK(ordered_map->Lookup(*obj3)->SameValue(*val3)); | 139 CHECK(ordered_map->Lookup(*obj3)->SameValue(*val3)); |
205 CHECK(ordered_map->Lookup(*obj4)->SameValue(*val4)); | 140 CHECK(ordered_map->Lookup(*obj4)->SameValue(*val4)); |
206 CHECK_EQ(5, ordered_map->NumberOfElements()); | 141 CHECK_EQ(5, ordered_map->NumberOfElements()); |
207 CHECK_EQ(4, ordered_map->NumberOfBuckets()); | 142 CHECK_EQ(4, ordered_map->NumberOfBuckets()); |
208 | 143 |
209 // Test iteration after growth | |
210 CheckIterResultObject( | |
211 factory, JSMapIterator::Next(key_iterator), obj1, false); | |
212 CheckIterResultObject( | |
213 factory, JSMapIterator::Next(key_iterator), obj2, false); | |
214 CheckIterResultObject( | |
215 factory, JSMapIterator::Next(key_iterator), obj3, false); | |
216 CheckIterResultObject( | |
217 factory, JSMapIterator::Next(key_iterator), obj, false); | |
218 CheckIterResultObject( | |
219 factory, JSMapIterator::Next(key_iterator), obj4, false); | |
220 CheckIterResultObject(factory, | |
221 JSMapIterator::Next(key_iterator), | |
222 factory->undefined_value(), | |
223 true); | |
224 | |
225 // Test shrinking | 144 // Test shrinking |
226 ordered_map = OrderedHashMap::Put( | 145 ordered_map = OrderedHashMap::Put( |
227 ordered_map, obj, factory->the_hole_value()); | 146 ordered_map, obj, factory->the_hole_value()); |
228 ordered_map = OrderedHashMap::Put( | 147 ordered_map = OrderedHashMap::Put( |
229 ordered_map, obj1, factory->the_hole_value()); | 148 ordered_map, obj1, factory->the_hole_value()); |
230 ordered_map = OrderedHashMap::Put( | 149 ordered_map = OrderedHashMap::Put( |
231 ordered_map, obj2, factory->the_hole_value()); | 150 ordered_map, obj2, factory->the_hole_value()); |
232 ordered_map = OrderedHashMap::Put( | 151 ordered_map = OrderedHashMap::Put( |
233 ordered_map, obj3, factory->the_hole_value()); | 152 ordered_map, obj3, factory->the_hole_value()); |
234 CHECK_EQ(1, ordered_map->NumberOfElements()); | 153 CHECK_EQ(1, ordered_map->NumberOfElements()); |
235 CHECK_EQ(2, ordered_map->NumberOfBuckets()); | 154 CHECK_EQ(2, ordered_map->NumberOfBuckets()); |
236 } | 155 } |
237 | 156 |
238 | 157 |
239 } | 158 } |
OLD | NEW |