1 package org.apache.lucene.util; 2 3 18 19 import java.util.Date ; 20 import java.util.Random ; 21 import junit.framework.TestCase; 22 23 public class TestPriorityQueue 24 extends TestCase 25 { 26 public TestPriorityQueue(String 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 a, Object b) 41 { 42 return ((Integer ) a).intValue() < ((Integer ) b).intValue(); 43 } 44 } 45 46 public void testPQ() 47 throws Exception 48 { 49 testPQ(10000); 50 } 51 52 public static void testPQ(int count) 53 { 54 PriorityQueue pq = new IntegerQueue(count); 55 Random gen = new Random (); 56 int sum = 0, sum2 = 0; 57 58 Date start = new Date (); 59 60 for (int i = 0; i < count; i++) 61 { 62 int next = gen.nextInt(); 63 sum += next; 64 pq.put(new Integer (next)); 65 } 66 67 69 72 74 int last = Integer.MIN_VALUE; 75 for (int i = 0; i < count; i++) 76 { 77 Integer next = (Integer )pq.pop(); 78 assertTrue(next.intValue() >= last); 79 last = next.intValue(); 80 sum2 += last; 81 } 82 83 assertEquals(sum, sum2); 84 86 } 89 90 public void testClear() 91 { 92 PriorityQueue pq = new IntegerQueue(3); 93 pq.put(new Integer (2)); 94 pq.put(new Integer (3)); 95 pq.put(new Integer (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 (2)); 104 pq.insert(new Integer (3)); 105 pq.insert(new Integer (1)); 106 pq.insert(new Integer (5)); 107 pq.insert(new Integer (7)); 108 pq.insert(new Integer (1)); 109 assertEquals(3, pq.size()); 110 assertEquals(3, ((Integer ) pq.top()).intValue()); 111 } 112 } 113 | Popular Tags |