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