arrayblockingqueuetest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,061 行 · 第 1/3 页
JAVA
1,061 行
/*
* 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 ArrayBlockingQueueTest extends JSR166TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run (suite());
}
public static Test suite() {
return new TestSuite(ArrayBlockingQueueTest.class);
}
/**
* Create a queue of given size containing consecutive
* Integers 0 ... n.
*/
private ArrayBlockingQueue populatedQueue(int n) {
ArrayBlockingQueue q = new ArrayBlockingQueue(n);
assertTrue(q.isEmpty());
for(int i = 0; i < n; i++)
assertTrue(q.offer(new Integer(i)));
assertFalse(q.isEmpty());
assertEquals(0, q.remainingCapacity());
assertEquals(n, q.size());
return q;
}
/**
* A new queue has the indicated capacity
*/
public void testConstructor1() {
assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
}
/**
* Constructor throws IAE if capacity argument nonpositive
*/
public void testConstructor2() {
try {
ArrayBlockingQueue q = new ArrayBlockingQueue(0);
shouldThrow();
}
catch (IllegalArgumentException success) {}
}
/**
* Initializing from null Collection throws NPE
*/
public void testConstructor3() {
try {
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* Initializing from Collection of null elements throws NPE
*/
public void testConstructor4() {
try {
Integer[] ints = new Integer[SIZE];
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, 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);
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* Initializing from too large collection throws IAE
*/
public void testConstructor6() {
try {
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
shouldThrow();
}
catch (IllegalArgumentException success) {}
}
/**
* Queue contains all elements of collection used to initialize
*/
public void testConstructor7() {
try {
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
for (int i = 0; i < SIZE; ++i)
assertEquals(ints[i], q.poll());
}
finally {}
}
/**
* Queue transitions from empty to full when elements added
*/
public void testEmptyFull() {
ArrayBlockingQueue q = new ArrayBlockingQueue(2);
assertTrue(q.isEmpty());
assertEquals(2, q.remainingCapacity());
q.add(one);
assertFalse(q.isEmpty());
q.add(two);
assertFalse(q.isEmpty());
assertEquals(0, q.remainingCapacity());
assertFalse(q.offer(three));
}
/**
* remainingCapacity decreases on add, increases on remove
*/
public void testRemainingCapacity() {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.remainingCapacity());
assertEquals(SIZE-i, q.size());
q.remove();
}
for (int i = 0; i < SIZE; ++i) {
assertEquals(SIZE-i, q.remainingCapacity());
assertEquals(i, q.size());
q.add(new Integer(i));
}
}
/**
* offer(null) throws NPE
*/
public void testOfferNull() {
try {
ArrayBlockingQueue q = new ArrayBlockingQueue(1);
q.offer(null);
shouldThrow();
} catch (NullPointerException success) { }
}
/**
* add(null) throws NPE
*/
public void testAddNull() {
try {
ArrayBlockingQueue q = new ArrayBlockingQueue(1);
q.add(null);
shouldThrow();
} catch (NullPointerException success) { }
}
/**
* Offer succeeds if not full; fails if full
*/
public void testOffer() {
ArrayBlockingQueue q = new ArrayBlockingQueue(1);
assertTrue(q.offer(zero));
assertFalse(q.offer(one));
}
/**
* add succeeds if not full; throws ISE if full
*/
public void testAdd() {
try {
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.add(new Integer(i)));
}
assertEquals(0, q.remainingCapacity());
q.add(new Integer(SIZE));
} catch (IllegalStateException success){
}
}
/**
* addAll(null) throws NPE
*/
public void testAddAll1() {
try {
ArrayBlockingQueue q = new ArrayBlockingQueue(1);
q.addAll(null);
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
try {
ArrayBlockingQueue q = populatedQueue(SIZE);
q.addAll(q);
shouldThrow();
}
catch (IllegalArgumentException success) {}
}
/**
* addAll of a collection with null elements throws NPE
*/
public void testAddAll2() {
try {
ArrayBlockingQueue q = new ArrayBlockingQueue(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 {
ArrayBlockingQueue q = new ArrayBlockingQueue(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) {}
}
/**
* addAll throws ISE if not enough room
*/
public void testAddAll4() {
try {
ArrayBlockingQueue q = new ArrayBlockingQueue(1);
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
shouldThrow();
}
catch (IllegalStateException success) {}
}
/**
* Queue contains all elements, in traversal order, of successful addAll
*/
public void testAddAll5() {
try {
Integer[] empty = new Integer[0];
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
ArrayBlockingQueue q = new ArrayBlockingQueue(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 {
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
q.put(null);
shouldThrow();
}
catch (NullPointerException success){
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* all elements successfully put are contained
*/
public void testPut() {
try {
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
Integer I = new Integer(i);
q.put(I);
assertTrue(q.contains(I));
}
assertEquals(0, q.remainingCapacity());
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* put blocks interruptibly if full
*/
public void testBlockingPut() {
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
Thread t = new Thread(new Runnable() {
public void run() {
int added = 0;
try {
for (int i = 0; i < SIZE; ++i) {
q.put(new Integer(i));
++added;
}
q.put(new Integer(SIZE));
threadShouldThrow();
} catch (InterruptedException ie){
threadAssertEquals(added, SIZE);
}
}});
try {
t.start();
Thread.sleep(MEDIUM_DELAY_MS);
t.interrupt();
t.join();
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* put blocks waiting for take when full
*/
public void testPutWithTake() {
final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
Thread t = new Thread(new Runnable() {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?