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 function assertThrows(src, error) {
  33     try {
  34         eval(src);
  35         Assert.fail("No error, expected " + error);
  36     } catch (e) {
  37         if (!(e instanceof error)) {
  38             Assert.fail("Wrong error, expected " + error + " but got " + e);
  39         }
  40     }
  41 }
  42 
  43 // Set constructor and prototype
  44 
  45 var desc = Object.getOwnPropertyDescriptor(this, "Set");
  46 Assert.assertEquals(desc.writable, true);
  47 Assert.assertEquals(desc.configurable, true);
  48 Assert.assertEquals(desc.enumerable, false);
  49 
  50 Assert.assertTrue(Object.getPrototypeOf(new Set()) === Set.prototype);
  51 Assert.assertTrue(Object.getPrototypeOf(Set.prototype) === Object.prototype);
  52 Assert.assertTrue(new Set().size === 0);
  53 Assert.assertTrue(Object.getPrototypeOf(new Set(["a", 3, false])) === Set.prototype);
  54 Assert.assertTrue(new Set(["a", 3, false]).size === 3);
  55 Assert.assertTrue(new Set("abc").size === 3);
  56 Assert.assertTrue(new Set("").size === 0);
  57 Assert.assertTrue(new Set([]).size === 0);
  58 
  59 assertThrows("Set()", TypeError);
  60 assertThrows("new Set(3)", TypeError);
  61 assertThrows("new Set({})", TypeError);
  62 
  63 assertThrows("Set.prototype.add.apply({}, [''])", TypeError);
  64 assertThrows("Set.prototype.has.apply(3, [''])", TypeError);
  65 assertThrows("Set.prototype.delete.apply('', [3])", TypeError);
  66 
  67 // Set methods
  68 
  69 var s = new Set(["a", 3, false]);
  70 Assert.assertTrue(s.size, 2);
  71 Assert.assertTrue(s.has("a") === true);
  72 Assert.assertTrue(s.has(3) === true);
  73 Assert.assertTrue(s.has(false) === true);
  74 
  75 Assert.assertTrue(s.clear() === undefined);
  76 Assert.assertTrue(s.size === 0);
  77 Assert.assertTrue(s.has("a") === false);
  78 Assert.assertTrue(s.has(3) === false);
  79 Assert.assertTrue(s.has(false) === false);
  80 
  81 var a = "a", x = "x"; // for ConsString keys
  82 Assert.assertTrue(s.add("ab", false) === s);
  83 Assert.assertTrue(s.add(x + "y", s) === s);
  84 Assert.assertTrue(s.has(a + "b") === true);
  85 Assert.assertTrue(s.has("xy") === true);
  86 
  87 // Special keys
  88 
  89 s.clear()
  90 Assert.assertTrue(s.add(NaN) === s);  // NaN should work as key
  91 Assert.assertTrue(s.size === 1);
  92 Assert.assertTrue(isNaN(s.keys().next().value));
  93 Assert.assertTrue(isNaN(s.values().next().value));
  94 Assert.assertTrue(s.has(NaN) === true);
  95 Assert.assertTrue(s.delete(NaN) === true);
  96 Assert.assertTrue(s.size === 0);
  97 Assert.assertTrue(s.keys().next().done);
  98 Assert.assertTrue(s.values().next().done);
  99 Assert.assertTrue(s.has(NaN) === false);
 100 
 101 s.clear()
 102 s.add(-0); // -0 key should be converted to +0
 103 Assert.assertTrue(s.size === 1);
 104 Assert.assertTrue(1 / s.keys().next().value === Infinity);
 105 Assert.assertTrue(1 / s.values().next().value === Infinity);
 106 Assert.assertTrue(s.has(-0) === true);
 107 Assert.assertTrue(s.has(0) === true);
 108 Assert.assertTrue(s.delete(-0) === true);
 109 Assert.assertTrue(s.size === 0);
 110 Assert.assertTrue(s.has(-0) === false);
 111 Assert.assertTrue(s.has(0) === false);
 112 
 113 // foreach
 114 
 115 s = new Set([1, 2, 3]);
 116 
 117 s.forEach(function(value, key, set) {
 118     Assert.assertTrue(this === s);
 119     Assert.assertTrue(set === s);
 120 }, s);
 121 
 122 function assertEqualArrays(a, b) {
 123     Assert.assertTrue(Array.isArray(a));
 124     Assert.assertTrue(Array.isArray(b));
 125     Assert.assertTrue(a.length === b.length);
 126     Assert.assertTrue(a.every(function(v, j) {
 127         return v === b[j];
 128     }));
 129 }
 130 
 131 let array = [];
 132 s = new Set([1, 2, 3]);
 133 s.forEach(function(value, key, set) {
 134     array.push(value);
 135 });
 136 assertEqualArrays(array, [1, 2, 3]);
 137 
 138 array = [];
 139 s = new Set([1, 2, 3]);
 140 s.forEach(function(value, key, set) {
 141     array.push(value);
 142     if (key == 3) {
 143         set.clear();
 144         set.add("four");
 145     }
 146 });
 147 assertEqualArrays(array, [1, 2, 3, "four"]);
 148 
 149 array = [];
 150 s = new Set([1, 2, 3]);
 151 s.forEach(function(value, key, set) {
 152     array.push(value);
 153     if (key == 1) {
 154         set.delete(1);
 155     }
 156     if (key == 2) {
 157         set.delete(3);
 158     }
 159 });
 160 assertEqualArrays(array, [1, 2]);
 161 
 162 array = [];
 163 s = new Set([1, 2, 3]);
 164 s.forEach(function(value, key, set) {
 165     array.push(value);
 166     if (key < 4) {
 167         set.delete(key);
 168         set.add(key + 3)
 169     }
 170 });
 171 assertEqualArrays(array, [1, 2, 3, 4, 5, 6]);
 172 
 173