Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/CheckedSetBash.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 712918526* @summary Unit test for Collections.checkedSet27* @author Josh Bloch28* @run testng CheckedSetBash29* @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 CheckedSetBash {41static final int numItr = 100;42static final int setSize = 100;43static final Random rnd = new Random();4445@Test(dataProvider = "Supplier<Set<Integer>>")46public static void testCheckedSet(String description, Supplier<Set<Integer>> supplier) {4748Set<Integer> s1 = supplier.get();49assertTrue(s1.isEmpty());5051AddRandoms(s1, setSize);5253Set<Integer> s2 = supplier.get();5455assertTrue(s2.isEmpty());5657AddRandoms(s2, setSize);5859Set<Integer> intersection = clone(s1, supplier);60intersection.retainAll(s2);61Set<Integer> diff1 = clone(s1, supplier); diff1.removeAll(s2);62Set<Integer> diff2 = clone(s2, supplier); diff2.removeAll(s1);63Set<Integer> union = clone(s1, supplier); union.addAll(s2);6465if (diff1.removeAll(diff2))66fail("Set algebra identity 2 failed");67if (diff1.removeAll(intersection))68fail("Set algebra identity 3 failed");69if (diff2.removeAll(diff1))70fail("Set algebra identity 4 failed");71if (diff2.removeAll(intersection))72fail("Set algebra identity 5 failed");73if (intersection.removeAll(diff1))74fail("Set algebra identity 6 failed");75if (intersection.removeAll(diff1))76fail("Set algebra identity 7 failed");7778intersection.addAll(diff1); intersection.addAll(diff2);79if (!intersection.equals(union))80fail("Set algebra identity 1 failed");8182if (new HashSet(union).hashCode() != union.hashCode())83fail("Incorrect hashCode computation.");8485Iterator e = union.iterator();86while (e.hasNext())87if (!intersection.remove(e.next()))88fail("Couldn't remove element from copy.");89if (!intersection.isEmpty())90fail("Copy nonempty after deleting all elements.");9192e = union.iterator();93while (e.hasNext()) {94Object o = e.next();95if (!union.contains(o))96fail("Set doesn't contain one of its elements.");97e.remove();98if (union.contains(o))99fail("Set contains element after deletion.");100}101if (!union.isEmpty())102fail("Set nonempty after deleting all elements.");103104s1.clear();105if (!s1.isEmpty())106fail("Set nonempty after clear.");107}108109// Done inefficiently so as to exercise toArray110static <T> Set<T> clone(Set<T> s, Supplier<Set<T>> supplier) {111Set<T> clone = supplier.get();112List<T> arrayList = Arrays.asList((T[]) s.toArray());113clone.addAll(arrayList);114if (!s.equals(clone))115fail("Set not equal to copy.");116if (!s.containsAll(clone))117fail("Set does not contain copy.");118if (!clone.containsAll(s))119fail("Copy does not contain set.");120return clone;121}122123static void AddRandoms(Set s, int n) {124for (int i=0; i<n; i++) {125int r = rnd.nextInt() % n;126Integer e = new Integer(r < 0 ? -r : r);127128int preSize = s.size();129boolean prePresent = s.contains(e);130boolean added = s.add(e);131if (!s.contains(e))132fail ("Element not present after addition.");133if (added == prePresent)134fail ("added == alreadyPresent");135int postSize = s.size();136if (added && preSize == postSize)137fail ("Add returned true, but size didn't change.");138if (!added && preSize != postSize)139fail ("Add returned false, but size changed.");140}141}142143@DataProvider(name = "Supplier<Set<Integer>>", parallel = true)144public static Iterator<Object[]> navigableSetsProvider() {145ArrayList<Object[]> iters = new ArrayList<>(makeCheckedSets());146iters.ensureCapacity(numItr * iters.size());147for(int each=1; each < numItr; each++) {148iters.addAll( makeCheckedSets());149}150return iters.iterator();151}152153public static Collection<Object[]> makeCheckedSets() {154return Arrays.asList(155new Object[]{"Collections.checkedSet(HashSet)",156(Supplier) () -> {return Collections.checkedSet(new HashSet(), Integer.class);}},157new Object[]{"Collections.checkedSet(TreeSet(reverseOrder)",158(Supplier) () -> {return Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},159new Object[]{"Collections.checkedSet(TreeSet).descendingSet()",160(Supplier) () -> {return Collections.checkedSet(new TreeSet().descendingSet(), Integer.class);}},161new Object[]{"Collections.checkedNavigableSet(TreeSet)",162(Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(), Integer.class);}},163new Object[]{"Collections.checkedNavigableSet(TreeSet(reverseOrder)",164(Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},165new Object[]{"Collections.checkedNavigableSet().descendingSet()",166(Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class);}}167);168}169}170171172