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