KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > util > TestPriorityQueue


1 package org.apache.lucene.util;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Date JavaDoc;
20 import java.util.Random JavaDoc;
21 import junit.framework.TestCase;
22
23 public class TestPriorityQueue
24     extends TestCase
25 {
26     public TestPriorityQueue(String JavaDoc name)
27     {
28     super(name);
29     }
30
31     private static class IntegerQueue
32     extends PriorityQueue
33     {
34     public IntegerQueue(int count)
35     {
36         super();
37         initialize(count);
38     }
39
40     protected boolean lessThan(Object JavaDoc a, Object JavaDoc b)
41     {
42         return ((Integer JavaDoc) a).intValue() < ((Integer JavaDoc) b).intValue();
43     }
44     }
45
46     public void testPQ()
47     throws Exception JavaDoc
48     {
49     testPQ(10000);
50     }
51
52     public static void testPQ(int count)
53     {
54     PriorityQueue pq = new IntegerQueue(count);
55     Random JavaDoc gen = new Random JavaDoc();
56     int sum = 0, sum2 = 0;
57
58     Date JavaDoc start = new Date JavaDoc();
59
60     for (int i = 0; i < count; i++)
61     {
62         int next = gen.nextInt();
63         sum += next;
64         pq.put(new Integer JavaDoc(next));
65     }
66
67     // Date end = new Date();
68

69     // System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
70
// System.out.println(" microseconds/put");
71

72     // start = new Date();
73

74     int last = Integer.MIN_VALUE;
75     for (int i = 0; i < count; i++)
76     {
77         Integer JavaDoc next = (Integer JavaDoc)pq.pop();
78         assertTrue(next.intValue() >= last);
79         last = next.intValue();
80         sum2 += last;
81     }
82
83     assertEquals(sum, sum2);
84     // end = new Date();
85

86     // System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
87
// System.out.println(" microseconds/pop");
88
}
89
90     public void testClear()
91     {
92     PriorityQueue pq = new IntegerQueue(3);
93     pq.put(new Integer JavaDoc(2));
94     pq.put(new Integer JavaDoc(3));
95     pq.put(new Integer JavaDoc(1));
96     assertEquals(3, pq.size());
97     pq.clear();
98     assertEquals(0, pq.size());
99     }
100     
101     public void testFixedSize(){
102         PriorityQueue pq = new IntegerQueue(3);
103         pq.insert(new Integer JavaDoc(2));
104         pq.insert(new Integer JavaDoc(3));
105         pq.insert(new Integer JavaDoc(1));
106         pq.insert(new Integer JavaDoc(5));
107         pq.insert(new Integer JavaDoc(7));
108         pq.insert(new Integer JavaDoc(1));
109         assertEquals(3, pq.size());
110         assertEquals(3, ((Integer JavaDoc) pq.top()).intValue());
111     }
112 }
113
Popular Tags