ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.6
Committed: Sun Oct 5 23:00:39 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.5: +153 -0 lines
Log Message:
Added tests and documentation

File Contents

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