Path: blob/master/test/jdk/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java
41149 views
/*1* Copyright (c) 2013, 2017, 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* @summary Spliterator traversing and splitting tests26* @library /lib/testlibrary/bootlib27* @build java.base/java.util.SpliteratorOfIntDataBuilder28* java.base/java.util.SpliteratorTestHelper29* @run testng SpliteratorTraversingAndSplittingTest30* @bug 8020016 8071477 8072784 816983831*/3233import org.testng.annotations.DataProvider;34import org.testng.annotations.Test;3536import java.nio.CharBuffer;37import java.util.AbstractCollection;38import java.util.AbstractList;39import java.util.AbstractSet;40import java.util.ArrayDeque;41import java.util.ArrayList;42import java.util.Arrays;43import java.util.Collection;44import java.util.Collections;45import java.util.Comparator;46import java.util.HashMap;47import java.util.HashSet;48import java.util.IdentityHashMap;49import java.util.Iterator;50import java.util.LinkedHashMap;51import java.util.LinkedHashSet;52import java.util.LinkedList;53import java.util.List;54import java.util.ListIterator;55import java.util.Map;56import java.util.PriorityQueue;57import java.util.RandomAccess;58import java.util.Set;59import java.util.SortedSet;60import java.util.Spliterator;61import java.util.SpliteratorOfIntDataBuilder;62import java.util.SpliteratorTestHelper;63import java.util.Spliterators;64import java.util.Stack;65import java.util.TreeMap;66import java.util.TreeSet;67import java.util.Vector;68import java.util.WeakHashMap;69import java.util.concurrent.ArrayBlockingQueue;70import java.util.concurrent.ConcurrentHashMap;71import java.util.concurrent.ConcurrentLinkedQueue;72import java.util.concurrent.ConcurrentSkipListMap;73import java.util.concurrent.ConcurrentSkipListSet;74import java.util.concurrent.CopyOnWriteArrayList;75import java.util.concurrent.CopyOnWriteArraySet;76import java.util.concurrent.LinkedBlockingDeque;77import java.util.concurrent.LinkedBlockingQueue;78import java.util.concurrent.LinkedTransferQueue;79import java.util.concurrent.PriorityBlockingQueue;80import java.util.function.Consumer;81import java.util.function.DoubleConsumer;82import java.util.function.Function;83import java.util.function.IntConsumer;84import java.util.function.LongConsumer;85import java.util.function.Supplier;86import java.util.function.UnaryOperator;8788public class SpliteratorTraversingAndSplittingTest extends SpliteratorTestHelper {8990private static final List<Integer> SIZES = Arrays.asList(0, 1, 10, 42);9192private static final String LOW = new String(new char[] {Character.MIN_LOW_SURROGATE});93private static final String HIGH = new String(new char[] {Character.MIN_HIGH_SURROGATE});94private static final String HIGH_LOW = HIGH + LOW;95private static final String CHAR_HIGH_LOW = "A" + HIGH_LOW;96private static final String HIGH_LOW_CHAR = HIGH_LOW + "A";97private static final String CHAR_HIGH_LOW_CHAR = "A" + HIGH_LOW + "A";9899private static final List<String> STRINGS = generateTestStrings();100101private static List<String> generateTestStrings() {102List<String> strings = new ArrayList<>();103for (int n : Arrays.asList(1, 2, 3, 16, 17)) {104strings.add(generate("A", n));105strings.add(generate(LOW, n));106strings.add(generate(HIGH, n));107strings.add(generate(HIGH_LOW, n));108strings.add(generate(CHAR_HIGH_LOW, n));109strings.add(generate(HIGH_LOW_CHAR, n));110strings.add(generate(CHAR_HIGH_LOW_CHAR, n));111}112return strings;113}114115private static String generate(String s, int n) {116StringBuilder sb = new StringBuilder();117for (int i = 0; i < n; i++) {118sb.append(s);119}120return sb.toString();121}122123private static class SpliteratorDataBuilder<T> {124List<Object[]> data;125126List<T> exp;127128Map<T, T> mExp;129130SpliteratorDataBuilder(List<Object[]> data, List<T> exp) {131this.data = data;132this.exp = exp;133this.mExp = createMap(exp);134}135136Map<T, T> createMap(List<T> l) {137Map<T, T> m = new LinkedHashMap<>();138for (T t : l) {139m.put(t, t);140}141return m;142}143144void add(String description, Collection<?> expected, Supplier<Spliterator<?>> s) {145description = joiner(description).toString();146data.add(new Object[]{description, expected, s});147}148149void add(String description, Supplier<Spliterator<?>> s) {150add(description, exp, s);151}152153void addCollection(Function<Collection<T>, ? extends Collection<T>> c) {154add("new " + c.apply(Collections.<T>emptyList()).getClass().getName() + ".spliterator()",155() -> c.apply(exp).spliterator());156}157158void addList(Function<Collection<T>, ? extends List<T>> l) {159addCollection(l);160addCollection(l.andThen(list -> list.subList(0, list.size())));161}162163void addMap(Function<Map<T, T>, ? extends Map<T, T>> m) {164String description = "new " + m.apply(Collections.<T, T>emptyMap()).getClass().getName();165addMap(m, description);166}167168void addMap(Function<Map<T, T>, ? extends Map<T, T>> m, String description) {169add(description + ".keySet().spliterator()", () -> m.apply(mExp).keySet().spliterator());170add(description + ".values().spliterator()", () -> m.apply(mExp).values().spliterator());171add(description + ".entrySet().spliterator()", mExp.entrySet(), () -> m.apply(mExp).entrySet().spliterator());172}173174StringBuilder joiner(String description) {175return new StringBuilder(description).176append(" {").177append("size=").append(exp.size()).178append("}");179}180}181182static Object[][] spliteratorDataProvider;183184@DataProvider(name = "Spliterator<Integer>")185public static Object[][] spliteratorDataProvider() {186if (spliteratorDataProvider != null) {187return spliteratorDataProvider;188}189190List<Object[]> data = new ArrayList<>();191for (int size : SIZES) {192List<Integer> exp = listIntRange(size);193SpliteratorDataBuilder<Integer> db = new SpliteratorDataBuilder<>(data, exp);194195// Direct spliterator methods196197db.add("Spliterators.spliterator(Collection, ...)",198() -> Spliterators.spliterator(exp, 0));199200db.add("Spliterators.spliterator(Iterator, ...)",201() -> Spliterators.spliterator(exp.iterator(), exp.size(), 0));202203db.add("Spliterators.spliteratorUnknownSize(Iterator, ...)",204() -> Spliterators.spliteratorUnknownSize(exp.iterator(), 0));205206db.add("Spliterators.spliterator(Spliterators.iteratorFromSpliterator(Spliterator ), ...)",207() -> Spliterators.spliterator(Spliterators.iterator(exp.spliterator()), exp.size(), 0));208209db.add("Spliterators.spliterator(T[], ...)",210() -> Spliterators.spliterator(exp.toArray(new Integer[0]), 0));211212db.add("Arrays.spliterator(T[], ...)",213() -> Arrays.spliterator(exp.toArray(new Integer[0])));214215class SpliteratorFromIterator extends Spliterators.AbstractSpliterator<Integer> {216Iterator<Integer> it;217218SpliteratorFromIterator(Iterator<Integer> it, long est) {219super(est, Spliterator.SIZED);220this.it = it;221}222223@Override224public boolean tryAdvance(Consumer<? super Integer> action) {225if (action == null)226throw new NullPointerException();227if (it.hasNext()) {228action.accept(it.next());229return true;230}231else {232return false;233}234}235}236db.add("new Spliterators.AbstractSpliterator()",237() -> new SpliteratorFromIterator(exp.iterator(), exp.size()));238239// Collections240241// default method implementations242243class AbstractCollectionImpl extends AbstractCollection<Integer> {244Collection<Integer> c;245246AbstractCollectionImpl(Collection<Integer> c) {247this.c = c;248}249250@Override251public Iterator<Integer> iterator() {252return c.iterator();253}254255@Override256public int size() {257return c.size();258}259}260db.addCollection(261c -> new AbstractCollectionImpl(c));262263class AbstractListImpl extends AbstractList<Integer> {264List<Integer> l;265266AbstractListImpl(Collection<Integer> c) {267this.l = new ArrayList<>(c);268}269270@Override271public Integer get(int index) {272return l.get(index);273}274275@Override276public int size() {277return l.size();278}279}280db.addCollection(281c -> new AbstractListImpl(c));282283class AbstractSetImpl extends AbstractSet<Integer> {284Set<Integer> s;285286AbstractSetImpl(Collection<Integer> c) {287this.s = new HashSet<>(c);288}289290@Override291public Iterator<Integer> iterator() {292return s.iterator();293}294295@Override296public int size() {297return s.size();298}299}300db.addCollection(301c -> new AbstractSetImpl(c));302303class AbstractSortedSetImpl extends AbstractSet<Integer> implements SortedSet<Integer> {304SortedSet<Integer> s;305306AbstractSortedSetImpl(Collection<Integer> c) {307this.s = new TreeSet<>(c);308}309310@Override311public Iterator<Integer> iterator() {312return s.iterator();313}314315@Override316public int size() {317return s.size();318}319320@Override321public Comparator<? super Integer> comparator() {322return s.comparator();323}324325@Override326public SortedSet<Integer> subSet(Integer fromElement, Integer toElement) {327return s.subSet(fromElement, toElement);328}329330@Override331public SortedSet<Integer> headSet(Integer toElement) {332return s.headSet(toElement);333}334335@Override336public SortedSet<Integer> tailSet(Integer fromElement) {337return s.tailSet(fromElement);338}339340@Override341public Integer first() {342return s.first();343}344345@Override346public Integer last() {347return s.last();348}349350@Override351public Spliterator<Integer> spliterator() {352return SortedSet.super.spliterator();353}354}355db.addCollection(356c -> new AbstractSortedSetImpl(c));357358class IterableWrapper implements Iterable<Integer> {359final Iterable<Integer> it;360361IterableWrapper(Iterable<Integer> it) {362this.it = it;363}364365@Override366public Iterator<Integer> iterator() {367return it.iterator();368}369}370db.add("new Iterable.spliterator()",371() -> new IterableWrapper(exp).spliterator());372373//374375db.add("Arrays.asList().spliterator()",376() -> Spliterators.spliterator(Arrays.asList(exp.toArray(new Integer[0])), 0));377378db.addList(ArrayList::new);379380db.addList(LinkedList::new);381382db.addList(Vector::new);383384class AbstractRandomAccessListImpl extends AbstractList<Integer> implements RandomAccess {385Integer[] ia;386387AbstractRandomAccessListImpl(Collection<Integer> c) {388this.ia = c.toArray(new Integer[c.size()]);389}390391@Override392public Integer get(int index) {393return ia[index];394}395396@Override397public int size() {398return ia.length;399}400}401db.addList(AbstractRandomAccessListImpl::new);402403class RandomAccessListImpl implements List<Integer>, RandomAccess {404Integer[] ia;405List<Integer> l;406407RandomAccessListImpl(Collection<Integer> c) {408this.ia = c.toArray(new Integer[c.size()]);409this.l = Arrays.asList(ia);410}411412@Override413public Integer get(int index) {414return ia[index];415}416417@Override418public Integer set(int index, Integer element) {419throw new UnsupportedOperationException();420}421422@Override423public void add(int index, Integer element) {424throw new UnsupportedOperationException();425}426427@Override428public Integer remove(int index) {429throw new UnsupportedOperationException();430}431432@Override433public int indexOf(Object o) {434return l.indexOf(o);435}436437@Override438public int lastIndexOf(Object o) {439return Arrays.asList(ia).lastIndexOf(o);440}441442@Override443public ListIterator<Integer> listIterator() {444return l.listIterator();445}446447@Override448public ListIterator<Integer> listIterator(int index) {449return l.listIterator(index);450}451452@Override453public List<Integer> subList(int fromIndex, int toIndex) {454return l.subList(fromIndex, toIndex);455}456457@Override458public int size() {459return ia.length;460}461462@Override463public boolean isEmpty() {464return size() != 0;465}466467@Override468public boolean contains(Object o) {469return l.contains(o);470}471472@Override473public Iterator<Integer> iterator() {474return l.iterator();475}476477@Override478public Object[] toArray() {479return l.toArray();480}481482@Override483public <T> T[] toArray(T[] a) {484return l.toArray(a);485}486487@Override488public boolean add(Integer integer) {489throw new UnsupportedOperationException();490}491492@Override493public boolean remove(Object o) {494throw new UnsupportedOperationException();495}496497@Override498public boolean containsAll(Collection<?> c) {499return l.containsAll(c);500}501502@Override503public boolean addAll(Collection<? extends Integer> c) {504throw new UnsupportedOperationException();505}506507@Override508public boolean addAll(int index, Collection<? extends Integer> c) {509throw new UnsupportedOperationException();510}511512@Override513public boolean removeAll(Collection<?> c) {514throw new UnsupportedOperationException();515}516517@Override518public boolean retainAll(Collection<?> c) {519throw new UnsupportedOperationException();520}521522@Override523public void clear() {524throw new UnsupportedOperationException();525}526}527db.addList(RandomAccessListImpl::new);528529db.addCollection(HashSet::new);530531db.addCollection(LinkedHashSet::new);532533db.addCollection(TreeSet::new);534535536db.addCollection(c -> { Stack<Integer> s = new Stack<>(); s.addAll(c); return s;});537538db.addCollection(PriorityQueue::new);539540db.addCollection(ArrayDeque::new);541542543db.addCollection(ConcurrentSkipListSet::new);544545if (size > 0) {546db.addCollection(c -> {547ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(size);548abq.addAll(c);549return abq;550});551}552553db.addCollection(PriorityBlockingQueue::new);554555db.addCollection(LinkedBlockingQueue::new);556557db.addCollection(LinkedTransferQueue::new);558559db.addCollection(ConcurrentLinkedQueue::new);560561db.addCollection(LinkedBlockingDeque::new);562563db.addCollection(CopyOnWriteArrayList::new);564565db.addCollection(CopyOnWriteArraySet::new);566567if (size == 0) {568db.addCollection(c -> Collections.<Integer>emptySet());569db.addList(c -> Collections.<Integer>emptyList());570}571else if (size == 1) {572db.addCollection(c -> Collections.singleton(exp.get(0)));573db.addCollection(c -> Collections.singletonList(exp.get(0)));574}575576{577Integer[] ai = new Integer[size];578Arrays.fill(ai, 1);579db.add(String.format("Collections.nCopies(%d, 1)", exp.size()),580Arrays.asList(ai),581() -> Collections.nCopies(exp.size(), 1).spliterator());582}583584// Collections.synchronized/unmodifiable/checked wrappers585db.addCollection(Collections::unmodifiableCollection);586db.addCollection(c -> Collections.unmodifiableSet(new HashSet<>(c)));587db.addCollection(c -> Collections.unmodifiableSortedSet(new TreeSet<>(c)));588db.addList(c -> Collections.unmodifiableList(new ArrayList<>(c)));589db.addMap(Collections::unmodifiableMap);590db.addMap(m -> Collections.unmodifiableSortedMap(new TreeMap<>(m)));591592db.addCollection(Collections::synchronizedCollection);593db.addCollection(c -> Collections.synchronizedSet(new HashSet<>(c)));594db.addCollection(c -> Collections.synchronizedSortedSet(new TreeSet<>(c)));595db.addList(c -> Collections.synchronizedList(new ArrayList<>(c)));596db.addMap(Collections::synchronizedMap);597db.addMap(m -> Collections.synchronizedSortedMap(new TreeMap<>(m)));598599db.addCollection(c -> Collections.checkedCollection(c, Integer.class));600db.addCollection(c -> Collections.checkedQueue(new ArrayDeque<>(c), Integer.class));601db.addCollection(c -> Collections.checkedSet(new HashSet<>(c), Integer.class));602db.addCollection(c -> Collections.checkedSortedSet(new TreeSet<>(c), Integer.class));603db.addList(c -> Collections.checkedList(new ArrayList<>(c), Integer.class));604db.addMap(c -> Collections.checkedMap(c, Integer.class, Integer.class));605db.addMap(m -> Collections.checkedSortedMap(new TreeMap<>(m), Integer.class, Integer.class));606607// Maps608609db.addMap(HashMap::new);610611db.addMap(m -> {612// Create a Map ensuring that for large sizes613// buckets will contain 2 or more entries614HashMap<Integer, Integer> cm = new HashMap<>(1, m.size() + 1);615// Don't use putAll which inflates the table by616// m.size() * loadFactor, thus creating a very sparse617// map for 1000 entries defeating the purpose of this test,618// in addition it will cause the split until null test to fail619// because the number of valid splits is larger than the620// threshold621for (Map.Entry<Integer, Integer> e : m.entrySet())622cm.put(e.getKey(), e.getValue());623return cm;624}, "new java.util.HashMap(1, size + 1)");625626db.addMap(LinkedHashMap::new);627628db.addMap(IdentityHashMap::new);629630db.addMap(WeakHashMap::new);631632db.addMap(m -> {633// Create a Map ensuring that for large sizes634// buckets will be consist of 2 or more entries635WeakHashMap<Integer, Integer> cm = new WeakHashMap<>(1, m.size() + 1);636for (Map.Entry<Integer, Integer> e : m.entrySet())637cm.put(e.getKey(), e.getValue());638return cm;639}, "new java.util.WeakHashMap(1, size + 1)");640641// @@@ Descending maps etc642db.addMap(TreeMap::new);643644db.addMap(ConcurrentHashMap::new);645646db.addMap(ConcurrentSkipListMap::new);647648if (size == 0) {649db.addMap(m -> Collections.<Integer, Integer>emptyMap());650}651else if (size == 1) {652db.addMap(m -> Collections.singletonMap(exp.get(0), exp.get(0)));653}654}655656return spliteratorDataProvider = data.toArray(new Object[0][]);657}658659private static List<Integer> listIntRange(int upTo) {660List<Integer> exp = new ArrayList<>();661for (int i = 0; i < upTo; i++)662exp.add(i);663return Collections.unmodifiableList(exp);664}665666@Test(dataProvider = "Spliterator<Integer>")667public void testNullPointerException(String description, Collection<Integer> exp, Supplier<Spliterator<Integer>> s) {668assertThrowsNPE(() -> s.get().forEachRemaining(null));669assertThrowsNPE(() -> s.get().tryAdvance(null));670}671672@Test(dataProvider = "Spliterator<Integer>")673public void testForEach(String description, Collection<Integer> exp, Supplier<Spliterator<Integer>> s) {674testForEach(exp, s, UnaryOperator.identity());675}676677@Test(dataProvider = "Spliterator<Integer>")678public void testTryAdvance(String description, Collection<Integer> exp, Supplier<Spliterator<Integer>> s) {679testTryAdvance(exp, s, UnaryOperator.identity());680}681682@Test(dataProvider = "Spliterator<Integer>")683public void testMixedTryAdvanceForEach(String description, Collection<Integer> exp, Supplier<Spliterator<Integer>> s) {684testMixedTryAdvanceForEach(exp, s, UnaryOperator.identity());685}686687@Test(dataProvider = "Spliterator<Integer>")688public void testMixedTraverseAndSplit(String description, Collection<Integer> exp, Supplier<Spliterator<Integer>> s) {689testMixedTraverseAndSplit(exp, s, UnaryOperator.identity());690}691692@Test(dataProvider = "Spliterator<Integer>")693public void testSplitAfterFullTraversal(String description, Collection<Integer> exp, Supplier<Spliterator<Integer>> s) {694testSplitAfterFullTraversal(s, UnaryOperator.identity());695}696697@Test(dataProvider = "Spliterator<Integer>")698public void testSplitOnce(String description, Collection<Integer> exp, Supplier<Spliterator<Integer>> s) {699testSplitOnce(exp, s, UnaryOperator.identity());700}701702@Test(dataProvider = "Spliterator<Integer>")703public void testSplitSixDeep(String description, Collection<Integer> exp, Supplier<Spliterator<Integer>> s) {704testSplitSixDeep(exp, s, UnaryOperator.identity());705}706707@Test(dataProvider = "Spliterator<Integer>")708public void testSplitUntilNull(String description, Collection<Integer> exp, Supplier<Spliterator<Integer>> s) {709testSplitUntilNull(exp, s, UnaryOperator.identity());710}711712//713private static class SpliteratorOfIntCharDataBuilder {714List<Object[]> data;715716String s;717718List<Integer> expChars;719720List<Integer> expCodePoints;721722SpliteratorOfIntCharDataBuilder(List<Object[]> data, String s) {723this.data = data;724this.s = s;725this.expChars = transform(s, false);726this.expCodePoints = transform(s, true);727}728729static List<Integer> transform(String s, boolean toCodePoints) {730List<Integer> l = new ArrayList<>();731732if (!toCodePoints) {733for (int i = 0; i < s.length(); i++) {734l.add((int) s.charAt(i));735}736}737else {738for (int i = 0; i < s.length();) {739char c1 = s.charAt(i++);740int cp = c1;741if (Character.isHighSurrogate(c1) && i < s.length()) {742char c2 = s.charAt(i);743if (Character.isLowSurrogate(c2)) {744i++;745cp = Character.toCodePoint(c1, c2);746}747}748l.add(cp);749}750}751return l;752}753754void add(String description, Function<String, CharSequence> f) {755description = description.replace("%s", s);756{757Supplier<Spliterator.OfInt> supplier = () -> f.apply(s).chars().spliterator();758data.add(new Object[]{description + ".chars().spliterator()", expChars, supplier});759}760{761Supplier<Spliterator.OfInt> supplier = () -> f.apply(s).codePoints().spliterator();762data.add(new Object[]{description + ".codePoints().spliterator()", expCodePoints, supplier});763}764}765}766767static Object[][] spliteratorOfIntDataProvider;768769@DataProvider(name = "Spliterator.OfInt")770public static Object[][] spliteratorOfIntDataProvider() {771if (spliteratorOfIntDataProvider != null) {772return spliteratorOfIntDataProvider;773}774775List<Object[]> data = new ArrayList<>();776for (int size : SIZES) {777int exp[] = arrayIntRange(size);778SpliteratorOfIntDataBuilder db = new SpliteratorOfIntDataBuilder(data, listIntRange(size));779780db.add("Spliterators.spliterator(int[], ...)",781() -> Spliterators.spliterator(exp, 0));782783db.add("Arrays.spliterator(int[], ...)",784() -> Arrays.spliterator(exp));785786db.add("Spliterators.spliterator(PrimitiveIterator.OfInt, ...)",787() -> Spliterators.spliterator(Spliterators.iterator(Arrays.spliterator(exp)), exp.length, 0));788789db.add("Spliterators.spliteratorUnknownSize(PrimitiveIterator.OfInt, ...)",790() -> Spliterators.spliteratorUnknownSize(Spliterators.iterator(Arrays.spliterator(exp)), 0));791792class IntSpliteratorFromArray extends Spliterators.AbstractIntSpliterator {793int[] a;794int index = 0;795796IntSpliteratorFromArray(int[] a) {797super(a.length, Spliterator.SIZED);798this.a = a;799}800801@Override802public boolean tryAdvance(IntConsumer action) {803if (action == null)804throw new NullPointerException();805if (index < a.length) {806action.accept(a[index++]);807return true;808}809else {810return false;811}812}813}814db.add("new Spliterators.AbstractIntAdvancingSpliterator()",815() -> new IntSpliteratorFromArray(exp));816}817818// Class for testing default methods819class CharSequenceImpl implements CharSequence {820final String s;821822public CharSequenceImpl(String s) {823this.s = s;824}825826@Override827public int length() {828return s.length();829}830831@Override832public char charAt(int index) {833return s.charAt(index);834}835836@Override837public CharSequence subSequence(int start, int end) {838return s.subSequence(start, end);839}840841@Override842public String toString() {843return s;844}845}846847for (String string : STRINGS) {848SpliteratorOfIntCharDataBuilder cdb = new SpliteratorOfIntCharDataBuilder(data, string);849cdb.add("\"%s\"", s -> s);850cdb.add("new CharSequenceImpl(\"%s\")", CharSequenceImpl::new);851cdb.add("new StringBuilder(\"%s\")", StringBuilder::new);852cdb.add("new StringBuffer(\"%s\")", StringBuffer::new);853cdb.add("CharBuffer.wrap(\"%s\".toCharArray())", s -> CharBuffer.wrap(s.toCharArray()));854}855856return spliteratorOfIntDataProvider = data.toArray(new Object[0][]);857}858859private static int[] arrayIntRange(int upTo) {860int[] exp = new int[upTo];861for (int i = 0; i < upTo; i++)862exp[i] = i;863return exp;864}865866@Test(dataProvider = "Spliterator.OfInt")867public void testIntNullPointerException(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {868assertThrowsNPE(() -> s.get().forEachRemaining((IntConsumer) null));869assertThrowsNPE(() -> s.get().tryAdvance((IntConsumer) null));870}871872@Test(dataProvider = "Spliterator.OfInt")873public void testIntForEach(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {874testForEach(exp, s, intBoxingConsumer());875}876877@Test(dataProvider = "Spliterator.OfInt")878public void testIntTryAdvance(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {879testTryAdvance(exp, s, intBoxingConsumer());880}881882@Test(dataProvider = "Spliterator.OfInt")883public void testIntMixedTryAdvanceForEach(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {884testMixedTryAdvanceForEach(exp, s, intBoxingConsumer());885}886887@Test(dataProvider = "Spliterator.OfInt")888public void testIntMixedTraverseAndSplit(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {889testMixedTraverseAndSplit(exp, s, intBoxingConsumer());890}891892@Test(dataProvider = "Spliterator.OfInt")893public void testIntSplitAfterFullTraversal(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {894testSplitAfterFullTraversal(s, intBoxingConsumer());895}896897@Test(dataProvider = "Spliterator.OfInt")898public void testIntSplitOnce(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {899testSplitOnce(exp, s, intBoxingConsumer());900}901902@Test(dataProvider = "Spliterator.OfInt")903public void testIntSplitSixDeep(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {904testSplitSixDeep(exp, s, intBoxingConsumer());905}906907@Test(dataProvider = "Spliterator.OfInt")908public void testIntSplitUntilNull(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {909testSplitUntilNull(exp, s, intBoxingConsumer());910}911912//913914private static class SpliteratorOfLongDataBuilder {915List<Object[]> data;916917List<Long> exp;918919SpliteratorOfLongDataBuilder(List<Object[]> data, List<Long> exp) {920this.data = data;921this.exp = exp;922}923924void add(String description, List<Long> expected, Supplier<Spliterator.OfLong> s) {925description = joiner(description).toString();926data.add(new Object[]{description, expected, s});927}928929void add(String description, Supplier<Spliterator.OfLong> s) {930add(description, exp, s);931}932933StringBuilder joiner(String description) {934return new StringBuilder(description).935append(" {").936append("size=").append(exp.size()).937append("}");938}939}940941static Object[][] spliteratorOfLongDataProvider;942943@DataProvider(name = "Spliterator.OfLong")944public static Object[][] spliteratorOfLongDataProvider() {945if (spliteratorOfLongDataProvider != null) {946return spliteratorOfLongDataProvider;947}948949List<Object[]> data = new ArrayList<>();950for (int size : SIZES) {951long exp[] = arrayLongRange(size);952SpliteratorOfLongDataBuilder db = new SpliteratorOfLongDataBuilder(data, listLongRange(size));953954db.add("Spliterators.spliterator(long[], ...)",955() -> Spliterators.spliterator(exp, 0));956957db.add("Arrays.spliterator(long[], ...)",958() -> Arrays.spliterator(exp));959960db.add("Spliterators.spliterator(PrimitiveIterator.OfLong, ...)",961() -> Spliterators.spliterator(Spliterators.iterator(Arrays.spliterator(exp)), exp.length, 0));962963db.add("Spliterators.spliteratorUnknownSize(PrimitiveIterator.OfLong, ...)",964() -> Spliterators.spliteratorUnknownSize(Spliterators.iterator(Arrays.spliterator(exp)), 0));965966class LongSpliteratorFromArray extends Spliterators.AbstractLongSpliterator {967long[] a;968int index = 0;969970LongSpliteratorFromArray(long[] a) {971super(a.length, Spliterator.SIZED);972this.a = a;973}974975@Override976public boolean tryAdvance(LongConsumer action) {977if (action == null)978throw new NullPointerException();979if (index < a.length) {980action.accept(a[index++]);981return true;982}983else {984return false;985}986}987}988db.add("new Spliterators.AbstractLongAdvancingSpliterator()",989() -> new LongSpliteratorFromArray(exp));990}991992return spliteratorOfLongDataProvider = data.toArray(new Object[0][]);993}994995private static List<Long> listLongRange(int upTo) {996List<Long> exp = new ArrayList<>();997for (long i = 0; i < upTo; i++)998exp.add(i);999return Collections.unmodifiableList(exp);1000}10011002private static long[] arrayLongRange(int upTo) {1003long[] exp = new long[upTo];1004for (int i = 0; i < upTo; i++)1005exp[i] = i;1006return exp;1007}10081009@Test(dataProvider = "Spliterator.OfLong")1010public void testLongNullPointerException(String description, Collection<Long> exp, Supplier<Spliterator.OfLong> s) {1011assertThrowsNPE(() -> s.get().forEachRemaining((LongConsumer) null));1012assertThrowsNPE(() -> s.get().tryAdvance((LongConsumer) null));1013}10141015@Test(dataProvider = "Spliterator.OfLong")1016public void testLongForEach(String description, Collection<Long> exp, Supplier<Spliterator.OfLong> s) {1017testForEach(exp, s, longBoxingConsumer());1018}10191020@Test(dataProvider = "Spliterator.OfLong")1021public void testLongTryAdvance(String description, Collection<Long> exp, Supplier<Spliterator.OfLong> s) {1022testTryAdvance(exp, s, longBoxingConsumer());1023}10241025@Test(dataProvider = "Spliterator.OfLong")1026public void testLongMixedTryAdvanceForEach(String description, Collection<Long> exp, Supplier<Spliterator.OfLong> s) {1027testMixedTryAdvanceForEach(exp, s, longBoxingConsumer());1028}10291030@Test(dataProvider = "Spliterator.OfLong")1031public void testLongMixedTraverseAndSplit(String description, Collection<Long> exp, Supplier<Spliterator.OfLong> s) {1032testMixedTraverseAndSplit(exp, s, longBoxingConsumer());1033}10341035@Test(dataProvider = "Spliterator.OfLong")1036public void testLongSplitAfterFullTraversal(String description, Collection<Long> exp, Supplier<Spliterator.OfLong> s) {1037testSplitAfterFullTraversal(s, longBoxingConsumer());1038}10391040@Test(dataProvider = "Spliterator.OfLong")1041public void testLongSplitOnce(String description, Collection<Long> exp, Supplier<Spliterator.OfLong> s) {1042testSplitOnce(exp, s, longBoxingConsumer());1043}10441045@Test(dataProvider = "Spliterator.OfLong")1046public void testLongSplitSixDeep(String description, Collection<Long> exp, Supplier<Spliterator.OfLong> s) {1047testSplitSixDeep(exp, s, longBoxingConsumer());1048}10491050@Test(dataProvider = "Spliterator.OfLong")1051public void testLongSplitUntilNull(String description, Collection<Long> exp, Supplier<Spliterator.OfLong> s) {1052testSplitUntilNull(exp, s, longBoxingConsumer());1053}10541055//10561057private static class SpliteratorOfDoubleDataBuilder {1058List<Object[]> data;10591060List<Double> exp;10611062SpliteratorOfDoubleDataBuilder(List<Object[]> data, List<Double> exp) {1063this.data = data;1064this.exp = exp;1065}10661067void add(String description, List<Double> expected, Supplier<Spliterator.OfDouble> s) {1068description = joiner(description).toString();1069data.add(new Object[]{description, expected, s});1070}10711072void add(String description, Supplier<Spliterator.OfDouble> s) {1073add(description, exp, s);1074}10751076StringBuilder joiner(String description) {1077return new StringBuilder(description).1078append(" {").1079append("size=").append(exp.size()).1080append("}");1081}1082}10831084static Object[][] spliteratorOfDoubleDataProvider;10851086@DataProvider(name = "Spliterator.OfDouble")1087public static Object[][] spliteratorOfDoubleDataProvider() {1088if (spliteratorOfDoubleDataProvider != null) {1089return spliteratorOfDoubleDataProvider;1090}10911092List<Object[]> data = new ArrayList<>();1093for (int size : SIZES) {1094double exp[] = arrayDoubleRange(size);1095SpliteratorOfDoubleDataBuilder db = new SpliteratorOfDoubleDataBuilder(data, listDoubleRange(size));10961097db.add("Spliterators.spliterator(double[], ...)",1098() -> Spliterators.spliterator(exp, 0));10991100db.add("Arrays.spliterator(double[], ...)",1101() -> Arrays.spliterator(exp));11021103db.add("Spliterators.spliterator(PrimitiveIterator.OfDouble, ...)",1104() -> Spliterators.spliterator(Spliterators.iterator(Arrays.spliterator(exp)), exp.length, 0));11051106db.add("Spliterators.spliteratorUnknownSize(PrimitiveIterator.OfDouble, ...)",1107() -> Spliterators.spliteratorUnknownSize(Spliterators.iterator(Arrays.spliterator(exp)), 0));11081109class DoubleSpliteratorFromArray extends Spliterators.AbstractDoubleSpliterator {1110double[] a;1111int index = 0;11121113DoubleSpliteratorFromArray(double[] a) {1114super(a.length, Spliterator.SIZED);1115this.a = a;1116}11171118@Override1119public boolean tryAdvance(DoubleConsumer action) {1120if (action == null)1121throw new NullPointerException();1122if (index < a.length) {1123action.accept(a[index++]);1124return true;1125}1126else {1127return false;1128}1129}1130}1131db.add("new Spliterators.AbstractDoubleAdvancingSpliterator()",1132() -> new DoubleSpliteratorFromArray(exp));1133}11341135return spliteratorOfDoubleDataProvider = data.toArray(new Object[0][]);1136}11371138private static List<Double> listDoubleRange(int upTo) {1139List<Double> exp = new ArrayList<>();1140for (double i = 0; i < upTo; i++)1141exp.add(i);1142return Collections.unmodifiableList(exp);1143}11441145private static double[] arrayDoubleRange(int upTo) {1146double[] exp = new double[upTo];1147for (int i = 0; i < upTo; i++)1148exp[i] = i;1149return exp;1150}11511152@Test(dataProvider = "Spliterator.OfDouble")1153public void testDoubleNullPointerException(String description, Collection<Double> exp, Supplier<Spliterator.OfDouble> s) {1154assertThrowsNPE(() -> s.get().forEachRemaining((DoubleConsumer) null));1155assertThrowsNPE(() -> s.get().tryAdvance((DoubleConsumer) null));1156}11571158@Test(dataProvider = "Spliterator.OfDouble")1159public void testDoubleForEach(String description, Collection<Double> exp, Supplier<Spliterator.OfDouble> s) {1160testForEach(exp, s, doubleBoxingConsumer());1161}11621163@Test(dataProvider = "Spliterator.OfDouble")1164public void testDoubleTryAdvance(String description, Collection<Double> exp, Supplier<Spliterator.OfDouble> s) {1165testTryAdvance(exp, s, doubleBoxingConsumer());1166}11671168@Test(dataProvider = "Spliterator.OfDouble")1169public void testDoubleMixedTryAdvanceForEach(String description, Collection<Double> exp, Supplier<Spliterator.OfDouble> s) {1170testMixedTryAdvanceForEach(exp, s, doubleBoxingConsumer());1171}11721173@Test(dataProvider = "Spliterator.OfDouble")1174public void testDoubleMixedTraverseAndSplit(String description, Collection<Double> exp, Supplier<Spliterator.OfDouble> s) {1175testMixedTraverseAndSplit(exp, s, doubleBoxingConsumer());1176}11771178@Test(dataProvider = "Spliterator.OfDouble")1179public void testDoubleSplitAfterFullTraversal(String description, Collection<Double> exp, Supplier<Spliterator.OfDouble> s) {1180testSplitAfterFullTraversal(s, doubleBoxingConsumer());1181}11821183@Test(dataProvider = "Spliterator.OfDouble")1184public void testDoubleSplitOnce(String description, Collection<Double> exp, Supplier<Spliterator.OfDouble> s) {1185testSplitOnce(exp, s, doubleBoxingConsumer());1186}11871188@Test(dataProvider = "Spliterator.OfDouble")1189public void testDoubleSplitSixDeep(String description, Collection<Double> exp, Supplier<Spliterator.OfDouble> s) {1190testSplitSixDeep(exp, s, doubleBoxingConsumer());1191}11921193@Test(dataProvider = "Spliterator.OfDouble")1194public void testDoubleSplitUntilNull(String description, Collection<Double> exp, Supplier<Spliterator.OfDouble> s) {1195testSplitUntilNull(exp, s, doubleBoxingConsumer());1196}11971198}119912001201