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