📄 linkedblockingdequetest.java
字号:
/*
* 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
*/
import junit.framework.*;
import java.util.*;
import edu.emory.mathcs.backport.java.util.concurrent.*;
import java.io.*;
public class LinkedBlockingDequeTest extends JSR166TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run (suite());
}
public static Test suite() {
return new TestSuite(LinkedBlockingDequeTest.class);
}
/**
* Create a deque of given size containing consecutive
* Integers 0 ... n.
*/
private LinkedBlockingDeque populatedDeque(int n) {
LinkedBlockingDeque q = new LinkedBlockingDeque(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;
}
/**
* isEmpty is true before add, false after
*/
public void testEmpty() {
LinkedBlockingDeque q = new LinkedBlockingDeque();
assertTrue(q.isEmpty());
q.add(new Integer(1));
assertFalse(q.isEmpty());
q.add(new Integer(2));
q.removeFirst();
q.removeFirst();
assertTrue(q.isEmpty());
}
/**
* size changes when elements added and removed
*/
public void testSize() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(SIZE-i, q.size());
q.removeFirst();
}
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.size());
q.add(new Integer(i));
}
}
/**
* offer(null) throws NPE
*/
public void testOfferFirstNull() {
try {
LinkedBlockingDeque q = new LinkedBlockingDeque();
q.offerFirst(null);
shouldThrow();
} catch (NullPointerException success) {
}
}
/**
* OfferFirst succeeds
*/
public void testOfferFirst() {
LinkedBlockingDeque q = new LinkedBlockingDeque();
assertTrue(q.offerFirst(new Integer(0)));
assertTrue(q.offerFirst(new Integer(1)));
}
/**
* OfferLast succeeds
*/
public void testOfferLast() {
LinkedBlockingDeque q = new LinkedBlockingDeque();
assertTrue(q.offerLast(new Integer(0)));
assertTrue(q.offerLast(new Integer(1)));
}
/**
* pollFirst succeeds unless empty
*/
public void testPollFirst() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.pollFirst()).intValue());
}
assertNull(q.pollFirst());
}
/**
* pollLast succeeds unless empty
*/
public void testPollLast() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = SIZE-1; i >= 0; --i) {
assertEquals(i, ((Integer)q.pollLast()).intValue());
}
assertNull(q.pollLast());
}
/**
* peekFirst returns next element, or null if empty
*/
public void testPeekFirst() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.peekFirst()).intValue());
q.pollFirst();
assertTrue(q.peekFirst() == null ||
i != ((Integer)q.peekFirst()).intValue());
}
assertNull(q.peekFirst());
}
/**
* peek returns next element, or null if empty
*/
public void testPeek() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.peek()).intValue());
q.pollFirst();
assertTrue(q.peek() == null ||
i != ((Integer)q.peek()).intValue());
}
assertNull(q.peek());
}
/**
* peekLast returns next element, or null if empty
*/
public void testPeekLast() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = SIZE-1; i >= 0; --i) {
assertEquals(i, ((Integer)q.peekLast()).intValue());
q.pollLast();
assertTrue(q.peekLast() == null ||
i != ((Integer)q.peekLast()).intValue());
}
assertNull(q.peekLast());
}
/**
* getFirst returns next getFirst, or throws NSEE if empty
*/
public void testFirstElement() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.getFirst()).intValue());
q.pollFirst();
}
try {
q.getFirst();
shouldThrow();
}
catch (NoSuchElementException success) {}
}
/**
* getLast returns next element, or throws NSEE if empty
*/
public void testLastElement() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = SIZE-1; i >= 0; --i) {
assertEquals(i, ((Integer)q.getLast()).intValue());
q.pollLast();
}
try {
q.getLast();
shouldThrow();
}
catch (NoSuchElementException success) {}
assertNull(q.peekLast());
}
/**
* removeFirst removes next element, or throws NSEE if empty
*/
public void testRemoveFirst() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.removeFirst()).intValue());
}
try {
q.removeFirst();
shouldThrow();
} catch (NoSuchElementException success){
}
}
/**
* remove removes next element, or throws NSEE if empty
*/
public void testRemove() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.remove()).intValue());
}
try {
q.remove();
shouldThrow();
} catch (NoSuchElementException success){
}
}
/**
* removeFirstOccurrence(x) removes x and returns true if present
*/
public void testRemoveFirstOccurrence() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 1; i < SIZE; i+=2) {
assertTrue(q.removeFirstOccurrence(new Integer(i)));
}
for (int i = 0; i < SIZE; i+=2) {
assertTrue(q.removeFirstOccurrence(new Integer(i)));
assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
}
assertTrue(q.isEmpty());
}
/**
* removeLastOccurrence(x) removes x and returns true if present
*/
public void testRemoveLastOccurrence() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 1; i < SIZE; i+=2) {
assertTrue(q.removeLastOccurrence(new Integer(i)));
}
for (int i = 0; i < SIZE; i+=2) {
assertTrue(q.removeLastOccurrence(new Integer(i)));
assertFalse(q.removeLastOccurrence(new Integer(i+1)));
}
assertTrue(q.isEmpty());
}
/**
* peekFirst returns element inserted with addFirst
*/
public void testAddFirst() {
LinkedBlockingDeque q = populatedDeque(3);
q.pollLast();
q.addFirst(four);
assertEquals(four,q.peekFirst());
}
/**
* peekLast returns element inserted with addLast
*/
public void testAddLast() {
LinkedBlockingDeque q = populatedDeque(3);
q.pollLast();
q.addLast(four);
assertEquals(four,q.peekLast());
}
/**
* A new deque has the indicated capacity, or Integer.MAX_VALUE if
* none given
*/
public void testConstructor1() {
assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity());
assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity());
}
/**
* Constructor throws IAE if capacity argument nonpositive
*/
public void testConstructor2() {
try {
LinkedBlockingDeque q = new LinkedBlockingDeque(0);
shouldThrow();
}
catch (IllegalArgumentException success) {}
}
/**
* Initializing from null Collection throws NPE
*/
public void testConstructor3() {
try {
LinkedBlockingDeque q = new LinkedBlockingDeque(null);
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* Initializing from Collection of null elements throws NPE
*/
public void testConstructor4() {
try {
Integer[] ints = new Integer[SIZE];
LinkedBlockingDeque q = new LinkedBlockingDeque(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);
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* Deque 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);
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
for (int i = 0; i < SIZE; ++i)
assertEquals(ints[i], q.poll());
}
finally {}
}
/**
* Deque transitions from empty to full when elements added
*/
public void testEmptyFull() {
LinkedBlockingDeque q = new LinkedBlockingDeque(2);
assertTrue(q.isEmpty());
assertEquals("should have room for 2", 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() {
LinkedBlockingDeque q = populatedDeque(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 {
LinkedBlockingDeque q = new LinkedBlockingDeque(1);
q.offer(null);
shouldThrow();
} catch (NullPointerException success) { }
}
/**
* add(null) throws NPE
*/
public void testAddNull() {
try {
LinkedBlockingDeque q = new LinkedBlockingDeque(1);
q.add(null);
shouldThrow();
} catch (NullPointerException success) { }
}
/**
* push(null) throws NPE
*/
public void testPushNull() {
try {
LinkedBlockingDeque q = new LinkedBlockingDeque(1);
q.push(null);
shouldThrow();
} catch (NullPointerException success) { }
}
/**
* push succeeds if not full; throws ISE if full
*/
public void testPush() {
try {
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
Integer I = new Integer(i);
q.push(I);
assertEquals(I, q.peek());
}
assertEquals(0, q.remainingCapacity());
q.push(new Integer(SIZE));
} catch (IllegalStateException success){
}
}
/**
* peekFirst returns element inserted with push
*/
public void testPushWithPeek() {
LinkedBlockingDeque q = populatedDeque(3);
q.pollLast();
q.push(four);
assertEquals(four,q.peekFirst());
}
/**
* pop removes next element, or throws NSEE if empty
*/
public void testPop() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.pop()).intValue());
}
try {
q.pop();
shouldThrow();
} catch (NoSuchElementException success){
}
}
/**
* Offer succeeds if not full; fails if full
*/
public void testOffer() {
LinkedBlockingDeque q = new LinkedBlockingDeque(1);
assertTrue(q.offer(zero));
assertFalse(q.offer(one));
}
/**
* add succeeds if not full; throws ISE if full
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -