< prev index next >

test/jdk/java/util/Spliterator/SpliteratorCharacteristics.java

Print this page

  1 /*
  2  * Copyright (c) 2013, 2015, 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 /**
 25  * @test
 26  * @bug 8020156 8020009 8022326 8012913 8024405 8024408 8071477
 27  * @run testng SpliteratorCharacteristics
 28  */
 29 
 30 import org.testng.annotations.Test;
 31 
 32 import java.util.Arrays;
 33 import java.util.Collection;
 34 import java.util.Comparator;
 35 import java.util.HashMap;
 36 import java.util.HashSet;
 37 import java.util.LinkedHashMap;
 38 import java.util.LinkedHashSet;
 39 import java.util.List;
 40 import java.util.Map;
 41 import java.util.PrimitiveIterator;
 42 import java.util.Set;
 43 import java.util.SortedMap;
 44 import java.util.SortedSet;
 45 import java.util.Spliterator;
 46 import java.util.Spliterators;

 94                                  Spliterator.SIZED | Spliterator.SUBSIZED);
 95         assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
 96         s = cs.codePoints().spliterator();
 97         assertCharacteristics(s, Spliterator.IMMUTABLE | Spliterator.ORDERED);
 98         assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
 99 
100         for (CharSequence c : Arrays.asList(new CharSequenceImpl("A"),
101                                              new StringBuilder("A"),
102                                              new StringBuffer("A"))) {
103             s = cs.chars().spliterator();
104             assertCharacteristics(s, Spliterator.ORDERED |
105                                      Spliterator.SIZED | Spliterator.SUBSIZED);
106             assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
107             s = cs.codePoints().spliterator();
108             assertCharacteristics(s, Spliterator.ORDERED);
109             assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
110         }
111     }
112 
113     public void testSpliteratorFromCollection() {
114         List<Integer> l = Arrays.asList(1, 2, 3, 4);
115 
116         {
117             Spliterator<?> s = Spliterators.spliterator(l, 0);
118             assertCharacteristics(s, Spliterator.SIZED | Spliterator.SUBSIZED);
119             assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
120         }
121 
122         {
123             Spliterator<?> s = Spliterators.spliterator(l, Spliterator.CONCURRENT);
124             assertHasNotCharacteristics(s, Spliterator.SIZED | Spliterator.SUBSIZED);
125             assertCharacteristics(s, Spliterator.CONCURRENT);
126         }
127 
128         {
129             Spliterator<?> s = Spliterators.spliterator(l.iterator(), 1, 0);
130             assertCharacteristics(s, Spliterator.SIZED | Spliterator.SUBSIZED);
131             assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
132         }
133 
134         {

274     public void testTreeSet() {
275         assertSortedSetCharacteristics(new TreeSet<>(),
276                                        Spliterator.SIZED | Spliterator.DISTINCT |
277                                        Spliterator.SORTED | Spliterator.ORDERED);
278     }
279 
280     public void testTreeSetWithComparator() {
281         assertSortedSetCharacteristics(new TreeSet<>(Comparator.reverseOrder()),
282                                        Spliterator.SIZED | Spliterator.DISTINCT |
283                                        Spliterator.SORTED | Spliterator.ORDERED);
284     }
285 
286     public void testConcurrentSkipListMap() {
287         assertSortedMapCharacteristics(new ConcurrentSkipListMap<>(),
288                                        Spliterator.CONCURRENT | Spliterator.NONNULL |
289                                        Spliterator.DISTINCT | Spliterator.SORTED |
290                                        Spliterator.ORDERED);
291     }
292 
293     public void testConcurrentSkipListMapWithComparator() {
294         assertSortedMapCharacteristics(new ConcurrentSkipListMap<>(Comparator.<Integer>reverseOrder()),
295                                        Spliterator.CONCURRENT | Spliterator.NONNULL |
296                                        Spliterator.DISTINCT | Spliterator.SORTED |
297                                        Spliterator.ORDERED);
298     }
299 
300     public void testConcurrentSkipListSet() {
301         assertSortedSetCharacteristics(new ConcurrentSkipListSet<>(),
302                                        Spliterator.CONCURRENT | Spliterator.NONNULL |
303                                        Spliterator.DISTINCT | Spliterator.SORTED |
304                                        Spliterator.ORDERED);
305     }
306 
307     public void testConcurrentSkipListSetWithComparator() {
308         assertSortedSetCharacteristics(new ConcurrentSkipListSet<>(Comparator.reverseOrder()),
309                                        Spliterator.CONCURRENT | Spliterator.NONNULL |
310                                        Spliterator.DISTINCT | Spliterator.SORTED |
311                                        Spliterator.ORDERED);
312     }
313 
314 
315     //
316 
317 
318     void assertMapCharacteristics(Map<Integer, String> m, int keyCharacteristics) {
319         assertMapCharacteristics(m, keyCharacteristics, 0);
320     }
321 
322     void assertMapCharacteristics(Map<Integer, String> m, int keyCharacteristics, int notValueCharacteristics) {
323         initMap(m);
324 
325         assertCharacteristics(m.keySet(), keyCharacteristics);
326 
327         assertCharacteristics(m.values(),
328                               keyCharacteristics & ~(Spliterator.DISTINCT | notValueCharacteristics));
329 
330         assertCharacteristics(m.entrySet(), keyCharacteristics);
331 
332         if ((keyCharacteristics & Spliterator.SORTED) == 0) {
333             assertISEComparator(m.keySet());
334             assertISEComparator(m.values());
335             assertISEComparator(m.entrySet());
336         }
337     }
338 
339     void assertSetCharacteristics(Set<Integer> s, int keyCharacteristics) {
340         initSet(s);
341 
342         assertCharacteristics(s, keyCharacteristics);
343 
344         if ((keyCharacteristics & Spliterator.SORTED) == 0) {
345             assertISEComparator(s);
346         }
347     }
348 
349     void assertSortedMapCharacteristics(SortedMap<Integer, String> m, int keyCharacteristics) {
350         assertMapCharacteristics(m, keyCharacteristics, Spliterator.SORTED);
351 
352         Set<Integer> keys = m.keySet();
353         if (m.comparator() != null) {
354             assertNotNullComparator(keys);
355         }
356         else {
357             assertNullComparator(keys);
358         }
359 
360         assertISEComparator(m.values());
361 
362         assertNotNullComparator(m.entrySet());
363     }
364 
365     void assertSortedSetCharacteristics(SortedSet<Integer> s, int keyCharacteristics) {
366         assertSetCharacteristics(s, keyCharacteristics);
367 
368         if (s.comparator() != null) {
369             assertNotNullComparator(s);
370         }
371         else {
372             assertNullComparator(s);
373         }
374     }
375 
376     void initMap(Map<Integer, String> m) {
377         m.put(1, "4");
378         m.put(2, "3");
379         m.put(3, "2");
380         m.put(4, "1");
381     }
382 
383     void initSet(Set<Integer> s) {
384         s.addAll(Arrays.asList(1, 2, 3, 4));
385     }
386 
387     void assertCharacteristics(Collection<?> c, int expectedCharacteristics) {
388         assertCharacteristics(c.spliterator(), expectedCharacteristics);
389     }
390 
391     void assertCharacteristics(Spliterator<?> s, int expectedCharacteristics) {
392         assertTrue(s.hasCharacteristics(expectedCharacteristics),
393                    "Spliterator characteristics");
394     }
395 
396     void assertHasNotCharacteristics(Spliterator<?> s, int expectedCharacteristics) {
397         assertFalse(s.hasCharacteristics(expectedCharacteristics),
398                     "Spliterator characteristics");
399     }
400 
401     void assertNullComparator(Collection<?> c) {
402         assertNull(c.spliterator().getComparator(),
403                    "Comparator of Spliterator of Collection");
404     }

  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 /**
 25  * @test
 26  * @bug 8020156 8020009 8022326 8012913 8024405 8024408 8071477 8336672
 27  * @run testng SpliteratorCharacteristics
 28  */
 29 
 30 import org.testng.annotations.Test;
 31 
 32 import java.util.Arrays;
 33 import java.util.Collection;
 34 import java.util.Comparator;
 35 import java.util.HashMap;
 36 import java.util.HashSet;
 37 import java.util.LinkedHashMap;
 38 import java.util.LinkedHashSet;
 39 import java.util.List;
 40 import java.util.Map;
 41 import java.util.PrimitiveIterator;
 42 import java.util.Set;
 43 import java.util.SortedMap;
 44 import java.util.SortedSet;
 45 import java.util.Spliterator;
 46 import java.util.Spliterators;

 94                                  Spliterator.SIZED | Spliterator.SUBSIZED);
 95         assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
 96         s = cs.codePoints().spliterator();
 97         assertCharacteristics(s, Spliterator.IMMUTABLE | Spliterator.ORDERED);
 98         assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
 99 
100         for (CharSequence c : Arrays.asList(new CharSequenceImpl("A"),
101                                              new StringBuilder("A"),
102                                              new StringBuffer("A"))) {
103             s = cs.chars().spliterator();
104             assertCharacteristics(s, Spliterator.ORDERED |
105                                      Spliterator.SIZED | Spliterator.SUBSIZED);
106             assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
107             s = cs.codePoints().spliterator();
108             assertCharacteristics(s, Spliterator.ORDERED);
109             assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
110         }
111     }
112 
113     public void testSpliteratorFromCollection() {
114         List<String> l = Arrays.asList("A", "B", "C", "D");
115 
116         {
117             Spliterator<?> s = Spliterators.spliterator(l, 0);
118             assertCharacteristics(s, Spliterator.SIZED | Spliterator.SUBSIZED);
119             assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
120         }
121 
122         {
123             Spliterator<?> s = Spliterators.spliterator(l, Spliterator.CONCURRENT);
124             assertHasNotCharacteristics(s, Spliterator.SIZED | Spliterator.SUBSIZED);
125             assertCharacteristics(s, Spliterator.CONCURRENT);
126         }
127 
128         {
129             Spliterator<?> s = Spliterators.spliterator(l.iterator(), 1, 0);
130             assertCharacteristics(s, Spliterator.SIZED | Spliterator.SUBSIZED);
131             assertHasNotCharacteristics(s, Spliterator.CONCURRENT);
132         }
133 
134         {

274     public void testTreeSet() {
275         assertSortedSetCharacteristics(new TreeSet<>(),
276                                        Spliterator.SIZED | Spliterator.DISTINCT |
277                                        Spliterator.SORTED | Spliterator.ORDERED);
278     }
279 
280     public void testTreeSetWithComparator() {
281         assertSortedSetCharacteristics(new TreeSet<>(Comparator.reverseOrder()),
282                                        Spliterator.SIZED | Spliterator.DISTINCT |
283                                        Spliterator.SORTED | Spliterator.ORDERED);
284     }
285 
286     public void testConcurrentSkipListMap() {
287         assertSortedMapCharacteristics(new ConcurrentSkipListMap<>(),
288                                        Spliterator.CONCURRENT | Spliterator.NONNULL |
289                                        Spliterator.DISTINCT | Spliterator.SORTED |
290                                        Spliterator.ORDERED);
291     }
292 
293     public void testConcurrentSkipListMapWithComparator() {
294         assertSortedMapCharacteristics(new ConcurrentSkipListMap<>(Comparator.<String>reverseOrder()),
295                                        Spliterator.CONCURRENT | Spliterator.NONNULL |
296                                        Spliterator.DISTINCT | Spliterator.SORTED |
297                                        Spliterator.ORDERED);
298     }
299 
300     public void testConcurrentSkipListSet() {
301         assertSortedSetCharacteristics(new ConcurrentSkipListSet<>(),
302                                        Spliterator.CONCURRENT | Spliterator.NONNULL |
303                                        Spliterator.DISTINCT | Spliterator.SORTED |
304                                        Spliterator.ORDERED);
305     }
306 
307     public void testConcurrentSkipListSetWithComparator() {
308         assertSortedSetCharacteristics(new ConcurrentSkipListSet<>(Comparator.reverseOrder()),
309                                        Spliterator.CONCURRENT | Spliterator.NONNULL |
310                                        Spliterator.DISTINCT | Spliterator.SORTED |
311                                        Spliterator.ORDERED);
312     }
313 
314 
315     //
316 
317 
318     void assertMapCharacteristics(Map<String, String> m, int keyCharacteristics) {
319         assertMapCharacteristics(m, keyCharacteristics, 0);
320     }
321 
322     void assertMapCharacteristics(Map<String, String> m, int keyCharacteristics, int notValueCharacteristics) {
323         initMap(m);
324 
325         assertCharacteristics(m.keySet(), keyCharacteristics);
326 
327         assertCharacteristics(m.values(),
328                               keyCharacteristics & ~(Spliterator.DISTINCT | notValueCharacteristics));
329 
330         assertCharacteristics(m.entrySet(), keyCharacteristics);
331 
332         if ((keyCharacteristics & Spliterator.SORTED) == 0) {
333             assertISEComparator(m.keySet());
334             assertISEComparator(m.values());
335             assertISEComparator(m.entrySet());
336         }
337     }
338 
339     void assertSetCharacteristics(Set<String> s, int keyCharacteristics) {
340         initSet(s);
341 
342         assertCharacteristics(s, keyCharacteristics);
343 
344         if ((keyCharacteristics & Spliterator.SORTED) == 0) {
345             assertISEComparator(s);
346         }
347     }
348 
349     void assertSortedMapCharacteristics(SortedMap<String, String> m, int keyCharacteristics) {
350         assertMapCharacteristics(m, keyCharacteristics, Spliterator.SORTED);
351 
352         Set<String> keys = m.keySet();
353         if (m.comparator() != null) {
354             assertNotNullComparator(keys);
355         }
356         else {
357             assertNullComparator(keys);
358         }
359 
360         assertISEComparator(m.values());
361 
362         assertNotNullComparator(m.entrySet());
363     }
364 
365     void assertSortedSetCharacteristics(SortedSet<String> s, int keyCharacteristics) {
366         assertSetCharacteristics(s, keyCharacteristics);
367 
368         if (s.comparator() != null) {
369             assertNotNullComparator(s);
370         }
371         else {
372             assertNullComparator(s);
373         }
374     }
375 
376     void initMap(Map<String, String> m) {
377         m.put("A", "4");
378         m.put("B", "3");
379         m.put("C", "2");
380         m.put("D", "1");
381     }
382 
383     void initSet(Set<String> s) {
384         s.addAll(Arrays.asList("A", "B", "C", "D"));
385     }
386 
387     void assertCharacteristics(Collection<?> c, int expectedCharacteristics) {
388         assertCharacteristics(c.spliterator(), expectedCharacteristics);
389     }
390 
391     void assertCharacteristics(Spliterator<?> s, int expectedCharacteristics) {
392         assertTrue(s.hasCharacteristics(expectedCharacteristics),
393                    "Spliterator characteristics");
394     }
395 
396     void assertHasNotCharacteristics(Spliterator<?> s, int expectedCharacteristics) {
397         assertFalse(s.hasCharacteristics(expectedCharacteristics),
398                     "Spliterator characteristics");
399     }
400 
401     void assertNullComparator(Collection<?> c) {
402         assertNull(c.spliterator().getComparator(),
403                    "Comparator of Spliterator of Collection");
404     }
< prev index next >