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-8151809: ES6 Map/Set insertion with existing keys changes iteration order 26 * 27 * @test 28 * @run 29 * @option --language=es6 30 */ 31 32 function assertSetIteratorResult(result, expectedDone, expectedValue) { 33 Assert.assertEquals(result.done, expectedDone); 34 Assert.assertEquals(result.value, expectedValue); 35 } 36 37 function assertMapIteratorResult(result, expectedDone, expectedKey, expectedValue) { 38 Assert.assertEquals(result.done, expectedDone); 39 if (expectedDone) { 40 Assert.assertEquals(result.value, undefined); 41 } else { 42 Assert.assertEquals(result.value[0], expectedKey); 43 Assert.assertEquals(result.value[1], expectedValue); 44 } 45 } 46 47 let set = new Set(["foo", "bar", "foo"]); 48 let iter = set[Symbol.iterator](); 49 assertSetIteratorResult(iter.next(), false, "foo"); 50 assertSetIteratorResult(iter.next(), false, "bar"); 51 assertSetIteratorResult(iter.next(), true); 52 53 set.add ("foo"); 54 iter = set[Symbol.iterator](); 55 assertSetIteratorResult(iter.next(), false, "foo", false); 56 assertSetIteratorResult(iter.next(), false, "bar", false); 57 assertSetIteratorResult(iter.next(), true); 58 59 set.delete("foo"); 60 set.add ("foo"); 61 assertSetIteratorResult(iter.next(), true); 62 iter = set[Symbol.iterator](); 63 assertSetIteratorResult(iter.next(), false, "bar", false); 64 assertSetIteratorResult(iter.next(), false, "foo", false); 65 assertSetIteratorResult(iter.next(), true); 66 67 68 let map = new Map([["foo", 1], ["bar", 2], ["foo", 3]]); 69 iter = map[Symbol.iterator](); 70 assertMapIteratorResult(iter.next(), false, "foo", 3); 71 assertMapIteratorResult(iter.next(), false, "bar", 2); 72 assertMapIteratorResult(iter.next(), true); 73 74 75 map.set("foo", 4); 76 iter = map[Symbol.iterator](); 77 assertMapIteratorResult(iter.next(), false, "foo", 4); 78 assertMapIteratorResult(iter.next(), false, "bar", 2); 79 assertMapIteratorResult(iter.next(), true); 80 81 map.delete("foo"); 82 map.set("foo", 5); 83 assertMapIteratorResult(iter.next(), true); 84 iter = map[Symbol.iterator](); 85 assertMapIteratorResult(iter.next(), false, "bar", 2); 86 assertMapIteratorResult(iter.next(), false, "foo", 5); 87 assertMapIteratorResult(iter.next(), true);