jdk/test/java/util/PriorityQueue/PriorityQueueSort.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 9242 ef138d47df58
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @bug 4486658
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @summary Checks that a priority queue returns elements in sorted order across various operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class PriorityQueueSort {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static class MyComparator implements Comparator<Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        public int compare(Integer x, Integer y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            int i = x.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            int j = y.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            if (i < j) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            if (i > j) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int n = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            n = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        List<Integer> sorted = new ArrayList<Integer>(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            sorted.add(new Integer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        List<Integer> shuffled = new ArrayList<Integer>(sorted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Collections.shuffle(shuffled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Queue<Integer> pq = new PriorityQueue<Integer>(n, new MyComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        for (Iterator<Integer> i = shuffled.iterator(); i.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            pq.add(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        List<Integer> recons = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        while (!pq.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            recons.add(pq.remove());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (!recons.equals(sorted))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new RuntimeException("Sort test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        recons.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        pq = new PriorityQueue<Integer>(shuffled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        while (!pq.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            recons.add(pq.remove());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (!recons.equals(sorted))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new RuntimeException("Sort test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // Remove all odd elements from queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        pq = new PriorityQueue<Integer>(shuffled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        for (Iterator<Integer> i = pq.iterator(); i.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if ((i.next().intValue() & 1) == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        recons.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        while (!pq.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            recons.add(pq.remove());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        for (Iterator<Integer> i = sorted.iterator(); i.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if ((i.next().intValue() & 1) == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (!recons.equals(sorted))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new RuntimeException("Iterator remove test failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
}