6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.Arrays; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
|
import java.util.ArrayList; |
12 |
+ |
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.Iterator; |
15 |
|
import java.util.NoSuchElementException; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
22 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
26 |
|
|
108 |
|
ints[i] = i; |
109 |
|
Collection<Integer> elements = Arrays.asList(ints); |
110 |
|
try { |
111 |
< |
new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
111 |
> |
new ArrayBlockingQueue(SIZE, false, elements); |
112 |
|
shouldThrow(); |
113 |
|
} catch (NullPointerException success) {} |
114 |
|
} |
185 |
|
* add succeeds if not full; throws ISE if full |
186 |
|
*/ |
187 |
|
public void testAdd() { |
188 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
189 |
+ |
for (int i = 0; i < SIZE; ++i) { |
190 |
+ |
assertTrue(q.add(new Integer(i))); |
191 |
+ |
} |
192 |
+ |
assertEquals(0, q.remainingCapacity()); |
193 |
|
try { |
187 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
188 |
– |
for (int i = 0; i < SIZE; ++i) { |
189 |
– |
assertTrue(q.add(new Integer(i))); |
190 |
– |
} |
191 |
– |
assertEquals(0, q.remainingCapacity()); |
194 |
|
q.add(new Integer(SIZE)); |
195 |
|
shouldThrow(); |
196 |
|
} catch (IllegalStateException success) {} |
200 |
|
* addAll(this) throws IAE |
201 |
|
*/ |
202 |
|
public void testAddAllSelf() { |
203 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
204 |
|
try { |
202 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
205 |
|
q.addAll(q); |
206 |
|
shouldThrow(); |
207 |
|
} catch (IllegalArgumentException success) {} |
212 |
|
* possibly adding some elements |
213 |
|
*/ |
214 |
|
public void testAddAll3() { |
215 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
216 |
+ |
Integer[] ints = new Integer[SIZE]; |
217 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
218 |
+ |
ints[i] = new Integer(i); |
219 |
|
try { |
214 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
215 |
– |
Integer[] ints = new Integer[SIZE]; |
216 |
– |
for (int i = 0; i < SIZE-1; ++i) |
217 |
– |
ints[i] = new Integer(i); |
220 |
|
q.addAll(Arrays.asList(ints)); |
221 |
|
shouldThrow(); |
222 |
|
} catch (NullPointerException success) {} |
226 |
|
* addAll throws ISE if not enough room |
227 |
|
*/ |
228 |
|
public void testAddAll4() { |
229 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
230 |
+ |
Integer[] ints = new Integer[SIZE]; |
231 |
+ |
for (int i = 0; i < SIZE; ++i) |
232 |
+ |
ints[i] = new Integer(i); |
233 |
|
try { |
228 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
229 |
– |
Integer[] ints = new Integer[SIZE]; |
230 |
– |
for (int i = 0; i < SIZE; ++i) |
231 |
– |
ints[i] = new Integer(i); |
234 |
|
q.addAll(Arrays.asList(ints)); |
235 |
|
shouldThrow(); |
236 |
|
} catch (IllegalStateException success) {} |
257 |
|
public void testPut() throws InterruptedException { |
258 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
259 |
|
for (int i = 0; i < SIZE; ++i) { |
260 |
< |
Integer I = new Integer(i); |
261 |
< |
q.put(I); |
262 |
< |
assertTrue(q.contains(I)); |
260 |
> |
Integer x = new Integer(i); |
261 |
> |
q.put(x); |
262 |
> |
assertTrue(q.contains(x)); |
263 |
|
} |
264 |
|
assertEquals(0, q.remainingCapacity()); |
265 |
|
} |
587 |
|
assertTrue(q.removeAll(p)); |
588 |
|
assertEquals(SIZE-i, q.size()); |
589 |
|
for (int j = 0; j < i; ++j) { |
590 |
< |
Integer I = (Integer)(p.remove()); |
591 |
< |
assertFalse(q.contains(I)); |
590 |
> |
Integer x = (Integer)(p.remove()); |
591 |
> |
assertFalse(q.contains(x)); |
592 |
|
} |
593 |
|
} |
594 |
|
} |
698 |
|
public void testIterator() throws InterruptedException { |
699 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
700 |
|
Iterator it = q.iterator(); |
701 |
< |
while (it.hasNext()) { |
701 |
> |
int i; |
702 |
> |
for (i = 0; it.hasNext(); i++) |
703 |
> |
assertTrue(q.contains(it.next())); |
704 |
> |
assertEquals(i, SIZE); |
705 |
> |
assertIteratorExhausted(it); |
706 |
> |
|
707 |
> |
it = q.iterator(); |
708 |
> |
for (i = 0; it.hasNext(); i++) |
709 |
|
assertEquals(it.next(), q.take()); |
710 |
< |
} |
710 |
> |
assertEquals(i, SIZE); |
711 |
> |
assertIteratorExhausted(it); |
712 |
> |
} |
713 |
> |
|
714 |
> |
/** |
715 |
> |
* iterator of empty collection has no elements |
716 |
> |
*/ |
717 |
> |
public void testEmptyIterator() { |
718 |
> |
assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator()); |
719 |
|
} |
720 |
|
|
721 |
|
/** |
909 |
|
assertEquals(SIZE-k, q.size()); |
910 |
|
for (int j = 0; j < k; ++j) |
911 |
|
assertEquals(l.get(j), new Integer(j)); |
912 |
< |
while (q.poll() != null) ; |
912 |
> |
do {} while (q.poll() != null); |
913 |
|
} |
914 |
|
} |
915 |
|
|
916 |
+ |
/** |
917 |
+ |
* remove(null), contains(null) always return false |
918 |
+ |
*/ |
919 |
+ |
public void testNeverContainsNull() { |
920 |
+ |
Collection<?>[] qs = { |
921 |
+ |
new ArrayBlockingQueue<Object>(10), |
922 |
+ |
populatedQueue(2), |
923 |
+ |
}; |
924 |
+ |
|
925 |
+ |
for (Collection<?> q : qs) { |
926 |
+ |
assertFalse(q.contains(null)); |
927 |
+ |
assertFalse(q.remove(null)); |
928 |
+ |
} |
929 |
+ |
} |
930 |
|
} |