jdk/test/java/util/PriorityQueue/ForgetMeNot.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 24692 268fbc344d53
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6394004
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test ForgetMeNot implementation feature (and more)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class ForgetMeNot {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static void checkQ(PriorityQueue<Integer> q, Integer...elts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        check(Arrays.equals(q.toArray(), elts));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static void noMoreElements(final Iterator<Integer> it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        for (int j = 0; j < 2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            THROWS(NoSuchElementException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                   new Fun() { void f() { it.next(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static void removeIsCurrentlyIllegal(final Iterator<Integer> it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        for (int j = 0; j < 2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                   new Fun() { void f() { it.remove(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static void remove(Iterator<Integer> it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                               Queue<Integer> q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int size = q.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        equal(size, q.size()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        final PriorityQueue<Integer> q = new PriorityQueue<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Iterator<Integer> it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        checkQ(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        check(q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        check(! q.contains(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        it = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        check(q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // Singleton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        q.add(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        checkQ(q, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        check(! q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        check(q.contains(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        it = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        equal(it.next(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        check(q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        checkQ(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // @see PriorityQueue.forgetMeNot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        final Integer[] a = {0, 4, 1, 6, 7, 2, 3}; // Carefully chosen!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        q.addAll(Arrays.asList(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        it = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        equal(it.next(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        equal(it.next(), 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        equal(it.next(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        equal(it.next(), 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        checkQ(q, 0, 3, 1, 4, 7, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        equal(it.next(), 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        checkQ(q, 0, 2, 1, 4, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        equal(it.next(), 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        equal(it.next(), 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        checkQ(q, 0, 3, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static abstract class Fun {abstract void f() throws Throwable;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}