]> git.saurik.com Git - apple/javascriptcore.git/blame - tests/stress/iterators-shape.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / iterators-shape.js
CommitLineData
ed1e77d3
A
1// This test checks the shape of builtin iterators.
2
3function iteratorShape(iter) {
4 if (iter.hasOwnProperty('next'))
5 throw "Error: iterator should not have next method.";
6 if (!iter.__proto__.hasOwnProperty('next'))
7 throw "Error: iterator prototype should have next method.";
8 if (typeof iter.__proto__.next !== "function")
9 throw "Error: iterator prototype should have next method.";
10}
11
12function sameNextMethods(iterators) {
13 var iterator = iterators[0];
14 for (var i = 1; i < iterators.length; ++i) {
15 if (iterator.next !== iterators[i].next)
16 throw "Error: next method is not the same.";
17 }
18}
19
20var array = ['Cocoa', 'Cappuccino', 'The des Alizes', 'Matcha', 'Kilimanjaro'];
21var iterator = array[Symbol.iterator]();
22iteratorShape(iterator);
23
24var keyIterator = array.keys();
25iteratorShape(keyIterator);
26
27var keyValueIterator = array.entries();
28iteratorShape(keyValueIterator);
29
30sameNextMethods([array[Symbol.iterator](), array.keys(), array.entries()]);
31
32var set = new Set(['Cocoa', 'Cappuccino', 'The des Alizes', 'Matcha', 'Kilimanjaro']);
33var iterator = set[Symbol.iterator]();
34iteratorShape(iterator);
35
36var keyIterator = set.keys();
37iteratorShape(keyIterator);
38
39var keyValueIterator = set.entries();
40iteratorShape(keyValueIterator);
41
42sameNextMethods([set[Symbol.iterator](), set.keys(), set.entries()]);
43
44var map = new Map();
45[
46 [ 'Cocoa', 2, ],
47 [ 'Cappuccino', 0 ],
48 [ 'The des Alizes', 3 ],
49 [ 'Matcha', 2 ],
50 [ 'Kilimanjaro', 1]
51].forEach(function ([ key, value ]) {
52 map.set(key, value);
53});
54var iterator = map[Symbol.iterator]();
55iteratorShape(iterator);
56
57var keyIterator = map.keys();
58iteratorShape(keyIterator);
59
60var keyValueIterator = map.entries();
61iteratorShape(keyValueIterator);
62
63sameNextMethods([map[Symbol.iterator](), map.keys(), map.entries()]);