jdk/test/java/util/Collection/MOAT.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 40788 01ffe34b7340
child 43522 f9c6f543c4db
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. 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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4186
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4186
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4186
diff changeset
    21
 * questions.
2
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
20757
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
    29
 *          4802647 7123424 8024709
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary Run many tests on many Collection and Map implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author  Martin Buchholz
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40788
diff changeset
    32
 * @modules java.base/java.util:open
8536
03570b625eef 7019705: Add -XX:+AggressiveOpts options to MOAT test
mduigou
parents: 7668
diff changeset
    33
 * @run main MOAT
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 24692
diff changeset
    34
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/* Mother Of All (Collection) Tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Testing of collection classes is often spotty, because many tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * need to be performed on many implementations, but the onus on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * writing the tests falls on the engineer introducing the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The idea of this mega-test is that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * An engineer adding a new collection implementation could simply add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * their new implementation to a list of implementations in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * test's main method.  Any general purpose Collection<Integer> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Map<Integer,Integer> class is appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * An engineer fixing a regression could add their regression test here and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * simultaneously test all other implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import static java.util.Collections.*;
23580
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
    59
import java.lang.reflect.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class MOAT {
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    62
    // Collections under test must not be initialized to contain this value,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    63
    // and maps under test must not contain this value as a key.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    64
    // It's used as a sentinel for absent-element testing.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    65
    static final int ABSENT_VALUE = 778347983;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    66
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    67
    static final Integer[] integerArray;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    68
    static {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    69
        Integer[] ia = new Integer[20];
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    70
        // fill with 1..20 inclusive
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    71
        for (int i = 0; i < ia.length; i++) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    72
            ia[i] = i + 1;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    73
        }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    74
        integerArray = ia;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    75
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static void realMain(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
    79
        testCollection(new NewAbstractCollection<Integer>());
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
    80
        testCollection(new NewAbstractSet<Integer>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        testCollection(new LinkedHashSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        testCollection(new HashSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        testCollection(new Vector<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        testCollection(new Vector<Integer>().subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        testCollection(new ArrayDeque<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        testCollection(new ArrayList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        testCollection(new ArrayList<Integer>().subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        testCollection(new LinkedList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        testCollection(new LinkedList<Integer>().subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        testCollection(new TreeSet<Integer>());
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
    91
        testCollection(Collections.checkedList(new ArrayList<Integer>(), Integer.class));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
    92
        testCollection(Collections.synchronizedList(new ArrayList<Integer>()));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
    93
        testCollection(Collections.checkedSet(new HashSet<Integer>(), Integer.class));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
    94
        testCollection(Collections.checkedSortedSet(new TreeSet<Integer>(), Integer.class));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
    95
        testCollection(Collections.checkedNavigableSet(new TreeSet<Integer>(), Integer.class));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
    96
        testCollection(Collections.synchronizedSet(new HashSet<Integer>()));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
    97
        testCollection(Collections.synchronizedSortedSet(new TreeSet<Integer>()));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
    98
        testCollection(Collections.synchronizedNavigableSet(new TreeSet<Integer>()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        testCollection(new CopyOnWriteArrayList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        testCollection(new CopyOnWriteArraySet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        testCollection(new PriorityQueue<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        testCollection(new PriorityBlockingQueue<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        testCollection(new ArrayBlockingQueue<Integer>(20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        testCollection(new LinkedBlockingQueue<Integer>(20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        testCollection(new LinkedBlockingDeque<Integer>(20));
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   108
        testCollection(new ConcurrentLinkedDeque<Integer>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        testCollection(new ConcurrentLinkedQueue<Integer>());
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   110
        testCollection(new LinkedTransferQueue<Integer>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        testCollection(new ConcurrentSkipListSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        testCollection(Arrays.asList(new Integer(42)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        testCollection(Arrays.asList(1,2,3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        testCollection(nCopies(25,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        testImmutableList(nCopies(25,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        testMap(new HashMap<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        testMap(new LinkedHashMap<Integer,Integer>());
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   119
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   120
        // TODO: Add reliable support for WeakHashMap.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   121
        // This test is subject to very rare failures because the GC
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   122
        // may remove unreferenced-keys from the map at any time.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   123
        // testMap(new WeakHashMap<Integer,Integer>());
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   124
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        testMap(new IdentityHashMap<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        testMap(new TreeMap<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        testMap(new Hashtable<Integer,Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        testMap(new ConcurrentSkipListMap<Integer,Integer>());
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   130
        testMap(Collections.checkedMap(new HashMap<Integer,Integer>(), Integer.class, Integer.class));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   131
        testMap(Collections.checkedSortedMap(new TreeMap<Integer,Integer>(), Integer.class, Integer.class));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   132
        testMap(Collections.checkedNavigableMap(new TreeMap<Integer,Integer>(), Integer.class, Integer.class));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   133
        testMap(Collections.synchronizedMap(new HashMap<Integer,Integer>()));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   134
        testMap(Collections.synchronizedSortedMap(new TreeMap<Integer,Integer>()));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   135
        testMap(Collections.synchronizedNavigableMap(new TreeMap<Integer,Integer>()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
40788
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   137
        // Unmodifiable wrappers
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   138
        testImmutableSet(unmodifiableSet(new HashSet<>(Arrays.asList(1,2,3))));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   139
        testImmutableList(unmodifiableList(Arrays.asList(1,2,3)));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   140
        testImmutableMap(unmodifiableMap(Collections.singletonMap(1,2)));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   141
        testCollMutatorsAlwaysThrow(unmodifiableSet(new HashSet<>(Arrays.asList(1,2,3))));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   142
        testCollMutatorsAlwaysThrow(unmodifiableSet(Collections.emptySet()));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   143
        testEmptyCollMutatorsAlwaysThrow(unmodifiableSet(Collections.emptySet()));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   144
        testListMutatorsAlwaysThrow(unmodifiableList(Arrays.asList(1,2,3)));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   145
        testListMutatorsAlwaysThrow(unmodifiableList(Collections.emptyList()));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   146
        testEmptyListMutatorsAlwaysThrow(unmodifiableList(Collections.emptyList()));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   147
        testMapMutatorsAlwaysThrow(unmodifiableMap(Collections.singletonMap(1,2)));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   148
        testMapMutatorsAlwaysThrow(unmodifiableMap(Collections.emptyMap()));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   149
        testEmptyMapMutatorsAlwaysThrow(unmodifiableMap(Collections.emptyMap()));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   150
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // Empty collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        final List<Integer> emptyArray = Arrays.asList(new Integer[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        testCollection(emptyArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        testEmptyList(emptyArray);
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   155
        THROWS(IndexOutOfBoundsException.class, () -> emptyArray.set(0,1));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   156
        THROWS(UnsupportedOperationException.class, () -> emptyArray.add(0,1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        List<Integer> noOne = nCopies(0,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        testCollection(noOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        testEmptyList(noOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        testImmutableList(noOne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        Set<Integer> emptySet = emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        testCollection(emptySet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        testEmptySet(emptySet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        testEmptySet(EMPTY_SET);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   167
        testEmptySet(Collections.emptySet());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   168
        testEmptySet(Collections.emptySortedSet());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   169
        testEmptySet(Collections.emptyNavigableSet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        testImmutableSet(emptySet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        List<Integer> emptyList = emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        testCollection(emptyList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        testEmptyList(emptyList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        testEmptyList(EMPTY_LIST);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   176
        testEmptyList(Collections.emptyList());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        testImmutableList(emptyList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        Map<Integer,Integer> emptyMap = emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        testMap(emptyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        testEmptyMap(emptyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        testEmptyMap(EMPTY_MAP);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   183
        testEmptyMap(Collections.emptyMap());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   184
        testEmptyMap(Collections.emptySortedMap());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   185
        testEmptyMap(Collections.emptyNavigableMap());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        testImmutableMap(emptyMap);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   187
        testImmutableMap(Collections.emptyMap());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   188
        testImmutableMap(Collections.emptySortedMap());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 17441
diff changeset
   189
        testImmutableMap(Collections.emptyNavigableMap());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // Singleton collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Set<Integer> singletonSet = singleton(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        equal(singletonSet.size(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        testCollection(singletonSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        testImmutableSet(singletonSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        List<Integer> singletonList = singletonList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        equal(singletonList.size(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        testCollection(singletonList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        testImmutableList(singletonList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        testImmutableList(singletonList.subList(0,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        testImmutableList(singletonList.subList(0,1).subList(0,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        testEmptyList(singletonList.subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        testEmptyList(singletonList.subList(0,0).subList(0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        Map<Integer,Integer> singletonMap = singletonMap(1,2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        equal(singletonMap.size(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        testMap(singletonMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        testImmutableMap(singletonMap);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   210
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   211
        // Immutable List
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   212
        testEmptyList(List.of());
40788
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   213
        testListMutatorsAlwaysThrow(List.of());
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   214
        testEmptyListMutatorsAlwaysThrow(List.of());
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   215
        for (List<Integer> list : Arrays.asList(
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   216
                List.<Integer>of(),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   217
                List.of(1),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   218
                List.of(1, 2),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   219
                List.of(1, 2, 3),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   220
                List.of(1, 2, 3, 4),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   221
                List.of(1, 2, 3, 4, 5),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   222
                List.of(1, 2, 3, 4, 5, 6),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   223
                List.of(1, 2, 3, 4, 5, 6, 7),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   224
                List.of(1, 2, 3, 4, 5, 6, 7, 8),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   225
                List.of(1, 2, 3, 4, 5, 6, 7, 8, 9),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   226
                List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   227
                List.of(integerArray))) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   228
            testCollection(list);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   229
            testImmutableList(list);
40788
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   230
            testListMutatorsAlwaysThrow(list);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   231
        }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   232
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   233
        // Immutable Set
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   234
        testEmptySet(Set.of());
40788
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   235
        testCollMutatorsAlwaysThrow(Set.of());
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   236
        testEmptyCollMutatorsAlwaysThrow(Set.of());
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   237
        for (Set<Integer> set : Arrays.asList(
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   238
                Set.<Integer>of(),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   239
                Set.of(1),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   240
                Set.of(1, 2),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   241
                Set.of(1, 2, 3),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   242
                Set.of(1, 2, 3, 4),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   243
                Set.of(1, 2, 3, 4, 5),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   244
                Set.of(1, 2, 3, 4, 5, 6),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   245
                Set.of(1, 2, 3, 4, 5, 6, 7),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   246
                Set.of(1, 2, 3, 4, 5, 6, 7, 8),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   247
                Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   248
                Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   249
                Set.of(integerArray))) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   250
            testCollection(set);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   251
            testImmutableSet(set);
40788
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   252
            testCollMutatorsAlwaysThrow(set);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   253
        }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   254
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   255
        // Immutable Map
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   256
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   257
        @SuppressWarnings("unchecked")
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   258
        Map.Entry<Integer,Integer>[] ea = (Map.Entry<Integer,Integer>[])new Map.Entry<?,?>[20];
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   259
        for (int i = 0; i < ea.length; i++) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   260
            ea[i] = Map.entry(i+1, i+101);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   261
        }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   262
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   263
        testEmptyMap(Map.of());
40788
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   264
        testMapMutatorsAlwaysThrow(Map.of());
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   265
        testEmptyMapMutatorsAlwaysThrow(Map.of());
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   266
        for (Map<Integer,Integer> map : Arrays.asList(
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   267
                Map.<Integer,Integer>of(),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   268
                Map.of(1, 101),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   269
                Map.of(1, 101, 2, 202),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   270
                Map.of(1, 101, 2, 202, 3, 303),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   271
                Map.of(1, 101, 2, 202, 3, 303, 4, 404),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   272
                Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   273
                Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   274
                Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   275
                Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   276
                Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808, 9, 909),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   277
                Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808, 9, 909, 10, 1010),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   278
                Map.ofEntries(ea))) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   279
            testMap(map);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   280
            testImmutableMap(map);
40788
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   281
            testMapMutatorsAlwaysThrow(map);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   282
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private static void checkContainsSelf(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        check(c.containsAll(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        check(c.containsAll(Arrays.asList(c.toArray())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        check(c.containsAll(Arrays.asList(c.toArray(new Integer[0]))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private static void checkContainsEmpty(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        check(c.containsAll(new ArrayList<Integer>()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   295
    private static void checkUnique(Set<Integer> s) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   296
        for (Integer i : s) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   297
            int count = 0;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   298
            for (Integer j : s) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   299
                if (Objects.equals(i,j))
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   300
                    ++count;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   301
            }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   302
            check(count == 1);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   303
        }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   304
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   305
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   306
    private static <T> void testEmptyCollection(Collection<T> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        check(c.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        equal(c.size(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        equal(c.toString(),"[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        equal(c.toArray().length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        equal(c.toArray(new Object[0]).length, 0);
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 3415
diff changeset
   312
        check(c.toArray(new Object[]{42})[0] == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        Object[] a = new Object[1]; a[0] = Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        equal(c.toArray(a), a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        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
   317
        testEmptyIterator(c.iterator());
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   318
    }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   319
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   320
    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
   321
        if (rnd.nextBoolean())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   322
            check(! it.hasNext());
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   323
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   324
        THROWS(NoSuchElementException.class, () -> it.next());
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   325
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   326
        try { it.remove(); }
20476
2d01671506ba 8023339: Refined Collection.removeIf UOE conditions
mduigou
parents: 19855
diff changeset
   327
        catch (IllegalStateException ignored) { pass(); }
2d01671506ba 8023339: Refined Collection.removeIf UOE conditions
mduigou
parents: 19855
diff changeset
   328
        catch (UnsupportedOperationException ignored) { pass(); }
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   329
        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
   330
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   331
        if (rnd.nextBoolean())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   332
            check(! it.hasNext());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private static void testEmptyList(List<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        testEmptyCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        equal(c.hashCode(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        equal2(c, Collections.<Integer>emptyList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   341
    private static <T> void testEmptySet(Set<T> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        testEmptyCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        equal(c.hashCode(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        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
   345
        if (c instanceof NavigableSet<?>)
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   346
            testEmptyIterator(((NavigableSet<T>)c).descendingIterator());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private static void testImmutableCollection(final Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        THROWS(UnsupportedOperationException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   351
               () -> c.add(99),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   352
               () -> c.addAll(singleton(99)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (! c.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            final Integer first = c.iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            THROWS(UnsupportedOperationException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   356
                   () -> c.clear(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   357
                   () -> c.remove(first),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   358
                   () -> c.removeAll(singleton(first)),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   359
                   () -> c.retainAll(emptyList()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private static void testImmutableSet(final Set<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        testImmutableCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private static void testImmutableList(final List<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        testList(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        testImmutableCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        THROWS(UnsupportedOperationException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   371
               () -> c.set(0,42),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   372
               () -> c.add(0,42),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   373
               () -> c.addAll(0,singleton(86)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (! c.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            THROWS(UnsupportedOperationException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   376
                   () -> { Iterator<Integer> it = c.iterator();
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   377
                           it.next();
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   378
                           it.remove(); },
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   379
                   () -> { ListIterator<Integer> it = c.listIterator();
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   380
                           it.next();
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   381
                           it.remove(); });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
40788
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   384
    /**
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   385
     * Test that calling a mutator always throws UOE, even if the mutator
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   386
     * wouldn't actually do anything, given its arguments.
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   387
     *
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   388
     * @param c the collection instance to test
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   389
     */
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   390
    private static void testCollMutatorsAlwaysThrow(Collection<Integer> c) {
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   391
        THROWS(UnsupportedOperationException.class,
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   392
                () -> c.addAll(Collections.emptyList()),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   393
                () -> c.remove(ABSENT_VALUE),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   394
                () -> c.removeAll(Collections.emptyList()),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   395
                () -> c.removeIf(x -> false),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   396
                () -> c.retainAll(c));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   397
    }
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   398
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   399
    /**
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   400
     * Test that calling a mutator always throws UOE, even if the mutator
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   401
     * wouldn't actually do anything on an empty collection.
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   402
     *
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   403
     * @param c the collection instance to test, must be empty
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   404
     */
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   405
    private static void testEmptyCollMutatorsAlwaysThrow(Collection<Integer> c) {
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   406
        if (! c.isEmpty()) {
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   407
            fail("collection is not empty");
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   408
        }
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   409
        THROWS(UnsupportedOperationException.class,
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   410
                () -> c.clear());
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   411
    }
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   412
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   413
    /**
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   414
     * As above, for a list.
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   415
     *
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   416
     * @param c the list instance to test
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   417
     */
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   418
    private static void testListMutatorsAlwaysThrow(List<Integer> c) {
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   419
        testCollMutatorsAlwaysThrow(c);
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   420
        THROWS(UnsupportedOperationException.class,
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   421
                () -> c.addAll(0, Collections.emptyList()));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   422
    }
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   423
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   424
    /**
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   425
     * As above, for an empty list.
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   426
     *
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   427
     * @param c the list instance to test, must be empty
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   428
     */
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   429
    private static void testEmptyListMutatorsAlwaysThrow(List<Integer> c) {
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   430
        if (! c.isEmpty()) {
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   431
            fail("list is not empty");
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   432
        }
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   433
        testEmptyCollMutatorsAlwaysThrow(c);
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   434
        THROWS(UnsupportedOperationException.class,
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   435
                () -> c.replaceAll(x -> x),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   436
                () -> c.sort(null));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   437
    }
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   438
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   439
    /**
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   440
     * As above, for a map.
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   441
     *
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   442
     * @param m the map instance to test
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   443
     */
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   444
    private static void testMapMutatorsAlwaysThrow(Map<Integer,Integer> m) {
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   445
        THROWS(UnsupportedOperationException.class,
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   446
                () -> m.compute(ABSENT_VALUE, (k, v) -> null),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   447
                () -> m.computeIfAbsent(ABSENT_VALUE, k -> null),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   448
                () -> m.computeIfPresent(ABSENT_VALUE, (k, v) -> null),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   449
                () -> m.merge(ABSENT_VALUE, 0, (k, v) -> null),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   450
                () -> m.putAll(Collections.emptyMap()),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   451
                () -> m.remove(ABSENT_VALUE),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   452
                () -> m.remove(ABSENT_VALUE, 0),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   453
                () -> m.replace(ABSENT_VALUE, 0),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   454
                () -> m.replace(ABSENT_VALUE, 0, 1));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   455
    }
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   456
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   457
    /**
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   458
     * As above, for an empty map.
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   459
     *
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   460
     * @param map the map instance to test, must be empty
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   461
     */
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   462
    private static void testEmptyMapMutatorsAlwaysThrow(Map<Integer,Integer> m) {
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   463
        if (! m.isEmpty()) {
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   464
            fail("map is not empty");
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   465
        }
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   466
        THROWS(UnsupportedOperationException.class,
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   467
                () -> m.clear(),
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   468
                () -> m.replaceAll((k, v) -> v));
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   469
    }
