[cvs] / jsr166 / src / test / tck / ArrayBlockingQueueTest.java Repository:
ViewVC logotype

Diff of /jsr166/src/test/tck/ArrayBlockingQueueTest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.76, Sun Nov 6 02:40:38 2016 UTC revision 1.77, Sun Nov 6 03:11:15 2016 UTC
# Line 11  Line 11 
11  import java.util.ArrayList;  import java.util.ArrayList;
12  import java.util.Arrays;  import java.util.Arrays;
13  import java.util.Collection;  import java.util.Collection;
14    import java.util.Collections;
15  import java.util.Iterator;  import java.util.Iterator;
16  import java.util.NoSuchElementException;  import java.util.NoSuchElementException;
17  import java.util.Queue;  import java.util.Queue;
# Line 104  Line 105 
105      /**      /**
106       * Constructor throws IAE if capacity argument nonpositive       * Constructor throws IAE if capacity argument nonpositive
107       */       */
108      public void testConstructor2() {      public void testConstructor_nonPositiveCapacity() {
109            for (int i : new int[] { 0, -1, Integer.MIN_VALUE }) {
110          try {          try {
111              new ArrayBlockingQueue(0);                  new ArrayBlockingQueue(i);
112              shouldThrow();              shouldThrow();
113          } catch (IllegalArgumentException success) {}          } catch (IllegalArgumentException success) {}
114                for (boolean fair : new boolean[] { true, false }) {
115                    try {
116                        new ArrayBlockingQueue(i, fair);
117                        shouldThrow();
118                    } catch (IllegalArgumentException success) {}
119                }
120            }
121      }      }
122    
123      /**      /**
124       * Initializing from null Collection throws NPE       * Initializing from null Collection throws NPE
125       */       */
126      public void testConstructor3() {      public void testConstructor_nullCollection() {
127          try {          try {
128              new ArrayBlockingQueue(1, true, null);              new ArrayBlockingQueue(1, true, null);
129              shouldThrow();              shouldThrow();
# Line 149  Line 158 
158      /**      /**
159       * Initializing from too large collection throws IAE       * Initializing from too large collection throws IAE
160       */       */
161      public void testConstructor6() {      public void testConstructor_collectionTooLarge() {
162          Integer[] ints = new Integer[SIZE];          // just barely fits - succeeds
163          for (int i = 0; i < SIZE; ++i)          new ArrayBlockingQueue(SIZE, false,
164              ints[i] = i;                                 Collections.nCopies(SIZE, ""));
         Collection<Integer> elements = Arrays.asList(ints);  
165          try {          try {
166              new ArrayBlockingQueue(SIZE - 1, false, elements);              new ArrayBlockingQueue(SIZE - 1, false,
167                                       Collections.nCopies(SIZE, ""));
168              shouldThrow();              shouldThrow();
169          } catch (IllegalArgumentException success) {}          } catch (IllegalArgumentException success) {}
170      }      }
# Line 177  Line 186 
186       * Queue transitions from empty to full when elements added       * Queue transitions from empty to full when elements added
187       */       */
188      public void testEmptyFull() {      public void testEmptyFull() {
189          ArrayBlockingQueue q = new ArrayBlockingQueue(2);          BlockingQueue q = populatedQueue(0, 2, 2, false);
190          assertTrue(q.isEmpty());          assertTrue(q.isEmpty());
191          assertEquals(2, q.remainingCapacity());          assertEquals(2, q.remainingCapacity());
192          q.add(one);          q.add(one);
193          assertFalse(q.isEmpty());          assertFalse(q.isEmpty());
194          q.add(two);          assertTrue(q.offer(two));
195          assertFalse(q.isEmpty());          assertFalse(q.isEmpty());
196          assertEquals(0, q.remainingCapacity());          assertEquals(0, q.remainingCapacity());
197          assertFalse(q.offer(three));          assertFalse(q.offer(three));
# Line 192  Line 201 
201       * remainingCapacity decreases on add, increases on remove       * remainingCapacity decreases on add, increases on remove
202       */       */
203      public void testRemainingCapacity() {      public void testRemainingCapacity() {
204          BlockingQueue q = populatedQueue(SIZE);          int size = ThreadLocalRandom.current().nextInt(1, SIZE);
205          for (int i = 0; i < SIZE; ++i) {          BlockingQueue q = populatedQueue(size, size, 2 * size, false);
206              assertEquals(i, q.remainingCapacity());          int spare = q.remainingCapacity();
207              assertEquals(SIZE, q.size() + q.remainingCapacity());          int capacity = spare + size;
208            for (int i = 0; i < size; i++) {
209                assertEquals(spare + i, q.remainingCapacity());
210                assertEquals(capacity, q.size() + q.remainingCapacity());
211              assertEquals(i, q.remove());              assertEquals(i, q.remove());
212          }          }
213          for (int i = 0; i < SIZE; ++i) {          for (int i = 0; i < size; i++) {
214              assertEquals(SIZE - i, q.remainingCapacity());              assertEquals(capacity - i, q.remainingCapacity());
215              assertEquals(SIZE, q.size() + q.remainingCapacity());              assertEquals(capacity, q.size() + q.remainingCapacity());
216              assertTrue(q.add(i));              assertTrue(q.add(i));
217          }          }
218      }      }

Legend:
Removed from v.1.76  
changed lines
  Added in v.1.77

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8