1 /*
  2  * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 import org.testng.annotations.DataProvider;
 25 import org.testng.annotations.Test;
 26 
 27 import java.nio.CharBuffer;
 28 import java.util.ArrayDeque;
 29 import java.util.ArrayList;
 30 import java.util.Arrays;
 31 import java.util.BitSet;
 32 import java.util.HashMap;
 33 import java.util.HashSet;
 34 import java.util.IdentityHashMap;
 35 import java.util.LinkedHashMap;
 36 import java.util.LinkedHashSet;
 37 import java.util.LinkedList;
 38 import java.util.List;
 39 import java.util.PriorityQueue;
 40 import java.util.Set;
 41 import java.util.Spliterator;
 42 import java.util.Stack;
 43 import java.util.TreeMap;
 44 import java.util.TreeSet;
 45 import java.util.Vector;
 46 import java.util.WeakHashMap;
 47 import java.util.function.Function;
 48 import java.util.function.Supplier;
 49 import java.util.stream.Stream;
 50 
 51 import static org.testng.Assert.assertEquals;
 52 
 53 /**
 54  * @test
 55  * @bug 8148748 8170155 8336672
 56  * @summary Spliterator last-binding tests
 57  * @run testng SpliteratorLateBindingTest
 58  */
 59 
 60 @Test
 61 public class SpliteratorLateBindingTest extends SpliteratorLateBindingFailFastHelper {
 62 
 63     static Object[][] spliteratorDataProvider;
 64 
 65     @DataProvider(name = "Source")
 66     public static Object[][] sourceDataProvider() {
 67         if (spliteratorDataProvider != null) {
 68             return spliteratorDataProvider;
 69         }
 70 
 71         List<Object[]> data = new ArrayList<>();
 72         SpliteratorDataBuilder<String> db =
 73                 new SpliteratorDataBuilder<>(data, "Z", Arrays.asList("A", "B", "C", "D"));
 74 
 75         // Collections
 76 
 77         db.addList(ArrayList::new);
 78 
 79         db.addList(LinkedList::new);
 80 
 81         db.addList(Vector::new);
 82 
 83         db.addList(AbstractRandomAccessListImpl::new);
 84 
 85         db.addCollection(HashSet::new);
 86 
 87         db.addCollection(LinkedHashSet::new);
 88 
 89         db.addCollection(TreeSet::new);
 90 
 91         db.addCollection(c -> {
 92             Stack<String> s = new Stack<>();
 93             s.addAll(c);
 94             return s;
 95         });
 96 
 97         db.addCollection(PriorityQueue::new);
 98 
 99         db.addCollection(ArrayDeque::new);
100 
101         // Maps
102 
103         db.addMap(HashMap::new);
104 
105         db.addMap(LinkedHashMap::new);
106 
107         db.addMap(IdentityHashMap::new);
108 
109         // BUG: Assumes identity
110         db.addMap(WeakHashMap::new);
111 
112         // @@@  Descending maps etc
113         db.addMap(TreeMap::new);
114 
115         // BitSet
116 
117         // BUG: Assumes identity in WeakHashMap
118         List<Integer> bits = List.of(0, 1, 2);
119         Function<BitSet, Spliterator.OfInt> bitsSource = bs -> bs.stream().spliterator();
120         db.add("new BitSet.stream().spliterator() ADD",
121                () -> new IntSource<>(toBitSet(bits), bitsSource, bs -> bs.set(3)));
122         db.add("new BitSet.stream().spliterator() REMOVE",
123                () -> new IntSource<>(toBitSet(bits), bitsSource, bs -> bs.clear(2)));
124 
125         // CharSequence
126 
127         Function<CharSequence, Spliterator.OfInt> charsSource = sb -> sb.chars().spliterator();
128         Function<CharSequence, Spliterator.OfInt> pointsSource = sb -> sb.codePoints().spliterator();
129 
130         db.add("new StringBuilder.chars().spliterator() ADD",
131                () -> new IntSource<>(new StringBuilder("ABC"), charsSource, bs -> bs.append("D"), true));
132         db.add("new StringBuilder.chars().spliterator() REMOVE",
133                () -> new IntSource<>(new StringBuilder("ABC"), charsSource, bs -> bs.deleteCharAt(2), true));
134         db.add("new StringBuilder.codePoints().spliterator() ADD",
135                () -> new IntSource<>(new StringBuilder("ABC"), pointsSource, bs -> bs.append("D"), true));
136         db.add("new StringBuilder.codePoints().spliterator() REMOVE",
137                () -> new IntSource<>(new StringBuilder("ABC"), pointsSource, bs -> bs.deleteCharAt(2), true));
138 
139         db.add("new StringBuffer.chars().spliterator() ADD",
140                () -> new IntSource<>(new StringBuffer("ABC"), charsSource, bs -> bs.append("D"), true));
141         db.add("new StringBuffer.chars().spliterator() REMOVE",
142                () -> new IntSource<>(new StringBuffer("ABC"), charsSource, bs -> bs.deleteCharAt(2), true));
143         db.add("new StringBuffer.codePoints().spliterator() ADD",
144                () -> new IntSource<>(new StringBuffer("ABC"), pointsSource, bs -> bs.append("D"), true));
145         db.add("new StringBuffer.codePoints().spliterator() REMOVE",
146                () -> new IntSource<>(new StringBuffer("ABC"), pointsSource, bs -> bs.deleteCharAt(2), true));
147 
148         db.add("CharBuffer.wrap().chars().spliterator() ADD",
149                () -> new IntSource<>(CharBuffer.wrap("ABCD").limit(3), charsSource, bs -> bs.limit(4), true));
150         db.add("CharBuffer.wrap().chars().spliterator() REMOVE",
151                () -> new IntSource<>(CharBuffer.wrap("ABCD"), charsSource, bs -> bs.limit(3), true));
152         db.add("CharBuffer.wrap().codePoints().spliterator() ADD",
153                () -> new IntSource<>(CharBuffer.wrap("ABCD").limit(3), pointsSource, bs -> bs.limit(4), true));
154         db.add("CharBuffer.wrap().codePoints().spliterator() REMOVE",
155                () -> new IntSource<>(CharBuffer.wrap("ABCD"), pointsSource, bs -> bs.limit(3), true));
156 
157         return spliteratorDataProvider = data.toArray(new Object[0][]);
158     }
159 
160 
161     @DataProvider(name = "Source.Non.Binding.Characteristics")
162     public static Object[][] sourceCharacteristicsDataProvider() {
163         return Stream.of(sourceDataProvider()).filter(tc -> {
164             @SuppressWarnings("unchecked")
165             Supplier<Source<?>> s = (Supplier<Source<?>>) tc[1];
166             return !s.get().bindOnCharacteristics();
167         }).toArray(Object[][]::new);
168     }
169 
170     static BitSet toBitSet(List<Integer> bits) {
171         BitSet bs = new BitSet();
172         bits.forEach(bs::set);
173         return bs;
174     }
175 
176 
177     @Test(dataProvider = "Source")
178     public <T> void testForEach(String description, Supplier<Source<T>> ss) {
179         Source<T> source = ss.get();
180         Spliterator<T> s = source.spliterator();
181 
182         source.update();
183 
184         Set<T> a = new HashSet<>();
185         s.forEachRemaining(a::add);
186 
187         Set<T> e = new HashSet<>();
188         source.spliterator().forEachRemaining(e::add);
189         assertEquals(a, e);
190     }
191 
192     @Test(dataProvider = "Source")
193     public <T> void testTryAdvance(String description, Supplier<Source<T>> ss) {
194         Source<T> source = ss.get();
195         Spliterator<T> s = source.spliterator();
196 
197         source.update();
198 
199         Set<T> a = new HashSet<>();
200         while (s.tryAdvance(a::add)) {
201         }
202 
203         Set<T> e = new HashSet<>();
204         source.spliterator().forEachRemaining(e::add);
205         assertEquals(a, e);
206     }
207 
208     @Test(dataProvider = "Source.Non.Binding.Characteristics")
209     public <T> void testCharacteristics(String description, Supplier<Source<T>> ss) {
210         Source<T> source = ss.get();
211         Spliterator<T> s = source.spliterator();
212 
213         s.characteristics();
214         source.update();
215 
216         Set<T> a = new HashSet<>();
217         s.forEachRemaining(a::add);
218 
219         Set<T> e = new HashSet<>();
220         source.spliterator().forEachRemaining(e::add);
221         assertEquals(a, e);
222     }
223 }