ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.81 by jsr166, Thu Sep 5 21:11:13 2019 UTC vs.
Revision 1.82 by dl, Tue Jan 26 13:33:06 2021 UTC

# Line 44 | Line 44 | public class LinkedBlockingQueueTest ext
44          class Implementation implements CollectionImplementation {
45              public Class<?> klazz() { return LinkedBlockingQueue.class; }
46              public Collection emptyCollection() { return new LinkedBlockingQueue(); }
47 <            public Object makeElement(int i) { return i; }
47 >            public Object makeElement(int i) { return JSR166TestCase.itemFor(i); }
48              public boolean isConcurrent() { return true; }
49              public boolean permitsNulls() { return false; }
50          }
# Line 56 | Line 56 | public class LinkedBlockingQueueTest ext
56  
57      /**
58       * Returns a new queue of given size containing consecutive
59 <     * Integers 0 ... n - 1.
59 >     * Items 0 ... n - 1.
60       */
61 <    private static LinkedBlockingQueue<Integer> populatedQueue(int n) {
62 <        LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>(n);
61 >    private static LinkedBlockingQueue<Item> populatedQueue(int n) {
62 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(n);
63          assertTrue(q.isEmpty());
64          for (int i = 0; i < n; i++)
65 <            assertTrue(q.offer(new Integer(i)));
65 >            mustOffer(q, i);
66          assertFalse(q.isEmpty());
67 <        assertEquals(0, q.remainingCapacity());
68 <        assertEquals(n, q.size());
69 <        assertEquals((Integer) 0, q.peek());
67 >        mustEqual(0, q.remainingCapacity());
68 >        mustEqual(n, q.size());
69 >        mustEqual(0, q.peek());
70          return q;
71      }
72  
# Line 75 | Line 75 | public class LinkedBlockingQueueTest ext
75       * none given
76       */
77      public void testConstructor1() {
78 <        assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
79 <        assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity());
78 >        mustEqual(SIZE, new LinkedBlockingQueue<Item>(SIZE).remainingCapacity());
79 >        mustEqual(Integer.MAX_VALUE, new LinkedBlockingQueue<Item>().remainingCapacity());
80      }
81  
82      /**
# Line 84 | Line 84 | public class LinkedBlockingQueueTest ext
84       */
85      public void testConstructor2() {
86          try {
87 <            new LinkedBlockingQueue(0);
87 >            new LinkedBlockingQueue<Item>(0);
88              shouldThrow();
89          } catch (IllegalArgumentException success) {}
90      }
# Line 94 | Line 94 | public class LinkedBlockingQueueTest ext
94       */
95      public void testConstructor3() {
96          try {
97 <            new LinkedBlockingQueue(null);
97 >            new LinkedBlockingQueue<Item>(null);
98              shouldThrow();
99          } catch (NullPointerException success) {}
100      }
# Line 103 | Line 103 | public class LinkedBlockingQueueTest ext
103       * Initializing from Collection of null elements throws NullPointerException
104       */
105      public void testConstructor4() {
106 <        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
106 >        Collection<Item> elements = Arrays.asList(new Item[SIZE]);
107          try {
108 <            new LinkedBlockingQueue(elements);
108 >            new LinkedBlockingQueue<Item>(elements);
109              shouldThrow();
110          } catch (NullPointerException success) {}
111      }
# Line 115 | Line 115 | public class LinkedBlockingQueueTest ext
115       * NullPointerException
116       */
117      public void testConstructor5() {
118 <        Integer[] ints = new Integer[SIZE];
119 <        for (int i = 0; i < SIZE - 1; ++i)
120 <            ints[i] = new Integer(i);
121 <        Collection<Integer> elements = Arrays.asList(ints);
118 >        Item[] items = new Item[2];
119 >        items[0] = zero;
120 >        Collection<Item> elements = Arrays.asList(items);
121          try {
122 <            new LinkedBlockingQueue(elements);
122 >            new LinkedBlockingQueue<Item>(elements);
123              shouldThrow();
124          } catch (NullPointerException success) {}
125      }
# Line 129 | Line 128 | public class LinkedBlockingQueueTest ext
128       * Queue contains all elements of collection used to initialize
129       */
130      public void testConstructor6() {
131 <        Integer[] ints = new Integer[SIZE];
131 >        Item[] items = defaultItems;
132 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(Arrays.asList(items));
133          for (int i = 0; i < SIZE; ++i)
134 <            ints[i] = new Integer(i);
135 <        LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
136 <        for (int i = 0; i < SIZE; ++i)
137 <            assertEquals(ints[i], q.poll());
134 >            mustEqual(items[i], q.poll());
135      }
136  
137      /**
138       * Queue transitions from empty to full when elements added
139       */
140      public void testEmptyFull() {
141 <        LinkedBlockingQueue q = new LinkedBlockingQueue(2);
141 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2);
142          assertTrue(q.isEmpty());
143 <        assertEquals("should have room for 2", 2, q.remainingCapacity());
143 >        mustEqual(2, q.remainingCapacity());
144          q.add(one);
145          assertFalse(q.isEmpty());
146          q.add(two);
147          assertFalse(q.isEmpty());
148 <        assertEquals(0, q.remainingCapacity());
148 >        mustEqual(0, q.remainingCapacity());
149          assertFalse(q.offer(three));
150      }
151  
# Line 156 | Line 153 | public class LinkedBlockingQueueTest ext
153       * remainingCapacity decreases on add, increases on remove
154       */
155      public void testRemainingCapacity() {
156 <        BlockingQueue q = populatedQueue(SIZE);
156 >        BlockingQueue<Item> q = populatedQueue(SIZE);
157          for (int i = 0; i < SIZE; ++i) {
158 <            assertEquals(i, q.remainingCapacity());
159 <            assertEquals(SIZE, q.size() + q.remainingCapacity());
160 <            assertEquals(i, q.remove());
158 >            mustEqual(i, q.remainingCapacity());
159 >            mustEqual(SIZE, q.size() + q.remainingCapacity());
160 >            mustEqual(i, q.remove());
161          }
162          for (int i = 0; i < SIZE; ++i) {
163 <            assertEquals(SIZE - i, q.remainingCapacity());
164 <            assertEquals(SIZE, q.size() + q.remainingCapacity());
165 <            assertTrue(q.add(i));
163 >            mustEqual(SIZE - i, q.remainingCapacity());
164 >            mustEqual(SIZE, q.size() + q.remainingCapacity());
165 >            mustAdd(q, i);
166          }
167      }
168  
# Line 173 | Line 170 | public class LinkedBlockingQueueTest ext
170       * Offer succeeds if not full; fails if full
171       */
172      public void testOffer() {
173 <        LinkedBlockingQueue q = new LinkedBlockingQueue(1);
173 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(1);
174          assertTrue(q.offer(zero));
175          assertFalse(q.offer(one));
176      }
# Line 182 | Line 179 | public class LinkedBlockingQueueTest ext
179       * add succeeds if not full; throws IllegalStateException if full
180       */
181      public void testAdd() {
182 <        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
182 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE);
183          for (int i = 0; i < SIZE; ++i)
184 <            assertTrue(q.add(new Integer(i)));
185 <        assertEquals(0, q.remainingCapacity());
184 >            mustAdd(q, i);
185 >        mustEqual(0, q.remainingCapacity());
186          try {
187 <            q.add(new Integer(SIZE));
187 >            q.add(new Item(SIZE));
188              shouldThrow();
189          } catch (IllegalStateException success) {}
190      }
# Line 196 | Line 193 | public class LinkedBlockingQueueTest ext
193       * addAll(this) throws IllegalArgumentException
194       */
195      public void testAddAllSelf() {
196 <        LinkedBlockingQueue q = populatedQueue(SIZE);
196 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
197          try {
198              q.addAll(q);
199              shouldThrow();
# Line 208 | Line 205 | public class LinkedBlockingQueueTest ext
205       * possibly adding some elements
206       */
207      public void testAddAll3() {
208 <        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
209 <        Integer[] ints = new Integer[SIZE];
210 <        for (int i = 0; i < SIZE - 1; ++i)
214 <            ints[i] = new Integer(i);
215 <        Collection<Integer> elements = Arrays.asList(ints);
208 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE);
209 >        Item[] items = new Item[2]; items[0] = zero;
210 >        Collection<Item> elements = Arrays.asList(items);
211          try {
212              q.addAll(elements);
213              shouldThrow();
# Line 223 | Line 218 | public class LinkedBlockingQueueTest ext
218       * addAll throws IllegalStateException if not enough room
219       */
220      public void testAddAll4() {
221 <        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1);
222 <        Integer[] ints = new Integer[SIZE];
223 <        for (int i = 0; i < SIZE; ++i)
229 <            ints[i] = new Integer(i);
230 <        Collection<Integer> elements = Arrays.asList(ints);
221 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE - 1);
222 >        Item[] items = defaultItems;
223 >        Collection<Item> elements = Arrays.asList(items);
224          try {
225              q.addAll(elements);
226              shouldThrow();
# Line 238 | Line 231 | public class LinkedBlockingQueueTest ext
231       * Queue contains all elements, in traversal order, of successful addAll
232       */
233      public void testAddAll5() {
234 <        Integer[] empty = new Integer[0];
235 <        Integer[] ints = new Integer[SIZE];
236 <        for (int i = 0; i < SIZE; ++i)
244 <            ints[i] = new Integer(i);
245 <        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
234 >        Item[] empty = new Item[0];
235 >        Item[] items = defaultItems;
236 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE);
237          assertFalse(q.addAll(Arrays.asList(empty)));
238 <        assertTrue(q.addAll(Arrays.asList(ints)));
238 >        assertTrue(q.addAll(Arrays.asList(items)));
239          for (int i = 0; i < SIZE; ++i)
240 <            assertEquals(ints[i], q.poll());
240 >            mustEqual(items[i], q.poll());
241      }
242  
243      /**
244       * all elements successfully put are contained
245       */
246      public void testPut() throws InterruptedException {
247 <        LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
247 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE);
248          for (int i = 0; i < SIZE; ++i) {
249 <            Integer x = new Integer(i);
249 >            Item x = itemFor(i);
250              q.put(x);
251 <            assertTrue(q.contains(x));
251 >            mustContain(q, x);
252          }
253 <        assertEquals(0, q.remainingCapacity());
253 >        mustEqual(0, q.remainingCapacity());
254      }
255  
256      /**
257       * put blocks interruptibly if full
258       */
259      public void testBlockingPut() throws InterruptedException {
260 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
260 >        final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE);
261          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
262          Thread t = newStartedThread(new CheckedRunnable() {
263              public void realRun() throws InterruptedException {
264                  for (int i = 0; i < SIZE; ++i)
265 <                    q.put(i);
266 <                assertEquals(SIZE, q.size());
267 <                assertEquals(0, q.remainingCapacity());
265 >                    q.put(itemFor(i));
266 >                mustEqual(SIZE, q.size());
267 >                mustEqual(0, q.remainingCapacity());
268  
269                  Thread.currentThread().interrupt();
270                  try {
271 <                    q.put(99);
271 >                    q.put(ninetynine);
272                      shouldThrow();
273                  } catch (InterruptedException success) {}
274                  assertFalse(Thread.interrupted());
275  
276                  pleaseInterrupt.countDown();
277                  try {
278 <                    q.put(99);
278 >                    q.put(ninetynine);
279                      shouldThrow();
280                  } catch (InterruptedException success) {}
281                  assertFalse(Thread.interrupted());
# Line 294 | Line 285 | public class LinkedBlockingQueueTest ext
285          if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
286          t.interrupt();
287          awaitTermination(t);
288 <        assertEquals(SIZE, q.size());
289 <        assertEquals(0, q.remainingCapacity());
288 >        mustEqual(SIZE, q.size());
289 >        mustEqual(0, q.remainingCapacity());
290      }
291  
292      /**
# Line 303 | Line 294 | public class LinkedBlockingQueueTest ext
294       */
295      public void testPutWithTake() throws InterruptedException {
296          final int capacity = 2;
297 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
297 >        final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2);
298          final CountDownLatch pleaseTake = new CountDownLatch(1);
299          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
300          Thread t = newStartedThread(new CheckedRunnable() {
301              public void realRun() throws InterruptedException {
302                  for (int i = 0; i < capacity; i++)
303 <                    q.put(i);
303 >                    q.put(itemFor(i));
304                  pleaseTake.countDown();
305 <                q.put(86);
305 >                q.put(eightysix);
306  
307                  Thread.currentThread().interrupt();
308                  try {
309 <                    q.put(99);
309 >                    q.put(ninetynine);
310                      shouldThrow();
311                  } catch (InterruptedException success) {}
312                  assertFalse(Thread.interrupted());
313  
314                  pleaseInterrupt.countDown();
315                  try {
316 <                    q.put(99);
316 >                    q.put(ninetynine);
317                      shouldThrow();
318                  } catch (InterruptedException success) {}
319                  assertFalse(Thread.interrupted());
320              }});
321  
322          await(pleaseTake);
323 <        assertEquals(0, q.remainingCapacity());
324 <        assertEquals(0, q.take());
323 >        mustEqual(0, q.remainingCapacity());
324 >        mustEqual(0, q.take());
325  
326          await(pleaseInterrupt);
327          if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
328          t.interrupt();
329          awaitTermination(t);
330 <        assertEquals(0, q.remainingCapacity());
330 >        mustEqual(0, q.remainingCapacity());
331      }
332  
333      /**
334       * timed offer times out if full and elements not taken
335       */
336      public void testTimedOffer() {
337 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
337 >        final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2);
338          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
339          Thread t = newStartedThread(new CheckedRunnable() {
340              public void realRun() throws InterruptedException {
341 <                q.put(new Object());
342 <                q.put(new Object());
341 >                q.put(zero);
342 >                q.put(one);
343                  long startTime = System.nanoTime();
344  
345 <                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
345 >                assertFalse(q.offer(two, timeoutMillis(), MILLISECONDS));
346                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
347  
348                  Thread.currentThread().interrupt();
349                  try {
350 <                    q.offer(new Object(), randomTimeout(), randomTimeUnit());
350 >                    q.offer(three, randomTimeout(), randomTimeUnit());
351                      shouldThrow();
352                  } catch (InterruptedException success) {}
353                  assertFalse(Thread.interrupted());
354  
355                  pleaseInterrupt.countDown();
356                  try {
357 <                    q.offer(new Object(), LONGER_DELAY_MS, MILLISECONDS);
357 >                    q.offer(two, LONGER_DELAY_MS, MILLISECONDS);
358                      shouldThrow();
359                  } catch (InterruptedException success) {}
360                  assertFalse(Thread.interrupted());
# Line 379 | Line 370 | public class LinkedBlockingQueueTest ext
370       * take retrieves elements in FIFO order
371       */
372      public void testTake() throws InterruptedException {
373 <        LinkedBlockingQueue q = populatedQueue(SIZE);
373 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
374          for (int i = 0; i < SIZE; ++i) {
375 <            assertEquals(i, q.take());
375 >            mustEqual(i, q.take());
376          }
377      }
378  
# Line 389 | Line 380 | public class LinkedBlockingQueueTest ext
380       * Take removes existing elements until empty, then blocks interruptibly
381       */
382      public void testBlockingTake() throws InterruptedException {
383 <        final BlockingQueue q = populatedQueue(SIZE);
383 >        final BlockingQueue<Item> q = populatedQueue(SIZE);
384          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
385          Thread t = newStartedThread(new CheckedRunnable() {
386              public void realRun() throws InterruptedException {
387 <                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
387 >                for (int i = 0; i < SIZE; i++) mustEqual(i, q.take());
388  
389                  Thread.currentThread().interrupt();
390                  try {
# Line 420 | Line 411 | public class LinkedBlockingQueueTest ext
411       * poll succeeds unless empty
412       */
413      public void testPoll() {
414 <        LinkedBlockingQueue q = populatedQueue(SIZE);
414 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
415          for (int i = 0; i < SIZE; ++i) {
416 <            assertEquals(i, q.poll());
416 >            mustEqual(i, q.poll());
417          }
418          assertNull(q.poll());
419      }
# Line 431 | Line 422 | public class LinkedBlockingQueueTest ext
422       * timed poll with zero timeout succeeds when non-empty, else times out
423       */
424      public void testTimedPoll0() throws InterruptedException {
425 <        LinkedBlockingQueue q = populatedQueue(SIZE);
425 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
426          for (int i = 0; i < SIZE; ++i) {
427 <            assertEquals(i, q.poll(0, MILLISECONDS));
427 >            mustEqual(i, q.poll(0, MILLISECONDS));
428          }
429          assertNull(q.poll(0, MILLISECONDS));
430      }
# Line 442 | Line 433 | public class LinkedBlockingQueueTest ext
433       * timed poll with nonzero timeout succeeds when non-empty, else times out
434       */
435      public void testTimedPoll() throws InterruptedException {
436 <        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
436 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
437          for (int i = 0; i < SIZE; ++i) {
438              long startTime = System.nanoTime();
439 <            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
439 >            mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
440              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
441          }
442          long startTime = System.nanoTime();
# Line 459 | Line 450 | public class LinkedBlockingQueueTest ext
450       * returning timeout status
451       */
452      public void testInterruptedTimedPoll() throws InterruptedException {
453 <        final BlockingQueue<Integer> q = populatedQueue(SIZE);
453 >        final BlockingQueue<Item> q = populatedQueue(SIZE);
454          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
455          Thread t = newStartedThread(new CheckedRunnable() {
456              public void realRun() throws InterruptedException {
457                  for (int i = 0; i < SIZE; i++)
458 <                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
458 >                    mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
459  
460                  Thread.currentThread().interrupt();
461                  try {
# Line 492 | Line 483 | public class LinkedBlockingQueueTest ext
483       * peek returns next element, or null if empty
484       */
485      public void testPeek() {
486 <        LinkedBlockingQueue q = populatedQueue(SIZE);
486 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
487          for (int i = 0; i < SIZE; ++i) {
488 <            assertEquals(i, q.peek());
489 <            assertEquals(i, q.poll());
488 >            mustEqual(i, q.peek());
489 >            mustEqual(i, q.poll());
490              assertTrue(q.peek() == null ||
491                         !q.peek().equals(i));
492          }
# Line 506 | Line 497 | public class LinkedBlockingQueueTest ext
497       * element returns next element, or throws NSEE if empty
498       */
499      public void testElement() {
500 <        LinkedBlockingQueue q = populatedQueue(SIZE);
500 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
501          for (int i = 0; i < SIZE; ++i) {
502 <            assertEquals(i, q.element());
503 <            assertEquals(i, q.poll());
502 >            mustEqual(i, q.element());
503 >            mustEqual(i, q.poll());
504          }
505          try {
506              q.element();
# Line 521 | Line 512 | public class LinkedBlockingQueueTest ext
512       * remove removes next element, or throws NSEE if empty
513       */
514      public void testRemove() {
515 <        LinkedBlockingQueue q = populatedQueue(SIZE);
515 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
516          for (int i = 0; i < SIZE; ++i) {
517 <            assertEquals(i, q.remove());
517 >            mustEqual(i, q.remove());
518          }
519          try {
520              q.remove();
# Line 535 | Line 526 | public class LinkedBlockingQueueTest ext
526       * An add following remove(x) succeeds
527       */
528      public void testRemoveElementAndAdd() throws InterruptedException {
529 <        LinkedBlockingQueue q = new LinkedBlockingQueue();
530 <        assertTrue(q.add(new Integer(1)));
531 <        assertTrue(q.add(new Integer(2)));
532 <        assertTrue(q.remove(new Integer(1)));
533 <        assertTrue(q.remove(new Integer(2)));
534 <        assertTrue(q.add(new Integer(3)));
529 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>();
530 >        assertTrue(q.add(one));
531 >        assertTrue(q.add(two));
532 >        mustRemove(q, one);
533 >        mustRemove(q, two);
534 >        mustAdd(q, three);
535          assertNotNull(q.take());
536      }
537  
# Line 548 | Line 539 | public class LinkedBlockingQueueTest ext
539       * contains(x) reports true when elements added but not yet removed
540       */
541      public void testContains() {
542 <        LinkedBlockingQueue q = populatedQueue(SIZE);
542 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
543          for (int i = 0; i < SIZE; ++i) {
544 <            assertTrue(q.contains(new Integer(i)));
544 >            mustContain(q, i);
545              q.poll();
546 <            assertFalse(q.contains(new Integer(i)));
546 >            mustNotContain(q, i);
547          }
548      }
549  
# Line 560 | Line 551 | public class LinkedBlockingQueueTest ext
551       * clear removes all elements
552       */
553      public void testClear() {
554 <        LinkedBlockingQueue q = populatedQueue(SIZE);
554 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
555          q.clear();
556          assertTrue(q.isEmpty());
557 <        assertEquals(0, q.size());
558 <        assertEquals(SIZE, q.remainingCapacity());
557 >        mustEqual(0, q.size());
558 >        mustEqual(SIZE, q.remainingCapacity());
559          q.add(one);
560          assertFalse(q.isEmpty());
561          assertTrue(q.contains(one));
# Line 576 | Line 567 | public class LinkedBlockingQueueTest ext
567       * containsAll(c) is true when c contains a subset of elements
568       */
569      public void testContainsAll() {
570 <        LinkedBlockingQueue q = populatedQueue(SIZE);
571 <        LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
570 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
571 >        LinkedBlockingQueue<Item> p = new LinkedBlockingQueue<Item>(SIZE);
572          for (int i = 0; i < SIZE; ++i) {
573              assertTrue(q.containsAll(p));
574              assertFalse(p.containsAll(q));
575 <            p.add(new Integer(i));
575 >            mustAdd(p, i);
576          }
577          assertTrue(p.containsAll(q));
578      }
# Line 590 | Line 581 | public class LinkedBlockingQueueTest ext
581       * retainAll(c) retains only those elements of c and reports true if changed
582       */
583      public void testRetainAll() {
584 <        LinkedBlockingQueue q = populatedQueue(SIZE);
585 <        LinkedBlockingQueue p = populatedQueue(SIZE);
584 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
585 >        LinkedBlockingQueue<Item> p = populatedQueue(SIZE);
586          for (int i = 0; i < SIZE; ++i) {
587              boolean changed = q.retainAll(p);
588              if (i == 0)
# Line 600 | Line 591 | public class LinkedBlockingQueueTest ext
591                  assertTrue(changed);
592  
593              assertTrue(q.containsAll(p));
594 <            assertEquals(SIZE - i, q.size());
594 >            mustEqual(SIZE - i, q.size());
595              p.remove();
596          }
597      }
# Line 610 | Line 601 | public class LinkedBlockingQueueTest ext
601       */
602      public void testRemoveAll() {
603          for (int i = 1; i < SIZE; ++i) {
604 <            LinkedBlockingQueue q = populatedQueue(SIZE);
605 <            LinkedBlockingQueue p = populatedQueue(i);
604 >            LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
605 >            LinkedBlockingQueue<Item> p = populatedQueue(i);
606              assertTrue(q.removeAll(p));
607 <            assertEquals(SIZE - i, q.size());
607 >            mustEqual(SIZE - i, q.size());
608              for (int j = 0; j < i; ++j) {
609 <                Integer x = (Integer)(p.remove());
619 <                assertFalse(q.contains(x));
609 >                mustNotContain(q, p.remove());
610              }
611          }
612      }
# Line 625 | Line 615 | public class LinkedBlockingQueueTest ext
615       * toArray contains all elements in FIFO order
616       */
617      public void testToArray() {
618 <        LinkedBlockingQueue q = populatedQueue(SIZE);
618 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
619          Object[] a = q.toArray();
620          assertSame(Object[].class, a.getClass());
621          for (Object o : a)
# Line 637 | Line 627 | public class LinkedBlockingQueueTest ext
627       * toArray(a) contains all elements in FIFO order
628       */
629      public void testToArray2() throws InterruptedException {
630 <        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
631 <        Integer[] ints = new Integer[SIZE];
632 <        Integer[] array = q.toArray(ints);
633 <        assertSame(ints, array);
634 <        for (Integer o : ints)
630 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
631 >        Item[] items = new Item[SIZE];
632 >        Item[] array = q.toArray(items);
633 >        assertSame(items, array);
634 >        for (Item o : items)
635              assertSame(o, q.poll());
636          assertTrue(q.isEmpty());
637      }
# Line 650 | Line 640 | public class LinkedBlockingQueueTest ext
640       * toArray(incompatible array type) throws ArrayStoreException
641       */
642      public void testToArray1_BadArg() {
643 <        LinkedBlockingQueue q = populatedQueue(SIZE);
643 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
644          try {
645              q.toArray(new String[10]);
646              shouldThrow();
# Line 661 | Line 651 | public class LinkedBlockingQueueTest ext
651       * iterator iterates through all elements
652       */
653      public void testIterator() throws InterruptedException {
654 <        LinkedBlockingQueue q = populatedQueue(SIZE);
655 <        Iterator it = q.iterator();
654 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
655 >        Iterator<? extends Item> it = q.iterator();
656          int i;
657          for (i = 0; it.hasNext(); i++)
658 <            assertTrue(q.contains(it.next()));
659 <        assertEquals(i, SIZE);
658 >            mustContain(q, it.next());
659 >        mustEqual(i, SIZE);
660          assertIteratorExhausted(it);
661  
662          it = q.iterator();
663          for (i = 0; it.hasNext(); i++)
664 <            assertEquals(it.next(), q.take());
665 <        assertEquals(i, SIZE);
664 >            mustEqual(it.next(), q.take());
665 >        mustEqual(i, SIZE);
666          assertIteratorExhausted(it);
667      }
668  
# Line 680 | Line 670 | public class LinkedBlockingQueueTest ext
670       * iterator of empty collection has no elements
671       */
672      public void testEmptyIterator() {
673 <        assertIteratorExhausted(new LinkedBlockingQueue().iterator());
673 >        assertIteratorExhausted(new LinkedBlockingQueue<Item>().iterator());
674      }
675  
676      /**
677       * iterator.remove removes current element
678       */
679      public void testIteratorRemove() {
680 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
680 >        final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(3);
681          q.add(two);
682          q.add(one);
683          q.add(three);
684  
685 <        Iterator it = q.iterator();
685 >        Iterator<? extends Item> it = q.iterator();
686          it.next();
687          it.remove();
688  
# Line 706 | Line 696 | public class LinkedBlockingQueueTest ext
696       * iterator ordering is FIFO
697       */
698      public void testIteratorOrdering() {
699 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
699 >        final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(3);
700          q.add(one);
701          q.add(two);
702          q.add(three);
703 <        assertEquals(0, q.remainingCapacity());
703 >        mustEqual(0, q.remainingCapacity());
704          int k = 0;
705 <        for (Iterator it = q.iterator(); it.hasNext();) {
706 <            assertEquals(++k, it.next());
705 >        for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) {
706 >            mustEqual(++k, it.next());
707          }
708 <        assertEquals(3, k);
708 >        mustEqual(3, k);
709      }
710  
711      /**
712       * Modifications do not cause iterators to fail
713       */
714      public void testWeaklyConsistentIteration() {
715 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
715 >        final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(3);
716          q.add(one);
717          q.add(two);
718          q.add(three);
719 <        for (Iterator it = q.iterator(); it.hasNext();) {
719 >        for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) {
720              q.remove();
721              it.next();
722          }
723 <        assertEquals(0, q.size());
723 >        mustEqual(0, q.size());
724      }
725  
726      /**
727       * toString contains toStrings of elements
728       */
729      public void testToString() {
730 <        LinkedBlockingQueue q = populatedQueue(SIZE);
730 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
731          String s = q.toString();
732          for (int i = 0; i < SIZE; ++i) {
733              assertTrue(s.contains(String.valueOf(i)));
# Line 748 | Line 738 | public class LinkedBlockingQueueTest ext
738       * offer transfers elements across Executor tasks
739       */
740      public void testOfferInExecutor() {
741 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
741 >        final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2);
742          q.add(one);
743          q.add(two);
744          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
# Line 759 | Line 749 | public class LinkedBlockingQueueTest ext
749                      assertFalse(q.offer(three));
750                      threadsStarted.await();
751                      assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
752 <                    assertEquals(0, q.remainingCapacity());
752 >                    mustEqual(0, q.remainingCapacity());
753                  }});
754  
755              executor.execute(new CheckedRunnable() {
# Line 774 | Line 764 | public class LinkedBlockingQueueTest ext
764       * timed poll retrieves elements across Executor threads
765       */
766      public void testPollInExecutor() {
767 <        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
767 >        final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2);
768          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
769          final ExecutorService executor = Executors.newFixedThreadPool(2);
770          try (PoolCleaner cleaner = cleaner(executor)) {
# Line 798 | Line 788 | public class LinkedBlockingQueueTest ext
788       * A deserialized/reserialized queue has same elements in same order
789       */
790      public void testSerialization() throws Exception {
791 <        Queue x = populatedQueue(SIZE);
792 <        Queue y = serialClone(x);
791 >        Queue<Item> x = populatedQueue(SIZE);
792 >        Queue<Item> y = serialClone(x);
793  
794          assertNotSame(x, y);
795 <        assertEquals(x.size(), y.size());
796 <        assertEquals(x.toString(), y.toString());
795 >        mustEqual(x.size(), y.size());
796 >        mustEqual(x.toString(), y.toString());
797          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
798          while (!x.isEmpty()) {
799              assertFalse(y.isEmpty());
800 <            assertEquals(x.remove(), y.remove());
800 >            mustEqual(x.remove(), y.remove());
801          }
802          assertTrue(y.isEmpty());
803      }
# Line 816 | Line 806 | public class LinkedBlockingQueueTest ext
806       * drainTo(c) empties queue into another collection c
807       */
808      public void testDrainTo() {
809 <        LinkedBlockingQueue q = populatedQueue(SIZE);
810 <        ArrayList l = new ArrayList();
809 >        LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
810 >        ArrayList<Item> l = new ArrayList<Item>();
811          q.drainTo(l);
812 <        assertEquals(0, q.size());
813 <        assertEquals(SIZE, l.size());
812 >        mustEqual(0, q.size());
813 >        mustEqual(SIZE, l.size());
814          for (int i = 0; i < SIZE; ++i)
815 <            assertEquals(l.get(i), new Integer(i));
815 >            mustEqual(l.get(i), i);
816          q.add(zero);
817          q.add(one);
818          assertFalse(q.isEmpty());
819 <        assertTrue(q.contains(zero));
820 <        assertTrue(q.contains(one));
819 >        mustContain(q, zero);
820 >        mustContain(q, one);
821          l.clear();
822          q.drainTo(l);
823 <        assertEquals(0, q.size());
824 <        assertEquals(2, l.size());
823 >        mustEqual(0, q.size());
824 >        mustEqual(2, l.size());
825          for (int i = 0; i < 2; ++i)
826 <            assertEquals(l.get(i), new Integer(i));
826 >            mustEqual(l.get(i), i);
827      }
828  
829      /**
830       * drainTo empties full queue, unblocking a waiting put.
831       */
832      public void testDrainToWithActivePut() throws InterruptedException {
833 <        final LinkedBlockingQueue q = populatedQueue(SIZE);
833 >        final LinkedBlockingQueue<Item> q = populatedQueue(SIZE);
834          Thread t = new Thread(new CheckedRunnable() {
835              public void realRun() throws InterruptedException {
836 <                q.put(new Integer(SIZE + 1));
836 >                q.put(new Item(SIZE + 1));
837              }});
838  
839          t.start();
840 <        ArrayList l = new ArrayList();
840 >        ArrayList<Item> l = new ArrayList<Item>();
841          q.drainTo(l);
842          assertTrue(l.size() >= SIZE);
843          for (int i = 0; i < SIZE; ++i)
844 <            assertEquals(l.get(i), new Integer(i));
844 >            mustEqual(l.get(i), i);
845          t.join();
846          assertTrue(q.size() + l.size() >= SIZE);
847      }
# Line 860 | Line 850 | public class LinkedBlockingQueueTest ext
850       * drainTo(c, n) empties first min(n, size) elements of queue into c
851       */
852      public void testDrainToN() {
853 <        LinkedBlockingQueue q = new LinkedBlockingQueue();
853 >        LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>();
854          for (int i = 0; i < SIZE + 2; ++i) {
855              for (int j = 0; j < SIZE; j++)
856 <                assertTrue(q.offer(new Integer(j)));
857 <            ArrayList l = new ArrayList();
856 >                mustOffer(q, j);
857 >            ArrayList<Item> l = new ArrayList<Item>();
858              q.drainTo(l, i);
859              int k = (i < SIZE) ? i : SIZE;
860 <            assertEquals(k, l.size());
861 <            assertEquals(SIZE - k, q.size());
860 >            mustEqual(k, l.size());
861 >            mustEqual(SIZE - k, q.size());
862              for (int j = 0; j < k; ++j)
863 <                assertEquals(l.get(j), new Integer(j));
863 >                mustEqual(l.get(j), j);
864              do {} while (q.poll() != null);
865          }
866      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines