1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.22 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
import junit.framework.*; |
8 |
jsr166 |
1.24 |
import java.util.Arrays; |
9 |
|
|
import java.util.ArrayDeque; |
10 |
|
|
import java.util.Collection; |
11 |
|
|
import java.util.Deque; |
12 |
|
|
import java.util.Iterator; |
13 |
|
|
import java.util.NoSuchElementException; |
14 |
|
|
import java.util.Queue; |
15 |
|
|
import java.util.Random; |
16 |
dl |
1.1 |
|
17 |
|
|
public class ArrayDequeTest extends JSR166TestCase { |
18 |
|
|
public static void main(String[] args) { |
19 |
jsr166 |
1.16 |
junit.textui.TestRunner.run(suite()); |
20 |
dl |
1.1 |
} |
21 |
|
|
|
22 |
|
|
public static Test suite() { |
23 |
jsr166 |
1.8 |
return new TestSuite(ArrayDequeTest.class); |
24 |
dl |
1.1 |
} |
25 |
|
|
|
26 |
|
|
/** |
27 |
jsr166 |
1.16 |
* Create a deque of given size containing consecutive |
28 |
dl |
1.1 |
* Integers 0 ... n. |
29 |
|
|
*/ |
30 |
jsr166 |
1.20 |
private ArrayDeque<Integer> populatedDeque(int n) { |
31 |
|
|
ArrayDeque<Integer> q = new ArrayDeque<Integer>(); |
32 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
33 |
jsr166 |
1.8 |
for (int i = 0; i < n; ++i) |
34 |
|
|
assertTrue(q.offerLast(new Integer(i))); |
35 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
36 |
jsr166 |
1.8 |
assertEquals(n, q.size()); |
37 |
dl |
1.1 |
return q; |
38 |
|
|
} |
39 |
jsr166 |
1.5 |
|
40 |
dl |
1.1 |
/** |
41 |
jsr166 |
1.16 |
* new deque is empty |
42 |
dl |
1.1 |
*/ |
43 |
|
|
public void testConstructor1() { |
44 |
|
|
assertEquals(0, new ArrayDeque().size()); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
/** |
48 |
|
|
* Initializing from null Collection throws NPE |
49 |
|
|
*/ |
50 |
|
|
public void testConstructor3() { |
51 |
|
|
try { |
52 |
|
|
ArrayDeque q = new ArrayDeque((Collection)null); |
53 |
|
|
shouldThrow(); |
54 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
55 |
dl |
1.1 |
} |
56 |
|
|
|
57 |
|
|
/** |
58 |
jsr166 |
1.16 |
* Initializing from Collection of null elements throws NPE |
59 |
|
|
*/ |
60 |
|
|
public void testConstructor4() { |
61 |
|
|
try { |
62 |
|
|
Integer[] ints = new Integer[SIZE]; |
63 |
|
|
ArrayDeque q = new ArrayDeque(Arrays.asList(ints)); |
64 |
|
|
shouldThrow(); |
65 |
|
|
} catch (NullPointerException success) {} |
66 |
|
|
} |
67 |
dl |
1.1 |
|
68 |
jsr166 |
1.16 |
/** |
69 |
|
|
* Initializing from Collection with some null elements throws NPE |
70 |
|
|
*/ |
71 |
|
|
public void testConstructor5() { |
72 |
|
|
try { |
73 |
|
|
Integer[] ints = new Integer[SIZE]; |
74 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
75 |
|
|
ints[i] = new Integer(i); |
76 |
|
|
ArrayDeque q = new ArrayDeque(Arrays.asList(ints)); |
77 |
|
|
shouldThrow(); |
78 |
|
|
} catch (NullPointerException success) {} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
/** |
82 |
|
|
* Deque contains all elements of collection used to initialize |
83 |
dl |
1.1 |
*/ |
84 |
|
|
public void testConstructor6() { |
85 |
jsr166 |
1.9 |
Integer[] ints = new Integer[SIZE]; |
86 |
|
|
for (int i = 0; i < SIZE; ++i) |
87 |
|
|
ints[i] = new Integer(i); |
88 |
|
|
ArrayDeque q = new ArrayDeque(Arrays.asList(ints)); |
89 |
|
|
for (int i = 0; i < SIZE; ++i) |
90 |
|
|
assertEquals(ints[i], q.pollFirst()); |
91 |
dl |
1.1 |
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* isEmpty is true before add, false after |
95 |
|
|
*/ |
96 |
|
|
public void testEmpty() { |
97 |
|
|
ArrayDeque q = new ArrayDeque(); |
98 |
|
|
assertTrue(q.isEmpty()); |
99 |
|
|
q.add(new Integer(1)); |
100 |
|
|
assertFalse(q.isEmpty()); |
101 |
|
|
q.add(new Integer(2)); |
102 |
|
|
q.removeFirst(); |
103 |
|
|
q.removeFirst(); |
104 |
|
|
assertTrue(q.isEmpty()); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
/** |
108 |
|
|
* size changes when elements added and removed |
109 |
|
|
*/ |
110 |
|
|
public void testSize() { |
111 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
112 |
|
|
for (int i = 0; i < SIZE; ++i) { |
113 |
|
|
assertEquals(SIZE-i, q.size()); |
114 |
|
|
q.removeFirst(); |
115 |
|
|
} |
116 |
|
|
for (int i = 0; i < SIZE; ++i) { |
117 |
|
|
assertEquals(i, q.size()); |
118 |
|
|
q.add(new Integer(i)); |
119 |
|
|
} |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* push(null) throws NPE |
124 |
|
|
*/ |
125 |
|
|
public void testPushNull() { |
126 |
jsr166 |
1.8 |
try { |
127 |
dl |
1.1 |
ArrayDeque q = new ArrayDeque(1); |
128 |
|
|
q.push(null); |
129 |
|
|
shouldThrow(); |
130 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
131 |
dl |
1.1 |
} |
132 |
|
|
|
133 |
|
|
/** |
134 |
jsr166 |
1.16 |
* peekFirst() returns element inserted with push |
135 |
dl |
1.1 |
*/ |
136 |
|
|
public void testPush() { |
137 |
|
|
ArrayDeque q = populatedDeque(3); |
138 |
|
|
q.pollLast(); |
139 |
jsr166 |
1.8 |
q.push(four); |
140 |
jsr166 |
1.12 |
assertSame(four, q.peekFirst()); |
141 |
jsr166 |
1.5 |
} |
142 |
dl |
1.1 |
|
143 |
|
|
/** |
144 |
jsr166 |
1.16 |
* pop() removes next element, or throws NSEE if empty |
145 |
dl |
1.1 |
*/ |
146 |
|
|
public void testPop() { |
147 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
148 |
|
|
for (int i = 0; i < SIZE; ++i) { |
149 |
jsr166 |
1.12 |
assertEquals(i, q.pop()); |
150 |
dl |
1.1 |
} |
151 |
|
|
try { |
152 |
|
|
q.pop(); |
153 |
|
|
shouldThrow(); |
154 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
155 |
dl |
1.1 |
} |
156 |
|
|
|
157 |
|
|
/** |
158 |
|
|
* offer(null) throws NPE |
159 |
|
|
*/ |
160 |
jsr166 |
1.16 |
public void testOfferNull() { |
161 |
|
|
try { |
162 |
|
|
ArrayDeque q = new ArrayDeque(); |
163 |
|
|
q.offer(null); |
164 |
|
|
shouldThrow(); |
165 |
|
|
} catch (NullPointerException success) {} |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
/** |
169 |
|
|
* offerFirst(null) throws NPE |
170 |
|
|
*/ |
171 |
dl |
1.1 |
public void testOfferFirstNull() { |
172 |
jsr166 |
1.8 |
try { |
173 |
dl |
1.1 |
ArrayDeque q = new ArrayDeque(); |
174 |
|
|
q.offerFirst(null); |
175 |
|
|
shouldThrow(); |
176 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
177 |
dl |
1.1 |
} |
178 |
|
|
|
179 |
|
|
/** |
180 |
jsr166 |
1.16 |
* offerLast(null) throws NPE |
181 |
|
|
*/ |
182 |
|
|
public void testOfferLastNull() { |
183 |
|
|
try { |
184 |
|
|
ArrayDeque q = new ArrayDeque(); |
185 |
|
|
q.offerLast(null); |
186 |
|
|
shouldThrow(); |
187 |
|
|
} catch (NullPointerException success) {} |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
/** |
191 |
|
|
* offer(x) succeeds |
192 |
|
|
*/ |
193 |
|
|
public void testOffer() { |
194 |
|
|
ArrayDeque q = new ArrayDeque(); |
195 |
|
|
assertTrue(q.offer(zero)); |
196 |
|
|
assertTrue(q.offer(one)); |
197 |
|
|
assertSame(zero, q.peekFirst()); |
198 |
|
|
assertSame(one, q.peekLast()); |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
/** |
202 |
|
|
* offerFirst(x) succeeds |
203 |
dl |
1.1 |
*/ |
204 |
|
|
public void testOfferFirst() { |
205 |
|
|
ArrayDeque q = new ArrayDeque(); |
206 |
jsr166 |
1.16 |
assertTrue(q.offerFirst(zero)); |
207 |
|
|
assertTrue(q.offerFirst(one)); |
208 |
|
|
assertSame(one, q.peekFirst()); |
209 |
|
|
assertSame(zero, q.peekLast()); |
210 |
dl |
1.1 |
} |
211 |
|
|
|
212 |
|
|
/** |
213 |
jsr166 |
1.16 |
* offerLast(x) succeeds |
214 |
dl |
1.1 |
*/ |
215 |
|
|
public void testOfferLast() { |
216 |
|
|
ArrayDeque q = new ArrayDeque(); |
217 |
jsr166 |
1.16 |
assertTrue(q.offerLast(zero)); |
218 |
|
|
assertTrue(q.offerLast(one)); |
219 |
|
|
assertSame(zero, q.peekFirst()); |
220 |
|
|
assertSame(one, q.peekLast()); |
221 |
dl |
1.1 |
} |
222 |
|
|
|
223 |
|
|
/** |
224 |
jsr166 |
1.16 |
* add(null) throws NPE |
225 |
|
|
*/ |
226 |
|
|
public void testAddNull() { |
227 |
|
|
try { |
228 |
|
|
ArrayDeque q = new ArrayDeque(); |
229 |
|
|
q.add(null); |
230 |
|
|
shouldThrow(); |
231 |
|
|
} catch (NullPointerException success) {} |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
/** |
235 |
|
|
* addFirst(null) throws NPE |
236 |
|
|
*/ |
237 |
|
|
public void testAddFirstNull() { |
238 |
|
|
try { |
239 |
|
|
ArrayDeque q = new ArrayDeque(); |
240 |
|
|
q.addFirst(null); |
241 |
|
|
shouldThrow(); |
242 |
|
|
} catch (NullPointerException success) {} |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
/** |
246 |
|
|
* addLast(null) throws NPE |
247 |
|
|
*/ |
248 |
|
|
public void testAddLastNull() { |
249 |
|
|
try { |
250 |
|
|
ArrayDeque q = new ArrayDeque(); |
251 |
|
|
q.addLast(null); |
252 |
|
|
shouldThrow(); |
253 |
|
|
} catch (NullPointerException success) {} |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
/** |
257 |
|
|
* add(x) succeeds |
258 |
dl |
1.1 |
*/ |
259 |
|
|
public void testAdd() { |
260 |
|
|
ArrayDeque q = new ArrayDeque(); |
261 |
jsr166 |
1.16 |
assertTrue(q.add(zero)); |
262 |
|
|
assertTrue(q.add(one)); |
263 |
|
|
assertSame(zero, q.peekFirst()); |
264 |
|
|
assertSame(one, q.peekLast()); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
/** |
268 |
|
|
* addFirst(x) succeeds |
269 |
|
|
*/ |
270 |
|
|
public void testAddFirst() { |
271 |
|
|
ArrayDeque q = new ArrayDeque(); |
272 |
|
|
q.addFirst(zero); |
273 |
|
|
q.addFirst(one); |
274 |
|
|
assertSame(one, q.peekFirst()); |
275 |
|
|
assertSame(zero, q.peekLast()); |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
/** |
279 |
|
|
* addLast(x) succeeds |
280 |
|
|
*/ |
281 |
|
|
public void testAddLast() { |
282 |
|
|
ArrayDeque q = new ArrayDeque(); |
283 |
|
|
q.addLast(zero); |
284 |
|
|
q.addLast(one); |
285 |
|
|
assertSame(zero, q.peekFirst()); |
286 |
|
|
assertSame(one, q.peekLast()); |
287 |
dl |
1.1 |
} |
288 |
|
|
|
289 |
|
|
/** |
290 |
|
|
* addAll(null) throws NPE |
291 |
|
|
*/ |
292 |
|
|
public void testAddAll1() { |
293 |
|
|
try { |
294 |
|
|
ArrayDeque q = new ArrayDeque(); |
295 |
|
|
q.addAll(null); |
296 |
|
|
shouldThrow(); |
297 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
298 |
dl |
1.1 |
} |
299 |
|
|
|
300 |
|
|
/** |
301 |
jsr166 |
1.16 |
* addAll of a collection with null elements throws NPE |
302 |
|
|
*/ |
303 |
|
|
public void testAddAll2() { |
304 |
|
|
try { |
305 |
|
|
ArrayDeque q = new ArrayDeque(); |
306 |
|
|
Integer[] ints = new Integer[SIZE]; |
307 |
|
|
q.addAll(Arrays.asList(ints)); |
308 |
|
|
shouldThrow(); |
309 |
|
|
} catch (NullPointerException success) {} |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
/** |
313 |
|
|
* addAll of a collection with any null elements throws NPE after |
314 |
|
|
* possibly adding some elements |
315 |
|
|
*/ |
316 |
|
|
public void testAddAll3() { |
317 |
|
|
try { |
318 |
|
|
ArrayDeque q = new ArrayDeque(); |
319 |
|
|
Integer[] ints = new Integer[SIZE]; |
320 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
321 |
|
|
ints[i] = new Integer(i); |
322 |
|
|
q.addAll(Arrays.asList(ints)); |
323 |
|
|
shouldThrow(); |
324 |
|
|
} catch (NullPointerException success) {} |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
/** |
328 |
|
|
* Deque contains all elements, in traversal order, of successful addAll |
329 |
dl |
1.1 |
*/ |
330 |
|
|
public void testAddAll5() { |
331 |
jsr166 |
1.10 |
Integer[] empty = new Integer[0]; |
332 |
|
|
Integer[] ints = new Integer[SIZE]; |
333 |
|
|
for (int i = 0; i < SIZE; ++i) |
334 |
|
|
ints[i] = new Integer(i); |
335 |
|
|
ArrayDeque q = new ArrayDeque(); |
336 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
337 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
338 |
|
|
for (int i = 0; i < SIZE; ++i) |
339 |
|
|
assertEquals(ints[i], q.pollFirst()); |
340 |
dl |
1.1 |
} |
341 |
|
|
|
342 |
|
|
/** |
343 |
jsr166 |
1.16 |
* pollFirst() succeeds unless empty |
344 |
dl |
1.1 |
*/ |
345 |
|
|
public void testPollFirst() { |
346 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
347 |
|
|
for (int i = 0; i < SIZE; ++i) { |
348 |
jsr166 |
1.12 |
assertEquals(i, q.pollFirst()); |
349 |
dl |
1.1 |
} |
350 |
jsr166 |
1.8 |
assertNull(q.pollFirst()); |
351 |
dl |
1.1 |
} |
352 |
|
|
|
353 |
|
|
/** |
354 |
jsr166 |
1.16 |
* pollLast() succeeds unless empty |
355 |
dl |
1.1 |
*/ |
356 |
|
|
public void testPollLast() { |
357 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
358 |
|
|
for (int i = SIZE-1; i >= 0; --i) { |
359 |
jsr166 |
1.12 |
assertEquals(i, q.pollLast()); |
360 |
dl |
1.1 |
} |
361 |
jsr166 |
1.8 |
assertNull(q.pollLast()); |
362 |
dl |
1.1 |
} |
363 |
|
|
|
364 |
|
|
/** |
365 |
jsr166 |
1.16 |
* poll() succeeds unless empty |
366 |
dl |
1.1 |
*/ |
367 |
|
|
public void testPoll() { |
368 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
369 |
|
|
for (int i = 0; i < SIZE; ++i) { |
370 |
jsr166 |
1.12 |
assertEquals(i, q.poll()); |
371 |
dl |
1.1 |
} |
372 |
jsr166 |
1.8 |
assertNull(q.poll()); |
373 |
dl |
1.1 |
} |
374 |
|
|
|
375 |
|
|
/** |
376 |
jsr166 |
1.16 |
* remove() removes next element, or throws NSEE if empty |
377 |
dl |
1.1 |
*/ |
378 |
|
|
public void testRemove() { |
379 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
380 |
|
|
for (int i = 0; i < SIZE; ++i) { |
381 |
jsr166 |
1.12 |
assertEquals(i, q.remove()); |
382 |
dl |
1.1 |
} |
383 |
|
|
try { |
384 |
|
|
q.remove(); |
385 |
|
|
shouldThrow(); |
386 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
387 |
dl |
1.1 |
} |
388 |
|
|
|
389 |
|
|
/** |
390 |
jsr166 |
1.16 |
* remove(x) removes x and returns true if present |
391 |
|
|
*/ |
392 |
|
|
public void testRemoveElement() { |
393 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
394 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
395 |
jsr166 |
1.21 |
assertTrue(q.contains(i)); |
396 |
|
|
assertTrue(q.remove(i)); |
397 |
|
|
assertFalse(q.contains(i)); |
398 |
|
|
assertTrue(q.contains(i-1)); |
399 |
jsr166 |
1.16 |
} |
400 |
|
|
for (int i = 0; i < SIZE; i+=2) { |
401 |
jsr166 |
1.21 |
assertTrue(q.contains(i)); |
402 |
|
|
assertTrue(q.remove(i)); |
403 |
|
|
assertFalse(q.contains(i)); |
404 |
|
|
assertFalse(q.remove(i+1)); |
405 |
|
|
assertFalse(q.contains(i+1)); |
406 |
jsr166 |
1.16 |
} |
407 |
|
|
assertTrue(q.isEmpty()); |
408 |
|
|
} |
409 |
|
|
|
410 |
|
|
/** |
411 |
|
|
* peekFirst() returns next element, or null if empty |
412 |
dl |
1.1 |
*/ |
413 |
|
|
public void testPeekFirst() { |
414 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
415 |
|
|
for (int i = 0; i < SIZE; ++i) { |
416 |
jsr166 |
1.12 |
assertEquals(i, q.peekFirst()); |
417 |
|
|
assertEquals(i, q.pollFirst()); |
418 |
dl |
1.1 |
assertTrue(q.peekFirst() == null || |
419 |
jsr166 |
1.12 |
!q.peekFirst().equals(i)); |
420 |
dl |
1.1 |
} |
421 |
jsr166 |
1.8 |
assertNull(q.peekFirst()); |
422 |
dl |
1.1 |
} |
423 |
|
|
|
424 |
|
|
/** |
425 |
jsr166 |
1.16 |
* peek() returns next element, or null if empty |
426 |
dl |
1.1 |
*/ |
427 |
|
|
public void testPeek() { |
428 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
429 |
|
|
for (int i = 0; i < SIZE; ++i) { |
430 |
jsr166 |
1.12 |
assertEquals(i, q.peek()); |
431 |
|
|
assertEquals(i, q.poll()); |
432 |
dl |
1.1 |
assertTrue(q.peek() == null || |
433 |
jsr166 |
1.12 |
!q.peek().equals(i)); |
434 |
dl |
1.1 |
} |
435 |
jsr166 |
1.8 |
assertNull(q.peek()); |
436 |
dl |
1.1 |
} |
437 |
|
|
|
438 |
|
|
/** |
439 |
jsr166 |
1.16 |
* peekLast() returns next element, or null if empty |
440 |
dl |
1.1 |
*/ |
441 |
|
|
public void testPeekLast() { |
442 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
443 |
|
|
for (int i = SIZE-1; i >= 0; --i) { |
444 |
jsr166 |
1.12 |
assertEquals(i, q.peekLast()); |
445 |
|
|
assertEquals(i, q.pollLast()); |
446 |
dl |
1.1 |
assertTrue(q.peekLast() == null || |
447 |
jsr166 |
1.12 |
!q.peekLast().equals(i)); |
448 |
dl |
1.1 |
} |
449 |
jsr166 |
1.8 |
assertNull(q.peekLast()); |
450 |
dl |
1.1 |
} |
451 |
|
|
|
452 |
|
|
/** |
453 |
jsr166 |
1.16 |
* element() returns first element, or throws NSEE if empty |
454 |
|
|
*/ |
455 |
|
|
public void testElement() { |
456 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
457 |
|
|
for (int i = 0; i < SIZE; ++i) { |
458 |
|
|
assertEquals(i, q.element()); |
459 |
|
|
assertEquals(i, q.poll()); |
460 |
|
|
} |
461 |
|
|
try { |
462 |
|
|
q.element(); |
463 |
|
|
shouldThrow(); |
464 |
|
|
} catch (NoSuchElementException success) {} |
465 |
|
|
} |
466 |
|
|
|
467 |
|
|
/** |
468 |
|
|
* getFirst() returns first element, or throws NSEE if empty |
469 |
dl |
1.1 |
*/ |
470 |
|
|
public void testFirstElement() { |
471 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
472 |
|
|
for (int i = 0; i < SIZE; ++i) { |
473 |
jsr166 |
1.12 |
assertEquals(i, q.getFirst()); |
474 |
|
|
assertEquals(i, q.pollFirst()); |
475 |
dl |
1.1 |
} |
476 |
|
|
try { |
477 |
|
|
q.getFirst(); |
478 |
|
|
shouldThrow(); |
479 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
480 |
dl |
1.1 |
} |
481 |
|
|
|
482 |
|
|
/** |
483 |
jsr166 |
1.16 |
* getLast() returns last element, or throws NSEE if empty |
484 |
dl |
1.1 |
*/ |
485 |
|
|
public void testLastElement() { |
486 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
487 |
|
|
for (int i = SIZE-1; i >= 0; --i) { |
488 |
jsr166 |
1.12 |
assertEquals(i, q.getLast()); |
489 |
|
|
assertEquals(i, q.pollLast()); |
490 |
dl |
1.1 |
} |
491 |
|
|
try { |
492 |
|
|
q.getLast(); |
493 |
|
|
shouldThrow(); |
494 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
495 |
jsr166 |
1.8 |
assertNull(q.peekLast()); |
496 |
dl |
1.1 |
} |
497 |
|
|
|
498 |
|
|
/** |
499 |
jsr166 |
1.16 |
* removeFirst() removes first element, or throws NSEE if empty |
500 |
dl |
1.1 |
*/ |
501 |
|
|
public void testRemoveFirst() { |
502 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
503 |
|
|
for (int i = 0; i < SIZE; ++i) { |
504 |
jsr166 |
1.12 |
assertEquals(i, q.removeFirst()); |
505 |
dl |
1.1 |
} |
506 |
|
|
try { |
507 |
|
|
q.removeFirst(); |
508 |
|
|
shouldThrow(); |
509 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
510 |
jsr166 |
1.16 |
assertNull(q.peekFirst()); |
511 |
|
|
} |
512 |
|
|
|
513 |
|
|
/** |
514 |
|
|
* removeLast() removes last element, or throws NSEE if empty |
515 |
|
|
*/ |
516 |
|
|
public void testRemoveLast() { |
517 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
518 |
|
|
for (int i = SIZE - 1; i >= 0; --i) { |
519 |
|
|
assertEquals(i, q.removeLast()); |
520 |
|
|
} |
521 |
|
|
try { |
522 |
|
|
q.removeLast(); |
523 |
|
|
shouldThrow(); |
524 |
|
|
} catch (NoSuchElementException success) {} |
525 |
|
|
assertNull(q.peekLast()); |
526 |
dl |
1.1 |
} |
527 |
|
|
|
528 |
|
|
/** |
529 |
|
|
* removeFirstOccurrence(x) removes x and returns true if present |
530 |
|
|
*/ |
531 |
|
|
public void testRemoveFirstOccurrence() { |
532 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
533 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
534 |
|
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
535 |
|
|
} |
536 |
|
|
for (int i = 0; i < SIZE; i+=2) { |
537 |
|
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
538 |
|
|
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
539 |
|
|
} |
540 |
|
|
assertTrue(q.isEmpty()); |
541 |
|
|
} |
542 |
|
|
|
543 |
|
|
/** |
544 |
|
|
* removeLastOccurrence(x) removes x and returns true if present |
545 |
|
|
*/ |
546 |
|
|
public void testRemoveLastOccurrence() { |
547 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
548 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
549 |
|
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
550 |
|
|
} |
551 |
|
|
for (int i = 0; i < SIZE; i+=2) { |
552 |
|
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
553 |
|
|
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
554 |
|
|
} |
555 |
|
|
assertTrue(q.isEmpty()); |
556 |
|
|
} |
557 |
|
|
|
558 |
|
|
/** |
559 |
|
|
* contains(x) reports true when elements added but not yet removed |
560 |
|
|
*/ |
561 |
|
|
public void testContains() { |
562 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
563 |
|
|
for (int i = 0; i < SIZE; ++i) { |
564 |
|
|
assertTrue(q.contains(new Integer(i))); |
565 |
jsr166 |
1.12 |
assertEquals(i, q.pollFirst()); |
566 |
dl |
1.1 |
assertFalse(q.contains(new Integer(i))); |
567 |
|
|
} |
568 |
|
|
} |
569 |
|
|
|
570 |
|
|
/** |
571 |
|
|
* clear removes all elements |
572 |
|
|
*/ |
573 |
|
|
public void testClear() { |
574 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
575 |
|
|
q.clear(); |
576 |
|
|
assertTrue(q.isEmpty()); |
577 |
|
|
assertEquals(0, q.size()); |
578 |
jsr166 |
1.12 |
assertTrue(q.add(new Integer(1))); |
579 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
580 |
|
|
q.clear(); |
581 |
|
|
assertTrue(q.isEmpty()); |
582 |
|
|
} |
583 |
|
|
|
584 |
|
|
/** |
585 |
|
|
* containsAll(c) is true when c contains a subset of elements |
586 |
|
|
*/ |
587 |
|
|
public void testContainsAll() { |
588 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
589 |
|
|
ArrayDeque p = new ArrayDeque(); |
590 |
|
|
for (int i = 0; i < SIZE; ++i) { |
591 |
|
|
assertTrue(q.containsAll(p)); |
592 |
|
|
assertFalse(p.containsAll(q)); |
593 |
jsr166 |
1.12 |
assertTrue(p.add(new Integer(i))); |
594 |
dl |
1.1 |
} |
595 |
|
|
assertTrue(p.containsAll(q)); |
596 |
|
|
} |
597 |
|
|
|
598 |
|
|
/** |
599 |
|
|
* retainAll(c) retains only those elements of c and reports true if changed |
600 |
|
|
*/ |
601 |
|
|
public void testRetainAll() { |
602 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
603 |
|
|
ArrayDeque p = populatedDeque(SIZE); |
604 |
|
|
for (int i = 0; i < SIZE; ++i) { |
605 |
|
|
boolean changed = q.retainAll(p); |
606 |
jsr166 |
1.12 |
assertEquals(changed, (i > 0)); |
607 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
608 |
|
|
assertEquals(SIZE-i, q.size()); |
609 |
|
|
p.removeFirst(); |
610 |
|
|
} |
611 |
|
|
} |
612 |
|
|
|
613 |
|
|
/** |
614 |
|
|
* removeAll(c) removes only those elements of c and reports true if changed |
615 |
|
|
*/ |
616 |
|
|
public void testRemoveAll() { |
617 |
|
|
for (int i = 1; i < SIZE; ++i) { |
618 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
619 |
|
|
ArrayDeque p = populatedDeque(i); |
620 |
|
|
assertTrue(q.removeAll(p)); |
621 |
|
|
assertEquals(SIZE-i, q.size()); |
622 |
|
|
for (int j = 0; j < i; ++j) { |
623 |
jsr166 |
1.12 |
assertFalse(q.contains(p.removeFirst())); |
624 |
dl |
1.1 |
} |
625 |
|
|
} |
626 |
|
|
} |
627 |
|
|
|
628 |
|
|
/** |
629 |
jsr166 |
1.19 |
* toArray() contains all elements in FIFO order |
630 |
dl |
1.1 |
*/ |
631 |
|
|
public void testToArray() { |
632 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
633 |
jsr166 |
1.8 |
Object[] o = q.toArray(); |
634 |
|
|
for (int i = 0; i < o.length; i++) |
635 |
jsr166 |
1.19 |
assertSame(o[i], q.pollFirst()); |
636 |
dl |
1.1 |
} |
637 |
|
|
|
638 |
|
|
/** |
639 |
jsr166 |
1.19 |
* toArray(a) contains all elements in FIFO order |
640 |
dl |
1.1 |
*/ |
641 |
|
|
public void testToArray2() { |
642 |
jsr166 |
1.20 |
ArrayDeque<Integer> q = populatedDeque(SIZE); |
643 |
jsr166 |
1.8 |
Integer[] ints = new Integer[SIZE]; |
644 |
jsr166 |
1.20 |
Integer[] array = q.toArray(ints); |
645 |
|
|
assertSame(ints, array); |
646 |
jsr166 |
1.6 |
for (int i = 0; i < ints.length; i++) |
647 |
jsr166 |
1.19 |
assertSame(ints[i], q.remove()); |
648 |
dl |
1.1 |
} |
649 |
|
|
|
650 |
|
|
/** |
651 |
jsr166 |
1.18 |
* toArray(null) throws NullPointerException |
652 |
dl |
1.1 |
*/ |
653 |
jsr166 |
1.18 |
public void testToArray_NullArg() { |
654 |
jsr166 |
1.12 |
ArrayDeque l = new ArrayDeque(); |
655 |
|
|
l.add(new Object()); |
656 |
jsr166 |
1.8 |
try { |
657 |
jsr166 |
1.18 |
l.toArray(null); |
658 |
jsr166 |
1.8 |
shouldThrow(); |
659 |
|
|
} catch (NullPointerException success) {} |
660 |
dl |
1.1 |
} |
661 |
|
|
|
662 |
|
|
/** |
663 |
jsr166 |
1.17 |
* toArray(incompatible array type) throws ArrayStoreException |
664 |
dl |
1.1 |
*/ |
665 |
|
|
public void testToArray1_BadArg() { |
666 |
jsr166 |
1.12 |
ArrayDeque l = new ArrayDeque(); |
667 |
|
|
l.add(new Integer(5)); |
668 |
jsr166 |
1.8 |
try { |
669 |
jsr166 |
1.17 |
l.toArray(new String[10]); |
670 |
jsr166 |
1.8 |
shouldThrow(); |
671 |
jsr166 |
1.9 |
} catch (ArrayStoreException success) {} |
672 |
dl |
1.1 |
} |
673 |
jsr166 |
1.5 |
|
674 |
dl |
1.1 |
/** |
675 |
jsr166 |
1.16 |
* Iterator iterates through all elements |
676 |
dl |
1.1 |
*/ |
677 |
|
|
public void testIterator() { |
678 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
679 |
|
|
int i = 0; |
680 |
jsr166 |
1.8 |
Iterator it = q.iterator(); |
681 |
jsr166 |
1.6 |
while (it.hasNext()) { |
682 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
683 |
|
|
++i; |
684 |
|
|
} |
685 |
|
|
assertEquals(i, SIZE); |
686 |
|
|
} |
687 |
|
|
|
688 |
|
|
/** |
689 |
jsr166 |
1.16 |
* Iterator ordering is FIFO |
690 |
dl |
1.1 |
*/ |
691 |
|
|
public void testIteratorOrdering() { |
692 |
|
|
final ArrayDeque q = new ArrayDeque(); |
693 |
jsr166 |
1.16 |
q.add(one); |
694 |
|
|
q.add(two); |
695 |
|
|
q.add(three); |
696 |
dl |
1.1 |
int k = 0; |
697 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
698 |
jsr166 |
1.12 |
assertEquals(++k, it.next()); |
699 |
dl |
1.1 |
} |
700 |
|
|
|
701 |
|
|
assertEquals(3, k); |
702 |
|
|
} |
703 |
|
|
|
704 |
|
|
/** |
705 |
jsr166 |
1.16 |
* iterator.remove() removes current element |
706 |
dl |
1.1 |
*/ |
707 |
jsr166 |
1.16 |
public void testIteratorRemove() { |
708 |
dl |
1.1 |
final ArrayDeque q = new ArrayDeque(); |
709 |
dl |
1.4 |
final Random rng = new Random(); |
710 |
dl |
1.3 |
for (int iters = 0; iters < 100; ++iters) { |
711 |
dl |
1.4 |
int max = rng.nextInt(5) + 2; |
712 |
|
|
int split = rng.nextInt(max-1) + 1; |
713 |
|
|
for (int j = 1; j <= max; ++j) |
714 |
|
|
q.add(new Integer(j)); |
715 |
dl |
1.3 |
Iterator it = q.iterator(); |
716 |
jsr166 |
1.5 |
for (int j = 1; j <= split; ++j) |
717 |
dl |
1.4 |
assertEquals(it.next(), new Integer(j)); |
718 |
dl |
1.3 |
it.remove(); |
719 |
dl |
1.4 |
assertEquals(it.next(), new Integer(split+1)); |
720 |
jsr166 |
1.5 |
for (int j = 1; j <= split; ++j) |
721 |
dl |
1.4 |
q.remove(new Integer(j)); |
722 |
dl |
1.3 |
it = q.iterator(); |
723 |
dl |
1.4 |
for (int j = split+1; j <= max; ++j) { |
724 |
|
|
assertEquals(it.next(), new Integer(j)); |
725 |
|
|
it.remove(); |
726 |
|
|
} |
727 |
dl |
1.3 |
assertFalse(it.hasNext()); |
728 |
dl |
1.4 |
assertTrue(q.isEmpty()); |
729 |
dl |
1.3 |
} |
730 |
dl |
1.1 |
} |
731 |
|
|
|
732 |
dl |
1.2 |
/** |
733 |
jsr166 |
1.16 |
* Descending iterator iterates through all elements |
734 |
dl |
1.2 |
*/ |
735 |
|
|
public void testDescendingIterator() { |
736 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
737 |
|
|
int i = 0; |
738 |
jsr166 |
1.8 |
Iterator it = q.descendingIterator(); |
739 |
jsr166 |
1.6 |
while (it.hasNext()) { |
740 |
dl |
1.2 |
assertTrue(q.contains(it.next())); |
741 |
|
|
++i; |
742 |
|
|
} |
743 |
|
|
assertEquals(i, SIZE); |
744 |
|
|
assertFalse(it.hasNext()); |
745 |
|
|
try { |
746 |
|
|
it.next(); |
747 |
jsr166 |
1.11 |
shouldThrow(); |
748 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
749 |
dl |
1.2 |
} |
750 |
|
|
|
751 |
|
|
/** |
752 |
jsr166 |
1.16 |
* Descending iterator ordering is reverse FIFO |
753 |
dl |
1.2 |
*/ |
754 |
|
|
public void testDescendingIteratorOrdering() { |
755 |
|
|
final ArrayDeque q = new ArrayDeque(); |
756 |
dl |
1.3 |
for (int iters = 0; iters < 100; ++iters) { |
757 |
|
|
q.add(new Integer(3)); |
758 |
|
|
q.add(new Integer(2)); |
759 |
|
|
q.add(new Integer(1)); |
760 |
|
|
int k = 0; |
761 |
|
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
762 |
jsr166 |
1.12 |
assertEquals(++k, it.next()); |
763 |
dl |
1.3 |
} |
764 |
jsr166 |
1.5 |
|
765 |
dl |
1.3 |
assertEquals(3, k); |
766 |
|
|
q.remove(); |
767 |
|
|
q.remove(); |
768 |
|
|
q.remove(); |
769 |
dl |
1.2 |
} |
770 |
|
|
} |
771 |
|
|
|
772 |
|
|
/** |
773 |
jsr166 |
1.16 |
* descendingIterator.remove() removes current element |
774 |
dl |
1.2 |
*/ |
775 |
jsr166 |
1.16 |
public void testDescendingIteratorRemove() { |
776 |
dl |
1.2 |
final ArrayDeque q = new ArrayDeque(); |
777 |
dl |
1.4 |
final Random rng = new Random(); |
778 |
dl |
1.3 |
for (int iters = 0; iters < 100; ++iters) { |
779 |
dl |
1.4 |
int max = rng.nextInt(5) + 2; |
780 |
|
|
int split = rng.nextInt(max-1) + 1; |
781 |
|
|
for (int j = max; j >= 1; --j) |
782 |
|
|
q.add(new Integer(j)); |
783 |
dl |
1.3 |
Iterator it = q.descendingIterator(); |
784 |
jsr166 |
1.5 |
for (int j = 1; j <= split; ++j) |
785 |
dl |
1.4 |
assertEquals(it.next(), new Integer(j)); |
786 |
dl |
1.3 |
it.remove(); |
787 |
dl |
1.4 |
assertEquals(it.next(), new Integer(split+1)); |
788 |
jsr166 |
1.5 |
for (int j = 1; j <= split; ++j) |
789 |
dl |
1.4 |
q.remove(new Integer(j)); |
790 |
dl |
1.3 |
it = q.descendingIterator(); |
791 |
dl |
1.4 |
for (int j = split+1; j <= max; ++j) { |
792 |
|
|
assertEquals(it.next(), new Integer(j)); |
793 |
|
|
it.remove(); |
794 |
|
|
} |
795 |
dl |
1.3 |
assertFalse(it.hasNext()); |
796 |
dl |
1.4 |
assertTrue(q.isEmpty()); |
797 |
dl |
1.3 |
} |
798 |
dl |
1.2 |
} |
799 |
|
|
|
800 |
dl |
1.1 |
/** |
801 |
jsr166 |
1.16 |
* toString() contains toStrings of elements |
802 |
dl |
1.1 |
*/ |
803 |
|
|
public void testToString() { |
804 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
805 |
|
|
String s = q.toString(); |
806 |
|
|
for (int i = 0; i < SIZE; ++i) { |
807 |
jsr166 |
1.23 |
assertTrue(s.contains(String.valueOf(i))); |
808 |
dl |
1.1 |
} |
809 |
jsr166 |
1.5 |
} |
810 |
dl |
1.1 |
|
811 |
|
|
/** |
812 |
jsr166 |
1.16 |
* A deserialized serialized deque has same elements in same order |
813 |
dl |
1.1 |
*/ |
814 |
jsr166 |
1.16 |
public void testSerialization() throws Exception { |
815 |
jsr166 |
1.24 |
Queue x = populatedDeque(SIZE); |
816 |
|
|
Queue y = serialClone(x); |
817 |
|
|
|
818 |
|
|
assertTrue(x != y); |
819 |
|
|
assertEquals(x.size(), y.size()); |
820 |
|
|
assertEquals(x.toString(), y.toString()); |
821 |
|
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
822 |
|
|
while (!x.isEmpty()) { |
823 |
|
|
assertFalse(y.isEmpty()); |
824 |
|
|
assertEquals(x.remove(), y.remove()); |
825 |
|
|
} |
826 |
|
|
assertTrue(y.isEmpty()); |
827 |
jsr166 |
1.5 |
} |
828 |
dl |
1.1 |
|
829 |
|
|
} |