01ffe34b7340 8159404: throw UnsupportedOperationException unconditionally for mutator methods
smarks
parents: 34527
diff changeset
   470
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    private static void clear(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        try { c.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        testEmptyCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   477
    private static <K,V> void testEmptyMap(final Map<K,V> m) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        check(m.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        equal(m.size(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        equal(m.toString(),"{}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        testEmptySet(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        testEmptySet(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        testEmptyCollection(m.values());
494
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   484
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   485
        try { check(! m.containsValue(null)); }
20476
2d01671506ba 8023339: Refined Collection.removeIf UOE conditions
mduigou
parents: 19855
diff changeset
   486
        catch (NullPointerException ignored) { /* OK */ }
494
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   487
        try { check(! m.containsKey(null)); }
20476
2d01671506ba 8023339: Refined Collection.removeIf UOE conditions
mduigou
parents: 19855
diff changeset
   488
        catch (NullPointerException ignored) { /* OK */ }
494
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   489
        check(! m.containsValue(1));
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 493
diff changeset
   490
        check(! m.containsKey(1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private static void testImmutableMap(final Map<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        THROWS(UnsupportedOperationException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   495
               () -> m.put(1,1),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   496
               () -> m.putAll(singletonMap(1,1)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (! m.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            final Integer first = m.keySet().iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            THROWS(UnsupportedOperationException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   500
                   () -> m.remove(first),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   501
                   () -> m.clear());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            final Map.Entry<Integer,Integer> me
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                = m.entrySet().iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            Integer key = me.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            Integer val = me.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            THROWS(UnsupportedOperationException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   507
                   () -> me.setValue(3));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            equal(key, me.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            equal(val, me.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        testImmutableSet(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        testImmutableCollection(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        //testImmutableSet(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    private static void clear(Map<?,?> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        try { m.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        testEmptyMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    private static void oneElement(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        clear(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            check(c.add(-42));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            equal(c.toString(), "[-42]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (c instanceof Set) check(! c.add(-42));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        check(! c.isEmpty()); check(c.size() == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private static boolean supportsAdd(Collection<Integer> c) {
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   533
        try { check(c.add(ABSENT_VALUE)); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        catch (UnsupportedOperationException t) { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        try {
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   538
            check(c.contains(ABSENT_VALUE));
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   539
            check(c.remove(ABSENT_VALUE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    private static boolean supportsRemove(Collection<Integer> c) {
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   545
        try { check(! c.remove(ABSENT_VALUE)); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        catch (UnsupportedOperationException t) { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   551
    // 6260652: (coll) Arrays.asList(x).toArray().getClass()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   552
    //          should be Object[].class
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   553
    // Fixed in jdk9, but not jdk8 ...
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   554
    static final boolean needToWorkAround6260652 =
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   555
        Arrays.asList("").toArray().getClass() != Object[].class;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   556
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    private static void checkFunctionalInvariants(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            checkContainsSelf(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            checkContainsEmpty(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            check(c.size() != 0 ^ c.isEmpty());
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   562
            check(! c.contains(ABSENT_VALUE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                for (Integer i : c) size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                check(c.size() == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   570
            if (c instanceof Set) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   571
                checkUnique((Set<Integer>)c);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   572
            }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
   573
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            check(c.toArray().length == c.size());
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   575
            check(c.toArray().getClass() == Object[].class
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   576
                  ||
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   577
                  (needToWorkAround6260652 &&
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
   578
                   c.getClass().getName().equals("java.util.Arrays$ArrayList")));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            for (int size : new int[]{0,1,c.size(), c.size()+1}) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                Integer[] a = c.toArray(new Integer[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                check((size > c.size()) || a.length == c.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                int i = 0; for (Integer j : c) check(a[i++] == j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                check((size <= c.size()) || (a[c.size()] == null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                check(a.getClass() == Integer[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            check(c.equals(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            if (c instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                //System.out.printf("Serializing %s%n", c.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    Object clone = serialClone(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    equal(c instanceof Serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                          clone instanceof Serializable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    equal(c instanceof RandomAccess,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                          clone instanceof RandomAccess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    if ((c instanceof List) || (c instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                        equal(c, clone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                        equal(new HashSet<Integer>(c),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                              new HashSet<Integer>(serialClone(c)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                } catch (Error xxx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    if (! (xxx.getCause() instanceof NotSerializableException))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                        throw xxx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    // If add(null) succeeds, contains(null) & remove(null) should succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    private static void testNullElement(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            check(c.add(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            if (c.size() == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                equal(c.toString(), "[null]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                checkFunctionalInvariants(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                check(c.contains(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                check(c.remove(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        catch (NullPointerException e) { /* OK */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    // If add("x") succeeds, contains("x") & remove("x") should succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    private static void testStringElement(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        Collection x = (Collection)c; // Make type-unsafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            check(x.add("x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                check(x.contains("x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                check(x.remove("x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        catch (ClassCastException e) { /* OK */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    private static void testConcurrentCollection(Collection<Integer> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            c.add(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            Iterator<Integer> it = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            clear(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            check(it.next() instanceof Integer); // No CME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            check(c.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    private static void testQueue(Queue<Integer> q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        q.clear();
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   661
        for (int i = 0; i < 5; i++) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   662
            testQueueAddRemove(q, null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   663
            testQueueAddRemove(q, 537);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            q.add(i);
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   665
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        equal(q.size(), 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        checkFunctionalInvariants(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        q.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        equal(q.size(), 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        checkFunctionalInvariants(q);
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   671
        if ((q instanceof LinkedBlockingQueue)   ||
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   672
            (q instanceof LinkedBlockingDeque)   ||
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   673
            (q instanceof ConcurrentLinkedDeque) ||
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   674
            (q instanceof ConcurrentLinkedQueue)) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   675
            testQueueIteratorRemove(q);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   676
        }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   677
    }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   678
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   679
    private static void testQueueAddRemove(final Queue<Integer> q,
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   680
                                           final Integer e) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   681
        final List<Integer> originalContents = new ArrayList<Integer>(q);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   682
        final boolean isEmpty = q.isEmpty();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   683
        final boolean isList = (q instanceof List);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   684
        final List asList = isList ? (List) q : null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   685
        check(!q.contains(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   686
        try {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   687
            q.add(e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   688
        } catch (NullPointerException npe) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   689
            check(e == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   690
            return;                     // Null elements not supported
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   691
        }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   692
        check(q.contains(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   693
        check(q.remove(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   694
        check(!q.contains(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   695
        equal(new ArrayList<Integer>(q), originalContents);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   696
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   697
        if (q instanceof Deque<?>) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   698
            final Deque<Integer> deq = (Deque<Integer>) q;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   699
            final List<Integer> singleton = Collections.singletonList(e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   700
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   701
            // insert, query, remove element at head
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   702
            if (isEmpty) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   703
                THROWS(NoSuchElementException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   704
                       () -> deq.getFirst(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   705
                       () -> deq.element(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   706
                       () -> deq.iterator().next());
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   707
                check(deq.peekFirst() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   708
                check(deq.peek() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   709
            } else {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   710
                check(deq.getFirst() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   711
                check(deq.element() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   712
                check(deq.iterator().next() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   713
                check(deq.peekFirst() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   714
                check(deq.peek() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   715
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   716
            check(!deq.contains(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   717
            check(!deq.removeFirstOccurrence(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   718
            check(!deq.removeLastOccurrence(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   719
            if (isList) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   720
                check(asList.indexOf(e) == -1);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   721
                check(asList.lastIndexOf(e) == -1);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   722
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   723
            switch (rnd.nextInt(isList ? 4 : 3)) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   724
            case 0: deq.addFirst(e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   725
            case 1: check(deq.offerFirst(e)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   726
            case 2: deq.push(e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   727
            case 3: asList.add(0, e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   728
            default: throw new AssertionError();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   729
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   730
            check(deq.peekFirst() == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   731
            check(deq.getFirst() == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   732
            check(deq.element() == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   733
            check(deq.peek() == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   734
            check(deq.iterator().next() == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   735
            check(deq.contains(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   736
            if (isList) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   737
                check(asList.get(0) == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   738
                check(asList.indexOf(e) == 0);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   739
                check(asList.lastIndexOf(e) == 0);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   740
                check(asList.subList(0, 1).equals(singleton));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   741
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   742
            switch (rnd.nextInt(isList ? 11 : 9)) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   743
            case 0: check(deq.pollFirst() == e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   744
            case 1: check(deq.removeFirst() == e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   745
            case 2: check(deq.remove() == e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   746
            case 3: check(deq.pop() == e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   747
            case 4: check(deq.removeFirstOccurrence(e)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   748
            case 5: check(deq.removeLastOccurrence(e)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   749
            case 6: check(deq.remove(e)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   750
            case 7: check(deq.removeAll(singleton)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   751
            case 8: Iterator it = deq.iterator(); it.next(); it.remove(); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   752
            case 9: asList.remove(0); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   753
            case 10: asList.subList(0, 1).clear(); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   754
            default: throw new AssertionError();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   755
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   756
            if (isEmpty) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   757
                THROWS(NoSuchElementException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   758
                       () -> deq.getFirst(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   759
                       () -> deq.element(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   760
                       () -> deq.iterator().next());
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   761
                check(deq.peekFirst() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   762
                check(deq.peek() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   763
            } else {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   764
                check(deq.getFirst() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   765
                check(deq.element() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   766
                check(deq.iterator().next() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   767
                check(deq.peekFirst() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   768
                check(deq.peek() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   769
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   770
            check(!deq.contains(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   771
            check(!deq.removeFirstOccurrence(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   772
            check(!deq.removeLastOccurrence(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   773
            if (isList) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   774
                check(isEmpty || asList.get(0) != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   775
                check(asList.indexOf(e) == -1);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   776
                check(asList.lastIndexOf(e) == -1);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   777
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   778
            equal(new ArrayList<Integer>(deq), originalContents);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   779
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   780
            // insert, query, remove element at tail
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   781
            if (isEmpty) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   782
                check(deq.peekLast() == null);
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   783
                THROWS(NoSuchElementException.class, () -> deq.getLast());
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   784
            } else {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   785
                check(deq.peekLast() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   786
                check(deq.getLast() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   787
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   788
            switch (rnd.nextInt(isList ? 6 : 4)) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   789
            case 0: deq.addLast(e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   790
            case 1: check(deq.offerLast(e)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   791
            case 2: check(deq.add(e)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   792
            case 3: deq.addAll(singleton); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   793
            case 4: asList.addAll(deq.size(), singleton); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   794
            case 5: asList.add(deq.size(), e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   795
            default: throw new AssertionError();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   796
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   797
            check(deq.peekLast() == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   798
            check(deq.getLast() == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   799
            check(deq.contains(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   800
            if (isList) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   801
                ListIterator it = asList.listIterator(asList.size());
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   802
                check(it.previous() == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   803
                check(asList.get(asList.size() - 1) == e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   804
                check(asList.indexOf(e) == asList.size() - 1);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   805
                check(asList.lastIndexOf(e) == asList.size() - 1);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   806
                int size = asList.size();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   807
                check(asList.subList(size - 1, size).equals(singleton));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   808
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   809
            switch (rnd.nextInt(isList ? 8 : 6)) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   810
            case 0: check(deq.pollLast() == e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   811
            case 1: check(deq.removeLast() == e); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   812
            case 2: check(deq.removeFirstOccurrence(e)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   813
            case 3: check(deq.removeLastOccurrence(e)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   814
            case 4: check(deq.remove(e)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   815
            case 5: check(deq.removeAll(singleton)); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   816
            case 6: asList.remove(asList.size() - 1); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   817
            case 7:
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   818
                ListIterator it = asList.listIterator(asList.size());
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   819
                it.previous();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   820
                it.remove();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   821
                break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   822
            default: throw new AssertionError();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   823
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   824
            if (isEmpty) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   825
                check(deq.peekLast() == null);
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   826
                THROWS(NoSuchElementException.class, () -> deq.getLast());
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   827
            } else {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   828
                check(deq.peekLast() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   829
                check(deq.getLast() != e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   830
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   831
            check(!deq.contains(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   832
            equal(new ArrayList<Integer>(deq), originalContents);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   833
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   834
            // Test operations on empty deque
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   835
            switch (rnd.nextInt(isList ? 4 : 2)) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   836
            case 0: deq.clear(); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   837
            case 1:
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   838
                Iterator it = deq.iterator();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   839
                while (it.hasNext()) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   840
                    it.next();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   841
                    it.remove();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   842
                }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   843
                break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   844
            case 2: asList.subList(0, asList.size()).clear(); break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   845
            case 3:
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   846
                ListIterator lit = asList.listIterator(asList.size());
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   847
                while (lit.hasPrevious()) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   848
                    lit.previous();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   849
                    lit.remove();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   850
                }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   851
                break;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   852
            default: throw new AssertionError();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   853
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   854
            testEmptyCollection(deq);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   855
            check(!deq.iterator().hasNext());
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   856
            if (isList) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   857
                check(!asList.listIterator().hasPrevious());
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   858
                THROWS(NoSuchElementException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   859
                       () -> asList.listIterator().previous());
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   860
            }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   861
            THROWS(NoSuchElementException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   862
                   () -> deq.iterator().next(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   863
                   () -> deq.element(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   864
                   () -> deq.getFirst(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   865
                   () -> deq.getLast(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   866
                   () -> deq.pop(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   867
                   () -> deq.remove(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   868
                   () -> deq.removeFirst(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   869
                   () -> deq.removeLast());
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   870
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   871
            check(deq.poll() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   872
            check(deq.pollFirst() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   873
            check(deq.pollLast() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   874
            check(deq.peek() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   875
            check(deq.peekFirst() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   876
            check(deq.peekLast() == null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   877
            check(!deq.removeFirstOccurrence(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   878
            check(!deq.removeLastOccurrence(e));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   879
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   880
            check(deq.addAll(originalContents) == !isEmpty);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   881
            equal(new ArrayList<Integer>(deq), originalContents);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   882
            check(!deq.addAll(Collections.<Integer>emptyList()));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   883
            equal(new ArrayList<Integer>(deq), originalContents);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   884
        }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   885
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 3708
diff changeset
   886
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   887
    private static void testQueueIteratorRemove(Queue<Integer> q) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   888
        System.err.printf("testQueueIteratorRemove %s%n",
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   889
                          q.getClass().getSimpleName());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   890
        q.clear();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   891
        for (int i = 0; i < 5; i++)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   892
            q.add(i);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   893
        Iterator<Integer> it = q.iterator();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   894
        check(it.hasNext());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   895
        for (int i = 3; i >= 0; i--)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   896
            q.remove(i);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   897
        equal(it.next(), 0);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   898
        equal(it.next(), 4);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   899
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   900
        q.clear();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   901
        for (int i = 0; i < 5; i++)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   902
            q.add(i);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   903
        it = q.iterator();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   904
        equal(it.next(), 0);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   905
        check(it.hasNext());
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   906
        for (int i = 1; i < 4; i++)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   907
            q.remove(i);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   908
        equal(it.next(), 1);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   909
        equal(it.next(), 4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    private static void testList(final List<Integer> l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        // doesn't throw IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            l.addAll(-1, Collections.<Integer>emptyList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            fail("Expected IndexOutOfBoundsException not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
20476
2d01671506ba 8023339: Refined Collection.removeIf UOE conditions
mduigou
parents: 19855
diff changeset
   921
        catch (UnsupportedOperationException ignored) {/* OK */}
2d01671506ba 8023339: Refined Collection.removeIf UOE conditions
mduigou
parents: 19855
diff changeset
   922
        catch (IndexOutOfBoundsException ignored) {/* OK */}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
//      equal(l instanceof Serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
//            l.subList(0,0) instanceof Serializable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        if (l.subList(0,0) instanceof Serializable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            check(l instanceof Serializable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        equal(l instanceof RandomAccess,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
              l.subList(0,0) instanceof RandomAccess);
23580
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   932
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   933
        l.iterator();
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   934
        l.listIterator();
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   935
        l.listIterator(0);
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   936
        l.listIterator(l.size());
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   937
        THROWS(IndexOutOfBoundsException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   938
               () -> l.listIterator(-1),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
   939
               () -> l.listIterator(l.size() + 1));
23580
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   940
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   941
        if (l instanceof AbstractList) {
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   942
            try {
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   943
                int size = l.size();
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   944
                AbstractList<Integer> abList = (AbstractList<Integer>) l;
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   945
                Method m = AbstractList.class.getDeclaredMethod("removeRange", new Class[] { int.class, int.class });
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   946
                m.setAccessible(true);
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   947
                m.invoke(abList, new Object[] { 0, 0 });
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   948
                m.invoke(abList, new Object[] { size, size });
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   949
                equal(size, l.size());
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   950
            }
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   951
            catch (UnsupportedOperationException ignored) {/* OK */}
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   952
            catch (Throwable t) { unexpected(t); }
1055a611c69b 8014066: Remove redundant restriction from ArrayList#removeRange() spec
igerasim
parents: 23010
diff changeset
   953
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    private static void testCollection(Collection<Integer> c) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   957
        try { testCollection1(c); }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   958
        catch (Throwable t) { unexpected(t); }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   959
    }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   960
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2428
diff changeset
   961
    private static void testCollection1(Collection<Integer> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        System.out.println("\n==> " + c.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        checkFunctionalInvariants(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        if (! supportsAdd(c)) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        //System.out.println("add() supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   970
        if (c instanceof NavigableSet) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   971
            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
   972
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   973
            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
   974
            testNavigableSet(ns);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   975
            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
   976
            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
   977
            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
   978
            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
   979
            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
   980
            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
   981
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if (c instanceof Queue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            testQueue((Queue<Integer>)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        if (c instanceof List)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            testList((List<Integer>)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        check(supportsRemove(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            checkFunctionalInvariants(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        clear(c);      testNullElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        oneElement(c); testNullElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        clear(c);      testStringElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        oneElement(c); testStringElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (c.getClass().getName().matches(".*concurrent.*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            testConcurrentCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        // The "all" operations should throw NPE when passed null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        {
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1010
            clear(c);
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1011
            try {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1012
                c.removeAll(null);
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1013
                fail("Expected NullPointerException");
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1014
            }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1015
            catch (NullPointerException e) { pass(); }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1016
            catch (Throwable t) { unexpected(t); }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1017
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                c.removeAll(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                fail("Expected NullPointerException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            catch (NullPointerException e) { pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1026
            clear(c);
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1027
            try {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1028
                c.retainAll(null);
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1029
                fail("Expected NullPointerException");
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1030
            }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1031
            catch (NullPointerException e) { pass(); }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1032
            catch (Throwable t) { unexpected(t); }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1033
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                c.retainAll(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                fail("Expected NullPointerException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            catch (NullPointerException e) { pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                c.addAll(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                fail("Expected NullPointerException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            catch (NullPointerException e) { pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            oneElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                c.containsAll(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                fail("Expected NullPointerException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            catch (NullPointerException e) { pass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    // Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    private static void checkFunctionalInvariants(Map<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        check(m.keySet().size() == m.entrySet().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        check(m.keySet().size() == m.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        checkFunctionalInvariants(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        checkFunctionalInvariants(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        check(m.size() != 0 ^ m.isEmpty());
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1069
        check(! m.containsKey(ABSENT_VALUE));
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1070
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1071
        if (m instanceof Serializable) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1072
            //System.out.printf("Serializing %s%n", m.getClass().getName());
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1073
            try {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1074
                Object clone = serialClone(m);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1075
                equal(m instanceof Serializable,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1076
                      clone instanceof Serializable);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1077
                equal(m, clone);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1078
            } catch (Error xxx) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1079
                if (! (xxx.getCause() instanceof NotSerializableException))
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1080
                    throw xxx;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1081
            }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1082
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    private static void testMap(Map<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        System.out.println("\n==> " + m.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        if (m instanceof ConcurrentMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            testConcurrentMap((ConcurrentMap<Integer,Integer>) m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1091
        if (m instanceof NavigableMap) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1092
            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
  1093
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1094
            NavigableMap<Integer,Integer> nm =
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1095
                (NavigableMap<Integer,Integer>) m;
2428
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1096
            testNavigableMapRemovers(nm);
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1097
            testNavigableMap(nm);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1098
            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
  1099
            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
  1100
            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
  1101
            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
  1102
            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
  1103
            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
  1104
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        checkFunctionalInvariants(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        if (supportsClear(m)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            try { clear(m); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        if (supportsPut(m)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                check(m.put(3333, 77777) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                check(m.put(9134, 74982) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                check(m.get(9134) == 74982);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                check(m.put(9134, 1382) == 74982);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                check(m.get(9134) == 1382);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                check(m.size() == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                checkFunctionalInvariants(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                checkNPEConsistency(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    private static boolean supportsPut(Map<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        // We're asking for .equals(...) semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        if (m instanceof IdentityHashMap) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1132
        try { check(m.put(ABSENT_VALUE,12735) == null); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        catch (UnsupportedOperationException t) { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        try {
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1137
            check(m.containsKey(ABSENT_VALUE));
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32991
diff changeset
  1138
            check(m.remove(ABSENT_VALUE) != null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    private static boolean supportsClear(Map<?,?> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        try { m.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        catch (UnsupportedOperationException t) { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    // ConcurrentMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        System.out.println("ConcurrentMap tests...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            clear(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            check(m.putIfAbsent(18357,7346) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            check(m.putIfAbsent(18357,8263) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            try { m.putIfAbsent(18357,null); fail("NPE"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            catch (NullPointerException t) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            check(! m.replace(18357,8888,7777));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            try { m.replace(18357,null,7777); fail("NPE"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            catch (NullPointerException t) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            check(m.replace(18357,7346,5555));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            check(m.replace(18357,5555,7346));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            check(m.replace(92347,7834) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            try { m.replace(18357,null); fail("NPE"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            catch (NullPointerException t) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            check(m.replace(18357,7346) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            check(m.replace(18357,5555) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            check(m.get(18357) == 5555);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            check(m.replace(18357,7346) == 5555);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            check(! m.remove(18357,9999));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            check(m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            check(! m.remove(18357,null)); // 6272521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            check(m.get(18357) == 7346);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            check(m.remove(18357,7346));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            check(m.get(18357) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            check(! m.containsKey(18357));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            check(m.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            m.putIfAbsent(1,2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            check(m.size() == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            check(! m.remove(1,null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            check(! m.remove(1,null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            check(! m.remove(1,1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            check(m.remove(1,2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            check(m.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            testEmptyMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    private static void throwsConsistently(Class<? extends Throwable> k,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                           Iterable<Fun> fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        List<Class<? extends Throwable>> threw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            = new ArrayList<Class<? extends Throwable>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            try { f.f(); threw.add(null); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                check(k.isAssignableFrom(t.getClass()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                threw.add(t.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        if (new HashSet<Object>(threw).size() != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            fail(threw.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        m.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            ? (ConcurrentMap<T,Integer>) m
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        List<Fun> fs = new ArrayList<Fun>();
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1228
        fs.add(() -> check(! m.containsKey(null)));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1229
        fs.add(() -> equal(m.remove(null), null));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1230
        fs.add(() -> equal(m.get(null), null));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1231
        if (cm != null)
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1232
            fs.add(() -> check(! cm.remove(null,null)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        throwsConsistently(NullPointerException.class, fs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        fs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        final Map<T,Integer> sm = singletonMap(null,1);
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1237
        fs.add(() -> { equal(m.put(null,1), null); m.clear();});
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1238
        fs.add(() -> { m.putAll(sm); m.clear();});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        if (cm != null) {
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1240
            fs.add(() -> check(! cm.remove(null,null)));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1241
            fs.add(() -> equal(cm.putIfAbsent(null,1), 1));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1242
            fs.add(() -> equal(cm.replace(null,1), null));
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1243
            fs.add(() -> equal(cm.replace(null,1, 1), 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        throwsConsistently(NullPointerException.class, fs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    // NavigableMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    private static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                              Integer i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                              Integer lower,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                              Integer floor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                              Integer ceiling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                              Integer higher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        equal(m.lowerKey(i),   lower);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        equal(m.floorKey(i),   floor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        equal(m.ceilingKey(i), ceiling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        equal(m.higherKey(i),  higher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    private static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        checkNavigableSetKeys(NavigableSet<Integer> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                              Integer i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                              Integer lower,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                              Integer floor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                              Integer ceiling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                              Integer higher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        equal(m.lower(i),   lower);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        equal(m.floor(i),   floor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        equal(m.ceiling(i), ceiling);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        equal(m.higher(i),  higher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    static final Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    static void equalNext(final Iterator<?> it, Object expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        equal(it.next(), expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
2428
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1284
    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
  1285
        equal(m1, m2);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1286
        equal(m2, m1);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1287
        equal(m1.size(), m2.size());
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1288
        equal(m1.isEmpty(), m2.isEmpty());
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1289
        equal(m1.toString(), m2.toString());
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1290
        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
  1291
    }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1292
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1293
    @SuppressWarnings({"unchecked", "rawtypes"})
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1294
    static void testNavigableMapRemovers(NavigableMap m)
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1295
    {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1296
        final Map emptyMap = new HashMap();
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1297
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1298
        final Map singletonMap = new HashMap();
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1299
        singletonMap.put(1, 2);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1300
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1301
        abstract class NavigableMapView {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1302
            abstract NavigableMap view(NavigableMap m);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1303
        }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1304
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1305
        NavigableMapView[] views = {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1306
            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
  1307
                return m; }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1308
            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
  1309
                return m.headMap(99, true); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1310
            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
  1311
                return m.tailMap(-99, false); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1312
            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
  1313
                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
  1314
        };
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1315
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1316
        abstract class Remover {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1317
            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
  1318
        }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1319
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1320
        Remover[] removers = {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1321
            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
  1322
                equal(m.remove(k), v); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1323
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1324
            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
  1325
                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
  1326
            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
  1327
                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
  1328
            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
  1329
                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
  1330
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1331
            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
  1332
                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
  1333
            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
  1334
                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
  1335
            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
  1336
                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
  1337
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1338
            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
  1339
                check(m.keySet().remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1340
            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
  1341
                check(m.navigableKeySet().remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1342
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1343
            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
  1344
                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
  1345
            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
  1346
                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
  1347
            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
  1348
                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
  1349
                      .remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1350
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1351
            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
  1352
                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
  1353
            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
  1354
                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
  1355
            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
  1356
                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
  1357
                      .remove(k)); }},
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1358
        };
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1359
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1360
        for (NavigableMapView view : views) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1361
            for (Remover remover : removers) {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1362
                try {
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1363
                    m.clear();
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1364
                    equalMaps(m, emptyMap);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1365
                    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
  1366
                    equalMaps(m, singletonMap);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1367
                    NavigableMap v = view.view(m);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1368
                    remover.remove(v, 1, 2);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1369
                    equalMaps(m, emptyMap);
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1370
                } catch (Throwable t) { unexpected(t); }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1371
            }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1372
        }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1373
    }
e63d91602813 6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents: 494
diff changeset
  1374
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    private static void testNavigableMap(NavigableMap<Integer,Integer> m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        clear(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        checkNavigableMapKeys(m, 1, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        equal(m.put(1, 2), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        equal(m.put(3, 4), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        equal(m.put(5, 9), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        equal(m.put(1, 2), 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        equal(m.put(3, 4), 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        equal(m.put(5, 6), 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        checkNavigableMapKeys(m, 0, null, null,    1,    1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        checkNavigableMapKeys(m, 1, null,    1,    1,    3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        checkNavigableMapKeys(m, 2,    1,    1,    3,    3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        checkNavigableMapKeys(m, 3,    1,    3,    3,    5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        checkNavigableMapKeys(m, 5,    3,    5,    5, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        checkNavigableMapKeys(m, 6,    5,    5, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1395
        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
  1396
                 (Iterator<Integer>[])
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1397
                 new Iterator<?>[] {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1398
                     m.descendingKeySet().iterator(),
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1399
                     m.navigableKeySet().descendingIterator()}) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            equalNext(it, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            equalNext(it, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            equalNext(it, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            check(! it.hasNext());
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1404
            THROWS(NoSuchElementException.class, () -> it.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            final Iterator<Map.Entry<Integer,Integer>> it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                = m.descendingMap().entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            check(it.hasNext()); equal(it.next().getKey(), 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            check(it.hasNext()); equal(it.next().getKey(), 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            check(it.hasNext()); equal(it.next().getKey(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            check(! it.hasNext());
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1414
            THROWS(NoSuchElementException.class, () -> it.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        }
20757
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1416
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1417
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1418
        checkDescItrRmFirst(m.keySet(), m.navigableKeySet().descendingIterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1419
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1420
        checkDescItrRmMid(m.keySet(), m.navigableKeySet().descendingIterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1421
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1422
        checkDescItrRmLast(m.keySet(), m.navigableKeySet().descendingIterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1423
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1424
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1425
        checkDescItrRmFirst(m.keySet(), m.descendingMap().keySet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1426
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1427
        checkDescItrRmMid(m.keySet(), m.descendingMap().keySet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1428
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1429
        checkDescItrRmLast(m.keySet(), m.descendingMap().keySet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1430
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1431
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1432
        checkDescItrRmFirst(m.keySet(), m.descendingKeySet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1433
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1434
        checkDescItrRmMid(m.keySet(), m.descendingKeySet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1435
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1436
        checkDescItrRmLast(m.keySet(), m.descendingKeySet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1437
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1438
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1439
        checkDescItrRmFirst(m.values(), m.descendingMap().values().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1440
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1441
        checkDescItrRmMid(m.values(), m.descendingMap().values().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1442
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1443
        checkDescItrRmLast(m.values(), m.descendingMap().values().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1444
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1445
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1446
        checkDescItrRmFirst((Collection)m.entrySet(),
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1447
                            m.descendingMap().entrySet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1448
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1449
        checkDescItrRmMid((Collection)m.entrySet(),
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1450
                          m.descendingMap().entrySet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1451
        prepMapForDescItrTests(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1452
        checkDescItrRmLast((Collection)m.entrySet(),
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1453
                           m.descendingMap().entrySet().iterator());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    private static void testNavigableSet(NavigableSet<Integer> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        clear(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        checkNavigableSetKeys(s, 1, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        check(s.add(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        check(s.add(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        check(s.add(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        check(! s.add(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        check(! s.add(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        check(! s.add(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        checkNavigableSetKeys(s, 0, null, null,    1,    1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        checkNavigableSetKeys(s, 1, null,    1,    1,    3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        checkNavigableSetKeys(s, 2,    1,    1,    3,    3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        checkNavigableSetKeys(s, 3,    1,    3,    3,    5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        checkNavigableSetKeys(s, 5,    3,    5,    5, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        checkNavigableSetKeys(s, 6,    5,    5, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1475
        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
  1476
                 (Iterator<Integer>[])
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1477
                 new Iterator<?>[] {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1478
                     s.descendingIterator(),
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
  1479
                     s.descendingSet().iterator()}) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            equalNext(it, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            equalNext(it, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            equalNext(it, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            check(! it.hasNext());
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1484
            THROWS(NoSuchElementException.class, () -> it.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        }
20757
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1486
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1487
        prepSetForDescItrTests(s);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1488
        checkDescItrRmFirst(s, s.descendingIterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1489
        prepSetForDescItrTests(s);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1490
        checkDescItrRmMid(s, s.descendingIterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1491
        prepSetForDescItrTests(s);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1492
        checkDescItrRmLast(s, s.descendingIterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1493
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1494
        prepSetForDescItrTests(s);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1495
        checkDescItrRmFirst(s, s.descendingSet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1496
        prepSetForDescItrTests(s);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1497
        checkDescItrRmMid(s, s.descendingSet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1498
        prepSetForDescItrTests(s);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1499
        checkDescItrRmLast(s, s.descendingSet().iterator());
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1500
    }
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1501
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1502
    private static void prepSetForDescItrTests(Set s) {
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1503
        clear(s);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1504
        check(s.add(1));
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1505
        check(s.add(3));
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1506
        check(s.add(5));
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1507
    }
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1508
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1509
    private static void prepMapForDescItrTests(Map m) {
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1510
        clear(m);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1511
        equal(m.put(1, 2), null);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1512
        equal(m.put(3, 4), null);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1513
        equal(m.put(5, 9), null);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1514
    }
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1515
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1516
    //--------------------------------------------------------------------
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1517
    // Check behavior of descending iterator when first element is removed
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1518
    //--------------------------------------------------------------------
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1519
    private static <T> void checkDescItrRmFirst(Collection<T> ascColl,
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1520
                                                Iterator<T> descItr) {
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1521
        T[] expected = (T[]) ascColl.toArray();
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1522
        int idx = expected.length -1;
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1523
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1524
        equalNext(descItr, expected[idx--]);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1525
        descItr.remove();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 31540
diff changeset
  1526
        while (idx >= 0 && descItr.hasNext()) {
20757
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1527
            equalNext(descItr, expected[idx--]);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1528
        }
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1529
        equal(descItr.hasNext(), false);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1530
        equal(idx, -1);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1531
    }
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1532
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1533
    //-----------------------------------------------------------------------
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1534
    // Check behavior of descending iterator when a middle element is removed
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1535
    //-----------------------------------------------------------------------
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1536
    private static <T> void checkDescItrRmMid(Collection<T> ascColl,
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1537
                                              Iterator<T> descItr) {
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1538
        T[] expected = (T[]) ascColl.toArray();
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1539
        int idx = expected.length -1;
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1540
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1541
        while (idx >= expected.length / 2) {
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1542
            equalNext(descItr, expected[idx--]);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1543
        }
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1544
        descItr.remove();
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1545
        while (idx >= 0 && descItr.hasNext()) {
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1546
            equalNext(descItr, expected[idx--]);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1547
        }
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1548
        equal(descItr.hasNext(), false);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1549
        equal(idx, -1);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1550
    }
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1551
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1552
    //-----------------------------------------------------------------------
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1553
    // Check behavior of descending iterator when the last element is removed
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1554
    //-----------------------------------------------------------------------
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1555
    private static <T> void checkDescItrRmLast(Collection<T> ascColl,
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1556
                                               Iterator<T> descItr) {
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1557
        T[] expected = (T[]) ascColl.toArray();
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1558
        int idx = expected.length -1;
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1559
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1560
        while (idx >= 0 && descItr.hasNext()) {
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1561
            equalNext(descItr, expected[idx--]);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1562
        }
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1563
        equal(idx, -1);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1564
        equal(descItr.hasNext(), false);
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1565
        descItr.remove();
1e9f01f43f5c 8024709: TreeMap.DescendingKeyIterator 'remove' confuses iterator position
bchristi
parents: 20476
diff changeset
  1566
        equal(ascColl.contains(expected[0]), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    static void pass() { passed++; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    static void fail() { failed++; Thread.dumpStack(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
    static void fail(String msg) { System.out.println(msg); fail(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    static void check(boolean cond) { if (cond) pass(); else fail(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        else {System.out.println(x + " not equal to " + y); fail();}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        try { realMain(args); } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        if (failed > 0) throw new Exception("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    }
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23580
diff changeset
  1586
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
          for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
              try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
              catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                  if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                  else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    static byte[] serializedForm(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            new ObjectOutputStream(baos).writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        } catch (IOException e) { throw new Error(e); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    static Object readObject(byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        InputStream is = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        return new ObjectInputStream(is).readObject();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    static <T> T serialClone(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        try { return (T) readObject(serializedForm(obj)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        catch (Exception e) { throw new Error(e); }}
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1607
    private static class NewAbstractCollection<E> extends AbstractCollection<E> {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1608
        ArrayList<E> list = new ArrayList<>();
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1609
        public boolean remove(Object obj) {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1610
            return list.remove(obj);
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1611
        }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1612
        public boolean add(E e) {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1613
            return list.add(e);
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1614
        }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1615
        public Iterator<E> iterator() {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1616
            return list.iterator();
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1617
        }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1618
        public int size() {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1619
            return list.size();
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1620
        }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1621
    }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1622
    private static class NewAbstractSet<E> extends AbstractSet<E> {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1623
        HashSet<E> set = new HashSet<>();
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1624
        public boolean remove(Object obj) {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1625
            return set.remove(obj);
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1626
        }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1627
        public boolean add(E e) {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1628
            return set.add(e);
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1629
        }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1630
        public Iterator<E> iterator() {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1631
            return set.iterator();
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1632
        }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1633
        public int size() {
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1634
            return set.size();
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1635
        }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1636
    }
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 9035
diff changeset
  1637
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
}