jdk-24/jdk/test/java/util/Spliterator/SpliteratorCharacteristics.java
Paul Sandoz 0fb014c2b3 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
8012913: LinkedHashMap key/value/entry spliterators should report ORDERED

Co-authored-by: Doug Lea <dl@cs.oswego.edu>
Reviewed-by: mduigou, forax, bchristi, alanb
2013-09-04 09:34:25 +02:00

231 lines
8.0 KiB
Java

/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8020156 8020009 8022326 8012913
* @run testng SpliteratorCharacteristics
*/
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.Spliterator;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentSkipListSet;
import static org.testng.Assert.*;
@Test
public class SpliteratorCharacteristics {
public void testHashMap() {
assertMapCharacteristics(new HashMap<>(),
Spliterator.SIZED | Spliterator.DISTINCT);
}
public void testHashSet() {
assertSetCharacteristics(new HashSet<>(),
Spliterator.SIZED | Spliterator.DISTINCT);
}
public void testLinkedHashMap() {
assertMapCharacteristics(new LinkedHashMap<>(),
Spliterator.SIZED | Spliterator.DISTINCT |
Spliterator.ORDERED);
}
public void testLinkedHashSet() {
assertSetCharacteristics(new LinkedHashSet<>(),
Spliterator.SIZED | Spliterator.DISTINCT |
Spliterator.ORDERED);
}
public void testTreeMap() {
assertSortedMapCharacteristics(new TreeMap<>(),
Spliterator.SIZED | Spliterator.DISTINCT |
Spliterator.SORTED | Spliterator.ORDERED);
}
public void testTreeMapWithComparator() {
assertSortedMapCharacteristics(new TreeMap<>(Comparator.reverseOrder()),
Spliterator.SIZED | Spliterator.DISTINCT |
Spliterator.SORTED | Spliterator.ORDERED);
}
public void testTreeSet() {
assertSortedSetCharacteristics(new TreeSet<>(),
Spliterator.SIZED | Spliterator.DISTINCT |
Spliterator.SORTED | Spliterator.ORDERED);
}
public void testTreeSetWithComparator() {
assertSortedSetCharacteristics(new TreeSet<>(Comparator.reverseOrder()),
Spliterator.SIZED | Spliterator.DISTINCT |
Spliterator.SORTED | Spliterator.ORDERED);
}
public void testConcurrentSkipListMap() {
assertSortedMapCharacteristics(new ConcurrentSkipListMap<>(),
Spliterator.CONCURRENT | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED |
Spliterator.ORDERED);
}
public void testConcurrentSkipListMapWithComparator() {
assertSortedMapCharacteristics(new ConcurrentSkipListMap<>(Comparator.<Integer>reverseOrder()),
Spliterator.CONCURRENT | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED |
Spliterator.ORDERED);
}
public void testConcurrentSkipListSet() {
assertSortedSetCharacteristics(new ConcurrentSkipListSet<>(),
Spliterator.CONCURRENT | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED |
Spliterator.ORDERED);
}
public void testConcurrentSkipListSetWithComparator() {
assertSortedSetCharacteristics(new ConcurrentSkipListSet<>(Comparator.reverseOrder()),
Spliterator.CONCURRENT | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED |
Spliterator.ORDERED);
}
//
void assertMapCharacteristics(Map<Integer, String> m, int keyCharacteristics) {
assertMapCharacteristics(m, keyCharacteristics, 0);
}
void assertMapCharacteristics(Map<Integer, String> m, int keyCharacteristics, int notValueCharacteristics) {
initMap(m);
assertCharacteristics(m.keySet(), keyCharacteristics);
assertCharacteristics(m.values(),
keyCharacteristics & ~(Spliterator.DISTINCT | notValueCharacteristics));
assertCharacteristics(m.entrySet(), keyCharacteristics);
if ((keyCharacteristics & Spliterator.SORTED) == 0) {
assertISEComparator(m.keySet());
assertISEComparator(m.values());
assertISEComparator(m.entrySet());
}
}
void assertSetCharacteristics(Set<Integer> s, int keyCharacteristics) {
initSet(s);
assertCharacteristics(s, keyCharacteristics);
if ((keyCharacteristics & Spliterator.SORTED) == 0) {
assertISEComparator(s);
}
}
void assertSortedMapCharacteristics(SortedMap<Integer, String> m, int keyCharacteristics) {
assertMapCharacteristics(m, keyCharacteristics, Spliterator.SORTED);
Set<Integer> keys = m.keySet();
if (m.comparator() != null) {
assertNotNullComparator(keys);
}
else {
assertNullComparator(keys);
}
assertISEComparator(m.values());
assertNotNullComparator(m.entrySet());
}
void assertSortedSetCharacteristics(SortedSet<Integer> s, int keyCharacteristics) {
assertSetCharacteristics(s, keyCharacteristics);
if (s.comparator() != null) {
assertNotNullComparator(s);
}
else {
assertNullComparator(s);
}
}
void initMap(Map<Integer, String> m) {
m.put(1, "4");
m.put(2, "3");
m.put(3, "2");
m.put(4, "1");
}
void initSet(Set<Integer> s) {
s.addAll(Arrays.asList(1, 2, 3, 4));
}
void assertCharacteristics(Collection<?> c, int expectedCharacteristics) {
assertTrue(c.spliterator().hasCharacteristics(expectedCharacteristics),
"Spliterator characteristics");
}
void assertNullComparator(Collection<?> c) {
assertNull(c.spliterator().getComparator(),
"Comparator of Spliterator of Collection");
}
void assertNotNullComparator(Collection<?> c) {
assertNotNull(c.spliterator().getComparator(),
"Comparator of Spliterator of Collection");
}
void assertISEComparator(Collection<?> c) {
assertISEComparator(c.spliterator());
}
void assertISEComparator(Spliterator<?> s) {
boolean caught = false;
try {
s.getComparator();
}
catch (IllegalStateException e) {
caught = true;
}
assertTrue(caught, "Throwing IllegalStateException");
}
}