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 
  33 function 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 
  57 const str = "abcdefg";
  58 const array = ["a", "b", "c", "d", "e", "f", "g"];
  59 const arrayKeys = [0, 1, 2, 3, 4, 5, 6];
  60 const setEntries = [["a", "a"], ["b", "b"], ["c", "c"], ["d", "d"], ["e", "e"], ["f", "f"], ["g", "g"]];
  61 const mapEntries = [["a", "A"], ["b", "B"], ["c", "C"], ["d", "D"], ["e", "E"], ["f", "F"], ["g", "G"]];
  62 const mapValues = ["A", "B", "C", "D", "E", "F", "G"];
  63 const arrayEntries = [[0, "a"], [1, "b"], [2, "c"], [3, "d"], [4, "e"], [5, "f"], [6, "g"]];
  64 
  65 // Set iterator tests
  66 const set = new Set(str);
  67 testIterator(set[Symbol.iterator](), str);
  68 testIterator(set.values(), str);
  69 testIterator(set.keys(), str);
  70 testIterator(set.entries(), setEntries);
  71 
  72 // Map iterator tests
  73 const map = new Map(mapEntries);
  74 testIterator(map[Symbol.iterator](), mapEntries);
  75 testIterator(map.values(), mapValues);
  76 testIterator(map.keys(), array);
  77 testIterator(map.entries(), mapEntries);
  78 
  79 // String iterator tests
  80 testIterator(str[Symbol.iterator](), str);
  81 
  82 // Array iterator tests
  83 testIterator(array[Symbol.iterator](), array);
  84 testIterator(array.values(), array);
  85 testIterator(array.keys(), arrayKeys);
  86 testIterator(array.entries(), arrayEntries);
  87