Path: blob/master/test/jdk/java/util/Collection/testlibrary/CollectionSupplier.java
41153 views
/*1* Copyright (c) 2012, 2014, 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*/2223import java.lang.Exception;24import java.lang.Integer;25import java.lang.Iterable;26import java.lang.Override;27import java.util.ArrayList;28import java.util.Arrays;29import java.util.LinkedList;30import java.util.List;31import java.util.Random;3233import org.testng.TestException;3435import static org.testng.Assert.assertTrue;3637import java.util.Collection;38import java.util.Collections;39import java.util.function.Function;40import java.util.function.Supplier;4142/**43* @library44* @summary A Supplier of test cases for Collection tests45*/46public final class CollectionSupplier<C extends Collection<Integer>> implements Supplier<Iterable<CollectionSupplier.TestCase<C>>> {4748private final List<Function<Collection<Integer>, C>> suppliers;49private final int size;5051/**52* A Collection test case.53*/54public static final class TestCase<C extends Collection<Integer>> {55/**56* The name of the test case.57*/58public final String name;5960/**61* The supplier of a collection62*/63public Function<Collection<Integer>, C> supplier;6465/**66* Unmodifiable reference collection, useful for comparisons.67*/68public final List<Integer> expected;6970/**71* A modifiable test collection.72*/73public final C collection;7475/**76* Create a Collection test case.77*78* @param name name of the test case79* @param collection the modifiable test collection80*/81public TestCase(String name, Function<Collection<Integer>, C> supplier, C collection) {82this.name = name;83this.supplier = supplier;84this.expected = Collections.unmodifiableList(85Arrays.asList(collection.toArray(new Integer[0])));86this.collection = collection;87}8889@Override90public String toString() {91return name + " " + collection.getClass().toString();92}93}9495/**96* Shuffle a list using a PRNG with known seed for repeatability97*98* @param list the list to be shuffled99*/100public static <E> void shuffle(final List<E> list) {101// PRNG with known seed for repeatable tests102final Random prng = new Random(13);103final int size = list.size();104for (int i = 0; i < size; i++) {105// random index in interval [i, size)106final int j = i + prng.nextInt(size - i);107// swap elements at indices i & j108final E e = list.get(i);109list.set(i, list.get(j));110list.set(j, e);111}112}113114/**115* Create a {@code CollectionSupplier} that creates instances of specified116* collection suppliers of the specified size.117*118* @param suppliers the suppliers names that supply {@code Collection}119* instances120* @param size the desired size of each collection121*/122public CollectionSupplier(List<Function<Collection<Integer>, C>> suppliers, int size) {123this.suppliers = suppliers;124this.size = size;125}126127@Override128public Iterable<TestCase<C>> get() {129final Collection<TestCase<C>> cases = new LinkedList<>();130for (final Function<Collection<Integer>, C> supplier : suppliers)131try {132cases.add(new TestCase<>("empty", supplier, supplier.apply(Collections.emptyList())));133134cases.add(new TestCase<>("single", supplier, supplier.apply(Arrays.asList(42))));135136final Collection<Integer> regular = new ArrayList<>();137for (int i = 0; i < size; i++) {138regular.add(i);139}140cases.add(new TestCase<>("regular", supplier, supplier.apply(regular)));141142final Collection<Integer> reverse = new ArrayList<>();143for (int i = size; i >= 0; i--) {144reverse.add(i);145}146cases.add(new TestCase<>("reverse", supplier, supplier.apply(reverse)));147148final Collection<Integer> odds = new ArrayList<>();149for (int i = 0; i < size; i++) {150odds.add((i * 2) + 1);151}152cases.add(new TestCase<>("odds", supplier, supplier.apply(odds)));153154final Collection<Integer> evens = new ArrayList<>();155for (int i = 0; i < size; i++) {156evens.add(i * 2);157}158cases.add(new TestCase<>("evens", supplier, supplier.apply(evens)));159160final Collection<Integer> fibonacci = new ArrayList<>();161int prev2 = 0;162int prev1 = 1;163for (int i = 0; i < size; i++) {164final int n = prev1 + prev2;165if (n < 0) { // stop on overflow166break;167}168fibonacci.add(n);169prev2 = prev1;170prev1 = n;171}172cases.add(new TestCase<>("fibonacci", supplier, supplier.apply(fibonacci)));173174175boolean isStructurallyModifiable = false;176try {177C t = supplier.apply(Collections.emptyList());178t.add(1);179isStructurallyModifiable = true;180} catch (UnsupportedOperationException e) { }181182if (!isStructurallyModifiable)183continue;184185186// variants where the size of the backing storage != reported size187// created by removing half of the elements188final C emptyWithSlack = supplier.apply(Collections.emptyList());189emptyWithSlack.add(42);190assertTrue(emptyWithSlack.remove(42));191cases.add(new TestCase<>("emptyWithSlack", supplier, emptyWithSlack));192193final C singleWithSlack = supplier.apply(Collections.emptyList());194singleWithSlack.add(42);195singleWithSlack.add(43);196assertTrue(singleWithSlack.remove(43));197cases.add(new TestCase<>("singleWithSlack", supplier, singleWithSlack));198199final C regularWithSlack = supplier.apply(Collections.emptyList());200for (int i = 0; i < (2 * size); i++) {201regularWithSlack.add(i);202}203assertTrue(regularWithSlack.removeIf(x -> x < size));204cases.add(new TestCase<>("regularWithSlack", supplier, regularWithSlack));205206final C reverseWithSlack = supplier.apply(Collections.emptyList());207for (int i = 2 * size; i >= 0; i--) {208reverseWithSlack.add(i);209}210assertTrue(reverseWithSlack.removeIf(x -> x < size));211cases.add(new TestCase<>("reverseWithSlack", supplier, reverseWithSlack));212213final C oddsWithSlack = supplier.apply(Collections.emptyList());214for (int i = 0; i < 2 * size; i++) {215oddsWithSlack.add((i * 2) + 1);216}217assertTrue(oddsWithSlack.removeIf(x -> x >= size));218cases.add(new TestCase<>("oddsWithSlack", supplier, oddsWithSlack));219220final C evensWithSlack = supplier.apply(Collections.emptyList());221for (int i = 0; i < 2 * size; i++) {222evensWithSlack.add(i * 2);223}224assertTrue(evensWithSlack.removeIf(x -> x >= size));225cases.add(new TestCase<>("evensWithSlack", supplier, evensWithSlack));226227final C fibonacciWithSlack = supplier.apply(Collections.emptyList());228prev2 = 0;229prev1 = 1;230for (int i = 0; i < size; i++) {231final int n = prev1 + prev2;232if (n < 0) { // stop on overflow233break;234}235fibonacciWithSlack.add(n);236prev2 = prev1;237prev1 = n;238}239assertTrue(fibonacciWithSlack.removeIf(x -> x < 20));240cases.add(new TestCase<>("fibonacciWithSlack", supplier, fibonacciWithSlack));241}242catch (Exception failed) {243throw new TestException(failed);244}245246return cases;247}248249}250251252