jdk/test/java/util/NavigableMap/LockStep.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 30046 cf2c86e1819e
child 32991 b27c76b82713
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
     2
 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 493
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 493
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 493
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
    26
 * @bug     6420753 6242436 6691185
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Compare NavigableMap implementations for identical behavior
7174
5a56e43d896c 6792706: Add JAR file to bootclasspath when using AggressiveOpts
ohair
parents: 5506
diff changeset
    28
 * @run main LockStep
5a56e43d896c 6792706: Add JAR file to bootclasspath when using AggressiveOpts
ohair
parents: 5506
diff changeset
    29
 * @run main/othervm -XX:+AggressiveOpts LockStep
5a56e43d896c 6792706: Add JAR file to bootclasspath when using AggressiveOpts
ohair
parents: 5506
diff changeset
    30
 * @run main/othervm -XX:+AggressiveOpts -Dthorough=true LockStep
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author  Martin Buchholz
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 24692
diff changeset
    32
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import static java.util.Collections.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
@SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class LockStep {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static final int DEFAULT_SIZE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static int size;            // Running time is O(size**2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static int intArg(String[] args, int i, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        return args.length > i ? Integer.parseInt(args[i]) : defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Pass -Dthorough=true for more exhaustive testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final boolean thorough = Boolean.getBoolean("thorough");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static boolean maybe(int n) { return rnd.nextInt(n) == 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void realMain(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        size = intArg(args, 0, DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        lockSteps(new TreeMap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                  new ConcurrentSkipListMap());
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
    59
        lockSteps(new TreeMap(),
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
    60
                  Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
    61
        lockSteps(new TreeMap(),
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
    62
                  Collections.synchronizedNavigableMap(new TreeMap()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        lockSteps(new TreeMap(reverseOrder()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                  new ConcurrentSkipListMap(reverseOrder()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        lockSteps(new TreeSet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                  new ConcurrentSkipListSet());
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
    68
        lockSteps(new TreeSet(),
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
    69
                  Collections.checkedNavigableSet(new TreeSet(), Integer.class));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
    70
        lockSteps(new TreeSet(),
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
    71
                  Collections.synchronizedNavigableSet(new TreeSet()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        lockSteps(new TreeSet(reverseOrder()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                  new ConcurrentSkipListSet(reverseOrder()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void lockSteps(NavigableMap m1, NavigableMap m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (maybe(4)) m1 = serialClone(m1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (maybe(4)) m2 = serialClone(m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        lockStep(m1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                 m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        lockStep(m1.descendingMap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                 m2.descendingMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        lockStep(fullSubMap(m1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                 fullSubMap(m2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        lockStep(fullSubMap(m1.descendingMap()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                 fullSubMap(m2.descendingMap()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        lockStep(fullHeadMap(m1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                 fullHeadMap(m2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        lockStep(fullHeadMap(m1.descendingMap()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                 fullHeadMap(m2.descendingMap()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        lockStep(fullTailMap(m1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                 fullTailMap(m2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        lockStep(fullTailMap(m1.descendingMap()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                 fullTailMap(m2.descendingMap()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static void lockSteps(NavigableSet s1, NavigableSet s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (maybe(4)) s1 = serialClone(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (maybe(4)) s2 = serialClone(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        lockStep(s1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                 s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        lockStep(s1.descendingSet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                 s2.descendingSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        lockStep(fullSubSet(s1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                 fullSubSet(s2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        lockStep(fullSubSet(s1.descendingSet()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                 fullSubSet(s2.descendingSet()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        lockStep(fullHeadSet(s1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                 fullHeadSet(s2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        lockStep(fullHeadSet(s1.descendingSet()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                 fullHeadSet(s2.descendingSet()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        lockStep(fullTailSet(s1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                 fullTailSet(s2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        lockStep(fullTailSet(s1.descendingSet()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                 fullTailSet(s2.descendingSet()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static boolean isAscending(NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Comparator cmp = m.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return (cmp == null || cmp.compare(1, 2) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static NavigableMap fullSubMap(NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return isAscending(m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            ? m.subMap(Integer.MIN_VALUE, true, Integer.MAX_VALUE, true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            : m.subMap(Integer.MAX_VALUE, true, Integer.MIN_VALUE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static NavigableMap fullHeadMap(NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return isAscending(m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ? m.headMap(Integer.MAX_VALUE, true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            : m.headMap(Integer.MIN_VALUE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static NavigableMap fullTailMap(NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return isAscending(m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            ? m.tailMap(Integer.MIN_VALUE, true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            : m.tailMap(Integer.MAX_VALUE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static boolean isAscending(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Comparator cmp = s.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return (cmp == null || cmp.compare(1, 2) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    static NavigableSet fullSubSet(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return isAscending(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            ? s.subSet(Integer.MIN_VALUE, true, Integer.MAX_VALUE, true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            : s.subSet(Integer.MAX_VALUE, true, Integer.MIN_VALUE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static NavigableSet fullHeadSet(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return isAscending(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            ? s.headSet(Integer.MAX_VALUE, true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            : s.headSet(Integer.MIN_VALUE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    static NavigableSet fullTailSet(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return isAscending(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            ? s.tailSet(Integer.MIN_VALUE, true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            : s.tailSet(Integer.MAX_VALUE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    static void testEmptyCollection(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        check(c.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        equal(c.size(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        equal(c.toString(),"[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        equal(c.toArray().length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        equal(c.toArray(new Object[0]).length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        Object[] a = new Object[1]; a[0] = Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        equal(c.toArray(a), a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        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
   174
        check(! c.iterator().hasNext());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static void testEmptySet(Set<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        testEmptyCollection(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        equal(c.hashCode(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        equal2(c, Collections.<Integer>emptySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    static void testEmptyMap(final Map<?,?> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        check(m.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        equal(m.size(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        equal(m.toString(),"{}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        equal(m.hashCode(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        testEmptySet(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        testEmptySet(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        testEmptyCollection(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   193
    static final Random rnd;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   194
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   195
    static {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   196
        // sufficiently random for this test
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   197
        long seed = System.nanoTime();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   198
        System.out.println(LockStep.class.getCanonicalName() + ": Trial random seed: " + seed );
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   199
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   200
        rnd = new Random(seed);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   201
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    static void equalNext(final Iterator<?> it, Object expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (maybe(2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        equal(it.next(), expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    static Comparator comparator(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Comparator cmp = s.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return cmp != null ? cmp : new Comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            public int compare(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                return ((Comparable) o1).compareTo(o2); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    static Comparator comparator(NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        Comparator cmp = m.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return cmp != null ? cmp : new Comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            public int compare(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                return ((Comparable) o1).compareTo(o2); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    static void checkNavigableSet(final NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (s.comparator() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            check(s.descendingSet().descendingSet().comparator() == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        equal(s.isEmpty(), s.size() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        equal2(s, s.descendingSet());
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   228
        if (maybe(4) && s instanceof Serializable) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   229
            try {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   230
                equal2(s, serialClone(s));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   231
            } catch(RuntimeException uhoh) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   232
                if(!(uhoh.getCause() instanceof NotSerializableException)) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   233
                    throw uhoh;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   234
                }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   235
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   236
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        Comparator cmp = comparator(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (s.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            THROWS(NoSuchElementException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   240
                   () -> s.first(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   241
                   () -> s.last());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            equal(null, s.lower(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            equal(null, s.floor(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            equal(null, s.ceiling(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            equal(null, s.higher(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Object a = s.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            Object z = s.last();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            equal(s.lower(a), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            equal(s.higher(z), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            equal2(s, s.tailSet(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            equal2(s, s.headSet(z, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            equal2(s, s.subSet(a, true, z, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            testEmptySet(s.subSet(a, true, a, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            testEmptySet(s.subSet(z, true, z, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            testEmptySet(s.headSet(a, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            testEmptySet(s.tailSet(z, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            equal2(s.headSet(a, true), singleton(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            equal2(s.tailSet(z, true), singleton(z));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        Iterator[] its = new Iterator[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            s.iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            s.descendingSet().descendingSet().iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        for (final Iterator it : its)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (maybe(4))
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   269
                THROWS(IllegalStateException.class, () -> it.remove());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        Object prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        for (Object e : s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            check(s.contains(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            for (Iterator it : its) equalNext(it, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            equal(e, s.ceiling(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            equal(e, s.floor(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            check(s.higher(e) == null || cmp.compare(e, s.higher(e)) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            equal(s.lower(e), prev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                check(cmp.compare(prev, e) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            prev = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        for (final Iterator it : its) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (maybe(2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                check(! it.hasNext());
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   287
            Fun fun = () -> it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            THROWS(NoSuchElementException.class, fun, fun, fun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   292
    static void equalIterators(final Iterator<?> it1,
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   293
                               final Iterator<?> it2) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   294
        while (it1.hasNext()) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   295
            if (maybe(2))
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   296
                check(it2.hasNext());
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   297
            equal(it1.next(), it2.next());
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   298
        }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   299
        check(! it2.hasNext());
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   300
    }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   301
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   302
    static void equalSetsLeaf(final Set s1, final Set s2) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   303
        equal2(s1,            s2);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   304
        equal( s1.size(),     s2.size());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   305
        equal( s1.isEmpty(),  s2.isEmpty());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   306
        equal( s1.hashCode(), s2.hashCode());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   307
        equal( s1.toString(), s2.toString());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   308
        equal( s1.containsAll(s2), s2.containsAll(s1));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   309
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   310
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    static void equalNavigableSetsLeaf(final NavigableSet s1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                       final NavigableSet s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        equal2(s1,            s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        equal( s1.size(),     s2.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        equal( s1.isEmpty(),  s2.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        equal( s1.hashCode(), s2.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        equal( s1.toString(), s2.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (! s1.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            equal(s1.first(), s2.first());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            equal(s1.last(),  s2.last());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   322
        equalIterators(s1.iterator(), s2.iterator());
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   323
        equalIterators(s1.descendingIterator(), s2.descendingIterator());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        checkNavigableSet(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        checkNavigableSet(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    static void equalNavigableSets(final NavigableSet s1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                   final NavigableSet s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        equalNavigableSetsLeaf(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        equalNavigableSetsLeaf(s1.descendingSet(), s2.descendingSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        equalNavigableSetsLeaf(s1.descendingSet().descendingSet(), s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        Object min = s1.isEmpty() ? Integer.MIN_VALUE : s1.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        Object max = s2.isEmpty() ? Integer.MAX_VALUE : s2.last();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (s1.comparator() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            s1.comparator().compare(min, max) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            Object tmp = min; min = max; max = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        equalNavigableSetsLeaf(s1.subSet(min, true, max, true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                               s2.subSet(min, true, max, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        equalNavigableSetsLeaf(s1.tailSet(min, true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                               s2.tailSet(min, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        equalNavigableSetsLeaf(s1.headSet(max, true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                               s2.headSet(max, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        equalNavigableSetsLeaf((NavigableSet) s1.subSet(min, max),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                               (NavigableSet) s2.subSet(min, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        equalNavigableSetsLeaf((NavigableSet) s1.tailSet(min),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                               (NavigableSet) s2.tailSet(min));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        equalNavigableSetsLeaf((NavigableSet) s1.headSet(max),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                               (NavigableSet) s2.headSet(max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    // Destined for a Collections.java near you?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    static <T> T[] concat(T[]... arrays) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        for (int i = 0; i < arrays.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            len += arrays[i].length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        T[] a = (T[])java.lang.reflect.Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            .newInstance(arrays[0].getClass().getComponentType(), len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        for (int i = 0; i < arrays.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            T[] array = arrays[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            System.arraycopy(array, 0, a, k, array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            k += array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    static void checkNavigableMap(final NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (m.comparator() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            check(m.descendingMap().descendingMap().comparator() == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            check(m.descendingKeySet().descendingSet().comparator() == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        equal(m.isEmpty(), m.size() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        equal2(m, m.descendingMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (maybe(4))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            equal2(m, serialClone(m));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        equal2(m.keySet(), m.descendingKeySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        Comparator cmp = comparator(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (m.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            THROWS(NoSuchElementException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   383
                   () -> m.firstKey(),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   384
                   () -> m.lastKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            equal(null, m.firstEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            equal(null, m.lastEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            equal(null, m.pollFirstEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            equal(null, m.pollLastEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            equal(null, m.lowerKey(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            equal(null, m.floorKey(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            equal(null, m.ceilingKey(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            equal(null, m.higherKey(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            equal(null, m.lowerEntry(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            equal(null, m.floorEntry(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            equal(null, m.ceilingEntry(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            equal(null, m.higherEntry(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            Object a = m.firstKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            Object z = m.lastKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            equal(m.lowerKey(a), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            equal(m.higherKey(z), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            equal(a, m.firstEntry().getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            equal(z, m.lastEntry().getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            equal2(m, m.tailMap(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            equal2(m, m.headMap(z, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            equal2(m, m.subMap(a, true, z, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            testEmptyMap(m.subMap(a, true, a, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            testEmptyMap(m.subMap(z, true, z, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            testEmptyMap(m.headMap(a, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            testEmptyMap(m.tailMap(z, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            equal2(m.headMap(a, true), singletonMap(a, m.get(a)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            equal2(m.tailMap(z, true), singletonMap(z, m.get(z)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        Iterator[] kits = new Iterator[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            m.keySet().iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            m.descendingMap().descendingKeySet().iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            m.descendingKeySet().descendingSet().iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        Iterator[] vits = new Iterator[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            m.values().iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            m.descendingMap().descendingMap().values().iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        Iterator[] eits = new Iterator[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            m.entrySet().iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            m.descendingMap().descendingMap().entrySet().iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        Iterator[] its = concat(kits, vits, eits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        for (final Iterator it : its)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (maybe(4))
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   433
                THROWS(IllegalStateException.class, () -> it.remove());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        Map.Entry prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        for (Map.Entry e : (Set<Map.Entry>) m.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            Object k = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            Object v = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            check(m.containsKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            check(m.containsValue(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            for (Iterator kit : kits) equalNext(kit, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            for (Iterator vit : vits) equalNext(vit, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            for (Iterator eit : eits) equalNext(eit, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            equal(k, m.ceilingKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            equal(k, m.ceilingEntry(k).getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            equal(k, m.floorKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            equal(k, m.floorEntry(k).getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            check(m.higherKey(k) == null || cmp.compare(k, m.higherKey(k)) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            check(m.lowerKey(k)  == null || cmp.compare(k, m.lowerKey(k))  > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            equal(m.lowerEntry(k), prev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                equal(m.lowerKey(k), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                equal(m.lowerKey(k), prev.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                check(cmp.compare(prev.getKey(), e.getKey()) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            prev = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        for (final Iterator it : its) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (maybe(2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                check(! it.hasNext());
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   461
            Fun fun = () -> it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            THROWS(NoSuchElementException.class, fun, fun, fun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    static void equalNavigableMapsLeaf(final NavigableMap m1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                       final NavigableMap m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        equal2(m1,              m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        equal( m1.size(),       m2.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        equal( m1.isEmpty(),    m2.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        equal( m1.hashCode(),   m2.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        equal( m1.toString(),   m2.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        equal2(m1.firstEntry(), m2.firstEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        equal2(m1.lastEntry(),  m2.lastEntry());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        checkNavigableMap(m1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        checkNavigableMap(m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    static void equalNavigableMaps(NavigableMap m1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                   NavigableMap m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        equalNavigableMapsLeaf(m1, m2);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   482
        equalSetsLeaf(m1.keySet(), m2.keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        equalNavigableSets(m1.navigableKeySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                           m2.navigableKeySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        equalNavigableSets(m1.descendingKeySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                           m2.descendingKeySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        equal2(m1.entrySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
               m2.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        equalNavigableMapsLeaf(m1.descendingMap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                               m2.descendingMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        equalNavigableMapsLeaf(m1.descendingMap().descendingMap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                               m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        equalNavigableSetsLeaf((NavigableSet) m1.descendingMap().keySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                               (NavigableSet) m2.descendingMap().keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        equalNavigableSetsLeaf(m1.descendingMap().descendingKeySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                               m2.descendingMap().descendingKeySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        equal2(m1.descendingMap().entrySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
               m2.descendingMap().entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        // submaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        Object min = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        Object max = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (m1.comparator() != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            && m1.comparator().compare(min, max) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            Object tmp = min; min = max; max = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        switch (rnd.nextInt(6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            equalNavigableMapsLeaf(m1.subMap(min, true, max, true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                   m2.subMap(min, true, max, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            equalNavigableMapsLeaf(m1.tailMap(min, true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                   m2.tailMap(min, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            equalNavigableMapsLeaf(m1.headMap(max, true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                   m2.headMap(max, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            equalNavigableMapsLeaf((NavigableMap) m1.subMap(min, max),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                   (NavigableMap) m2.subMap(min, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            equalNavigableMapsLeaf((NavigableMap) m1.tailMap(min),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                   (NavigableMap) m2.tailMap(min));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            equalNavigableMapsLeaf((NavigableMap) m1.headMap(max),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                   (NavigableMap) m2.headMap(max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30046
diff changeset
   537
    abstract static class MapFrobber { abstract void frob(NavigableMap m); }
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30046
diff changeset
   538
    abstract static class SetFrobber { abstract void frob(NavigableSet m); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    static MapFrobber randomAdder(NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        final Integer k = unusedKey(m);
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   542
        final MapFrobber[] randomAdders = {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   543
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   544
                equal(m.put(k, k+1), null);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   545
                equal(m.get(k), k+1);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   546
                if (maybe(4)) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   547
                    equal(m.put(k, k+1), k+1);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   548
                    equal(m.get(k), k+1);}}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   549
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   550
                m.descendingMap().put(k, k+1);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   551
                equal(m.get(k), k+1);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   552
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   553
                m.tailMap(k,true).headMap(k,true).put(k,k+1);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   554
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   555
                m.tailMap(k,true).headMap(k,true).descendingMap().put(k,k+1);}}
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   556
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return new MapFrobber() {void frob(NavigableMap m) {
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   558
            randomAdders[rnd.nextInt(randomAdders.length)].frob(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (maybe(2)) equal(m.get(k), k+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (maybe(4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                equal(m.put(k, k+1), k+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                equal(m.get(k), k+1);}}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    static SetFrobber randomAdder(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        final Integer e = unusedElt(s);
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   567
        final SetFrobber[] randomAdders = {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   568
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   569
                check(s.add(e));}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   570
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   571
                s.descendingSet().add(e);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   572
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   573
                s.tailSet(e,true).headSet(e,true).add(e);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   574
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   575
                s.descendingSet().tailSet(e,true).headSet(e,true).add(e);}}
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   576
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return new SetFrobber() {void frob(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            if (maybe(2)) check(! s.contains(e));
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   579
            randomAdders[rnd.nextInt(randomAdders.length)].frob(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            if (maybe(2)) check(! s.add(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (maybe(2)) check(s.contains(e));}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    static Integer unusedElt(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        Integer e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        do { e = rnd.nextInt(1024); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        while (s.contains(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    static Integer unusedKey(NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        Integer k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        do { k = rnd.nextInt(1024); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        while (m.containsKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        return k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    static Integer usedKey(NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        Integer x = rnd.nextInt(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        Integer floor   = (Integer) m.floorKey(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        Integer ceiling = (Integer) m.ceilingKey(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (floor != null) return floor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        check(ceiling != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return ceiling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    static Integer usedElt(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        Integer x = rnd.nextInt(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        Integer floor   = (Integer) s.floor(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        Integer ceiling = (Integer) s.ceiling(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (floor != null) return floor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        check(ceiling != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return ceiling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    static void checkUnusedKey(NavigableMap m, Object k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        check(! m.containsKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        equal(m.get(k), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (maybe(2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            equal(m.remove(k), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    static void checkUnusedElt(NavigableSet s, Object e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (maybe(2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            check(! s.contains(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        if (maybe(2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            check(s.ceiling(e) != e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            check(s.floor(e)   != e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (maybe(2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            check(! s.remove(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    static Fun remover(final Iterator it) {
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   635
        return () -> it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    static MapFrobber randomRemover(NavigableMap m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        final Integer k = usedKey(m);
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   640
        final MapFrobber[] randomRemovers = {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   641
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   642
                Map.Entry e = m.firstEntry();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   643
                equal(m.pollFirstEntry(), e);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   644
                checkUnusedKey(m, e.getKey());}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   645
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   646
                Map.Entry e = m.lastEntry();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   647
                equal(m.pollLastEntry(), e);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   648
                checkUnusedKey(m, e.getKey());}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   649
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   650
                check(m.remove(k) != null);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   651
                checkUnusedKey(m, k);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   652
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   653
                m.subMap(k, true, k, true).clear();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   654
                checkUnusedKey(m, k);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   655
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   656
                m.descendingMap().subMap(k, true, k, true).clear();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   657
                checkUnusedKey(m, k);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   658
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   659
                final Iterator it = m.keySet().iterator();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   660
                while (it.hasNext())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   661
                    if (it.next().equals(k)) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   662
                        it.remove();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   663
                        if (maybe(2))
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   664
                            THROWS(IllegalStateException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   665
                                   () -> it.remove());
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   666
                    }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   667
                checkUnusedKey(m, k);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   668
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   669
                final Iterator it = m.navigableKeySet().descendingIterator();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   670
                while (it.hasNext())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   671
                    if (it.next().equals(k)) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   672
                        it.remove();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   673
                        if (maybe(2))
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   674
                            THROWS(IllegalStateException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   675
                                   () -> it.remove());
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   676
                    }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   677
                checkUnusedKey(m, k);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   678
            new MapFrobber() {void frob(NavigableMap m) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   679
                final Iterator<Map.Entry> it = m.entrySet().iterator();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   680
                while (it.hasNext())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   681
                    if (it.next().getKey().equals(k)) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   682
                        it.remove();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   683
                        if (maybe(2))
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   684
                            THROWS(IllegalStateException.class, remover(it));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   685
                    }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   686
                checkUnusedKey(m, k);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   687
        };
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   688
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   689
        return randomRemovers[rnd.nextInt(randomRemovers.length)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    static SetFrobber randomRemover(NavigableSet s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        final Integer e = usedElt(s);
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   694
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   695
        final SetFrobber[] randomRemovers = {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   696
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   697
                Object e = s.first();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   698
                equal(s.pollFirst(), e);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   699
                checkUnusedElt(s, e);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   700
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   701
                Object e = s.last();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   702
                equal(s.pollLast(), e);
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   703
                checkUnusedElt(s, e);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   704
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   705
                check(s.remove(e));
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   706
                checkUnusedElt(s, e);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   707
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   708
                s.subSet(e, true, e, true).clear();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   709
                checkUnusedElt(s, e);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   710
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   711
                s.descendingSet().subSet(e, true, e, true).clear();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   712
                checkUnusedElt(s, e);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   713
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   714
                final Iterator it = s.iterator();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   715
                while (it.hasNext())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   716
                    if (it.next().equals(e)) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   717
                        it.remove();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   718
                        if (maybe(2))
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   719
                            THROWS(IllegalStateException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   720
                                   () -> it.remove());
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   721
                    }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   722
                checkUnusedElt(s, e);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   723
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   724
                final Iterator it = s.descendingSet().iterator();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   725
                while (it.hasNext())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   726
                    if (it.next().equals(e)) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   727
                        it.remove();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   728
                        if (maybe(2))
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   729
                            THROWS(IllegalStateException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   730
                                   () -> it.remove());
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   731
                    }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   732
                checkUnusedElt(s, e);}},
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   733
            new SetFrobber() {void frob(NavigableSet s) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   734
                final Iterator it = s.descendingIterator();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   735
                while (it.hasNext())
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   736
                    if (it.next().equals(e)) {
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   737
                        it.remove();
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   738
                        if (maybe(2))
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   739
                            THROWS(IllegalStateException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   740
                                   () -> it.remove());
493
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   741
                    }
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   742
                checkUnusedElt(s, e);}}
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   743
        };
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   744
b8102e80be10 6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents: 2
diff changeset
   745
        return randomRemovers[rnd.nextInt(randomRemovers.length)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    static void lockStep(NavigableMap m1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                         NavigableMap m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (! (thorough || maybe(3))) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        if (maybe(4)) m1 = serialClone(m1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        if (maybe(4)) m2 = serialClone(m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        List<NavigableMap> maps = Arrays.asList(m1, m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        for (NavigableMap m : maps) testEmptyMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        final Set<Integer> ints = new HashSet<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        while (ints.size() < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            ints.add(rnd.nextInt(1024));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        final Integer[] elts = ints.toArray(new Integer[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            MapFrobber adder = randomAdder(m1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            for (final NavigableMap m : maps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                adder.frob(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                equal(m.size(), i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            equalNavigableMaps(m1, m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        for (final NavigableMap m : maps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            final Object e = usedKey(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            THROWS(IllegalArgumentException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   771
                   () -> {m.subMap(e,true,e,false)
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   772
                           .subMap(e,true,e,true);},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   773
                   () -> {m.subMap(e,false,e,true)
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   774
                           .subMap(e,true,e,true);},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   775
                   () -> m.tailMap(e,false).tailMap(e,true),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   776
                   () -> m.headMap(e,false).headMap(e,true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        //System.out.printf("%s%n", m1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        for (int i = size; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            MapFrobber remover = randomRemover(m1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            for (final NavigableMap m : maps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                remover.frob(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                equal(m.size(), i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            equalNavigableMaps(m1, m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        for (NavigableMap m : maps) testEmptyMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    static void lockStep(NavigableSet s1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                         NavigableSet s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (! (thorough || maybe(3))) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if (maybe(4)) s1 = serialClone(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (maybe(4)) s2 = serialClone(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        List<NavigableSet> sets = Arrays.asList(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        for (NavigableSet s : sets) testEmptySet(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        final Set<Integer> ints = new HashSet<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        while (ints.size() < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            ints.add(rnd.nextInt(1024));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        final Integer[] elts = ints.toArray(new Integer[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            SetFrobber adder = randomAdder(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            for (final NavigableSet s : sets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                adder.frob(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                equal(s.size(), i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            equalNavigableSets(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        for (final NavigableSet s : sets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            final Object e = usedElt(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            THROWS(IllegalArgumentException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   813
                   () -> {s.subSet(e,true,e,false)
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   814
                           .subSet(e,true,e,true);},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   815
                   () -> {s.subSet(e,false,e,true)
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   816
                           .subSet(e,true,e,true);},
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   817
                   () -> s.tailSet(e,false).tailSet(e,true),
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   818
                   () -> s.headSet(e,false).headSet(e,true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        //System.out.printf("%s%n", s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        for (int i = size; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            SetFrobber remover = randomRemover(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            for (final NavigableSet s : sets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                remover.frob(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                equal(s.size(), i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            equalNavigableSets(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        for (NavigableSet s : sets) testEmptySet(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    static void pass() { passed++; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    static void fail() { failed++; Thread.dumpStack(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    static void fail(String msg) { System.out.println(msg); fail(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    static void check(boolean cond) { if (cond) pass(); else fail(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        else {System.out.println(x + " not equal to " + y); fail();}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        try { realMain(args); } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (failed > 0) throw new Exception("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 23010
diff changeset
   849
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
          for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
              try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
              catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                  if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                  else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    static byte[] serializedForm(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            new ObjectOutputStream(baos).writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        } catch (IOException e) { throw new RuntimeException(e); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    static Object readObject(byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        InputStream is = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        return new ObjectInputStream(is).readObject();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    static <T> T serialClone(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        try { return (T) readObject(serializedForm(obj)); }
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   869
        catch (Error|RuntimeException e) { throw e; }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   870
        catch (Throwable e) { throw new RuntimeException(e); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 7174
diff changeset
   871
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
}