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.16 |
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 |
dl |
1.1 |
import java.util.*; |
15 |
|
|
|
16 |
|
|
public class ArrayDequeTest extends JSR166TestCase { |
17 |
|
|
public static void main(String[] args) { |
18 |
jsr166 |
1.16 |
junit.textui.TestRunner.run(suite()); |
19 |
dl |
1.1 |
} |
20 |
|
|
|
21 |
|
|
public static Test suite() { |
22 |
jsr166 |
1.8 |
return new TestSuite(ArrayDequeTest.class); |
23 |
dl |
1.1 |
} |
24 |
|
|
|
25 |
|
|
/** |
26 |
jsr166 |
1.16 |
* Create a deque of given size containing consecutive |
27 |
dl |
1.1 |
* Integers 0 ... n. |
28 |
|
|
*/ |
29 |
jsr166 |
1.20 |
private ArrayDeque<Integer> populatedDeque(int n) { |
30 |
|
|
ArrayDeque<Integer> q = new ArrayDeque<Integer>(); |
31 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
32 |
jsr166 |
1.8 |
for (int i = 0; i < n; ++i) |
33 |
|
|
assertTrue(q.offerLast(new Integer(i))); |
34 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
35 |
jsr166 |
1.8 |
assertEquals(n, q.size()); |
36 |
dl |
1.1 |
return q; |
37 |
|
|
} |
38 |
jsr166 |
1.5 |
|
39 |
dl |
1.1 |
/** |
40 |
jsr166 |
1.16 |
* new deque is empty |
41 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
54 |
dl |
1.1 |
} |
55 |
|
|
|
56 |
|
|
/** |
57 |
jsr166 |
1.16 |
* 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 |
dl |
1.1 |
|
67 |
jsr166 |
1.16 |
/** |
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 |
dl |
1.1 |
*/ |
83 |
|
|
public void testConstructor6() { |
84 |
jsr166 |
1.9 |
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 |
dl |
1.1 |
} |
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 |
jsr166 |
1.8 |
try { |
126 |
dl |
1.1 |
ArrayDeque q = new ArrayDeque(1); |
127 |
|
|
q.push(null); |
128 |
|
|
shouldThrow(); |
129 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
130 |
dl |
1.1 |
} |
131 |
|
|
|
132 |
|
|
/** |
133 |
jsr166 |
1.16 |
* peekFirst() returns element inserted with push |
134 |
dl |
1.1 |
*/ |
135 |
|
|
public void testPush() { |
136 |
|
|
ArrayDeque q = populatedDeque(3); |
137 |
|
|
q.pollLast(); |
138 |
jsr166 |
1.8 |
q.push(four); |
139 |
jsr166 |
1.12 |
assertSame(four, q.peekFirst()); |
140 |
jsr166 |
1.5 |
} |
141 |
dl |
1.1 |
|
142 |
|
|
/** |
143 |
jsr166 |
1.16 |
* pop() removes next element, or throws NSEE if empty |
144 |
dl |
1.1 |
*/ |
145 |
|
|
public void testPop() { |
146 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
147 |
|
|
for (int i = 0; i < SIZE; ++i) { |
148 |
jsr166 |
1.12 |
assertEquals(i, q.pop()); |
149 |
dl |
1.1 |
} |
150 |
|
|
try { |
151 |
|
|
q.pop(); |
152 |
|
|
shouldThrow(); |
153 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
154 |
dl |
1.1 |
} |
155 |
|
|
|
156 |
|
|
/** |
157 |
|
|
* offer(null) throws NPE |
158 |
|
|
*/ |
159 |
jsr166 |
1.16 |
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 |
dl |
1.1 |
public void testOfferFirstNull() { |
171 |
jsr166 |
1.8 |
try { |
172 |
dl |
1.1 |
ArrayDeque q = new ArrayDeque(); |
173 |
|
|
q.offerFirst(null); |
174 |
|
|
shouldThrow(); |
175 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
176 |
dl |
1.1 |
} |
177 |
|
|
|
178 |
|
|
/** |
179 |
jsr166 |
1.16 |
* 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 |
dl |
1.1 |
*/ |
203 |
|
|
public void testOfferFirst() { |
204 |
|
|
ArrayDeque q = new ArrayDeque(); |
205 |
jsr166 |
1.16 |
assertTrue(q.offerFirst(zero)); |
206 |
|
|
assertTrue(q.offerFirst(one)); |
207 |
|
|
assertSame(one, q.peekFirst()); |
208 |
|
|
assertSame(zero, q.peekLast()); |
209 |
dl |
1.1 |
} |
210 |
|
|
|
211 |
|
|
/** |
212 |
jsr166 |
1.16 |
* offerLast(x) succeeds |
213 |
dl |
1.1 |
*/ |
214 |
|
|
public void testOfferLast() { |
215 |
|
|
ArrayDeque q = new ArrayDeque(); |
216 |
jsr166 |
1.16 |
assertTrue(q.offerLast(zero)); |
217 |
|
|
assertTrue(q.offerLast(one)); |
218 |
|
|
assertSame(zero, q.peekFirst()); |
219 |
|
|
assertSame(one, q.peekLast()); |
220 |
dl |
1.1 |
} |
221 |
|
|
|
222 |
|
|
/** |
223 |
jsr166 |
1.16 |
* 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 |
dl |
1.1 |
*/ |
258 |
|
|
public void testAdd() { |
259 |
|
|
ArrayDeque q = new ArrayDeque(); |
260 |
jsr166 |
1.16 |
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 |
dl |
1.1 |
} |
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 |
jsr166 |
1.9 |
} catch (NullPointerException success) {} |
297 |
dl |
1.1 |
} |
298 |
|
|
|
299 |
|
|
/** |
300 |
jsr166 |
1.16 |
* 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 |
dl |
1.1 |
*/ |
329 |
|
|
public void testAddAll5() { |
330 |
jsr166 |
1.10 |
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 |
dl |
1.1 |
} |
340 |
|
|
|
341 |
|
|
/** |
342 |
jsr166 |
1.16 |
* pollFirst() succeeds unless empty |
343 |
dl |
1.1 |
*/ |
344 |
|
|
public void testPollFirst() { |
345 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
346 |
|
|
for (int i = 0; i < SIZE; ++i) { |
347 |
jsr166 |
1.12 |
assertEquals(i, q.pollFirst()); |
348 |
dl |
1.1 |
} |
349 |
jsr166 |
1.8 |
assertNull(q.pollFirst()); |
350 |
dl |
1.1 |
} |
351 |
|
|
|
352 |
|
|
/** |
353 |
jsr166 |
1.16 |
* pollLast() succeeds unless empty |
354 |
dl |
1.1 |
*/ |
355 |
|
|
public void testPollLast() { |
356 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
357 |
|
|
for (int i = SIZE-1; i >= 0; --i) { |
358 |
jsr166 |
1.12 |
assertEquals(i, q.pollLast()); |
359 |
dl |
1.1 |
} |
360 |
jsr166 |
1.8 |
assertNull(q.pollLast()); |
361 |
dl |
1.1 |
} |
362 |
|
|
|
363 |
|
|
/** |
364 |
jsr166 |
1.16 |
* poll() succeeds unless empty |
365 |
dl |
1.1 |
*/ |
366 |
|
|
public void testPoll() { |
367 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
368 |
|
|
for (int i = 0; i < SIZE; ++i) { |
369 |
jsr166 |
1.12 |
assertEquals(i, q.poll()); |
370 |
dl |
1.1 |
} |
371 |
jsr166 |
1.8 |
assertNull(q.poll()); |
372 |
dl |
1.1 |
} |
373 |
|
|
|
374 |
|
|
/** |
375 |
jsr166 |
1.16 |
* remove() removes next element, or throws NSEE if empty |
376 |
dl |
1.1 |
*/ |
377 |
|
|
public void testRemove() { |
378 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
379 |
|
|
for (int i = 0; i < SIZE; ++i) { |
380 |
jsr166 |
1.12 |
assertEquals(i, q.remove()); |
381 |
dl |
1.1 |
} |
382 |
|
|
try { |
383 |
|
|
q.remove(); |
384 |
|
|
shouldThrow(); |
385 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
386 |
dl |
1.1 |
} |
387 |
|
|
|
388 |
|
|
/** |
389 |
jsr166 |
1.16 |
* 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 |
jsr166 |
1.21 |
assertTrue(q.contains(i)); |
395 |
|
|
assertTrue(q.remove(i)); |
396 |
|
|
assertFalse(q.contains(i)); |
397 |
|
|
assertTrue(q.contains(i-1)); |
398 |
jsr166 |
1.16 |
} |
399 |
|
|
for (int i = 0; i < SIZE; i+=2) { |
400 |
jsr166 |
1.21 |
assertTrue(q.contains(i)); |
401 |
|
|
assertTrue(q.remove(i)); |
402 |
|
|
assertFalse(q.contains(i)); |
403 |
|
|
assertFalse(q.remove(i+1)); |
404 |
|
|
assertFalse(q.contains(i+1)); |
405 |
jsr166 |
1.16 |
} |
406 |
|
|
assertTrue(q.isEmpty()); |
407 |
|
|
} |
408 |
|
|
|
409 |
|
|
/** |
410 |
|
|
* peekFirst() returns next element, or null if empty |
411 |
dl |
1.1 |
*/ |
412 |
|
|
public void testPeekFirst() { |
413 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
414 |
|
|
for (int i = 0; i < SIZE; ++i) { |
415 |
jsr166 |
1.12 |
assertEquals(i, q.peekFirst()); |
416 |
|
|
assertEquals(i, q.pollFirst()); |
417 |
dl |
1.1 |
assertTrue(q.peekFirst() == null || |
418 |
jsr166 |
1.12 |
!q.peekFirst().equals(i)); |
419 |
dl |
1.1 |
} |
420 |
jsr166 |
1.8 |
assertNull(q.peekFirst()); |
421 |
dl |
1.1 |
} |
422 |
|
|
|
423 |
|
|
/** |
424 |
jsr166 |
1.16 |
* peek() returns next element, or null if empty |
425 |
dl |
1.1 |
*/ |
426 |
|
|
public void testPeek() { |
427 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
428 |
|
|
for (int i = 0; i < SIZE; ++i) { |
429 |
jsr166 |
1.12 |
assertEquals(i, q.peek()); |
430 |
|
|
assertEquals(i, q.poll()); |
431 |
dl |
1.1 |
assertTrue(q.peek() == null || |
432 |
jsr166 |
1.12 |
!q.peek().equals(i)); |
433 |
dl |
1.1 |
} |
434 |
jsr166 |
1.8 |
assertNull(q.peek()); |
435 |
dl |
1.1 |
} |
436 |
|
|
|
437 |
|
|
/** |
438 |
jsr166 |
1.16 |
* peekLast() returns next element, or null if empty |
439 |
dl |
1.1 |
*/ |
440 |
|
|
public void testPeekLast() { |
441 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
442 |
|
|
for (int i = SIZE-1; i >= 0; --i) { |
443 |
jsr166 |
1.12 |
assertEquals(i, q.peekLast()); |
444 |
|
|
assertEquals(i, q.pollLast()); |
445 |
dl |
1.1 |
assertTrue(q.peekLast() == null || |
446 |
jsr166 |
1.12 |
!q.peekLast().equals(i)); |
447 |
dl |
1.1 |
} |
448 |
jsr166 |
1.8 |
assertNull(q.peekLast()); |
449 |
dl |
1.1 |
} |
450 |
|
|
|
451 |
|
|
/** |
452 |
jsr166 |
1.16 |
* element() returns first element, or throws NSEE if empty |
453 |
|
|
*/ |
454 |
|
|
public void testElement() { |
455 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
456 |
|
|
for (int i = 0; i < SIZE; ++i) { |
457 |
|
|
assertEquals(i, q.element()); |
458 |
|
|
assertEquals(i, q.poll()); |
459 |
|
|
} |
460 |
|
|
try { |
461 |
|
|
q.element(); |
462 |
|
|
shouldThrow(); |
463 |
|
|
} catch (NoSuchElementException success) {} |
464 |
|
|
} |
465 |
|
|
|
466 |
|
|
/** |
467 |
|
|
* getFirst() returns first element, or throws NSEE if empty |
468 |
dl |
1.1 |
*/ |
469 |
|
|
public void testFirstElement() { |
470 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
471 |
|
|
for (int i = 0; i < SIZE; ++i) { |
472 |
jsr166 |
1.12 |
assertEquals(i, q.getFirst()); |
473 |
|
|
assertEquals(i, q.pollFirst()); |
474 |
dl |
1.1 |
} |
475 |
|
|
try { |
476 |
|
|
q.getFirst(); |
477 |
|
|
shouldThrow(); |
478 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
479 |
dl |
1.1 |
} |
480 |
|
|
|
481 |
|
|
/** |
482 |
jsr166 |
1.16 |
* getLast() returns last element, or throws NSEE if empty |
483 |
dl |
1.1 |
*/ |
484 |
|
|
public void testLastElement() { |
485 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
486 |
|
|
for (int i = SIZE-1; i >= 0; --i) { |
487 |
jsr166 |
1.12 |
assertEquals(i, q.getLast()); |
488 |
|
|
assertEquals(i, q.pollLast()); |
489 |
dl |
1.1 |
} |
490 |
|
|
try { |
491 |
|
|
q.getLast(); |
492 |
|
|
shouldThrow(); |
493 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
494 |
jsr166 |
1.8 |
assertNull(q.peekLast()); |
495 |
dl |
1.1 |
} |
496 |
|
|
|
497 |
|
|
/** |
498 |
jsr166 |
1.16 |
* removeFirst() removes first element, or throws NSEE if empty |
499 |
dl |
1.1 |
*/ |
500 |
|
|
public void testRemoveFirst() { |
501 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
502 |
|
|
for (int i = 0; i < SIZE; ++i) { |
503 |
jsr166 |
1.12 |
assertEquals(i, q.removeFirst()); |
504 |
dl |
1.1 |
} |
505 |
|
|
try { |
506 |
|
|
q.removeFirst(); |
507 |
|
|
shouldThrow(); |
508 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
509 |
jsr166 |
1.16 |
assertNull(q.peekFirst()); |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
/** |
513 |
|
|
* removeLast() removes last element, or throws NSEE if empty |
514 |
|
|
*/ |
515 |
|
|
public void testRemoveLast() { |
516 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
517 |
|
|
for (int i = SIZE - 1; i >= 0; --i) { |
518 |
|
|
assertEquals(i, q.removeLast()); |
519 |
|
|
} |
520 |
|
|
try { |
521 |
|
|
q.removeLast(); |
522 |
|
|
shouldThrow(); |
523 |
|
|
} catch (NoSuchElementException success) {} |
524 |
|
|
assertNull(q.peekLast()); |
525 |
dl |
1.1 |
} |
526 |
|
|
|
527 |
|
|
/** |
528 |
|
|
* removeFirstOccurrence(x) removes x and returns true if present |
529 |
|
|
*/ |
530 |
|
|
public void testRemoveFirstOccurrence() { |
531 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
532 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
533 |
|
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
534 |
|
|
} |
535 |
|
|
for (int i = 0; i < SIZE; i+=2) { |
536 |
|
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
537 |
|
|
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
538 |
|
|
} |
539 |
|
|
assertTrue(q.isEmpty()); |
540 |
|
|
} |
541 |
|
|
|
542 |
|
|
/** |
543 |
|
|
* removeLastOccurrence(x) removes x and returns true if present |
544 |
|
|
*/ |
545 |
|
|
public void testRemoveLastOccurrence() { |
546 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
547 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
548 |
|
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
549 |
|
|
} |
550 |
|
|
for (int i = 0; i < SIZE; i+=2) { |
551 |
|
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
552 |
|
|
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
553 |
|
|
} |
554 |
|
|
assertTrue(q.isEmpty()); |
555 |
|
|
} |
556 |
|
|
|
557 |
|
|
/** |
558 |
|
|
* contains(x) reports true when elements added but not yet removed |
559 |
|
|
*/ |
560 |
|
|
public void testContains() { |
561 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
562 |
|
|
for (int i = 0; i < SIZE; ++i) { |
563 |
|
|
assertTrue(q.contains(new Integer(i))); |
564 |
jsr166 |
1.12 |
assertEquals(i, q.pollFirst()); |
565 |
dl |
1.1 |
assertFalse(q.contains(new Integer(i))); |
566 |
|
|
} |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
/** |
570 |
|
|
* clear removes all elements |
571 |
|
|
*/ |
572 |
|
|
public void testClear() { |
573 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
574 |
|
|
q.clear(); |
575 |
|
|
assertTrue(q.isEmpty()); |
576 |
|
|
assertEquals(0, q.size()); |
577 |
jsr166 |
1.12 |
assertTrue(q.add(new Integer(1))); |
578 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
579 |
|
|
q.clear(); |
580 |
|
|
assertTrue(q.isEmpty()); |
581 |
|
|
} |
582 |
|
|
|
583 |
|
|
/** |
584 |
|
|
* containsAll(c) is true when c contains a subset of elements |
585 |
|
|
*/ |
586 |
|
|
public void testContainsAll() { |
587 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
588 |
|
|
ArrayDeque p = new ArrayDeque(); |
589 |
|
|
for (int i = 0; i < SIZE; ++i) { |
590 |
|
|
assertTrue(q.containsAll(p)); |
591 |
|
|
assertFalse(p.containsAll(q)); |
592 |
jsr166 |
1.12 |
assertTrue(p.add(new Integer(i))); |
593 |
dl |
1.1 |
} |
594 |
|
|
assertTrue(p.containsAll(q)); |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
/** |
598 |
|
|
* retainAll(c) retains only those elements of c and reports true if changed |
599 |
|
|
*/ |
600 |
|
|
public void testRetainAll() { |
601 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
602 |
|
|
ArrayDeque p = populatedDeque(SIZE); |
603 |
|
|
for (int i = 0; i < SIZE; ++i) { |
604 |
|
|
boolean changed = q.retainAll(p); |
605 |
jsr166 |
1.12 |
assertEquals(changed, (i > 0)); |
606 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
607 |
|
|
assertEquals(SIZE-i, q.size()); |
608 |
|
|
p.removeFirst(); |
609 |
|
|
} |
610 |
|
|
} |
611 |
|
|
|
612 |
|
|
/** |
613 |
|
|
* removeAll(c) removes only those elements of c and reports true if changed |
614 |
|
|
*/ |
615 |
|
|
public void testRemoveAll() { |
616 |
|
|
for (int i = 1; i < SIZE; ++i) { |
617 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
618 |
|
|
ArrayDeque p = populatedDeque(i); |
619 |
|
|
assertTrue(q.removeAll(p)); |
620 |
|
|
assertEquals(SIZE-i, q.size()); |
621 |
|
|
for (int j = 0; j < i; ++j) { |
622 |
jsr166 |
1.12 |
assertFalse(q.contains(p.removeFirst())); |
623 |
dl |
1.1 |
} |
624 |
|
|
} |
625 |
|
|
} |
626 |
|
|
|
627 |
|
|
/** |
628 |
jsr166 |
1.19 |
* toArray() contains all elements in FIFO order |
629 |
dl |
1.1 |
*/ |
630 |
|
|
public void testToArray() { |
631 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
632 |
jsr166 |
1.8 |
Object[] o = q.toArray(); |
633 |
|
|
for (int i = 0; i < o.length; i++) |
634 |
jsr166 |
1.19 |
assertSame(o[i], q.pollFirst()); |
635 |
dl |
1.1 |
} |
636 |
|
|
|
637 |
|
|
/** |
638 |
jsr166 |
1.19 |
* toArray(a) contains all elements in FIFO order |
639 |
dl |
1.1 |
*/ |
640 |
|
|
public void testToArray2() { |
641 |
jsr166 |
1.20 |
ArrayDeque<Integer> q = populatedDeque(SIZE); |
642 |
jsr166 |
1.8 |
Integer[] ints = new Integer[SIZE]; |
643 |
jsr166 |
1.20 |
Integer[] array = q.toArray(ints); |
644 |
|
|
assertSame(ints, array); |
645 |
jsr166 |
1.6 |
for (int i = 0; i < ints.length; i++) |
646 |
jsr166 |
1.19 |
assertSame(ints[i], q.remove()); |
647 |
dl |
1.1 |
} |
648 |
|
|
|
649 |
|
|
/** |
650 |
jsr166 |
1.18 |
* toArray(null) throws NullPointerException |
651 |
dl |
1.1 |
*/ |
652 |
jsr166 |
1.18 |
public void testToArray_NullArg() { |
653 |
jsr166 |
1.12 |
ArrayDeque l = new ArrayDeque(); |
654 |
|
|
l.add(new Object()); |
655 |
jsr166 |
1.8 |
try { |
656 |
jsr166 |
1.18 |
l.toArray(null); |
657 |
jsr166 |
1.8 |
shouldThrow(); |
658 |
|
|
} catch (NullPointerException success) {} |
659 |
dl |
1.1 |
} |
660 |
|
|
|
661 |
|
|
/** |
662 |
jsr166 |
1.17 |
* toArray(incompatible array type) throws ArrayStoreException |
663 |
dl |
1.1 |
*/ |
664 |
|
|
public void testToArray1_BadArg() { |
665 |
jsr166 |
1.12 |
ArrayDeque l = new ArrayDeque(); |
666 |
|
|
l.add(new Integer(5)); |
667 |
jsr166 |
1.8 |
try { |
668 |
jsr166 |
1.17 |
l.toArray(new String[10]); |
669 |
jsr166 |
1.8 |
shouldThrow(); |
670 |
jsr166 |
1.9 |
} catch (ArrayStoreException success) {} |
671 |
dl |
1.1 |
} |
672 |
jsr166 |
1.5 |
|
673 |
dl |
1.1 |
/** |
674 |
jsr166 |
1.16 |
* Iterator iterates through all elements |
675 |
dl |
1.1 |
*/ |
676 |
|
|
public void testIterator() { |
677 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
678 |
|
|
int i = 0; |
679 |
jsr166 |
1.8 |
Iterator it = q.iterator(); |
680 |
jsr166 |
1.6 |
while (it.hasNext()) { |
681 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
682 |
|
|
++i; |
683 |
|
|
} |
684 |
|
|
assertEquals(i, SIZE); |
685 |
|
|
} |
686 |
|
|
|
687 |
|
|
/** |
688 |
jsr166 |
1.16 |
* Iterator ordering is FIFO |
689 |
dl |
1.1 |
*/ |
690 |
|
|
public void testIteratorOrdering() { |
691 |
|
|
final ArrayDeque q = new ArrayDeque(); |
692 |
jsr166 |
1.16 |
q.add(one); |
693 |
|
|
q.add(two); |
694 |
|
|
q.add(three); |
695 |
dl |
1.1 |
int k = 0; |
696 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
697 |
jsr166 |
1.12 |
assertEquals(++k, it.next()); |
698 |
dl |
1.1 |
} |
699 |
|
|
|
700 |
|
|
assertEquals(3, k); |
701 |
|
|
} |
702 |
|
|
|
703 |
|
|
/** |
704 |
jsr166 |
1.16 |
* iterator.remove() removes current element |
705 |
dl |
1.1 |
*/ |
706 |
jsr166 |
1.16 |
public void testIteratorRemove() { |
707 |
dl |
1.1 |
final ArrayDeque q = new ArrayDeque(); |
708 |
dl |
1.4 |
final Random rng = new Random(); |
709 |
dl |
1.3 |
for (int iters = 0; iters < 100; ++iters) { |
710 |
dl |
1.4 |
int max = rng.nextInt(5) + 2; |
711 |
|
|
int split = rng.nextInt(max-1) + 1; |
712 |
|
|
for (int j = 1; j <= max; ++j) |
713 |
|
|
q.add(new Integer(j)); |
714 |
dl |
1.3 |
Iterator it = q.iterator(); |
715 |
jsr166 |
1.5 |
for (int j = 1; j <= split; ++j) |
716 |
dl |
1.4 |
assertEquals(it.next(), new Integer(j)); |
717 |
dl |
1.3 |
it.remove(); |
718 |
dl |
1.4 |
assertEquals(it.next(), new Integer(split+1)); |
719 |
jsr166 |
1.5 |
for (int j = 1; j <= split; ++j) |
720 |
dl |
1.4 |
q.remove(new Integer(j)); |
721 |
dl |
1.3 |
it = q.iterator(); |
722 |
dl |
1.4 |
for (int j = split+1; j <= max; ++j) { |
723 |
|
|
assertEquals(it.next(), new Integer(j)); |
724 |
|
|
it.remove(); |
725 |
|
|
} |
726 |
dl |
1.3 |
assertFalse(it.hasNext()); |
727 |
dl |
1.4 |
assertTrue(q.isEmpty()); |
728 |
dl |
1.3 |
} |
729 |
dl |
1.1 |
} |
730 |
|
|
|
731 |
dl |
1.2 |
/** |
732 |
jsr166 |
1.16 |
* Descending iterator iterates through all elements |
733 |
dl |
1.2 |
*/ |
734 |
|
|
public void testDescendingIterator() { |
735 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
736 |
|
|
int i = 0; |
737 |
jsr166 |
1.8 |
Iterator it = q.descendingIterator(); |
738 |
jsr166 |
1.6 |
while (it.hasNext()) { |
739 |
dl |
1.2 |
assertTrue(q.contains(it.next())); |
740 |
|
|
++i; |
741 |
|
|
} |
742 |
|
|
assertEquals(i, SIZE); |
743 |
|
|
assertFalse(it.hasNext()); |
744 |
|
|
try { |
745 |
|
|
it.next(); |
746 |
jsr166 |
1.11 |
shouldThrow(); |
747 |
jsr166 |
1.9 |
} catch (NoSuchElementException success) {} |
748 |
dl |
1.2 |
} |
749 |
|
|
|
750 |
|
|
/** |
751 |
jsr166 |
1.16 |
* Descending iterator ordering is reverse FIFO |
752 |
dl |
1.2 |
*/ |
753 |
|
|
public void testDescendingIteratorOrdering() { |
754 |
|
|
final ArrayDeque q = new ArrayDeque(); |
755 |
dl |
1.3 |
for (int iters = 0; iters < 100; ++iters) { |
756 |
|
|
q.add(new Integer(3)); |
757 |
|
|
q.add(new Integer(2)); |
758 |
|
|
q.add(new Integer(1)); |
759 |
|
|
int k = 0; |
760 |
|
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
761 |
jsr166 |
1.12 |
assertEquals(++k, it.next()); |
762 |
dl |
1.3 |
} |
763 |
jsr166 |
1.5 |
|
764 |
dl |
1.3 |
assertEquals(3, k); |
765 |
|
|
q.remove(); |
766 |
|
|
q.remove(); |
767 |
|
|
q.remove(); |
768 |
dl |
1.2 |
} |
769 |
|
|
} |
770 |
|
|
|
771 |
|
|
/** |
772 |
jsr166 |
1.16 |
* descendingIterator.remove() removes current element |
773 |
dl |
1.2 |
*/ |
774 |
jsr166 |
1.16 |
public void testDescendingIteratorRemove() { |
775 |
dl |
1.2 |
final ArrayDeque q = new ArrayDeque(); |
776 |
dl |
1.4 |
final Random rng = new Random(); |
777 |
dl |
1.3 |
for (int iters = 0; iters < 100; ++iters) { |
778 |
dl |
1.4 |
int max = rng.nextInt(5) + 2; |
779 |
|
|
int split = rng.nextInt(max-1) + 1; |
780 |
|
|
for (int j = max; j >= 1; --j) |
781 |
|
|
q.add(new Integer(j)); |
782 |
dl |
1.3 |
Iterator it = q.descendingIterator(); |
783 |
jsr166 |
1.5 |
for (int j = 1; j <= split; ++j) |
784 |
dl |
1.4 |
assertEquals(it.next(), new Integer(j)); |
785 |
dl |
1.3 |
it.remove(); |
786 |
dl |
1.4 |
assertEquals(it.next(), new Integer(split+1)); |
787 |
jsr166 |
1.5 |
for (int j = 1; j <= split; ++j) |
788 |
dl |
1.4 |
q.remove(new Integer(j)); |
789 |
dl |
1.3 |
it = q.descendingIterator(); |
790 |
dl |
1.4 |
for (int j = split+1; j <= max; ++j) { |
791 |
|
|
assertEquals(it.next(), new Integer(j)); |
792 |
|
|
it.remove(); |
793 |
|
|
} |
794 |
dl |
1.3 |
assertFalse(it.hasNext()); |
795 |
dl |
1.4 |
assertTrue(q.isEmpty()); |
796 |
dl |
1.3 |
} |
797 |
dl |
1.2 |
} |
798 |
|
|
|
799 |
dl |
1.1 |
/** |
800 |
jsr166 |
1.16 |
* toString() contains toStrings of elements |
801 |
dl |
1.1 |
*/ |
802 |
|
|
public void testToString() { |
803 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
804 |
|
|
String s = q.toString(); |
805 |
|
|
for (int i = 0; i < SIZE; ++i) { |
806 |
jsr166 |
1.23 |
assertTrue(s.contains(String.valueOf(i))); |
807 |
dl |
1.1 |
} |
808 |
jsr166 |
1.5 |
} |
809 |
dl |
1.1 |
|
810 |
|
|
/** |
811 |
jsr166 |
1.16 |
* A deserialized serialized deque has same elements in same order |
812 |
dl |
1.1 |
*/ |
813 |
jsr166 |
1.16 |
public void testSerialization() throws Exception { |
814 |
|
|
ArrayDeque q = populatedDeque(SIZE); |
815 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
816 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
817 |
|
|
out.writeObject(q); |
818 |
|
|
out.close(); |
819 |
|
|
|
820 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
821 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
822 |
|
|
ArrayDeque r = (ArrayDeque)in.readObject(); |
823 |
|
|
assertEquals(q.size(), r.size()); |
824 |
|
|
while (!q.isEmpty()) |
825 |
|
|
assertEquals(q.remove(), r.remove()); |
826 |
jsr166 |
1.5 |
} |
827 |
dl |
1.1 |
|
828 |
|
|
} |