Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/CheckedMapBash.java
38812 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4904067 5023830 712918526* @summary Unit test for Collections.checkedMap27* @author Josh Bloch28* @run testng CheckedMapBash29* @key randomness30*/3132import java.util.*;33import java.util.function.Supplier;34import org.testng.annotations.Test;35import org.testng.annotations.DataProvider;3637import static org.testng.Assert.fail;38import static org.testng.Assert.assertTrue;3940public class CheckedMapBash {41static final Random rnd = new Random();42static final Object nil = new Integer(0);43static final int numItr = 100;44static final int mapSize = 100;4546@Test(dataProvider = "Bash.Supplier<Map<Integer,Integer>>")47public static void testCheckedMap(String description, Supplier<Map<Integer,Integer>> supplier) {48Map m = supplier.get();49Object head = nil;5051for (int j=0; j<mapSize; j++) {52Object newHead;53do {54newHead = new Integer(rnd.nextInt());55} while (m.containsKey(newHead));56m.put(newHead, head);57head = newHead;58}59if (m.size() != mapSize)60fail("Size not as expected.");6162{63HashMap hm = new HashMap(m);64if (! (hm.hashCode() == m.hashCode() &&65hm.entrySet().hashCode() == m.entrySet().hashCode() &&66hm.keySet().hashCode() == m.keySet().hashCode()))67fail("Incorrect hashCode computation.");6869if (! (hm.equals(m) &&70hm.entrySet().equals(m.entrySet()) &&71hm.keySet().equals(m.keySet()) &&72m.equals(hm) &&73m.entrySet().equals(hm.entrySet()) &&74m.keySet().equals(hm.keySet())))75fail("Incorrect equals computation.");76}7778Map m2 = supplier.get(); m2.putAll(m);79m2.values().removeAll(m.keySet());80if (m2.size()!= 1 || !m2.containsValue(nil))81fail("Collection views test failed.");8283int j=0;84while (head != nil) {85if (!m.containsKey(head))86fail("Linked list doesn't contain a link.");87Object newHead = m.get(head);88if (newHead == null)89fail("Could not retrieve a link.");90m.remove(head);91head = newHead;92j++;93}94if (!m.isEmpty())95fail("Map nonempty after removing all links.");96if (j != mapSize)97fail("Linked list size not as expected.");98}99100@Test(dataProvider = "Supplier<Map<Integer,Integer>>")101public static void testCheckeMap2(String description, Supplier<Map<Integer,Integer>> supplier) {102Map m = supplier.get();103for (int i=0; i<mapSize; i++)104if (m.put(new Integer(i), new Integer(2*i)) != null)105fail("put returns a non-null value erroenously.");106for (int i=0; i<2*mapSize; i++)107if (m.containsValue(new Integer(i)) != (i%2==0))108fail("contains value "+i);109if (m.put(nil, nil) == null)110fail("put returns a null value erroenously.");111Map m2 = supplier.get(); m2.putAll(m);112if (!m.equals(m2))113fail("Clone not equal to original. (1)");114if (!m2.equals(m))115fail("Clone not equal to original. (2)");116Set s = m.entrySet(), s2 = m2.entrySet();117if (!s.equals(s2))118fail("Clone not equal to original. (3)");119if (!s2.equals(s))120fail("Clone not equal to original. (4)");121if (!s.containsAll(s2))122fail("Original doesn't contain clone!");123if (!s2.containsAll(s))124fail("Clone doesn't contain original!");125126s2.removeAll(s);127if (!m2.isEmpty())128fail("entrySet().removeAll failed.");129130m2.putAll(m);131m2.clear();132if (!m2.isEmpty())133fail("clear failed.");134135Iterator i = m.entrySet().iterator();136while(i.hasNext()) {137i.next();138i.remove();139}140if (!m.isEmpty())141fail("Iterator.remove() failed");142}143144145@DataProvider(name = "Bash.Supplier<Map<Integer,Integer>>", parallel = true)146public static Iterator<Object[]> bashNavigableMapProvider() {147ArrayList<Object[]> iters = new ArrayList<>(makeCheckedMaps());148iters.ensureCapacity(numItr * iters.size());149for(int each=1; each < numItr; each++) {150iters.addAll( makeCheckedMaps());151}152return iters.iterator();153}154155@DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true)156public static Iterator<Object[]> navigableMapProvider() {157return makeCheckedMaps().iterator();158}159160public static Collection<Object[]> makeCheckedMaps() {161return Arrays.asList(162new Object[]{"Collections.checkedMap(HashMap)",163(Supplier) () -> {return Collections.checkedMap(new HashMap(), Integer.class, Integer.class);}},164new Object[]{"Collections.checkedMap(TreeSet(reverseOrder)",165(Supplier) () -> {return Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},166new Object[]{"Collections.checkedMap(TreeSet).descendingSet()",167(Supplier) () -> {return Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}},168new Object[]{"Collections.checkedNavigableMap(TreeSet)",169(Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class);}},170new Object[]{"Collections.checkedNavigableMap(TreeSet(reverseOrder)",171(Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},172new Object[]{"Collections.checkedNavigableMap().descendingSet()",173(Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}}174);175}176}177178179