jdk/test/java/util/PriorityQueue/PriorityQueueSort.java
author dl
Fri, 23 Sep 2016 13:24:33 -0700
changeset 41131 87edc8451f8a
parent 9242 ef138d47df58
child 43522 f9c6f543c4db
permissions -rw-r--r--
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21 Reviewed-by: martin, chegar, shade
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
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 5506
diff changeset
    31
 * http://creativecommons.org/publicdomain/zero/1.0/
2
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
41131
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 9242
diff changeset
    40
import java.util.ArrayList;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 9242
diff changeset
    41
import java.util.Collections;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 9242
diff changeset
    42
import java.util.Comparator;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 9242
diff changeset
    43
import java.util.Iterator;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 9242
diff changeset
    44
import java.util.List;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 9242
diff changeset
    45
import java.util.Queue;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 9242
diff changeset
    46
import java.util.PriorityQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class PriorityQueueSort {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static class MyComparator implements Comparator<Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public int compare(Integer x, Integer y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            int i = x.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            int j = y.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (i < j) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            if (i > j) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int n = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            n = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        List<Integer> sorted = new ArrayList<Integer>(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            sorted.add(new Integer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        List<Integer> shuffled = new ArrayList<Integer>(sorted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Collections.shuffle(shuffled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Queue<Integer> pq = new PriorityQueue<Integer>(n, new MyComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (Iterator<Integer> i = shuffled.iterator(); i.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            pq.add(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        List<Integer> recons = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        while (!pq.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            recons.add(pq.remove());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (!recons.equals(sorted))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new RuntimeException("Sort test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        recons.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        pq = new PriorityQueue<Integer>(shuffled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while (!pq.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            recons.add(pq.remove());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (!recons.equals(sorted))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new RuntimeException("Sort test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // Remove all odd elements from queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        pq = new PriorityQueue<Integer>(shuffled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (Iterator<Integer> i = pq.iterator(); i.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if ((i.next().intValue() & 1) == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        recons.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        while (!pq.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            recons.add(pq.remove());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        for (Iterator<Integer> i = sorted.iterator(); i.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if ((i.next().intValue() & 1) == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!recons.equals(sorted))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new RuntimeException("Iterator remove test failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}