ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.8
Committed: Mon Dec 29 19:05:40 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
spellcheck

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.7 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5     * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 dl 1.4
10 dl 1.1 import junit.framework.*;
11     import java.util.*;
12     import java.util.concurrent.*;
13 dl 1.2 import java.io.*;
14 dl 1.1
15 dl 1.3 public class ArrayBlockingQueueTest extends JSR166TestCase {
16 dl 1.1 public static void main(String[] args) {
17     junit.textui.TestRunner.run (suite());
18     }
19     public static Test suite() {
20     return new TestSuite(ArrayBlockingQueueTest.class);
21     }
22    
23     /**
24     * Create a queue of given size containing consecutive
25     * Integers 0 ... n.
26     */
27 dl 1.3 private ArrayBlockingQueue populatedQueue(int n) {
28 dl 1.1 ArrayBlockingQueue q = new ArrayBlockingQueue(n);
29     assertTrue(q.isEmpty());
30     for(int i = 0; i < n; i++)
31     assertTrue(q.offer(new Integer(i)));
32     assertFalse(q.isEmpty());
33     assertEquals(0, q.remainingCapacity());
34     assertEquals(n, q.size());
35     return q;
36     }
37    
38 dl 1.4 /**
39 dl 1.5 * A new queue has the indicated capacity
40 dl 1.4 */
41     public void testConstructor1() {
42 dl 1.3 assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
43 dl 1.1 }
44    
45 dl 1.4 /**
46 dl 1.5 * Constructor throws IAE if capacity argument nonpositive
47 dl 1.4 */
48     public void testConstructor2() {
49 dl 1.1 try {
50     ArrayBlockingQueue q = new ArrayBlockingQueue(0);
51 dl 1.4 shouldThrow();
52 dl 1.1 }
53     catch (IllegalArgumentException success) {}
54     }
55    
56 dl 1.4 /**
57 dl 1.5 * Initializing from null Collection throws NPE
58 dl 1.4 */
59     public void testConstructor3() {
60 dl 1.1 try {
61     ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
62 dl 1.4 shouldThrow();
63 dl 1.1 }
64     catch (NullPointerException success) {}
65     }
66    
67 dl 1.4 /**
68 dl 1.5 * Initializing from Collection of null elements throws NPE
69 dl 1.4 */
70     public void testConstructor4() {
71 dl 1.1 try {
72 dl 1.3 Integer[] ints = new Integer[SIZE];
73     ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
74 dl 1.4 shouldThrow();
75 dl 1.1 }
76     catch (NullPointerException success) {}
77     }
78    
79 dl 1.4 /**
80 dl 1.5 * Initializing from Collection with some null elements throws NPE
81 dl 1.4 */
82     public void testConstructor5() {
83 dl 1.1 try {
84 dl 1.3 Integer[] ints = new Integer[SIZE];
85     for (int i = 0; i < SIZE-1; ++i)
86 dl 1.1 ints[i] = new Integer(i);
87 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
88 dl 1.4 shouldThrow();
89 dl 1.1 }
90     catch (NullPointerException success) {}
91     }
92    
93 dl 1.4 /**
94 dl 1.5 * Initializing from too large collection throws IAE
95 dl 1.4 */
96     public void testConstructor6() {
97 dl 1.1 try {
98 dl 1.3 Integer[] ints = new Integer[SIZE];
99     for (int i = 0; i < SIZE; ++i)
100 dl 1.1 ints[i] = new Integer(i);
101     ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
102 dl 1.4 shouldThrow();
103 dl 1.1 }
104     catch (IllegalArgumentException success) {}
105     }
106    
107 dl 1.4 /**
108 dl 1.5 * Queue contains all elements of collection used to initialize
109 dl 1.4 */
110     public void testConstructor7() {
111 dl 1.1 try {
112 dl 1.3 Integer[] ints = new Integer[SIZE];
113     for (int i = 0; i < SIZE; ++i)
114 dl 1.1 ints[i] = new Integer(i);
115 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
116     for (int i = 0; i < SIZE; ++i)
117 dl 1.1 assertEquals(ints[i], q.poll());
118     }
119     finally {}
120     }
121    
122 dl 1.4 /**
123 dl 1.5 * Queue transitions from empty to full when elements added
124 dl 1.4 */
125 dl 1.1 public void testEmptyFull() {
126     ArrayBlockingQueue q = new ArrayBlockingQueue(2);
127     assertTrue(q.isEmpty());
128 dl 1.4 assertEquals(2, q.remainingCapacity());
129 dl 1.3 q.add(one);
130 dl 1.1 assertFalse(q.isEmpty());
131 dl 1.3 q.add(two);
132 dl 1.1 assertFalse(q.isEmpty());
133 dl 1.4 assertEquals(0, q.remainingCapacity());
134     assertFalse(q.offer(three));
135 dl 1.1 }
136    
137 dl 1.4 /**
138 dl 1.5 * remainingCapacity decreases on add, increases on remove
139 dl 1.4 */
140     public void testRemainingCapacity() {
141 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
142     for (int i = 0; i < SIZE; ++i) {
143 dl 1.1 assertEquals(i, q.remainingCapacity());
144 dl 1.3 assertEquals(SIZE-i, q.size());
145 dl 1.1 q.remove();
146     }
147 dl 1.3 for (int i = 0; i < SIZE; ++i) {
148     assertEquals(SIZE-i, q.remainingCapacity());
149 dl 1.1 assertEquals(i, q.size());
150     q.add(new Integer(i));
151     }
152     }
153    
154 dl 1.4 /**
155 dl 1.5 * offer(null) throws NPE
156 dl 1.4 */
157     public void testOfferNull() {
158 dl 1.1 try {
159     ArrayBlockingQueue q = new ArrayBlockingQueue(1);
160     q.offer(null);
161 dl 1.4 shouldThrow();
162 dl 1.1 } catch (NullPointerException success) { }
163     }
164    
165 dl 1.4 /**
166 dl 1.6 * add(null) throws NPE
167     */
168     public void testAddNull() {
169     try {
170     ArrayBlockingQueue q = new ArrayBlockingQueue(1);
171     q.add(null);
172     shouldThrow();
173     } catch (NullPointerException success) { }
174     }
175    
176     /**
177 dl 1.5 * Offer succeeds if not full; fails if full
178 dl 1.4 */
179     public void testOffer() {
180 dl 1.1 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
181 dl 1.3 assertTrue(q.offer(zero));
182     assertFalse(q.offer(one));
183 dl 1.1 }
184    
185 dl 1.4 /**
186 dl 1.5 * add succeeds if not full; throws ISE if full
187 dl 1.4 */
188     public void testAdd() {
189 dl 1.1 try {
190 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
191     for (int i = 0; i < SIZE; ++i) {
192 dl 1.1 assertTrue(q.add(new Integer(i)));
193     }
194     assertEquals(0, q.remainingCapacity());
195 dl 1.3 q.add(new Integer(SIZE));
196 dl 1.1 } catch (IllegalStateException success){
197     }
198     }
199    
200 dl 1.4 /**
201 dl 1.5 * addAll(null) throws NPE
202 dl 1.4 */
203     public void testAddAll1() {
204 dl 1.1 try {
205     ArrayBlockingQueue q = new ArrayBlockingQueue(1);
206     q.addAll(null);
207 dl 1.4 shouldThrow();
208 dl 1.1 }
209     catch (NullPointerException success) {}
210     }
211 dl 1.6
212     /**
213     * addAll(this) throws IAE
214     */
215     public void testAddAllSelf() {
216     try {
217     ArrayBlockingQueue q = populatedQueue(SIZE);
218     q.addAll(q);
219     shouldThrow();
220     }
221     catch (IllegalArgumentException success) {}
222     }
223    
224    
225 dl 1.4 /**
226 dl 1.5 * addAll of a collection with null elements throws NPE
227 dl 1.4 */
228     public void testAddAll2() {
229 dl 1.1 try {
230 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
231     Integer[] ints = new Integer[SIZE];
232 dl 1.1 q.addAll(Arrays.asList(ints));
233 dl 1.4 shouldThrow();
234 dl 1.1 }
235     catch (NullPointerException success) {}
236     }
237 dl 1.4 /**
238 dl 1.5 * addAll of a collection with any null elements throws NPE after
239     * possibly adding some elements
240 dl 1.4 */
241     public void testAddAll3() {
242 dl 1.1 try {
243 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
244     Integer[] ints = new Integer[SIZE];
245     for (int i = 0; i < SIZE-1; ++i)
246 dl 1.1 ints[i] = new Integer(i);
247     q.addAll(Arrays.asList(ints));
248 dl 1.4 shouldThrow();
249 dl 1.1 }
250     catch (NullPointerException success) {}
251     }
252 dl 1.4 /**
253 dl 1.5 * addAll throws ISE if not enough room
254 dl 1.4 */
255     public void testAddAll4() {
256 dl 1.1 try {
257     ArrayBlockingQueue q = new ArrayBlockingQueue(1);
258 dl 1.3 Integer[] ints = new Integer[SIZE];
259     for (int i = 0; i < SIZE; ++i)
260 dl 1.1 ints[i] = new Integer(i);
261     q.addAll(Arrays.asList(ints));
262 dl 1.4 shouldThrow();
263 dl 1.1 }
264     catch (IllegalStateException success) {}
265     }
266 dl 1.4 /**
267 dl 1.5 * Queue contains all elements, in traversal order, of successful addAll
268 dl 1.4 */
269     public void testAddAll5() {
270 dl 1.1 try {
271     Integer[] empty = new Integer[0];
272 dl 1.3 Integer[] ints = new Integer[SIZE];
273     for (int i = 0; i < SIZE; ++i)
274 dl 1.1 ints[i] = new Integer(i);
275 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
276 dl 1.1 assertFalse(q.addAll(Arrays.asList(empty)));
277     assertTrue(q.addAll(Arrays.asList(ints)));
278 dl 1.3 for (int i = 0; i < SIZE; ++i)
279 dl 1.1 assertEquals(ints[i], q.poll());
280     }
281     finally {}
282     }
283    
284 dl 1.4 /**
285 dl 1.5 * put(null) throws NPE
286 dl 1.4 */
287 dl 1.1 public void testPutNull() {
288     try {
289 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
290 dl 1.1 q.put(null);
291 dl 1.4 shouldThrow();
292 dl 1.1 }
293     catch (NullPointerException success){
294     }
295     catch (InterruptedException ie) {
296 dl 1.4 unexpectedException();
297 dl 1.1 }
298     }
299    
300 dl 1.4 /**
301 dl 1.5 * all elements successfully put are contained
302 dl 1.4 */
303 dl 1.1 public void testPut() {
304     try {
305 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
306     for (int i = 0; i < SIZE; ++i) {
307 dl 1.1 Integer I = new Integer(i);
308     q.put(I);
309     assertTrue(q.contains(I));
310     }
311     assertEquals(0, q.remainingCapacity());
312     }
313     catch (InterruptedException ie) {
314 dl 1.4 unexpectedException();
315 dl 1.1 }
316     }
317    
318 dl 1.4 /**
319 dl 1.5 * put blocks interruptibly if full
320 dl 1.4 */
321     public void testBlockingPut() {
322 dl 1.1 Thread t = new Thread(new Runnable() {
323     public void run() {
324     int added = 0;
325     try {
326 dl 1.3 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
327     for (int i = 0; i < SIZE; ++i) {
328 dl 1.1 q.put(new Integer(i));
329     ++added;
330     }
331 dl 1.3 q.put(new Integer(SIZE));
332 dl 1.4 threadShouldThrow();
333 dl 1.1 } catch (InterruptedException ie){
334 dl 1.3 threadAssertEquals(added, SIZE);
335 dl 1.1 }
336     }});
337     try {
338 dl 1.3 t.start();
339 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
340     t.interrupt();
341     t.join();
342     }
343     catch (InterruptedException ie) {
344 dl 1.4 unexpectedException();
345 dl 1.1 }
346     }
347    
348 dl 1.4 /**
349 dl 1.5 * put blocks waiting for take when full
350 dl 1.4 */
351 dl 1.1 public void testPutWithTake() {
352     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
353     Thread t = new Thread(new Runnable() {
354 dl 1.4 public void run() {
355 dl 1.1 int added = 0;
356     try {
357     q.put(new Object());
358     ++added;
359     q.put(new Object());
360     ++added;
361     q.put(new Object());
362     ++added;
363     q.put(new Object());
364     ++added;
365 dl 1.4 threadShouldThrow();
366 dl 1.1 } catch (InterruptedException e){
367 dl 1.3 threadAssertTrue(added >= 2);
368 dl 1.1 }
369     }
370     });
371     try {
372     t.start();
373     Thread.sleep(SHORT_DELAY_MS);
374     q.take();
375     t.interrupt();
376     t.join();
377     } catch (Exception e){
378 dl 1.4 unexpectedException();
379 dl 1.1 }
380     }
381    
382 dl 1.4 /**
383 dl 1.5 * timed offer times out if full and elements not taken
384 dl 1.4 */
385 dl 1.1 public void testTimedOffer() {
386     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
387     Thread t = new Thread(new Runnable() {
388 dl 1.4 public void run() {
389 dl 1.1 try {
390     q.put(new Object());
391     q.put(new Object());
392 dl 1.3 threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
393 dl 1.1 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
394 dl 1.4 threadShouldThrow();
395 dl 1.1 } catch (InterruptedException success){}
396     }
397     });
398    
399     try {
400     t.start();
401     Thread.sleep(SHORT_DELAY_MS);
402     t.interrupt();
403     t.join();
404     } catch (Exception e){
405 dl 1.4 unexpectedException();
406 dl 1.1 }
407     }
408    
409 dl 1.4 /**
410 dl 1.5 * take retrieves elements in FIFO order
411 dl 1.4 */
412     public void testTake() {
413 dl 1.1 try {
414 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
415     for (int i = 0; i < SIZE; ++i) {
416 dl 1.1 assertEquals(i, ((Integer)q.take()).intValue());
417     }
418     } catch (InterruptedException e){
419 dl 1.4 unexpectedException();
420 dl 1.1 }
421     }
422    
423 dl 1.4 /**
424 dl 1.5 * take blocks interruptibly when empty
425 dl 1.4 */
426 dl 1.1 public void testTakeFromEmpty() {
427     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
428     Thread t = new Thread(new Runnable() {
429 dl 1.4 public void run() {
430 dl 1.1 try {
431     q.take();
432 dl 1.4 threadShouldThrow();
433 dl 1.1 } catch (InterruptedException success){ }
434     }
435     });
436     try {
437     t.start();
438     Thread.sleep(SHORT_DELAY_MS);
439     t.interrupt();
440     t.join();
441     } catch (Exception e){
442 dl 1.4 unexpectedException();
443 dl 1.1 }
444     }
445    
446 dl 1.4 /**
447 dl 1.5 * Take removes existing elements until empty, then blocks interruptibly
448 dl 1.4 */
449     public void testBlockingTake() {
450 dl 1.1 Thread t = new Thread(new Runnable() {
451     public void run() {
452     try {
453 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
454     for (int i = 0; i < SIZE; ++i) {
455     threadAssertEquals(i, ((Integer)q.take()).intValue());
456 dl 1.1 }
457     q.take();
458 dl 1.4 threadShouldThrow();
459 dl 1.1 } catch (InterruptedException success){
460     }
461     }});
462     try {
463 dl 1.3 t.start();
464     Thread.sleep(SHORT_DELAY_MS);
465     t.interrupt();
466     t.join();
467 dl 1.1 }
468     catch (InterruptedException ie) {
469 dl 1.4 unexpectedException();
470 dl 1.1 }
471     }
472    
473    
474 dl 1.4 /**
475 dl 1.5 * poll succeeds unless empty
476 dl 1.4 */
477     public void testPoll() {
478 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
479     for (int i = 0; i < SIZE; ++i) {
480 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
481     }
482     assertNull(q.poll());
483     }
484    
485 dl 1.4 /**
486 dl 1.5 * timed pool with zero timeout succeeds when non-empty, else times out
487 dl 1.4 */
488 dl 1.1 public void testTimedPoll0() {
489     try {
490 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
491     for (int i = 0; i < SIZE; ++i) {
492 dl 1.1 assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
493     }
494     assertNull(q.poll(0, TimeUnit.MILLISECONDS));
495     } catch (InterruptedException e){
496 dl 1.4 unexpectedException();
497 dl 1.1 }
498     }
499    
500 dl 1.4 /**
501 dl 1.5 * timed pool with nonzero timeout succeeds when non-empty, else times out
502 dl 1.4 */
503 dl 1.1 public void testTimedPoll() {
504     try {
505 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
506     for (int i = 0; i < SIZE; ++i) {
507 dl 1.1 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
508     }
509     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
510     } catch (InterruptedException e){
511 dl 1.4 unexpectedException();
512 dl 1.1 }
513     }
514    
515 dl 1.4 /**
516 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
517     * returning timeout status
518 dl 1.4 */
519     public void testInterruptedTimedPoll() {
520 dl 1.1 Thread t = new Thread(new Runnable() {
521     public void run() {
522     try {
523 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
524     for (int i = 0; i < SIZE; ++i) {
525     threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
526 dl 1.1 }
527 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
528 dl 1.1 } catch (InterruptedException success){
529     }
530     }});
531     try {
532 dl 1.3 t.start();
533     Thread.sleep(SHORT_DELAY_MS);
534     t.interrupt();
535     t.join();
536 dl 1.1 }
537     catch (InterruptedException ie) {
538 dl 1.4 unexpectedException();
539 dl 1.1 }
540     }
541    
542 dl 1.4 /**
543 dl 1.5 * timed poll before a delayed offer fails; after offer succeeds;
544     * on interruption throws
545 dl 1.4 */
546     public void testTimedPollWithOffer() {
547 dl 1.1 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
548     Thread t = new Thread(new Runnable() {
549 dl 1.4 public void run() {
550 dl 1.1 try {
551 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
552 dl 1.1 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
553     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
554 dl 1.4 threadShouldThrow();
555 dl 1.1 } catch (InterruptedException success) { }
556     }
557     });
558     try {
559     t.start();
560 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
561     assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
562 dl 1.1 t.interrupt();
563     t.join();
564     } catch (Exception e){
565 dl 1.4 unexpectedException();
566 dl 1.1 }
567     }
568    
569    
570 dl 1.4 /**
571 dl 1.5 * peek returns next element, or null if empty
572 dl 1.4 */
573     public void testPeek() {
574 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
575     for (int i = 0; i < SIZE; ++i) {
576 dl 1.1 assertEquals(i, ((Integer)q.peek()).intValue());
577     q.poll();
578     assertTrue(q.peek() == null ||
579     i != ((Integer)q.peek()).intValue());
580     }
581     assertNull(q.peek());
582     }
583    
584 dl 1.4 /**
585 dl 1.5 * element returns next element, or throws NSEE if empty
586 dl 1.4 */
587     public void testElement() {
588 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
589     for (int i = 0; i < SIZE; ++i) {
590 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
591     q.poll();
592     }
593     try {
594     q.element();
595 dl 1.4 shouldThrow();
596 dl 1.1 }
597     catch (NoSuchElementException success) {}
598     }
599    
600 dl 1.4 /**
601 dl 1.5 * remove removes next element, or throws NSEE if empty
602 dl 1.4 */
603     public void testRemove() {
604 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
605     for (int i = 0; i < SIZE; ++i) {
606 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
607     }
608     try {
609     q.remove();
610 dl 1.4 shouldThrow();
611 dl 1.1 } catch (NoSuchElementException success){
612     }
613     }
614    
615 dl 1.4 /**
616 dl 1.5 * remove(x) removes x and returns true if present
617 dl 1.4 */
618     public void testRemoveElement() {
619 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
620     for (int i = 1; i < SIZE; i+=2) {
621 dl 1.1 assertTrue(q.remove(new Integer(i)));
622     }
623 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
624 dl 1.1 assertTrue(q.remove(new Integer(i)));
625     assertFalse(q.remove(new Integer(i+1)));
626     }
627 dl 1.2 assertTrue(q.isEmpty());
628 dl 1.1 }
629    
630 dl 1.4 /**
631 dl 1.5 * contains(x) reports true when elements added but not yet removed
632 dl 1.4 */
633     public void testContains() {
634 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
635     for (int i = 0; i < SIZE; ++i) {
636 dl 1.1 assertTrue(q.contains(new Integer(i)));
637     q.poll();
638     assertFalse(q.contains(new Integer(i)));
639     }
640     }
641    
642 dl 1.4 /**
643 dl 1.5 * clear removes all elements
644 dl 1.4 */
645     public void testClear() {
646 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
647 dl 1.1 q.clear();
648     assertTrue(q.isEmpty());
649     assertEquals(0, q.size());
650 dl 1.3 assertEquals(SIZE, q.remainingCapacity());
651     q.add(one);
652 dl 1.1 assertFalse(q.isEmpty());
653     q.clear();
654     assertTrue(q.isEmpty());
655     }
656    
657 dl 1.4 /**
658 dl 1.5 * containsAll(c) is true when c contains a subset of elements
659 dl 1.4 */
660     public void testContainsAll() {
661 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
662     ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
663     for (int i = 0; i < SIZE; ++i) {
664 dl 1.1 assertTrue(q.containsAll(p));
665     assertFalse(p.containsAll(q));
666     p.add(new Integer(i));
667     }
668     assertTrue(p.containsAll(q));
669     }
670    
671 dl 1.4 /**
672 dl 1.5 * retainAll(c) retains only those elements of c and reports true if changed
673 dl 1.4 */
674     public void testRetainAll() {
675 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
676     ArrayBlockingQueue p = populatedQueue(SIZE);
677     for (int i = 0; i < SIZE; ++i) {
678 dl 1.1 boolean changed = q.retainAll(p);
679     if (i == 0)
680     assertFalse(changed);
681     else
682     assertTrue(changed);
683    
684     assertTrue(q.containsAll(p));
685 dl 1.3 assertEquals(SIZE-i, q.size());
686 dl 1.1 p.remove();
687     }
688     }
689    
690 dl 1.4 /**
691 dl 1.5 * removeAll(c) removes only those elements of c and reports true if changed
692 dl 1.4 */
693     public void testRemoveAll() {
694 dl 1.3 for (int i = 1; i < SIZE; ++i) {
695     ArrayBlockingQueue q = populatedQueue(SIZE);
696     ArrayBlockingQueue p = populatedQueue(i);
697 dl 1.1 assertTrue(q.removeAll(p));
698 dl 1.3 assertEquals(SIZE-i, q.size());
699 dl 1.1 for (int j = 0; j < i; ++j) {
700     Integer I = (Integer)(p.remove());
701     assertFalse(q.contains(I));
702     }
703     }
704     }
705    
706 dl 1.4 /**
707 dl 1.5 * toArray contains all elements
708 dl 1.4 */
709     public void testToArray() {
710 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
711 dl 1.1 Object[] o = q.toArray();
712     try {
713     for(int i = 0; i < o.length; i++)
714     assertEquals(o[i], q.take());
715     } catch (InterruptedException e){
716 dl 1.4 unexpectedException();
717 dl 1.1 }
718     }
719    
720 dl 1.4 /**
721 dl 1.5 * toArray(a) contains all elements
722 dl 1.4 */
723     public void testToArray2() {
724 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
725     Integer[] ints = new Integer[SIZE];
726 dl 1.1 ints = (Integer[])q.toArray(ints);
727     try {
728     for(int i = 0; i < ints.length; i++)
729     assertEquals(ints[i], q.take());
730     } catch (InterruptedException e){
731 dl 1.4 unexpectedException();
732 dl 1.1 }
733     }
734 dl 1.6
735     /**
736     * toArray(null) throws NPE
737     */
738     public void testToArray_BadArg() {
739     try {
740     ArrayBlockingQueue q = populatedQueue(SIZE);
741     Object o[] = q.toArray(null);
742     shouldThrow();
743     } catch(NullPointerException success){}
744     }
745    
746     /**
747 dl 1.8 * toArray with incompatible array type throws CCE
748 dl 1.6 */
749     public void testToArray1_BadArg() {
750     try {
751     ArrayBlockingQueue q = populatedQueue(SIZE);
752     Object o[] = q.toArray(new String[10] );
753     shouldThrow();
754     } catch(ArrayStoreException success){}
755     }
756    
757 dl 1.1
758 dl 1.4 /**
759 dl 1.5 * iterator iterates through all elements
760 dl 1.4 */
761     public void testIterator() {
762 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
763 dl 1.1 Iterator it = q.iterator();
764     try {
765     while(it.hasNext()){
766     assertEquals(it.next(), q.take());
767     }
768     } catch (InterruptedException e){
769 dl 1.4 unexpectedException();
770 dl 1.1 }
771     }
772    
773 dl 1.4 /**
774 dl 1.5 * iterator.remove removes current element
775     */
776     public void testIteratorRemove () {
777     final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
778     q.add(two);
779     q.add(one);
780     q.add(three);
781    
782     Iterator it = q.iterator();
783     it.next();
784     it.remove();
785    
786     it = q.iterator();
787     assertEquals(it.next(), one);
788     assertEquals(it.next(), three);
789     assertFalse(it.hasNext());
790     }
791    
792     /**
793     * iterator ordering is FIFO
794 dl 1.4 */
795 dl 1.1 public void testIteratorOrdering() {
796     final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
797 dl 1.3 q.add(one);
798     q.add(two);
799     q.add(three);
800 dl 1.1
801     assertEquals("queue should be full", 0, q.remainingCapacity());
802    
803     int k = 0;
804     for (Iterator it = q.iterator(); it.hasNext();) {
805     int i = ((Integer)(it.next())).intValue();
806 dl 1.4 assertEquals(++k, i);
807 dl 1.1 }
808 dl 1.4 assertEquals(3, k);
809 dl 1.1 }
810    
811 dl 1.4 /**
812 dl 1.5 * Modifications do not cause iterators to fail
813 dl 1.4 */
814 dl 1.1 public void testWeaklyConsistentIteration () {
815     final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
816 dl 1.3 q.add(one);
817     q.add(two);
818     q.add(three);
819 dl 1.1 try {
820     for (Iterator it = q.iterator(); it.hasNext();) {
821     q.remove();
822     it.next();
823     }
824     }
825     catch (ConcurrentModificationException e) {
826 dl 1.4 unexpectedException();
827 dl 1.1 }
828 dl 1.4 assertEquals(0, q.size());
829 dl 1.1 }
830    
831    
832 dl 1.4 /**
833 dl 1.5 * toString contains toStrings of elements
834 dl 1.4 */
835     public void testToString() {
836 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
837 dl 1.1 String s = q.toString();
838 dl 1.3 for (int i = 0; i < SIZE; ++i) {
839 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
840     }
841     }
842    
843    
844 dl 1.4 /**
845 dl 1.5 * offer transfers elements across Executor tasks
846 dl 1.4 */
847 dl 1.1 public void testOfferInExecutor() {
848     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
849 dl 1.3 q.add(one);
850     q.add(two);
851 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
852     executor.execute(new Runnable() {
853     public void run() {
854 dl 1.3 threadAssertFalse(q.offer(three));
855 dl 1.1 try {
856 dl 1.3 threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
857     threadAssertEquals(0, q.remainingCapacity());
858 dl 1.1 }
859     catch (InterruptedException e) {
860 dl 1.4 threadUnexpectedException();
861 dl 1.1 }
862     }
863     });
864    
865     executor.execute(new Runnable() {
866     public void run() {
867     try {
868 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
869     threadAssertEquals(one, q.take());
870 dl 1.1 }
871     catch (InterruptedException e) {
872 dl 1.4 threadUnexpectedException();
873 dl 1.1 }
874     }
875     });
876    
877 dl 1.3 joinPool(executor);
878 dl 1.1
879     }
880    
881 dl 1.4 /**
882 dl 1.5 * poll retrieves elements across Executor threads
883 dl 1.4 */
884 dl 1.1 public void testPollInExecutor() {
885     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
886     ExecutorService executor = Executors.newFixedThreadPool(2);
887     executor.execute(new Runnable() {
888     public void run() {
889 dl 1.3 threadAssertNull(q.poll());
890 dl 1.1 try {
891 dl 1.3 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
892     threadAssertTrue(q.isEmpty());
893 dl 1.1 }
894     catch (InterruptedException e) {
895 dl 1.4 threadUnexpectedException();
896 dl 1.1 }
897     }
898     });
899    
900     executor.execute(new Runnable() {
901     public void run() {
902     try {
903 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
904     q.put(one);
905 dl 1.1 }
906     catch (InterruptedException e) {
907 dl 1.4 threadUnexpectedException();
908 dl 1.1 }
909     }
910     });
911    
912 dl 1.3 joinPool(executor);
913 dl 1.1 }
914 dl 1.2
915 dl 1.4 /**
916 dl 1.5 * A deserialized serialized queue has same elements in same order
917 dl 1.4 */
918 dl 1.2 public void testSerialization() {
919 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
920 dl 1.2
921     try {
922     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
923     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
924     out.writeObject(q);
925     out.close();
926    
927     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
928     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
929     ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
930     assertEquals(q.size(), r.size());
931     while (!q.isEmpty())
932     assertEquals(q.remove(), r.remove());
933     } catch(Exception e){
934 dl 1.4 unexpectedException();
935 dl 1.6 }
936     }
937    
938     /**
939     * drainTo(null) throws NPE
940     */
941     public void testDrainToNull() {
942     ArrayBlockingQueue q = populatedQueue(SIZE);
943     try {
944     q.drainTo(null);
945     shouldThrow();
946     } catch(NullPointerException success) {
947     }
948     }
949    
950     /**
951     * drainTo(this) throws IAE
952     */
953     public void testDrainToSelf() {
954     ArrayBlockingQueue q = populatedQueue(SIZE);
955     try {
956     q.drainTo(q);
957     shouldThrow();
958     } catch(IllegalArgumentException success) {
959     }
960     }
961    
962     /**
963     * drainTo(c) empties queue into another collection c
964     */
965     public void testDrainTo() {
966     ArrayBlockingQueue q = populatedQueue(SIZE);
967     ArrayList l = new ArrayList();
968     q.drainTo(l);
969     assertEquals(q.size(), 0);
970     assertEquals(l.size(), SIZE);
971     for (int i = 0; i < SIZE; ++i)
972     assertEquals(l.get(i), new Integer(i));
973     }
974    
975     /**
976     * drainTo empties full queue, unblocking a waiting put.
977     */
978     public void testDrainToWithActivePut() {
979     final ArrayBlockingQueue q = populatedQueue(SIZE);
980     Thread t = new Thread(new Runnable() {
981     public void run() {
982     try {
983     q.put(new Integer(SIZE+1));
984     } catch (InterruptedException ie){
985     threadUnexpectedException();
986     }
987     }
988     });
989     try {
990     t.start();
991     ArrayList l = new ArrayList();
992     q.drainTo(l);
993     assertTrue(l.size() >= SIZE);
994     for (int i = 0; i < SIZE; ++i)
995     assertEquals(l.get(i), new Integer(i));
996     t.join();
997     assertTrue(q.size() + l.size() == SIZE+1);
998     } catch(Exception e){
999     unexpectedException();
1000     }
1001     }
1002    
1003     /**
1004     * drainTo(null, n) throws NPE
1005     */
1006     public void testDrainToNullN() {
1007     ArrayBlockingQueue q = populatedQueue(SIZE);
1008     try {
1009     q.drainTo(null, 0);
1010     shouldThrow();
1011     } catch(NullPointerException success) {
1012     }
1013     }
1014    
1015     /**
1016     * drainTo(this, n) throws IAE
1017     */
1018     public void testDrainToSelfN() {
1019     ArrayBlockingQueue q = populatedQueue(SIZE);
1020     try {
1021     q.drainTo(q, 0);
1022     shouldThrow();
1023     } catch(IllegalArgumentException success) {
1024     }
1025     }
1026    
1027     /**
1028     * drainTo(c, n) empties first max {n, size} elements of queue into c
1029     */
1030     public void testDrainToN() {
1031     for (int i = 0; i < SIZE + 2; ++i) {
1032     ArrayBlockingQueue q = populatedQueue(SIZE);
1033     ArrayList l = new ArrayList();
1034     q.drainTo(l, i);
1035     int k = (i < SIZE)? i : SIZE;
1036     assertEquals(q.size(), SIZE-k);
1037     assertEquals(l.size(), k);
1038     for (int j = 0; j < k; ++j)
1039     assertEquals(l.get(j), new Integer(j));
1040 dl 1.2 }
1041     }
1042    
1043 dl 1.1
1044     }