Path: blob/master/test/jdk/java/util/Collections/CheckedMapBash.java
41149 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 7129185 807201526* @summary Unit test for Collections.checkedMap27* @author Josh Bloch28* @run testng CheckedMapBash29* @key randomness30*/3132import org.testng.annotations.DataProvider;33import org.testng.annotations.Test;3435import java.util.ArrayList;36import java.util.Arrays;37import java.util.Collection;38import java.util.Collections;39import java.util.HashMap;40import java.util.Iterator;41import java.util.Map;42import java.util.Random;43import java.util.Set;44import java.util.TreeMap;45import java.util.function.Supplier;4647import static org.testng.Assert.fail;4849public class CheckedMapBash {50static final Random rnd = new Random();51static final Object nil = new Integer(0);52static final int numItr = 100;53static final int mapSize = 100;5455@Test(dataProvider = "Bash.Supplier<Map<Integer,Integer>>")56public static void testCheckedMap(String description, Supplier<Map<Integer,Integer>> supplier) {57Map m = supplier.get();58Object head = nil;5960for (int j=0; j<mapSize; j++) {61Object newHead;62do {63newHead = new Integer(rnd.nextInt());64} while (m.containsKey(newHead) || newHead.equals(nil));65m.put(newHead, head);66head = newHead;67}68if (m.size() != mapSize)69fail("Size not as expected.");7071{72HashMap hm = new HashMap(m);73if (! (hm.hashCode() == m.hashCode() &&74hm.entrySet().hashCode() == m.entrySet().hashCode() &&75hm.keySet().hashCode() == m.keySet().hashCode()))76fail("Incorrect hashCode computation.");7778if (! (hm.equals(m) &&79hm.entrySet().equals(m.entrySet()) &&80hm.keySet().equals(m.keySet()) &&81m.equals(hm) &&82m.entrySet().equals(hm.entrySet()) &&83m.keySet().equals(hm.keySet())))84fail("Incorrect equals computation.");85}8687Map m2 = supplier.get(); m2.putAll(m);88m2.values().removeAll(m.keySet());89if (m2.size()!= 1 || !m2.containsValue(nil))90fail("Collection views test failed.");9192int j=0;93while (head != nil) {94if (!m.containsKey(head))95fail("Linked list doesn't contain a link.");96Object newHead = m.get(head);97if (newHead == null)98fail("Could not retrieve a link.");99m.remove(head);100head = newHead;101j++;102}103if (!m.isEmpty())104fail("Map nonempty after removing all links.");105if (j != mapSize)106fail("Linked list size not as expected.");107}108109@Test(dataProvider = "Supplier<Map<Integer,Integer>>")110public static void testCheckedMap2(String description, Supplier<Map<Integer,Integer>> supplier) {111Map m = supplier.get();112for (int i=0; i<mapSize; i++)113if (m.put(new Integer(i), new Integer(2*i)) != null)114fail("put returns a non-null value erroneously.");115for (int i=0; i<2*mapSize; i++)116if (m.containsValue(new Integer(i)) != (i%2==0))117fail("contains value "+i);118if (m.put(nil, nil) == null)119fail("put returns a null value erroneously.");120Map m2 = supplier.get(); m2.putAll(m);121if (!m.equals(m2))122fail("Clone not equal to original. (1)");123if (!m2.equals(m))124fail("Clone not equal to original. (2)");125Set s = m.entrySet(), s2 = m2.entrySet();126if (!s.equals(s2))127fail("Clone not equal to original. (3)");128if (!s2.equals(s))129fail("Clone not equal to original. (4)");130if (!s.containsAll(s2))131fail("Original doesn't contain clone!");132if (!s2.containsAll(s))133fail("Clone doesn't contain original!");134135s2.removeAll(s);136if (!m2.isEmpty())137fail("entrySet().removeAll failed.");138139m2.putAll(m);140m2.clear();141if (!m2.isEmpty())142fail("clear failed.");143144Iterator i = m.entrySet().iterator();145while (i.hasNext()) {146i.next();147i.remove();148}149if (!m.isEmpty())150fail("Iterator.remove() failed");151}152153@DataProvider(name = "Bash.Supplier<Map<Integer,Integer>>", parallel = true)154public static Iterator<Object[]> bashNavigableMapProvider() {155ArrayList<Object[]> iters = new ArrayList<>(makeCheckedMaps());156iters.ensureCapacity(numItr * iters.size());157for (int each=1; each < numItr; each++) {158iters.addAll(makeCheckedMaps());159}160return iters.iterator();161}162163@DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true)164public static Iterator<Object[]> navigableMapProvider() {165return makeCheckedMaps().iterator();166}167168public static Collection<Object[]> makeCheckedMaps() {169Object[][] params = {170{"Collections.checkedMap(HashMap)",171(Supplier) () -> Collections.checkedMap(new HashMap(), Integer.class, Integer.class)},172{"Collections.checkedMap(TreeMap(reverseOrder))",173(Supplier) () -> Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)},174{"Collections.checkedMap(TreeMap.descendingMap())",175(Supplier) () -> Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class)},176{"Collections.checkedNavigableMap(TreeMap)",177(Supplier) () -> Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class)},178{"Collections.checkedNavigableMap(TreeMap(reverseOrder))",179(Supplier) () -> Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)},180{"Collections.checkedNavigableMap(TreeMap.descendingMap())",181(Supplier) () -> Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class)},182};183return Arrays.asList(params);184}185}186187188