jdk/test/java/util/Collection/MOAT.java
author dl
Tue, 25 Aug 2009 19:19:42 -0700
changeset 3708 f838f712922e
parent 3415 79309d6eab38
child 4184 66ef929d58f2
child 4110 ac033ba6ede4
permissions -rw-r--r--
6868712: Improve concurrent queue tests Summary: Fix all known flaky tests, plus minor maintenance Reviewed-by: martin, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753
494
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
    28
 *          6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @summary Run many tests on many Collection and Map implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author  Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/* Mother Of All (Collection) Tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Testing of collection classes is often spotty, because many tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * need to be performed on many implementations, but the onus on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * writing the tests falls on the engineer introducing the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The idea of this mega-test is that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * An engineer adding a new collection implementation could simply add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * their new implementation to a list of implementations in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * test's main method.  Any general purpose Collection<Integer> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Map<Integer,Integer> class is appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * An engineer fixing a regression could add their regression test here and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * simultaneously test all other implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import static java.util.Collections.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class MOAT {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static void realMain(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        testCollection(new LinkedHashSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        testCollection(new HashSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        testCollection(new Vector<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        testCollection(new Vector<Integer>().subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        testCollection(new ArrayDeque<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        testCollection(new ArrayList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        testCollection(new ArrayList<Integer>().subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        testCollection(new LinkedList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        testCollection(new LinkedList<Integer>().subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        testCollection(new TreeSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        testCollection(new CopyOnWriteArrayList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        testCollection(new CopyOnWriteArraySet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        testCollection(new PriorityQueue<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        testCollection(new PriorityBlockingQueue<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        testCollection(new ArrayBlockingQueue<Integer>(20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        testCollection(new LinkedBlockingQueue<Integer>(20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        testCollection(new LinkedBlockingDeque<Integer>(20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        testCollection(new ConcurrentLinkedQueue<Integer>());
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3415
diff changeset
    79
//         testCollection(new LinkedTransferQueue<Integer>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        testCollection(new ConcurrentSkipListSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        testCollection(Arrays.asList(new Integer(42)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        testCollection(Arrays.asList(1,2,3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        testCollection(nCopies(25,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        testImmutableList(nCopies(25,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        testImmutableList(unmodifiableList(Arrays.asList(1,2,3)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        testMap(new HashMap<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        testMap(new LinkedHashMap<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        testMap(new WeakHashMap<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        testMap(new IdentityHashMap<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        testMap(new TreeMap<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        testMap(new Hashtable<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        testMap(new ConcurrentSkipListMap<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // Empty collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        final List<Integer> emptyArray = Arrays.asList(new Integer[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        testCollection(emptyArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        testEmptyList(emptyArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        THROWS(IndexOutOfBoundsException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
               new Fun(){void f(){ emptyArray.set(0,1); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        THROWS(UnsupportedOperationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
               new Fun(){void f(){ emptyArray.add(0,1); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        List<Integer> noOne = nCopies(0,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        testCollection(noOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        testEmptyList(noOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        testImmutableList(noOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Set<Integer> emptySet = emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        testCollection(emptySet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        testEmptySet(emptySet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        testEmptySet(EMPTY_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        testImmutableSet(emptySet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        List<Integer> emptyList = emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        testCollection(emptyList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        testEmptyList(emptyList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        testEmptyList(EMPTY_LIST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        testImmutableList(emptyList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Map<Integer,Integer> emptyMap = emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        testMap(emptyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        testEmptyMap(emptyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        testEmptyMap(EMPTY_MAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        testImmutableMap(emptyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Singleton collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Set<Integer> singletonSet = singleton(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        equal(singletonSet.size(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        testCollection(singletonSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        testImmutableSet(singletonSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        List<Integer> singletonList = singletonList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        equal(singletonList.size(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        testCollection(singletonList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        testImmutableList(singletonList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        testImmutableList(singletonList.subList(0,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        testImmutableList(singletonList.subList(0,1).subList(0,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        testEmptyList(singletonList.subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        testEmptyList(singletonList.subList(0,0).subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Map<Integer,Integer> singletonMap = singletonMap(1,2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        equal(singletonMap.size(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        testMap(singletonMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        testImmutableMap(singletonMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static void checkContainsSelf(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        check(c.containsAll(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        check(c.containsAll(Arrays.asList(c.toArray())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        check(c.containsAll(Arrays.asList(c.toArray(new Integer[0]))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static void checkContainsEmpty(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        check(c.containsAll(new ArrayList<Integer>()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   159
    private static <T> void testEmptyCollection(Collection<T> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        check(c.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        equal(c.size(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        equal(c.toString(),"[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        equal(c.toArray().length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        equal(c.toArray(new Object[0]).length, 0);
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3415
diff changeset
   165
        check(c.toArray(new Object[]{42})[0] == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        Object[] a = new Object[1]; a[0] = Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        equal(c.toArray(a), a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        equal(a[0], null);
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   170
        testEmptyIterator(c.iterator());
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   171
    }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   172
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   173
    static <T> void testEmptyIterator(final Iterator<T> it) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   174
        if (rnd.nextBoolean())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   175
            check(! it.hasNext());
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   176
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   177
        THROWS(NoSuchElementException.class,
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   178
               new Fun(){void f(){ it.next(); }});
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   179
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   180
        try { it.remove(); }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   181
        catch (IllegalStateException _) { pass(); }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   182
        catch (UnsupportedOperationException _) { pass(); }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   183
        catch (Throwable t) { unexpected(t); }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   184
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   185
        if (rnd.nextBoolean())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   186
            check(! it.hasNext());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static void testEmptyList(List<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        testEmptyCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        equal(c.hashCode(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        equal2(c, Collections.<Integer>emptyList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   195
    private static <T> void testEmptySet(Set<T> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        testEmptyCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        equal(c.hashCode(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        equal2(c, Collections.<Integer>emptySet());
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   199
        if (c instanceof NavigableSet<?>)
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   200
            testEmptyIterator(((NavigableSet<T>)c).descendingIterator());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private static void testImmutableCollection(final Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        THROWS(UnsupportedOperationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
               new Fun(){void f(){ c.add(99); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
               new Fun(){void f(){ c.addAll(singleton(99)); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (! c.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            final Integer first = c.iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            THROWS(UnsupportedOperationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                   new Fun(){void f(){ c.clear(); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                   new Fun(){void f(){ c.remove(first); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                   new Fun(){void f(){ c.removeAll(singleton(first)); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                   new Fun(){void f(){ c.retainAll(emptyList()); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                   );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private static void testImmutableSet(final Set<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        testImmutableCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private static void testImmutableList(final List<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        testList(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        testImmutableCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        THROWS(UnsupportedOperationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
               new Fun(){void f(){ c.set(0,42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
               new Fun(){void f(){ c.add(0,42); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
               new Fun(){void f(){ c.addAll(0,singleton(86)); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (! c.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            THROWS(UnsupportedOperationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                   new Fun(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                           Iterator<Integer> it = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                           it.next(); it.remove();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                   new Fun(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                           ListIterator<Integer> it = c.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                           it.next(); it.remove();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static void clear(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        try { c.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        testEmptyCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   245
    private static <K,V> void testEmptyMap(final Map<K,V> m) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        check(m.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        equal(m.size(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        equal(m.toString(),"{}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        testEmptySet(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        testEmptySet(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        testEmptyCollection(m.values());
494
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   252
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   253
        try { check(! m.containsValue(null)); }
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   254
        catch (NullPointerException _) { /* OK */ }
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   255
        try { check(! m.containsKey(null)); }
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   256
        catch (NullPointerException _) { /* OK */ }
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   257
        check(! m.containsValue(1));
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   258
        check(! m.containsKey(1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static void testImmutableMap(final Map<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        THROWS(UnsupportedOperationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
               new Fun(){void f(){ m.put(1,1); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
               new Fun(){void f(){ m.putAll(singletonMap(1,1)); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (! m.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            final Integer first = m.keySet().iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            THROWS(UnsupportedOperationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                   new Fun(){void f(){ m.remove(first); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                   new Fun(){void f(){ m.clear(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            final Map.Entry<Integer,Integer> me
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                = m.entrySet().iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            Integer key = me.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            Integer val = me.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            THROWS(UnsupportedOperationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                   new Fun(){void f(){ me.setValue(3); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            equal(key, me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            equal(val, me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        testImmutableSet(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        testImmutableCollection(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        //testImmutableSet(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private static void clear(Map<?,?> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        try { m.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        testEmptyMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private static void oneElement(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        clear(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            check(c.add(-42));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            equal(c.toString(), "[-42]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (c instanceof Set) check(! c.add(-42));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        check(! c.isEmpty()); check(c.size() == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private static boolean supportsAdd(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        try { check(c.add(778347983)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        catch (UnsupportedOperationException t) { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            check(c.contains(778347983));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            check(c.remove(778347983));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private static boolean supportsRemove(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        try { check(! c.remove(19134032)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        catch (UnsupportedOperationException t) { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private static void checkFunctionalInvariants(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            checkContainsSelf(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            checkContainsEmpty(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            check(c.size() != 0 ^ c.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                for (Integer i : c) size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                check(c.size() == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            check(c.toArray().length == c.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            check(c.toArray().getClass() == Object[].class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                  // !!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                  // 6260652: (coll) Arrays.asList(x).toArray().getClass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                  // should be Object[].class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                  (c.getClass().getName().equals("java.util.Arrays$ArrayList"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                  );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            for (int size : new int[]{0,1,c.size(), c.size()+1}) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                Integer[] a = c.toArray(new Integer[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                check((size > c.size()) || a.length == c.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                int i = 0; for (Integer j : c) check(a[i++] == j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                check((size <= c.size()) || (a[c.size()] == null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                check(a.getClass() == Integer[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            check(c.equals(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (c instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                //System.out.printf("Serializing %s%n", c.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    Object clone = serialClone(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    equal(c instanceof Serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                          clone instanceof Serializable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    equal(c instanceof RandomAccess,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                          clone instanceof RandomAccess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    if ((c instanceof List) || (c instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        equal(c, clone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        equal(new HashSet<Integer>(c),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                              new HashSet<Integer>(serialClone(c)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                } catch (Error xxx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    if (! (xxx.getCause() instanceof NotSerializableException))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        throw xxx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    // If add(null) succeeds, contains(null) & remove(null) should succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private static void testNullElement(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // !!!! 5018849: (coll) TreeSet.contains(null) does not agree with Javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (c instanceof TreeSet) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            check(c.add(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (c.size() == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                equal(c.toString(), "[null]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                checkFunctionalInvariants(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                check(c.contains(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                check(c.remove(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        catch (NullPointerException e) { /* OK */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    // If add("x") succeeds, contains("x") & remove("x") should succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    private static void testStringElement(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        Collection x = (Collection)c; // Make type-unsafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            check(x.add("x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                check(x.contains("x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                check(x.remove("x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        catch (ClassCastException e) { /* OK */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private static void testConcurrentCollection(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            c.add(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            Iterator<Integer> it = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            clear(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            check(it.next() instanceof Integer); // No CME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            check(c.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private static void testQueue(Queue<Integer> q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        for (int i = 0; i < 5; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            q.add(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        equal(q.size(), 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        checkFunctionalInvariants(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        q.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        equal(q.size(), 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        checkFunctionalInvariants(q);
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   431
        if ((q instanceof LinkedBlockingQueue) ||
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   432
            (q instanceof LinkedBlockingDeque) ||
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   433
            (q instanceof ConcurrentLinkedQueue)) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   434
            testQueueIteratorRemove(q);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   435
        }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   436
    }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   437
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   438
    private static void testQueueIteratorRemove(Queue<Integer> q) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   439
        System.err.printf("testQueueIteratorRemove %s%n",
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   440
                          q.getClass().getSimpleName());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   441
        q.clear();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   442
        for (int i = 0; i < 5; i++)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   443
            q.add(i);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   444
        Iterator<Integer> it = q.iterator();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   445
        check(it.hasNext());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   446
        for (int i = 3; i >= 0; i--)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   447
            q.remove(i);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   448
        equal(it.next(), 0);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   449
        equal(it.next(), 4);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   450
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   451
        q.clear();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   452
        for (int i = 0; i < 5; i++)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   453
            q.add(i);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   454
        it = q.iterator();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   455
        equal(it.next(), 0);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   456
        check(it.hasNext());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   457
        for (int i = 1; i < 4; i++)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   458
            q.remove(i);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   459
        equal(it.next(), 1);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   460
        equal(it.next(), 4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    private static void testList(final List<Integer> l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // doesn't throw IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            l.addAll(-1, Collections.<Integer>emptyList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            fail("Expected IndexOutOfBoundsException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        catch (UnsupportedOperationException _) {/* OK */}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        catch (IndexOutOfBoundsException _) {/* OK */}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
//      equal(l instanceof Serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
//            l.subList(0,0) instanceof Serializable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (l.subList(0,0) instanceof Serializable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            check(l instanceof Serializable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        equal(l instanceof RandomAccess,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
              l.subList(0,0) instanceof RandomAccess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    private static void testCollection(Collection<Integer> c) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   486
        try { testCollection1(c); }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   487
        catch (Throwable t) { unexpected(t); }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   488
    }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   489
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   490
    private static void testCollection1(Collection<Integer> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        System.out.println("\n==> " + c.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        checkFunctionalInvariants(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (! supportsAdd(c)) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        //System.out.println("add() supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   499
        if (c instanceof NavigableSet) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   500
            System.out.println("NavigableSet tests...");
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   501
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   502
            NavigableSet<Integer> ns = (NavigableSet<Integer>)c;
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   503
            testNavigableSet(ns);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   504
            testNavigableSet(ns.headSet(6, false));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   505
            testNavigableSet(ns.headSet(5, true));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   506
            testNavigableSet(ns.tailSet(0, false));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   507
            testNavigableSet(ns.tailSet(1, true));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   508
            testNavigableSet(ns.subSet(0, false, 5, true));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   509
            testNavigableSet(ns.subSet(1, true, 6, false));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   510
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (c instanceof Queue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            testQueue((Queue<Integer>)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (c instanceof List)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            testList((List<Integer>)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        check(supportsRemove(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            checkFunctionalInvariants(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        clear(c);      testNullElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        oneElement(c); testNullElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        clear(c);      testStringElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        oneElement(c); testStringElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (c.getClass().getName().matches(".*concurrent.*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            testConcurrentCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        // The "all" operations should throw NPE when passed null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                c.removeAll(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                fail("Expected NullPointerException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            catch (NullPointerException e) { pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                c.retainAll(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                fail("Expected NullPointerException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            catch (NullPointerException e) { pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                c.addAll(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                fail("Expected NullPointerException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            catch (NullPointerException e) { pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                c.containsAll(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                fail("Expected NullPointerException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            catch (NullPointerException e) { pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    // Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    private static void checkFunctionalInvariants(Map<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        check(m.keySet().size() == m.entrySet().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        check(m.keySet().size() == m.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        checkFunctionalInvariants(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        checkFunctionalInvariants(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        check(m.size() != 0 ^ m.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private static void testMap(Map<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        System.out.println("\n==> " + m.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (m instanceof ConcurrentMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            testConcurrentMap((ConcurrentMap<Integer,Integer>) m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   590
        if (m instanceof NavigableMap) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   591
            System.out.println("NavigableMap tests...");
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   592
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   593
            NavigableMap<Integer,Integer> nm =
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   594
                (NavigableMap<Integer,Integer>) m;
2428
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   595
            testNavigableMapRemovers(nm);
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   596
            testNavigableMap(nm);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   597
            testNavigableMap(nm.headMap(6, false));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   598
            testNavigableMap(nm.headMap(5, true));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   599
            testNavigableMap(nm.tailMap(0, false));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   600
            testNavigableMap(nm.tailMap(1, true));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   601
            testNavigableMap(nm.subMap(1, true, 6, false));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   602
            testNavigableMap(nm.subMap(0, false, 5, true));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   603
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        checkFunctionalInvariants(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (supportsClear(m)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            try { clear(m); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (supportsPut(m)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                check(m.put(3333, 77777) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                check(m.put(9134, 74982) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                check(m.get(9134) == 74982);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                check(m.put(9134, 1382) == 74982);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                check(m.get(9134) == 1382);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                check(m.size() == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                checkFunctionalInvariants(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                checkNPEConsistency(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private static boolean supportsPut(Map<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        // We're asking for .equals(...) semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if (m instanceof IdentityHashMap) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        try { check(m.put(778347983,12735) == null); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        catch (UnsupportedOperationException t) { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            check(m.containsKey(778347983));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            check(m.remove(778347983) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    private static boolean supportsClear(Map<?,?> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        try { m.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        catch (UnsupportedOperationException t) { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    // ConcurrentMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        System.out.println("ConcurrentMap tests...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            clear(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            check(m.putIfAbsent(18357,7346) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            check(m.putIfAbsent(18357,8263) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            try { m.putIfAbsent(18357,null); fail("NPE"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            catch (NullPointerException t) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            check(! m.replace(18357,8888,7777));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            try { m.replace(18357,null,7777); fail("NPE"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            catch (NullPointerException t) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            check(m.replace(18357,7346,5555));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            check(m.replace(18357,5555,7346));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            check(m.replace(92347,7834) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            try { m.replace(18357,null); fail("NPE"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            catch (NullPointerException t) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            check(m.replace(18357,7346) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            check(m.replace(18357,5555) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            check(m.get(18357) == 5555);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            check(m.replace(18357,7346) == 5555);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            check(! m.remove(18357,9999));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            check(! m.remove(18357,null)); // 6272521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            check(m.remove(18357,7346));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            check(m.get(18357) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            check(! m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            check(m.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            m.putIfAbsent(1,2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            check(m.size() == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            check(! m.remove(1,null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            check(! m.remove(1,null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            check(! m.remove(1,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            check(m.remove(1,2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            check(m.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            testEmptyMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    private static void throwsConsistently(Class<? extends Throwable> k,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                                           Iterable<Fun> fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        List<Class<? extends Throwable>> threw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            = new ArrayList<Class<? extends Throwable>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            try { f.f(); threw.add(null); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                check(k.isAssignableFrom(t.getClass()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                threw.add(t.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (new HashSet<Object>(threw).size() != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            fail(threw.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        m.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            ? (ConcurrentMap<T,Integer>) m
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        List<Fun> fs = new ArrayList<Fun>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        fs.add(new Fun(){void f(){ check(! m.containsKey(null));}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        fs.add(new Fun(){void f(){ equal(m.remove(null), null);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        fs.add(new Fun(){void f(){ equal(m.get(null), null);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (cm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        throwsConsistently(NullPointerException.class, fs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        fs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        final Map<T,Integer> sm = singletonMap(null,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (cm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        throwsConsistently(NullPointerException.class, fs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    // NavigableMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    private static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                              Integer i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                              Integer lower,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                              Integer floor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                              Integer ceiling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                              Integer higher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        equal(m.lowerKey(i),   lower);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        equal(m.floorKey(i),   floor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        equal(m.ceilingKey(i), ceiling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        equal(m.higherKey(i),  higher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    private static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        checkNavigableSetKeys(NavigableSet<Integer> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                              Integer i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                              Integer lower,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                              Integer floor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                              Integer ceiling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                              Integer higher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        equal(m.lower(i),   lower);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        equal(m.floor(i),   floor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        equal(m.ceiling(i), ceiling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        equal(m.higher(i),  higher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    static final Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    static void equalNext(final Iterator<?> it, Object expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        equal(it.next(), expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
2428
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   783
    static void equalMaps(Map m1, Map m2) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   784
        equal(m1, m2);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   785
        equal(m2, m1);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   786
        equal(m1.size(), m2.size());
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   787
        equal(m1.isEmpty(), m2.isEmpty());
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   788
        equal(m1.toString(), m2.toString());
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   789
        check(Arrays.equals(m1.entrySet().toArray(), m2.entrySet().toArray()));
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   790
    }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   791
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   792
    @SuppressWarnings({"unchecked", "rawtypes"})
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   793
    static void testNavigableMapRemovers(NavigableMap m)
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   794
    {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   795
        final Map emptyMap = new HashMap();
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   796
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   797
        final Map singletonMap = new HashMap();
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   798
        singletonMap.put(1, 2);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   799
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   800
        abstract class NavigableMapView {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   801
            abstract NavigableMap view(NavigableMap m);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   802
        }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   803
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   804
        NavigableMapView[] views = {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   805
            new NavigableMapView() { NavigableMap view(NavigableMap m) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   806
                return m; }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   807
            new NavigableMapView() { NavigableMap view(NavigableMap m) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   808
                return m.headMap(99, true); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   809
            new NavigableMapView() { NavigableMap view(NavigableMap m) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   810
                return m.tailMap(-99, false); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   811
            new NavigableMapView() { NavigableMap view(NavigableMap m) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   812
                return m.subMap(-99, true, 99, false); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   813
        };
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   814
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   815
        abstract class Remover {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   816
            abstract void remove(NavigableMap m, Object k, Object v);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   817
        }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   818
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   819
        Remover[] removers = {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   820
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   821
                equal(m.remove(k), v); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   822
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   823
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   824
                equal(m.descendingMap().remove(k), v); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   825
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   826
                equal(m.descendingMap().headMap(-86, false).remove(k), v); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   827
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   828
                equal(m.descendingMap().tailMap(86, true).remove(k), v); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   829
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   830
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   831
                equal(m.headMap(86, true).remove(k), v); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   832
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   833
                equal(m.tailMap(-86, true).remove(k), v); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   834
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   835
                equal(m.subMap(-86, false, 86, true).remove(k), v); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   836
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   837
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   838
                check(m.keySet().remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   839
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   840
                check(m.navigableKeySet().remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   841
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   842
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   843
                check(m.navigableKeySet().headSet(86, true).remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   844
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   845
                check(m.navigableKeySet().tailSet(-86, false).remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   846
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   847
                check(m.navigableKeySet().subSet(-86, true, 86, false)
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   848
                      .remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   849
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   850
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   851
                check(m.descendingKeySet().headSet(-86, false).remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   852
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   853
                check(m.descendingKeySet().tailSet(86, true).remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   854
            new Remover() { void remove(NavigableMap m, Object k, Object v) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   855
                check(m.descendingKeySet().subSet(86, true, -86, false)
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   856
                      .remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   857
        };
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   858
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   859
        for (NavigableMapView view : views) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   860
            for (Remover remover : removers) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   861
                try {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   862
                    m.clear();
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   863
                    equalMaps(m, emptyMap);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   864
                    equal(m.put(1, 2), null);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   865
                    equalMaps(m, singletonMap);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   866
                    NavigableMap v = view.view(m);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   867
                    remover.remove(v, 1, 2);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   868
                    equalMaps(m, emptyMap);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   869
                } catch (Throwable t) { unexpected(t); }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   870
            }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   871
        }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   872
    }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
   873
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    private static void testNavigableMap(NavigableMap<Integer,Integer> m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        clear(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        checkNavigableMapKeys(m, 1, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        equal(m.put(1, 2), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        equal(m.put(3, 4), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        equal(m.put(5, 9), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        equal(m.put(1, 2), 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        equal(m.put(3, 4), 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        equal(m.put(5, 6), 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        checkNavigableMapKeys(m, 0, null, null,    1,    1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        checkNavigableMapKeys(m, 1, null,    1,    1,    3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        checkNavigableMapKeys(m, 2,    1,    1,    3,    3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        checkNavigableMapKeys(m, 3,    1,    3,    3,    5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        checkNavigableMapKeys(m, 5,    3,    5,    5, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        checkNavigableMapKeys(m, 6,    5,    5, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   894
        for (final Iterator<Integer> it :
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   895
                 (Iterator<Integer>[])
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   896
                 new Iterator<?>[] {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   897
                     m.descendingKeySet().iterator(),
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   898
                     m.navigableKeySet().descendingIterator()}) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            equalNext(it, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            equalNext(it, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            equalNext(it, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            THROWS(NoSuchElementException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                   new Fun(){void f(){it.next();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            final Iterator<Map.Entry<Integer,Integer>> it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                = m.descendingMap().entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            check(it.hasNext()); equal(it.next().getKey(), 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            check(it.hasNext()); equal(it.next().getKey(), 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            check(it.hasNext()); equal(it.next().getKey(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            THROWS(NoSuchElementException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                   new Fun(){void f(){it.next();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    private static void testNavigableSet(NavigableSet<Integer> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        clear(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        checkNavigableSetKeys(s, 1, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        check(s.add(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        check(s.add(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        check(s.add(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        check(! s.add(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        check(! s.add(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        check(! s.add(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        checkNavigableSetKeys(s, 0, null, null,    1,    1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        checkNavigableSetKeys(s, 1, null,    1,    1,    3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        checkNavigableSetKeys(s, 2,    1,    1,    3,    3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        checkNavigableSetKeys(s, 3,    1,    3,    3,    5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        checkNavigableSetKeys(s, 5,    3,    5,    5, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        checkNavigableSetKeys(s, 6,    5,    5, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   939
        for (final Iterator<Integer> it :
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   940
                 (Iterator<Integer>[])
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   941
                 new Iterator<?>[] {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   942
                     s.descendingIterator(),
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   943
                     s.descendingSet().iterator()}) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            equalNext(it, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            equalNext(it, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            equalNext(it, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            THROWS(NoSuchElementException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                   new Fun(){void f(){it.next();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    static void pass() { passed++; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    static void fail() { failed++; Thread.dumpStack(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    static void fail(String msg) { System.out.println(msg); fail(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    static void check(boolean cond) { if (cond) pass(); else fail(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        else {System.out.println(x + " not equal to " + y); fail();}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        try { realMain(args); } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        if (failed > 0) throw new Exception("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    private static abstract class Fun {abstract void f() throws Throwable;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
          for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
              try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
              catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                  if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                  else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    static byte[] serializedForm(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            new ObjectOutputStream(baos).writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        } catch (IOException e) { throw new Error(e); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    static Object readObject(byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        InputStream is = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        return new ObjectInputStream(is).readObject();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    static <T> T serialClone(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        try { return (T) readObject(serializedForm(obj)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        catch (Exception e) { throw new Error(e); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
}