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