ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/PriorityQueue/PriorityQueueSort.java
Revision: 1.9
Committed: Tue Sep 1 00:31:17 2009 UTC (14 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +0 -1 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2 jsr166 1.7 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5     */
6    
7     /*
8 jsr166 1.8 * @test
9 jsr166 1.2 * @bug 4486658
10 jsr166 1.3 * @summary Checks that a priority queue returns elements in sorted order across various operations
11 dl 1.1 */
12    
13     import java.util.*;
14    
15     public class PriorityQueueSort {
16    
17 jsr166 1.6 static class MyComparator implements Comparator<Integer> {
18 dl 1.1 public int compare(Integer x, Integer y) {
19 jsr166 1.4 int i = x.intValue();
20     int j = y.intValue();
21 dl 1.1 if (i < j) return -1;
22     if (i > j) return 1;
23     return 0;
24     }
25     }
26    
27     public static void main(String[] args) {
28     int n = 10000;
29     if (args.length > 0)
30     n = Integer.parseInt(args[0]);
31 jsr166 1.6
32 dl 1.1 List<Integer> sorted = new ArrayList<Integer>(n);
33     for (int i = 0; i < n; i++)
34     sorted.add(new Integer(i));
35     List<Integer> shuffled = new ArrayList<Integer>(sorted);
36     Collections.shuffle(shuffled);
37    
38     Queue<Integer> pq = new PriorityQueue<Integer>(n, new MyComparator());
39     for (Iterator<Integer> i = shuffled.iterator(); i.hasNext(); )
40     pq.add(i.next());
41    
42     List<Integer> recons = new ArrayList<Integer>();
43     while (!pq.isEmpty())
44     recons.add(pq.remove());
45     if (!recons.equals(sorted))
46     throw new RuntimeException("Sort test failed");
47    
48     recons.clear();
49     pq = new PriorityQueue<Integer>(shuffled);
50     while (!pq.isEmpty())
51     recons.add(pq.remove());
52     if (!recons.equals(sorted))
53     throw new RuntimeException("Sort test failed");
54    
55     // Remove all odd elements from queue
56     pq = new PriorityQueue<Integer>(shuffled);
57     for (Iterator<Integer> i = pq.iterator(); i.hasNext(); )
58     if ((i.next().intValue() & 1) == 1)
59     i.remove();
60     recons.clear();
61     while (!pq.isEmpty())
62     recons.add(pq.remove());
63    
64     for (Iterator<Integer> i = sorted.iterator(); i.hasNext(); )
65     if ((i.next().intValue() & 1) == 1)
66     i.remove();
67    
68     if (!recons.equals(sorted))
69     throw new RuntimeException("Iterator remove test failed.");
70     }
71     }