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 |
39 TEST(Set) { | 50 TEST(Set) { |
| 51 i::FLAG_harmony_collections = true; |
| 52 |
40 LocalContext context; | 53 LocalContext context; |
41 Isolate* isolate = CcTest::i_isolate(); | 54 Isolate* isolate = CcTest::i_isolate(); |
42 Factory* factory = isolate->factory(); | 55 Factory* factory = isolate->factory(); |
43 HandleScope scope(isolate); | 56 HandleScope scope(isolate); |
44 Handle<OrderedHashSet> ordered_set = factory->NewOrderedHashSet(); | 57 Handle<OrderedHashSet> ordered_set = factory->NewOrderedHashSet(); |
45 CHECK_EQ(2, ordered_set->NumberOfBuckets()); | 58 CHECK_EQ(2, ordered_set->NumberOfBuckets()); |
46 CHECK_EQ(0, ordered_set->NumberOfElements()); | 59 CHECK_EQ(0, ordered_set->NumberOfElements()); |
47 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); | 60 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); |
48 | 61 |
| 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 |
49 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 67 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
50 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); | 68 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); |
51 CHECK(!ordered_set->Contains(*obj)); | 69 CHECK(!ordered_set->Contains(*obj)); |
52 ordered_set = OrderedHashSet::Add(ordered_set, obj); | 70 ordered_set = OrderedHashSet::Add(ordered_set, obj); |
53 CHECK_EQ(1, ordered_set->NumberOfElements()); | 71 CHECK_EQ(1, ordered_set->NumberOfElements()); |
54 CHECK(ordered_set->Contains(*obj)); | 72 CHECK(ordered_set->Contains(*obj)); |
55 ordered_set = OrderedHashSet::Remove(ordered_set, obj); | 73 ordered_set = OrderedHashSet::Remove(ordered_set, obj); |
56 CHECK_EQ(0, ordered_set->NumberOfElements()); | 74 CHECK_EQ(0, ordered_set->NumberOfElements()); |
57 CHECK(!ordered_set->Contains(*obj)); | 75 CHECK(!ordered_set->Contains(*obj)); |
58 | 76 |
59 // Test for collisions/chaining | 77 // Test for collisions/chaining |
60 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); | 78 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); |
61 ordered_set = OrderedHashSet::Add(ordered_set, obj1); | 79 ordered_set = OrderedHashSet::Add(ordered_set, obj1); |
62 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); | 80 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); |
63 ordered_set = OrderedHashSet::Add(ordered_set, obj2); | 81 ordered_set = OrderedHashSet::Add(ordered_set, obj2); |
64 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); | 82 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); |
65 ordered_set = OrderedHashSet::Add(ordered_set, obj3); | 83 ordered_set = OrderedHashSet::Add(ordered_set, obj3); |
66 CHECK_EQ(3, ordered_set->NumberOfElements()); | 84 CHECK_EQ(3, ordered_set->NumberOfElements()); |
67 CHECK(ordered_set->Contains(*obj1)); | 85 CHECK(ordered_set->Contains(*obj1)); |
68 CHECK(ordered_set->Contains(*obj2)); | 86 CHECK(ordered_set->Contains(*obj2)); |
69 CHECK(ordered_set->Contains(*obj3)); | 87 CHECK(ordered_set->Contains(*obj3)); |
70 | 88 |
| 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 |
71 // Test growth | 101 // Test growth |
72 ordered_set = OrderedHashSet::Add(ordered_set, obj); | 102 ordered_set = OrderedHashSet::Add(ordered_set, obj); |
73 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); | 103 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); |
74 ordered_set = OrderedHashSet::Add(ordered_set, obj4); | 104 ordered_set = OrderedHashSet::Add(ordered_set, obj4); |
75 CHECK(ordered_set->Contains(*obj)); | 105 CHECK(ordered_set->Contains(*obj)); |
76 CHECK(ordered_set->Contains(*obj1)); | 106 CHECK(ordered_set->Contains(*obj1)); |
77 CHECK(ordered_set->Contains(*obj2)); | 107 CHECK(ordered_set->Contains(*obj2)); |
78 CHECK(ordered_set->Contains(*obj3)); | 108 CHECK(ordered_set->Contains(*obj3)); |
79 CHECK(ordered_set->Contains(*obj4)); | 109 CHECK(ordered_set->Contains(*obj4)); |
80 CHECK_EQ(5, ordered_set->NumberOfElements()); | 110 CHECK_EQ(5, ordered_set->NumberOfElements()); |
81 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); | 111 CHECK_EQ(0, ordered_set->NumberOfDeletedElements()); |
82 CHECK_EQ(4, ordered_set->NumberOfBuckets()); | 112 CHECK_EQ(4, ordered_set->NumberOfBuckets()); |
83 | 113 |
| 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 |
84 // Test shrinking | 130 // Test shrinking |
85 ordered_set = OrderedHashSet::Remove(ordered_set, obj); | 131 ordered_set = OrderedHashSet::Remove(ordered_set, obj); |
86 ordered_set = OrderedHashSet::Remove(ordered_set, obj1); | 132 ordered_set = OrderedHashSet::Remove(ordered_set, obj1); |
87 ordered_set = OrderedHashSet::Remove(ordered_set, obj2); | 133 ordered_set = OrderedHashSet::Remove(ordered_set, obj2); |
88 ordered_set = OrderedHashSet::Remove(ordered_set, obj3); | 134 ordered_set = OrderedHashSet::Remove(ordered_set, obj3); |
89 CHECK_EQ(1, ordered_set->NumberOfElements()); | 135 CHECK_EQ(1, ordered_set->NumberOfElements()); |
90 CHECK_EQ(2, ordered_set->NumberOfBuckets()); | 136 CHECK_EQ(2, ordered_set->NumberOfBuckets()); |
91 } | 137 } |
92 | 138 |
93 | 139 |
94 TEST(Map) { | 140 TEST(Map) { |
| 141 i::FLAG_harmony_collections = true; |
| 142 |
95 LocalContext context; | 143 LocalContext context; |
96 Isolate* isolate = CcTest::i_isolate(); | 144 Isolate* isolate = CcTest::i_isolate(); |
97 Factory* factory = isolate->factory(); | 145 Factory* factory = isolate->factory(); |
98 HandleScope scope(isolate); | 146 HandleScope scope(isolate); |
99 Handle<OrderedHashMap> ordered_map = factory->NewOrderedHashMap(); | 147 Handle<OrderedHashMap> ordered_map = factory->NewOrderedHashMap(); |
100 CHECK_EQ(2, ordered_map->NumberOfBuckets()); | 148 CHECK_EQ(2, ordered_map->NumberOfBuckets()); |
101 CHECK_EQ(0, ordered_map->NumberOfElements()); | 149 CHECK_EQ(0, ordered_map->NumberOfElements()); |
102 CHECK_EQ(0, ordered_map->NumberOfDeletedElements()); | 150 CHECK_EQ(0, ordered_map->NumberOfDeletedElements()); |
103 | 151 |
| 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 |
104 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 157 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
105 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); | 158 Handle<JSObject> obj = factory->NewJSObjectFromMap(map); |
106 Handle<JSObject> val = factory->NewJSObjectFromMap(map); | 159 Handle<JSObject> val = factory->NewJSObjectFromMap(map); |
107 CHECK(ordered_map->Lookup(*obj)->IsTheHole()); | 160 CHECK(ordered_map->Lookup(*obj)->IsTheHole()); |
108 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); | 161 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); |
109 CHECK_EQ(1, ordered_map->NumberOfElements()); | 162 CHECK_EQ(1, ordered_map->NumberOfElements()); |
110 CHECK(ordered_map->Lookup(*obj)->SameValue(*val)); | 163 CHECK(ordered_map->Lookup(*obj)->SameValue(*val)); |
111 ordered_map = OrderedHashMap::Put( | 164 ordered_map = OrderedHashMap::Put( |
112 ordered_map, obj, factory->the_hole_value()); | 165 ordered_map, obj, factory->the_hole_value()); |
113 CHECK_EQ(0, ordered_map->NumberOfElements()); | 166 CHECK_EQ(0, ordered_map->NumberOfElements()); |
114 CHECK(ordered_map->Lookup(*obj)->IsTheHole()); | 167 CHECK(ordered_map->Lookup(*obj)->IsTheHole()); |
115 | 168 |
116 // Test for collisions/chaining | 169 // Test for collisions/chaining |
117 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); | 170 Handle<JSObject> obj1 = factory->NewJSObjectFromMap(map); |
118 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); | 171 Handle<JSObject> obj2 = factory->NewJSObjectFromMap(map); |
119 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); | 172 Handle<JSObject> obj3 = factory->NewJSObjectFromMap(map); |
120 Handle<JSObject> val1 = factory->NewJSObjectFromMap(map); | 173 Handle<JSObject> val1 = factory->NewJSObjectFromMap(map); |
121 Handle<JSObject> val2 = factory->NewJSObjectFromMap(map); | 174 Handle<JSObject> val2 = factory->NewJSObjectFromMap(map); |
122 Handle<JSObject> val3 = factory->NewJSObjectFromMap(map); | 175 Handle<JSObject> val3 = factory->NewJSObjectFromMap(map); |
123 ordered_map = OrderedHashMap::Put(ordered_map, obj1, val1); | 176 ordered_map = OrderedHashMap::Put(ordered_map, obj1, val1); |
124 ordered_map = OrderedHashMap::Put(ordered_map, obj2, val2); | 177 ordered_map = OrderedHashMap::Put(ordered_map, obj2, val2); |
125 ordered_map = OrderedHashMap::Put(ordered_map, obj3, val3); | 178 ordered_map = OrderedHashMap::Put(ordered_map, obj3, val3); |
126 CHECK_EQ(3, ordered_map->NumberOfElements()); | 179 CHECK_EQ(3, ordered_map->NumberOfElements()); |
127 CHECK(ordered_map->Lookup(*obj1)->SameValue(*val1)); | 180 CHECK(ordered_map->Lookup(*obj1)->SameValue(*val1)); |
128 CHECK(ordered_map->Lookup(*obj2)->SameValue(*val2)); | 181 CHECK(ordered_map->Lookup(*obj2)->SameValue(*val2)); |
129 CHECK(ordered_map->Lookup(*obj3)->SameValue(*val3)); | 182 CHECK(ordered_map->Lookup(*obj3)->SameValue(*val3)); |
130 | 183 |
| 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 |
131 // Test growth | 196 // Test growth |
132 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); | 197 ordered_map = OrderedHashMap::Put(ordered_map, obj, val); |
133 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); | 198 Handle<JSObject> obj4 = factory->NewJSObjectFromMap(map); |
134 Handle<JSObject> val4 = factory->NewJSObjectFromMap(map); | 199 Handle<JSObject> val4 = factory->NewJSObjectFromMap(map); |
135 ordered_map = OrderedHashMap::Put(ordered_map, obj4, val4); | 200 ordered_map = OrderedHashMap::Put(ordered_map, obj4, val4); |
136 CHECK(ordered_map->Lookup(*obj)->SameValue(*val)); | 201 CHECK(ordered_map->Lookup(*obj)->SameValue(*val)); |
137 CHECK(ordered_map->Lookup(*obj1)->SameValue(*val1)); | 202 CHECK(ordered_map->Lookup(*obj1)->SameValue(*val1)); |
138 CHECK(ordered_map->Lookup(*obj2)->SameValue(*val2)); | 203 CHECK(ordered_map->Lookup(*obj2)->SameValue(*val2)); |
139 CHECK(ordered_map->Lookup(*obj3)->SameValue(*val3)); | 204 CHECK(ordered_map->Lookup(*obj3)->SameValue(*val3)); |
140 CHECK(ordered_map->Lookup(*obj4)->SameValue(*val4)); | 205 CHECK(ordered_map->Lookup(*obj4)->SameValue(*val4)); |
141 CHECK_EQ(5, ordered_map->NumberOfElements()); | 206 CHECK_EQ(5, ordered_map->NumberOfElements()); |
142 CHECK_EQ(4, ordered_map->NumberOfBuckets()); | 207 CHECK_EQ(4, ordered_map->NumberOfBuckets()); |
143 | 208 |
| 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 |
144 // Test shrinking | 225 // Test shrinking |
145 ordered_map = OrderedHashMap::Put( | 226 ordered_map = OrderedHashMap::Put( |
146 ordered_map, obj, factory->the_hole_value()); | 227 ordered_map, obj, factory->the_hole_value()); |
147 ordered_map = OrderedHashMap::Put( | 228 ordered_map = OrderedHashMap::Put( |
148 ordered_map, obj1, factory->the_hole_value()); | 229 ordered_map, obj1, factory->the_hole_value()); |
149 ordered_map = OrderedHashMap::Put( | 230 ordered_map = OrderedHashMap::Put( |
150 ordered_map, obj2, factory->the_hole_value()); | 231 ordered_map, obj2, factory->the_hole_value()); |
151 ordered_map = OrderedHashMap::Put( | 232 ordered_map = OrderedHashMap::Put( |
152 ordered_map, obj3, factory->the_hole_value()); | 233 ordered_map, obj3, factory->the_hole_value()); |
153 CHECK_EQ(1, ordered_map->NumberOfElements()); | 234 CHECK_EQ(1, ordered_map->NumberOfElements()); |
154 CHECK_EQ(2, ordered_map->NumberOfBuckets()); | 235 CHECK_EQ(2, ordered_map->NumberOfBuckets()); |
155 } | 236 } |
156 | 237 |
157 | 238 |
158 } | 239 } |
OLD | NEW |