]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - tests/stress/modify-map-during-iteration.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / modify-map-during-iteration.js
... / ...
CommitLineData
1
2function testValue(value, expected) {
3 if (value !== expected)
4 throw new Error("bad value: expected:(" + expected + "),actual:(" + value +").");
5}
6
7function identityPairs(array) {
8 return array.map(function (i) { return [i, i]; });
9}
10
11var map = new Map(identityPairs([0]));
12var counter = 0;
13for (var [elm, _] of map) {
14 testValue(elm, counter);
15 map.set(elm + 1, elm + 1);
16 if (elm > 10000) {
17 map.clear();
18 }
19 ++counter;
20}
21testValue(counter, 10002);
22
23var map = new Map(identityPairs([0, 1, 2, 3]));
24var counter = 0;
25for (var [elm, _] of map) {
26 testValue(elm, counter);
27 map.clear();
28 ++counter;
29}
30testValue(counter, 1);
31
32var map = new Map(identityPairs([0, 1, 2, 3]));
33var exp = [0, 2, 3];
34var counter = 0;
35for (var [elm, _] of map) {
36 testValue(elm, exp[counter]);
37 map.delete(counter + 1);
38 ++counter;
39}
40testValue(counter, 3);
41
42var map = new Map(identityPairs([0, 1, 2, 3]));
43var iter = map.keys();
44var iter2 = map.keys();
45testValue(iter2.next().value, 0);
46
47// Consume all output of iter.
48for (var elm of iter);
49
50testValue(iter.next().done, true);
51testValue(iter.next().value, undefined);
52
53map.clear();
54map.set(1, 1).set(2, 2).set(3, 3);
55
56testValue(iter.next().done, true);
57testValue(iter.next().value, undefined);
58testValue(iter2.next().value, 1);
59testValue(iter2.next().value, 2);
60testValue(iter2.next().value, 3);
61
62var map = new Map();
63map.set(1, 1);
64map.delete(1);
65map.forEach(function (i) {
66 throw new Error("unreeachable.");
67});
68
69var map = new Map();
70var iter = map[Symbol.iterator]();
71map.set(1, 1);
72map.delete(1);
73for (var [elm, _] of map) {
74 throw new Error("unreeachable.");
75}
76
77var map = new Map();
78for (var i = 0; i < 5; ++i)
79 map.set(i, i);
80testValue(map.size, 5);
81var iter = map.keys();
82testValue(iter.next().value, 0);
83testValue(iter.next().value, 1);
84testValue(iter.next().value, 2);
85testValue(iter.next().value, 3);
86map.delete(0);
87map.delete(1);
88map.delete(2);
89map.delete(3);
90// It will cause MapData packing.
91for (var i = 5; i < 1000; ++i)
92 map.set(i, i);
93gc();
94for (var i = 4; i < 1000; ++i)
95 testValue(iter.next().value, i);
96testValue(iter.next().value, undefined);
97