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