priorityblockingqueuetest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 957 行 · 第 1/2 页
JAVA
957 行
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/licenses/publicdomain
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
import junit.framework.*;
import edu.emory.mathcs.backport.java.util.*;
import edu.emory.mathcs.backport.java.util.concurrent.*;
import java.io.*;
import java.util.*;
public class PriorityBlockingQueueTest extends JSR166TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run (suite());
}
public static Test suite() {
return new TestSuite(PriorityBlockingQueueTest.class);
}
private static final int NOCAP = Integer.MAX_VALUE;
/** Sample Comparator */
static class MyReverseComparator implements Comparator {
public int compare(Object x, Object y) {
int i = ((Integer)x).intValue();
int j = ((Integer)y).intValue();
if (i < j) return 1;
if (i > j) return -1;
return 0;
}
}
/**
* Create a queue of given size containing consecutive
* Integers 0 ... n.
*/
private PriorityBlockingQueue populatedQueue(int n) {
PriorityBlockingQueue q = new PriorityBlockingQueue(n);
assertTrue(q.isEmpty());
for(int i = n-1; i >= 0; i-=2)
assertTrue(q.offer(new Integer(i)));
for(int i = (n & 1); i < n; i+=2)
assertTrue(q.offer(new Integer(i)));
assertFalse(q.isEmpty());
assertEquals(NOCAP, q.remainingCapacity());
assertEquals(n, q.size());
return q;
}
/**
* A new queue has unbounded capacity
*/
public void testConstructor1() {
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity());
}
/**
* Constructor throws IAE if capacity argument nonpositive
*/
public void testConstructor2() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(0);
shouldThrow();
}
catch (IllegalArgumentException success) {}
}
/**
* Initializing from null Collection throws NPE
*/
public void testConstructor3() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(null);
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* Initializing from Collection of null elements throws NPE
*/
public void testConstructor4() {
try {
Integer[] ints = new Integer[SIZE];
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* Initializing from Collection with some null elements throws NPE
*/
public void testConstructor5() {
try {
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE-1; ++i)
ints[i] = new Integer(i);
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* Queue contains all elements of collection used to initialize
*/
public void testConstructor6() {
try {
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
for (int i = 0; i < SIZE; ++i)
assertEquals(ints[i], q.poll());
}
finally {}
}
/**
* The comparator used in constructor is used
*/
public void testConstructor7() {
try {
MyReverseComparator cmp = new MyReverseComparator();
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
assertEquals(cmp, q.comparator());
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
for (int i = SIZE-1; i >= 0; --i)
assertEquals(ints[i], q.poll());
}
finally {}
}
/**
* isEmpty is true before add, false after
*/
public void testEmpty() {
PriorityBlockingQueue q = new PriorityBlockingQueue(2);
assertTrue(q.isEmpty());
assertEquals(NOCAP, q.remainingCapacity());
q.add(one);
assertFalse(q.isEmpty());
q.add(two);
q.remove();
q.remove();
assertTrue(q.isEmpty());
}
/**
* remainingCapacity does not change when elements added or removed,
* but size does
*/
public void testRemainingCapacity() {
PriorityBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(NOCAP, q.remainingCapacity());
assertEquals(SIZE-i, q.size());
q.remove();
}
for (int i = 0; i < SIZE; ++i) {
assertEquals(NOCAP, q.remainingCapacity());
assertEquals(i, q.size());
q.add(new Integer(i));
}
}
/**
* offer(null) throws NPE
*/
public void testOfferNull() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(1);
q.offer(null);
shouldThrow();
} catch (NullPointerException success) { }
}
/**
* add(null) throws NPE
*/
public void testAddNull() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(1);
q.add(null);
shouldThrow();
} catch (NullPointerException success) { }
}
/**
* Offer of comparable element succeeds
*/
public void testOffer() {
PriorityBlockingQueue q = new PriorityBlockingQueue(1);
assertTrue(q.offer(zero));
assertTrue(q.offer(one));
}
/**
* Offer of non-Comparable throws CCE
*/
public void testOfferNonComparable() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(1);
q.offer(new Object());
q.offer(new Object());
q.offer(new Object());
shouldThrow();
}
catch(ClassCastException success) {}
}
/**
* add of comparable succeeds
*/
public void testAdd() {
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.size());
assertTrue(q.add(new Integer(i)));
}
}
/**
* addAll(null) throws NPE
*/
public void testAddAll1() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(1);
q.addAll(null);
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
try {
PriorityBlockingQueue q = populatedQueue(SIZE);
q.addAll(q);
shouldThrow();
}
catch (IllegalArgumentException success) {}
}
/**
* addAll of a collection with null elements throws NPE
*/
public void testAddAll2() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
Integer[] ints = new Integer[SIZE];
q.addAll(Arrays.asList(ints));
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* addAll of a collection with any null elements throws NPE after
* possibly adding some elements
*/
public void testAddAll3() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE-1; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* Queue contains all elements of successful addAll
*/
public void testAddAll5() {
try {
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = SIZE-1; i >= 0; --i)
ints[i] = new Integer(i);
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
assertFalse(q.addAll(Arrays.asList(empty)));
assertTrue(q.addAll(Arrays.asList(ints)));
for (int i = 0; i < SIZE; ++i)
assertEquals(ints[i], q.poll());
}
finally {}
}
/**
* put(null) throws NPE
*/
public void testPutNull() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
q.put(null);
shouldThrow();
}
catch (NullPointerException success){
}
}
/**
* all elements successfully put are contained
*/
public void testPut() {
try {
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
Integer I = new Integer(i);
q.put(I);
assertTrue(q.contains(I));
}
assertEquals(SIZE, q.size());
}
finally {
}
}
/**
* put doesn't block waiting for take
*/
public void testPutWithTake() {
final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
Thread t = new Thread(new Runnable() {
public void run() {
int added = 0;
try {
q.put(new Integer(0));
++added;
q.put(new Integer(0));
++added;
q.put(new Integer(0));
++added;
q.put(new Integer(0));
++added;
threadAssertTrue(added == 4);
} finally {
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
q.take();
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* timed offer does not time out
*/
public void testTimedOffer() {
final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
q.put(new Integer(0));
q.put(new Integer(0));
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
} finally { }
}
});
try {
t.start();
Thread.sleep(SMALL_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* take retrieves elements in priority order
*/
public void testTake() {
try {
PriorityBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.take()).intValue());
}
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* take blocks interruptibly when empty
*/
public void testTakeFromEmpty() {
final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
q.take();
threadShouldThrow();
} catch (InterruptedException success){ }
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* Take removes existing elements until empty, then blocks interruptibly
*/
public void testBlockingTake() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
PriorityBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
threadAssertEquals(i, ((Integer)q.take()).intValue());
}
q.take();
threadShouldThrow();
} catch (InterruptedException success){
}
}});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* poll succeeds unless empty
*/
public void testPoll() {
PriorityBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.poll()).intValue());
}
assertNull(q.poll());
}
/**
* timed pool with zero timeout succeeds when non-empty, else times out
*/
public void testTimedPoll0() {
try {
PriorityBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
}
assertNull(q.poll(0, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?