27 |
|
|
28 |
|
public static class Fair extends BlockingQueueTest { |
29 |
|
protected BlockingQueue emptyCollection() { |
30 |
< |
return new ArrayBlockingQueue(SIZE, true); |
30 |
> |
return populatedQueue(0, SIZE, 2 * SIZE, true); |
31 |
|
} |
32 |
|
} |
33 |
|
|
34 |
|
public static class NonFair extends BlockingQueueTest { |
35 |
|
protected BlockingQueue emptyCollection() { |
36 |
< |
return new ArrayBlockingQueue(SIZE, false); |
36 |
> |
return populatedQueue(0, SIZE, 2 * SIZE, false); |
37 |
|
} |
38 |
|
} |
39 |
|
|
44 |
|
public static Test suite() { |
45 |
|
class Implementation implements CollectionImplementation { |
46 |
|
public Class<?> klazz() { return ArrayBlockingQueue.class; } |
47 |
< |
public Collection emptyCollection() { return new ArrayBlockingQueue(SIZE, false); } |
47 |
> |
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; } |
52 |
|
public boolean isConcurrent() { return true; } |
53 |
|
public boolean permitsNulls() { return false; } |
62 |
|
* Returns a new queue of given size containing consecutive |
63 |
|
* Integers 0 ... n - 1. |
64 |
|
*/ |
65 |
< |
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
66 |
< |
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n); |
65 |
> |
static ArrayBlockingQueue<Integer> populatedQueue(int n) { |
66 |
> |
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()); |
79 |
< |
for (int i = 0; i < n; i++) |
80 |
< |
assertTrue(q.offer(new Integer(i))); |
81 |
< |
assertFalse(q.isEmpty()); |
82 |
< |
assertEquals(0, q.remainingCapacity()); |
83 |
< |
assertEquals(n, q.size()); |
84 |
< |
assertEquals((Integer) 0, q.peek()); |
79 |
> |
// shuffle circular array elements so they wrap |
80 |
> |
{ |
81 |
> |
int n = rnd.nextInt(capacity); |
82 |
> |
for (int i = 0; i < n; i++) q.add(42); |
83 |
> |
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()); |
92 |
|
return q; |
93 |
|
} |
94 |
|
|