iterator.js revision 1626:d99fa86747ee
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * JDK-8147558: Add support for ES6 collections
26 *
27 * @test
28 * @run
29 * @option --language=es6
30 */
31
32
33function testIterator(iter, expectedValues) {
34    let next;
35
36    for (var i = 0; i < expectedValues.length; i++) {
37        next = iter.next();
38        Assert.assertTrue(next.done === false);
39
40        if (Array.isArray(expectedValues[i])) {
41            Assert.assertTrue(Array.isArray(next.value));
42            Assert.assertTrue(next.value.length === expectedValues[i].length);
43            Assert.assertTrue(next.value.every(function(v, j) {
44                return v === expectedValues[i][j];
45            }));
46
47        } else {
48            Assert.assertTrue(next.value === expectedValues[i]);
49        }
50    }
51
52    next = iter.next();
53    Assert.assertTrue(next.done === true);
54    Assert.assertTrue(next.value === undefined);
55}
56
57const str = "abcdefg";
58const array = ["a", "b", "c", "d", "e", "f", "g"];
59const arrayKeys = [0, 1, 2, 3, 4, 5, 6];
60const setEntries = [["a", "a"], ["b", "b"], ["c", "c"], ["d", "d"], ["e", "e"], ["f", "f"], ["g", "g"]];
61const mapEntries = [["a", "A"], ["b", "B"], ["c", "C"], ["d", "D"], ["e", "E"], ["f", "F"], ["g", "G"]];
62const mapValues = ["A", "B", "C", "D", "E", "F", "G"];
63const arrayEntries = [[0, "a"], [1, "b"], [2, "c"], [3, "d"], [4, "e"], [5, "f"], [6, "g"]];
64
65// Set iterator tests
66const set = new Set(str);
67testIterator(set[Symbol.iterator](), str);
68testIterator(set.values(), str);
69testIterator(set.keys(), str);
70testIterator(set.entries(), setEntries);
71
72// Map iterator tests
73const map = new Map(mapEntries);
74testIterator(map[Symbol.iterator](), mapEntries);
75testIterator(map.values(), mapValues);
76testIterator(map.keys(), array);
77testIterator(map.entries(), mapEntries);
78
79// String iterator tests
80testIterator(str[Symbol.iterator](), str);
81
82// Array iterator tests
83testIterator(array[Symbol.iterator](), array);
84testIterator(array.values(), array);
85testIterator(array.keys(), arrayKeys);
86testIterator(array.entries(), arrayEntries);
87
88