| 1 |
|
package org.apache.lucene.util; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import java.util.Random; |
| 20 |
|
import junit.framework.TestCase; |
| 21 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (41) |
Complexity: 7 |
Complexity Density: 0.22 |
|
| 22 |
|
public class TestPriorityQueue |
| 23 |
|
extends TestCase |
| 24 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 25 |
3
|
public TestPriorityQueue(String name)... |
| 26 |
|
{ |
| 27 |
3
|
super(name); |
| 28 |
|
} |
| 29 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 30 |
|
private static class IntegerQueue |
| 31 |
|
extends PriorityQueue |
| 32 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 33 |
3
|
public IntegerQueue(int count)... |
| 34 |
|
{ |
| 35 |
3
|
super(); |
| 36 |
3
|
initialize(count); |
| 37 |
|
} |
| 38 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 39 |
239230
|
protected boolean lessThan(Object a, Object b)... |
| 40 |
|
{ |
| 41 |
239230
|
return ((Integer) a).intValue() < ((Integer) b).intValue(); |
| 42 |
|
} |
| 43 |
|
} |
| 44 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
| 45 |
1
|
public void testPQ()... |
| 46 |
|
throws Exception |
| 47 |
|
{ |
| 48 |
1
|
testPQ(10000); |
| 49 |
|
} |
| 50 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
|
| 51 |
1
|
public static void testPQ(int count)... |
| 52 |
|
{ |
| 53 |
1
|
PriorityQueue pq = new IntegerQueue(count); |
| 54 |
1
|
Random gen = new Random(); |
| 55 |
1
|
int sum = 0, sum2 = 0; |
| 56 |
|
|
| 57 |
10001
|
for (int i = 0; i < count; i++) |
| 58 |
|
{ |
| 59 |
10000
|
int next = gen.nextInt(); |
| 60 |
10000
|
sum += next; |
| 61 |
10000
|
pq.put(new Integer(next)); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
1
|
int last = Integer.MIN_VALUE; |
| 72 |
10001
|
for (int i = 0; i < count; i++) |
| 73 |
|
{ |
| 74 |
10000
|
Integer next = (Integer)pq.pop(); |
| 75 |
10000
|
assertTrue(next.intValue() >= last); |
| 76 |
10000
|
last = next.intValue(); |
| 77 |
10000
|
sum2 += last; |
| 78 |
|
} |
| 79 |
|
|
| 80 |
1
|
assertEquals(sum, sum2); |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
} |
| 86 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 87 |
1
|
public void testClear()... |
| 88 |
|
{ |
| 89 |
1
|
PriorityQueue pq = new IntegerQueue(3); |
| 90 |
1
|
pq.put(new Integer(2)); |
| 91 |
1
|
pq.put(new Integer(3)); |
| 92 |
1
|
pq.put(new Integer(1)); |
| 93 |
1
|
assertEquals(3, pq.size()); |
| 94 |
1
|
pq.clear(); |
| 95 |
1
|
assertEquals(0, pq.size()); |
| 96 |
|
} |
| 97 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1
PASS
|
|
| 98 |
1
|
public void testFixedSize(){... |
| 99 |
1
|
PriorityQueue pq = new IntegerQueue(3); |
| 100 |
1
|
pq.insert(new Integer(2)); |
| 101 |
1
|
pq.insert(new Integer(3)); |
| 102 |
1
|
pq.insert(new Integer(1)); |
| 103 |
1
|
pq.insert(new Integer(5)); |
| 104 |
1
|
pq.insert(new Integer(7)); |
| 105 |
1
|
pq.insert(new Integer(1)); |
| 106 |
1
|
assertEquals(3, pq.size()); |
| 107 |
1
|
assertEquals(3, ((Integer) pq.top()).intValue()); |
| 108 |
|
} |
| 109 |
|
} |