LUCENE-8402: Remove invalid identityHashCode assertions in TestPriorityQueue.

This commit is contained in:
Jim Ferenczi 2018-07-20 10:13:19 +02:00
parent f6e9d00b90
commit a19bc5ecb6
1 changed files with 84 additions and 94 deletions

View File

@ -32,13 +32,6 @@ public class TestPriorityQueue extends LuceneTestCase {
@Override @Override
protected boolean lessThan(Integer a, Integer b) { protected boolean lessThan(Integer a, Integer b) {
if (a.equals(b)) {
assert (a != b);
int hashA = System.identityHashCode(a);
int hashB = System.identityHashCode(b);
assert (hashA != hashB);
return hashA < hashB;
}
return (a < b); return (a < b);
} }
@ -47,93 +40,91 @@ public class TestPriorityQueue extends LuceneTestCase {
for (int i = 1; i <= size(); i++) { for (int i = 1; i <= size(); i++) {
int parent = i >>> 1; int parent = i >>> 1;
if (parent > 1) { if (parent > 1) {
assertTrue(lessThan((Integer) heapArray[parent], if (lessThan((Integer) heapArray[parent], (Integer) heapArray[i]) == false) {
(Integer) heapArray[i])); assertEquals(heapArray[parent], heapArray[i]);
}
} }
} }
} }
} }
public void testPQ() throws Exception { public void testPQ() throws Exception {
testPQ(atLeast(10000), random()); testPQ(atLeast(10000), random());
}
public static void testPQ(int count, Random gen) {
PriorityQueue<Integer> pq = new IntegerQueue(count);
int sum = 0, sum2 = 0;
for (int i = 0; i < count; i++) {
int next = gen.nextInt();
sum += next;
pq.add(next);
} }
public static void testPQ(int count, Random gen) { // Date end = new Date();
PriorityQueue<Integer> pq = new IntegerQueue(count);
int sum = 0, sum2 = 0;
for (int i = 0; i < count; i++) // System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
{ // System.out.println(" microseconds/put");
int next = gen.nextInt();
sum += next;
pq.add(next);
}
// Date end = new Date(); // start = new Date();
// System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000); int last = Integer.MIN_VALUE;
// System.out.println(" microseconds/put"); for (int i = 0; i < count; i++) {
Integer next = pq.pop();
// start = new Date(); assertTrue(next.intValue() >= last);
last = next.intValue();
int last = Integer.MIN_VALUE; sum2 += last;
for (int i = 0; i < count; i++)
{
Integer next = pq.pop();
assertTrue(next.intValue() >= last);
last = next.intValue();
sum2 += last;
}
assertEquals(sum, sum2);
// end = new Date();
// System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
// System.out.println(" microseconds/pop");
} }
public void testClear() { assertEquals(sum, sum2);
PriorityQueue<Integer> pq = new IntegerQueue(3); // end = new Date();
pq.add(2);
pq.add(3); // System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
pq.add(1); // System.out.println(" microseconds/pop");
assertEquals(3, pq.size()); }
pq.clear();
assertEquals(0, pq.size()); public void testClear() {
} PriorityQueue<Integer> pq = new IntegerQueue(3);
pq.add(2);
public void testFixedSize() { pq.add(3);
PriorityQueue<Integer> pq = new IntegerQueue(3); pq.add(1);
pq.insertWithOverflow(2); assertEquals(3, pq.size());
pq.insertWithOverflow(3); pq.clear();
pq.insertWithOverflow(1); assertEquals(0, pq.size());
pq.insertWithOverflow(5); }
pq.insertWithOverflow(7);
pq.insertWithOverflow(1); public void testFixedSize() {
assertEquals(3, pq.size()); PriorityQueue<Integer> pq = new IntegerQueue(3);
assertEquals((Integer) 3, pq.top()); pq.insertWithOverflow(2);
} pq.insertWithOverflow(3);
pq.insertWithOverflow(1);
public void testInsertWithOverflow() { pq.insertWithOverflow(5);
int size = 4; pq.insertWithOverflow(7);
PriorityQueue<Integer> pq = new IntegerQueue(size); pq.insertWithOverflow(1);
Integer i1 = 2; assertEquals(3, pq.size());
Integer i2 = 3; assertEquals((Integer) 3, pq.top());
Integer i3 = 1; }
Integer i4 = 5;
Integer i5 = 7; public void testInsertWithOverflow() {
Integer i6 = 1; int size = 4;
PriorityQueue<Integer> pq = new IntegerQueue(size);
assertNull(pq.insertWithOverflow(i1)); Integer i1 = 2;
assertNull(pq.insertWithOverflow(i2)); Integer i2 = 3;
assertNull(pq.insertWithOverflow(i3)); Integer i3 = 1;
assertNull(pq.insertWithOverflow(i4)); Integer i4 = 5;
assertTrue(pq.insertWithOverflow(i5) == i3); // i3 should have been dropped Integer i5 = 7;
assertTrue(pq.insertWithOverflow(i6) == i6); // i6 should not have been inserted Integer i6 = 1;
assertEquals(size, pq.size());
assertEquals((Integer) 2, pq.top()); assertNull(pq.insertWithOverflow(i1));
} assertNull(pq.insertWithOverflow(i2));
assertNull(pq.insertWithOverflow(i3));
assertNull(pq.insertWithOverflow(i4));
assertTrue(pq.insertWithOverflow(i5) == i3); // i3 should have been dropped
assertTrue(pq.insertWithOverflow(i6) == i6); // i6 should not have been inserted
assertEquals(size, pq.size());
assertEquals((Integer) 2, pq.top());
}
public void testRemovalsAndInsertions() { public void testRemovalsAndInsertions() {
Random random = random(); Random random = random();
@ -193,14 +184,14 @@ public class TestPriorityQueue extends LuceneTestCase {
public void testIteratorEmpty() { public void testIteratorEmpty() {
IntegerQueue queue = new IntegerQueue(3); IntegerQueue queue = new IntegerQueue(3);
Iterator<Integer> it = queue.iterator(); Iterator<Integer> it = queue.iterator();
assertFalse(it.hasNext()); assertFalse(it.hasNext());
expectThrows(NoSuchElementException.class, () -> { expectThrows(NoSuchElementException.class, () -> {
it.next(); it.next();
}); });
} }
public void testIteratorOne() { public void testIteratorOne() {
IntegerQueue queue = new IntegerQueue(3); IntegerQueue queue = new IntegerQueue(3);
@ -213,7 +204,7 @@ public class TestPriorityQueue extends LuceneTestCase {
it.next(); it.next();
}); });
} }
public void testIteratorTwo() { public void testIteratorTwo() {
IntegerQueue queue = new IntegerQueue(3); IntegerQueue queue = new IntegerQueue(3);
@ -230,7 +221,6 @@ public class TestPriorityQueue extends LuceneTestCase {
}); });
} }
@AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/LUCENE-8402")
public void testIteratorRandom() { public void testIteratorRandom() {
final int maxSize = TestUtil.nextInt(random(), 1, 20); final int maxSize = TestUtil.nextInt(random(), 1, 20);
IntegerQueue queue = new IntegerQueue(maxSize); IntegerQueue queue = new IntegerQueue(maxSize);
@ -256,13 +246,13 @@ public class TestPriorityQueue extends LuceneTestCase {
public void testMaxIntSize() { public void testMaxIntSize() {
expectThrows(IllegalArgumentException.class, () -> { expectThrows(IllegalArgumentException.class, () -> {
new PriorityQueue<Boolean>(Integer.MAX_VALUE) { new PriorityQueue<Boolean>(Integer.MAX_VALUE) {
@Override @Override
public boolean lessThan(Boolean a, Boolean b) { public boolean lessThan(Boolean a, Boolean b) {
// uncalled // uncalled
return true; return true;
} }
}; };
}); });
} }
} }