--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/09/25 11:02:41 1.5 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/10/05 23:00:40 1.6 @@ -152,6 +152,17 @@ public class LinkedBlockingQueueTest ext } /** + * add(null) throws NPE + */ + public void testAddNull() { + try { + LinkedBlockingQueue q = new LinkedBlockingQueue(1); + q.add(null); + shouldThrow(); + } catch (NullPointerException success) { } + } + + /** * Offer succeeds if not full; fails if full */ public void testOffer() { @@ -186,6 +197,19 @@ public class LinkedBlockingQueueTest ext } catch (NullPointerException success) {} } + + /** + * addAll(this) throws IAE + */ + public void testAddAllSelf() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + q.addAll(q); + shouldThrow(); + } + catch (IllegalArgumentException success) {} + } + /** * addAll of a collection with null elements throws NPE */ @@ -694,6 +718,29 @@ public class LinkedBlockingQueueTest ext unexpectedException(); } } + + /** + * toArray(null) throws NPE + */ + public void testToArray_BadArg() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(null); + shouldThrow(); + } catch(NullPointerException success){} + } + + /** + * toArray with incompatable array type throws CCE + */ + public void testToArray1_BadArg() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(new String[10] ); + shouldThrow(); + } catch(ArrayStoreException success){} + } + /** * iterator iterates through all elements @@ -873,4 +920,109 @@ public class LinkedBlockingQueueTest ext } } + /** + * drainTo(null) throws NPE + */ + public void testDrainToNull() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(null); + shouldThrow(); + } catch(NullPointerException success) { + } + } + + /** + * drainTo(this) throws IAE + */ + public void testDrainToSelf() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(q); + shouldThrow(); + } catch(IllegalArgumentException success) { + } + } + + /** + * drainTo(c) empties queue into another collection c + */ + public void testDrainTo() { + LinkedBlockingQueue q = populatedQueue(SIZE); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertEquals(q.size(), 0); + assertEquals(l.size(), SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + } + + /** + * drainTo empties full queue, unblocking a waiting put. + */ + public void testDrainToWithActivePut() { + final LinkedBlockingQueue q = populatedQueue(SIZE); + Thread t = new Thread(new Runnable() { + public void run() { + try { + q.put(new Integer(SIZE+1)); + } catch (InterruptedException ie){ + threadUnexpectedException(); + } + } + }); + try { + t.start(); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + t.join(); + assertTrue(q.size() + l.size() == SIZE+1); + } catch(Exception e){ + unexpectedException(); + } + } + + /** + * drainTo(null, n) throws NPE + */ + public void testDrainToNullN() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(null, 0); + shouldThrow(); + } catch(NullPointerException success) { + } + } + + /** + * drainTo(this, n) throws IAE + */ + public void testDrainToSelfN() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(q, 0); + shouldThrow(); + } catch(IllegalArgumentException success) { + } + } + + /** + * drainTo(c, n) empties first max {n, size} elements of queue into c + */ + public void testDrainToN() { + for (int i = 0; i < SIZE + 2; ++i) { + LinkedBlockingQueue q = populatedQueue(SIZE); + ArrayList l = new ArrayList(); + q.drainTo(l, i); + int k = (i < SIZE)? i : SIZE; + assertEquals(q.size(), SIZE-k); + assertEquals(l.size(), k); + for (int j = 0; j < k; ++j) + assertEquals(l.get(j), new Integer(j)); + } + } + }