1 /* 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * 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/publicdomain/zero/1.0/ 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 40 package test.java.util.PriorityQueue; 41 42 import java.util.ArrayList; 43 import java.util.Collections; 44 import java.util.Comparator; 45 import java.util.Iterator; 46 import java.util.List; 47 import java.util.PriorityQueue; 48 import java.util.Queue; 49 50 public class PriorityQueueSort { 51 52 static class MyComparator implements Comparator<Integer> { compare(Integer x, Integer y)53 public int compare(Integer x, Integer y) { 54 return Integer.compare(x.intValue(), y.intValue()); 55 } 56 } 57 main(String[] args)58 public static void main(String[] args) { 59 int n = 10000; 60 if (args.length > 0) 61 n = Integer.parseInt(args[0]); 62 63 List<Integer> sorted = new ArrayList<>(n); 64 for (int i = 0; i < n; i++) 65 sorted.add(new Integer(i)); 66 List<Integer> shuffled = new ArrayList<>(sorted); 67 Collections.shuffle(shuffled); 68 69 Queue<Integer> pq = new PriorityQueue<>(n, new MyComparator()); 70 for (Iterator<Integer> i = shuffled.iterator(); i.hasNext(); ) 71 pq.add(i.next()); 72 73 List<Integer> recons = new ArrayList<>(); 74 while (!pq.isEmpty()) 75 recons.add(pq.remove()); 76 if (!recons.equals(sorted)) 77 throw new RuntimeException("Sort test failed"); 78 79 recons.clear(); 80 pq = new PriorityQueue<>(shuffled); 81 while (!pq.isEmpty()) 82 recons.add(pq.remove()); 83 if (!recons.equals(sorted)) 84 throw new RuntimeException("Sort test failed"); 85 86 // Remove all odd elements from queue 87 pq = new PriorityQueue<>(shuffled); 88 for (Iterator<Integer> i = pq.iterator(); i.hasNext(); ) 89 if ((i.next().intValue() & 1) == 1) 90 i.remove(); 91 recons.clear(); 92 while (!pq.isEmpty()) 93 recons.add(pq.remove()); 94 95 for (Iterator<Integer> i = sorted.iterator(); i.hasNext(); ) 96 if ((i.next().intValue() & 1) == 1) 97 i.remove(); 98 99 if (!recons.equals(sorted)) 100 throw new RuntimeException("Iterator remove test failed."); 101 } 102 } 103