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