ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.41
Committed: Fri Nov 5 00:17:22 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.40: +5 -4 lines
Log Message:
very small improvements to testToArray2

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