Path: blob/master/test/jdk/java/util/Spliterator/SpliteratorLateBindingTest.java
41149 views
/*1* Copyright (c) 2013, 2016, 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 org.testng.annotations.DataProvider;24import org.testng.annotations.Test;2526import java.nio.CharBuffer;27import java.util.ArrayDeque;28import java.util.ArrayList;29import java.util.Arrays;30import java.util.BitSet;31import java.util.HashMap;32import java.util.HashSet;33import java.util.IdentityHashMap;34import java.util.LinkedHashMap;35import java.util.LinkedHashSet;36import java.util.LinkedList;37import java.util.List;38import java.util.PriorityQueue;39import java.util.Set;40import java.util.Spliterator;41import java.util.Stack;42import java.util.TreeMap;43import java.util.TreeSet;44import java.util.Vector;45import java.util.WeakHashMap;46import java.util.function.Function;47import java.util.function.Supplier;48import java.util.stream.Stream;4950import static org.testng.Assert.assertEquals;5152/**53* @test54* @bug 8148748 817015555* @summary Spliterator last-binding tests56* @run testng SpliteratorLateBindingTest57*/5859@Test60public class SpliteratorLateBindingTest extends SpliteratorLateBindingFailFastHelper {6162static Object[][] spliteratorDataProvider;6364@DataProvider(name = "Source")65public static Object[][] sourceDataProvider() {66if (spliteratorDataProvider != null) {67return spliteratorDataProvider;68}6970List<Object[]> data = new ArrayList<>();71SpliteratorDataBuilder<Integer> db =72new SpliteratorDataBuilder<>(data, 5, Arrays.asList(1, 2, 3, 4));7374// Collections7576db.addList(ArrayList::new);7778db.addList(LinkedList::new);7980db.addList(Vector::new);8182db.addList(AbstractRandomAccessListImpl::new);8384db.addCollection(HashSet::new);8586db.addCollection(LinkedHashSet::new);8788db.addCollection(TreeSet::new);8990db.addCollection(c -> {91Stack<Integer> s = new Stack<>();92s.addAll(c);93return s;94});9596db.addCollection(PriorityQueue::new);9798db.addCollection(ArrayDeque::new);99100// Maps101102db.addMap(HashMap::new);103104db.addMap(LinkedHashMap::new);105106db.addMap(IdentityHashMap::new);107108db.addMap(WeakHashMap::new);109110// @@@ Descending maps etc111db.addMap(TreeMap::new);112113// BitSet114115List<Integer> bits = List.of(0, 1, 2);116Function<BitSet, Spliterator.OfInt> bitsSource = bs -> bs.stream().spliterator();117db.add("new BitSet.stream().spliterator() ADD",118() -> new IntSource<>(toBitSet(bits), bitsSource, bs -> bs.set(3)));119db.add("new BitSet.stream().spliterator() REMOVE",120() -> new IntSource<>(toBitSet(bits), bitsSource, bs -> bs.clear(2)));121122// CharSequence123124Function<CharSequence, Spliterator.OfInt> charsSource = sb -> sb.chars().spliterator();125Function<CharSequence, Spliterator.OfInt> pointsSource = sb -> sb.codePoints().spliterator();126127db.add("new StringBuilder.chars().spliterator() ADD",128() -> new IntSource<>(new StringBuilder("ABC"), charsSource, bs -> bs.append("D"), true));129db.add("new StringBuilder.chars().spliterator() REMOVE",130() -> new IntSource<>(new StringBuilder("ABC"), charsSource, bs -> bs.deleteCharAt(2), true));131db.add("new StringBuilder.codePoints().spliterator() ADD",132() -> new IntSource<>(new StringBuilder("ABC"), pointsSource, bs -> bs.append("D"), true));133db.add("new StringBuilder.codePoints().spliterator() REMOVE",134() -> new IntSource<>(new StringBuilder("ABC"), pointsSource, bs -> bs.deleteCharAt(2), true));135136db.add("new StringBuffer.chars().spliterator() ADD",137() -> new IntSource<>(new StringBuffer("ABC"), charsSource, bs -> bs.append("D"), true));138db.add("new StringBuffer.chars().spliterator() REMOVE",139() -> new IntSource<>(new StringBuffer("ABC"), charsSource, bs -> bs.deleteCharAt(2), true));140db.add("new StringBuffer.codePoints().spliterator() ADD",141() -> new IntSource<>(new StringBuffer("ABC"), pointsSource, bs -> bs.append("D"), true));142db.add("new StringBuffer.codePoints().spliterator() REMOVE",143() -> new IntSource<>(new StringBuffer("ABC"), pointsSource, bs -> bs.deleteCharAt(2), true));144145db.add("CharBuffer.wrap().chars().spliterator() ADD",146() -> new IntSource<>(CharBuffer.wrap("ABCD").limit(3), charsSource, bs -> bs.limit(4), true));147db.add("CharBuffer.wrap().chars().spliterator() REMOVE",148() -> new IntSource<>(CharBuffer.wrap("ABCD"), charsSource, bs -> bs.limit(3), true));149db.add("CharBuffer.wrap().codePoints().spliterator() ADD",150() -> new IntSource<>(CharBuffer.wrap("ABCD").limit(3), pointsSource, bs -> bs.limit(4), true));151db.add("CharBuffer.wrap().codePoints().spliterator() REMOVE",152() -> new IntSource<>(CharBuffer.wrap("ABCD"), pointsSource, bs -> bs.limit(3), true));153154return spliteratorDataProvider = data.toArray(new Object[0][]);155}156157158@DataProvider(name = "Source.Non.Binding.Characteristics")159public static Object[][] sourceCharacteristicsDataProvider() {160return Stream.of(sourceDataProvider()).filter(tc -> {161@SuppressWarnings("unchecked")162Supplier<Source<?>> s = (Supplier<Source<?>>) tc[1];163return !s.get().bindOnCharacteristics();164}).toArray(Object[][]::new);165}166167static BitSet toBitSet(List<Integer> bits) {168BitSet bs = new BitSet();169bits.forEach(bs::set);170return bs;171}172173174@Test(dataProvider = "Source")175public <T> void testForEach(String description, Supplier<Source<T>> ss) {176Source<T> source = ss.get();177Spliterator<T> s = source.spliterator();178179source.update();180181Set<T> a = new HashSet<>();182s.forEachRemaining(a::add);183184Set<T> e = new HashSet<>();185source.spliterator().forEachRemaining(e::add);186assertEquals(a, e);187}188189@Test(dataProvider = "Source")190public <T> void testTryAdvance(String description, Supplier<Source<T>> ss) {191Source<T> source = ss.get();192Spliterator<T> s = source.spliterator();193194source.update();195196Set<T> a = new HashSet<>();197while (s.tryAdvance(a::add)) {198}199200Set<T> e = new HashSet<>();201source.spliterator().forEachRemaining(e::add);202assertEquals(a, e);203}204205@Test(dataProvider = "Source.Non.Binding.Characteristics")206public <T> void testCharacteristics(String description, Supplier<Source<T>> ss) {207Source<T> source = ss.get();208Spliterator<T> s = source.spliterator();209210s.characteristics();211source.update();212213Set<T> a = new HashSet<>();214s.forEachRemaining(a::add);215216Set<T> e = new HashSet<>();217source.spliterator().forEachRemaining(e::add);218assertEquals(a, e);219}220}221222223