[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.74, Sun Oct 30 21:07:27 2016 UTC revision 1.75, Sat Nov 5 23:38:21 2016 UTC
# Line 27  Line 27 
27    
28      public static class Fair extends BlockingQueueTest {      public static class Fair extends BlockingQueueTest {
29          protected BlockingQueue emptyCollection() {          protected BlockingQueue emptyCollection() {
30              return new ArrayBlockingQueue(SIZE, true);              return populatedQueue(0, SIZE, 2 * SIZE, true);
31          }          }
32      }      }
33    
34      public static class NonFair extends BlockingQueueTest {      public static class NonFair extends BlockingQueueTest {
35          protected BlockingQueue emptyCollection() {          protected BlockingQueue emptyCollection() {
36              return new ArrayBlockingQueue(SIZE, false);              return populatedQueue(0, SIZE, 2 * SIZE, false);
37          }          }
38      }      }
39    
# Line 44  Line 44 
44      public static Test suite() {      public static Test suite() {
45          class Implementation implements CollectionImplementation {          class Implementation implements CollectionImplementation {
46              public Class<?> klazz() { return ArrayBlockingQueue.class; }              public Class<?> klazz() { return ArrayBlockingQueue.class; }
47              public Collection emptyCollection() { return new ArrayBlockingQueue(SIZE, false); }              public Collection emptyCollection() {
48                    boolean fair = ThreadLocalRandom.current().nextBoolean();
49                    return populatedQueue(0, SIZE, 2 * SIZE, fair);
50                }
51              public Object makeElement(int i) { return i; }              public Object makeElement(int i) { return i; }
52              public boolean isConcurrent() { return true; }              public boolean isConcurrent() { return true; }
53              public boolean permitsNulls() { return false; }              public boolean permitsNulls() { return false; }
# Line 59  Line 62 
62       * Returns a new queue of given size containing consecutive       * Returns a new queue of given size containing consecutive
63       * Integers 0 ... n - 1.       * Integers 0 ... n - 1.
64       */       */
65      private ArrayBlockingQueue<Integer> populatedQueue(int n) {      static ArrayBlockingQueue<Integer> populatedQueue(int n) {
66          ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);          return populatedQueue(n, n, n, false);
67        }
68    
69        /**
70         * Returns a new queue of given size containing consecutive
71         * Integers 0 ... n - 1, with given capacity range and fairness.
72         */
73        static ArrayBlockingQueue<Integer> populatedQueue(
74            int size, int minCapacity, int maxCapacity, boolean fair) {
75            ThreadLocalRandom rnd = ThreadLocalRandom.current();
76            int capacity = rnd.nextInt(minCapacity, maxCapacity + 1);
77            ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
78          assertTrue(q.isEmpty());          assertTrue(q.isEmpty());
79          for (int i = 0; i < n; i++)          // shuffle circular array elements so they wrap
80              assertTrue(q.offer(new Integer(i)));          {
81          assertFalse(q.isEmpty());              int n = rnd.nextInt(capacity);
82          assertEquals(0, q.remainingCapacity());              for (int i = 0; i < n; i++) q.add(42);
83          assertEquals(n, q.size());              for (int i = 0; i < n; i++) q.remove();
84            }
85            for (int i = 0; i < size; i++)
86                assertTrue(q.offer((Integer) i));
87            assertEquals(size == 0, q.isEmpty());
88            assertEquals(capacity - size, q.remainingCapacity());
89            assertEquals(size, q.size());
90            if (size > 0)
91          assertEquals((Integer) 0, q.peek());          assertEquals((Integer) 0, q.peek());
92          return q;          return q;
93      }      }

Legend:
Removed from v.1.74  
changed lines
  Added in v.1.75

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8