author | dl |
Wed, 09 Aug 2017 17:40:38 -0700 | |
changeset 46146 | b3e220a04d3f |
parent 44589 | 64d9270bd24c |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
|
33 |
* Pat Fisher, Mike Judd. |
|
34 |
*/ |
|
35 |
||
36 |
import java.util.ArrayList; |
|
37 |
import java.util.Arrays; |
|
38 |
import java.util.Collection; |
|
39 |
import java.util.Iterator; |
|
40 |
import java.util.LinkedList; |
|
41 |
import java.util.List; |
|
42 |
import java.util.ListIterator; |
|
43 |
import java.util.NoSuchElementException; |
|
44 |
import java.util.concurrent.CopyOnWriteArrayList; |
|
45 |
||
46 |
import junit.framework.Test; |
|
47 |
||
48 |
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
|
49 |
||
50 |
public static void main(String[] args) { |
|
51 |
main(suite(), args); |
|
52 |
} |
|
53 |
||
54 |
public static Test suite() { |
|
42319 | 55 |
class Implementation implements CollectionImplementation { |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
56 |
public Class<?> klazz() { return CopyOnWriteArrayList.class; } |
42319 | 57 |
public List emptyCollection() { return new CopyOnWriteArrayList(); } |
58 |
public Object makeElement(int i) { return i; } |
|
59 |
public boolean isConcurrent() { return true; } |
|
60 |
public boolean permitsNulls() { return true; } |
|
61 |
} |
|
62 |
class SubListImplementation extends Implementation { |
|
63 |
public List emptyCollection() { |
|
64 |
return super.emptyCollection().subList(0, 0); |
|
65 |
} |
|
66 |
} |
|
67 |
return newTestSuite( |
|
68 |
CopyOnWriteArrayListTest.class, |
|
69 |
CollectionTest.testSuite(new Implementation()), |
|
70 |
CollectionTest.testSuite(new SubListImplementation())); |
|
35394 | 71 |
} |
72 |
||
73 |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
74 |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); |
35394 | 75 |
assertTrue(a.isEmpty()); |
76 |
for (int i = 0; i < n; i++) |
|
77 |
a.add(i); |
|
78 |
assertFalse(a.isEmpty()); |
|
79 |
assertEquals(n, a.size()); |
|
80 |
return a; |
|
81 |
} |
|
82 |
||
83 |
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
84 |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); |
35394 | 85 |
assertTrue(a.isEmpty()); |
86 |
for (int i = 0; i < elements.length; i++) |
|
87 |
a.add(elements[i]); |
|
88 |
assertFalse(a.isEmpty()); |
|
89 |
assertEquals(elements.length, a.size()); |
|
90 |
return a; |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* a new list is empty |
|
95 |
*/ |
|
96 |
public void testConstructor() { |
|
97 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
|
98 |
assertTrue(a.isEmpty()); |
|
99 |
} |
|
100 |
||
101 |
/** |
|
102 |
* new list contains all elements of initializing array |
|
103 |
*/ |
|
104 |
public void testConstructor2() { |
|
105 |
Integer[] ints = new Integer[SIZE]; |
|
106 |
for (int i = 0; i < SIZE - 1; ++i) |
|
107 |
ints[i] = new Integer(i); |
|
108 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
|
109 |
for (int i = 0; i < SIZE; ++i) |
|
110 |
assertEquals(ints[i], a.get(i)); |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* new list contains all elements of initializing collection |
|
115 |
*/ |
|
116 |
public void testConstructor3() { |
|
117 |
Integer[] ints = new Integer[SIZE]; |
|
118 |
for (int i = 0; i < SIZE - 1; ++i) |
|
119 |
ints[i] = new Integer(i); |
|
120 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
|
121 |
for (int i = 0; i < SIZE; ++i) |
|
122 |
assertEquals(ints[i], a.get(i)); |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* addAll adds each element from the given collection, including duplicates |
|
127 |
*/ |
|
128 |
public void testAddAll() { |
|
129 |
CopyOnWriteArrayList full = populatedArray(3); |
|
130 |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
|
131 |
assertEquals(6, full.size()); |
|
132 |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
|
133 |
assertEquals(9, full.size()); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* addAllAbsent adds each element from the given collection that did not |
|
138 |
* already exist in the List |
|
139 |
*/ |
|
140 |
public void testAddAllAbsent() { |
|
141 |
CopyOnWriteArrayList full = populatedArray(3); |
|
142 |
// "one" is duplicate and will not be added |
|
143 |
assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one))); |
|
144 |
assertEquals(5, full.size()); |
|
145 |
assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one))); |
|
146 |
assertEquals(5, full.size()); |
|
147 |
} |
|
148 |
||
149 |
/** |
|
150 |
* addIfAbsent will not add the element if it already exists in the list |
|
151 |
*/ |
|
152 |
public void testAddIfAbsent() { |
|
153 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
|
154 |
full.addIfAbsent(one); |
|
155 |
assertEquals(SIZE, full.size()); |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* addIfAbsent adds the element when it does not exist in the list |
|
160 |
*/ |
|
161 |
public void testAddIfAbsent2() { |
|
162 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
|
163 |
full.addIfAbsent(three); |
|
164 |
assertTrue(full.contains(three)); |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* clear removes all elements from the list |
|
169 |
*/ |
|
170 |
public void testClear() { |
|
171 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
|
172 |
full.clear(); |
|
173 |
assertEquals(0, full.size()); |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* Cloned list is equal |
|
178 |
*/ |
|
179 |
public void testClone() { |
|
180 |
CopyOnWriteArrayList l1 = populatedArray(SIZE); |
|
181 |
CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone()); |
|
182 |
assertEquals(l1, l2); |
|
183 |
l1.clear(); |
|
184 |
assertFalse(l1.equals(l2)); |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* contains is true for added elements |
|
189 |
*/ |
|
190 |
public void testContains() { |
|
191 |
CopyOnWriteArrayList full = populatedArray(3); |
|
192 |
assertTrue(full.contains(one)); |
|
193 |
assertFalse(full.contains(five)); |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* adding at an index places it in the indicated index |
|
198 |
*/ |
|
199 |
public void testAddIndex() { |
|
200 |
CopyOnWriteArrayList full = populatedArray(3); |
|
201 |
full.add(0, m1); |
|
202 |
assertEquals(4, full.size()); |
|
203 |
assertEquals(m1, full.get(0)); |
|
204 |
assertEquals(zero, full.get(1)); |
|
205 |
||
206 |
full.add(2, m2); |
|
207 |
assertEquals(5, full.size()); |
|
208 |
assertEquals(m2, full.get(2)); |
|
209 |
assertEquals(two, full.get(4)); |
|
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
* lists with same elements are equal and have same hashCode |
|
214 |
*/ |
|
215 |
public void testEquals() { |
|
216 |
CopyOnWriteArrayList a = populatedArray(3); |
|
217 |
CopyOnWriteArrayList b = populatedArray(3); |
|
218 |
assertTrue(a.equals(b)); |
|
219 |
assertTrue(b.equals(a)); |
|
220 |
assertTrue(a.containsAll(b)); |
|
221 |
assertTrue(b.containsAll(a)); |
|
222 |
assertEquals(a.hashCode(), b.hashCode()); |
|
223 |
a.add(m1); |
|
224 |
assertFalse(a.equals(b)); |
|
225 |
assertFalse(b.equals(a)); |
|
226 |
assertTrue(a.containsAll(b)); |
|
227 |
assertFalse(b.containsAll(a)); |
|
228 |
b.add(m1); |
|
229 |
assertTrue(a.equals(b)); |
|
230 |
assertTrue(b.equals(a)); |
|
231 |
assertTrue(a.containsAll(b)); |
|
232 |
assertTrue(b.containsAll(a)); |
|
233 |
assertEquals(a.hashCode(), b.hashCode()); |
|
234 |
||
235 |
assertFalse(a.equals(null)); |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* containsAll returns true for collections with subset of elements |
|
240 |
*/ |
|
241 |
public void testContainsAll() { |
|
242 |
CopyOnWriteArrayList full = populatedArray(3); |
|
243 |
assertTrue(full.containsAll(Arrays.asList())); |
|
244 |
assertTrue(full.containsAll(Arrays.asList(one))); |
|
245 |
assertTrue(full.containsAll(Arrays.asList(one, two))); |
|
246 |
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
|
247 |
assertFalse(full.containsAll(Arrays.asList(six))); |
|
248 |
||
249 |
try { |
|
250 |
full.containsAll(null); |
|
251 |
shouldThrow(); |
|
252 |
} catch (NullPointerException success) {} |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* get returns the value at the given index |
|
257 |
*/ |
|
258 |
public void testGet() { |
|
259 |
CopyOnWriteArrayList full = populatedArray(3); |
|
260 |
assertEquals(0, full.get(0)); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* indexOf gives the index for the given object |
|
265 |
*/ |
|
266 |
public void testIndexOf() { |
|
267 |
CopyOnWriteArrayList full = populatedArray(3); |
|
268 |
assertEquals(1, full.indexOf(one)); |
|
269 |
assertEquals(-1, full.indexOf("puppies")); |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* indexOf gives the index based on the given index |
|
274 |
* at which to start searching |
|
275 |
*/ |
|
276 |
public void testIndexOf2() { |
|
277 |
CopyOnWriteArrayList full = populatedArray(3); |
|
278 |
assertEquals(1, full.indexOf(one, 0)); |
|
279 |
assertEquals(-1, full.indexOf(one, 2)); |
|
280 |
} |
|
281 |
||
282 |
/** |
|
283 |
* isEmpty returns true when empty, else false |
|
284 |
*/ |
|
285 |
public void testIsEmpty() { |
|
286 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
|
287 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
|
288 |
assertTrue(empty.isEmpty()); |
|
289 |
assertFalse(full.isEmpty()); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* iterator() returns an iterator containing the elements of the |
|
294 |
* list in insertion order |
|
295 |
*/ |
|
296 |
public void testIterator() { |
|
297 |
Collection empty = new CopyOnWriteArrayList(); |
|
298 |
assertFalse(empty.iterator().hasNext()); |
|
299 |
try { |
|
300 |
empty.iterator().next(); |
|
301 |
shouldThrow(); |
|
302 |
} catch (NoSuchElementException success) {} |
|
303 |
||
304 |
Integer[] elements = new Integer[SIZE]; |
|
305 |
for (int i = 0; i < SIZE; i++) |
|
306 |
elements[i] = i; |
|
40280
5542e4584972
8162627: Miscellaneous changes imported from jsr166 CVS 2016-08
dl
parents:
35394
diff
changeset
|
307 |
shuffle(elements); |
35394 | 308 |
Collection<Integer> full = populatedArray(elements); |
309 |
||
310 |
Iterator it = full.iterator(); |
|
311 |
for (int j = 0; j < SIZE; j++) { |
|
312 |
assertTrue(it.hasNext()); |
|
313 |
assertEquals(elements[j], it.next()); |
|
314 |
} |
|
315 |
assertIteratorExhausted(it); |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* iterator of empty collection has no elements |
|
320 |
*/ |
|
321 |
public void testEmptyIterator() { |
|
322 |
Collection c = new CopyOnWriteArrayList(); |
|
323 |
assertIteratorExhausted(c.iterator()); |
|
324 |
} |
|
325 |
||
326 |
/** |
|
327 |
* iterator.remove throws UnsupportedOperationException |
|
328 |
*/ |
|
329 |
public void testIteratorRemove() { |
|
330 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
|
331 |
Iterator it = full.iterator(); |
|
332 |
it.next(); |
|
333 |
try { |
|
334 |
it.remove(); |
|
335 |
shouldThrow(); |
|
336 |
} catch (UnsupportedOperationException success) {} |
|
337 |
} |
|
338 |
||
339 |
/** |
|
340 |
* toString contains toString of elements |
|
341 |
*/ |
|
342 |
public void testToString() { |
|
343 |
assertEquals("[]", new CopyOnWriteArrayList().toString()); |
|
344 |
CopyOnWriteArrayList full = populatedArray(3); |
|
345 |
String s = full.toString(); |
|
346 |
for (int i = 0; i < 3; ++i) |
|
347 |
assertTrue(s.contains(String.valueOf(i))); |
|
348 |
assertEquals(new ArrayList(full).toString(), |
|
349 |
full.toString()); |
|
350 |
} |
|
351 |
||
352 |
/** |
|
353 |
* lastIndexOf returns the index for the given object |
|
354 |
*/ |
|
355 |
public void testLastIndexOf1() { |
|
356 |
CopyOnWriteArrayList full = populatedArray(3); |
|
357 |
full.add(one); |
|
358 |
full.add(three); |
|
359 |
assertEquals(3, full.lastIndexOf(one)); |
|
360 |
assertEquals(-1, full.lastIndexOf(six)); |
|
361 |
} |
|
362 |
||
363 |
/** |
|
364 |
* lastIndexOf returns the index from the given starting point |
|
365 |
*/ |
|
366 |
public void testLastIndexOf2() { |
|
367 |
CopyOnWriteArrayList full = populatedArray(3); |
|
368 |
full.add(one); |
|
369 |
full.add(three); |
|
370 |
assertEquals(3, full.lastIndexOf(one, 4)); |
|
371 |
assertEquals(-1, full.lastIndexOf(three, 3)); |
|
372 |
} |
|
373 |
||
374 |
/** |
|
375 |
* listIterator traverses all elements |
|
376 |
*/ |
|
377 |
public void testListIterator1() { |
|
378 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
|
379 |
ListIterator i = full.listIterator(); |
|
380 |
int j; |
|
381 |
for (j = 0; i.hasNext(); j++) |
|
382 |
assertEquals(j, i.next()); |
|
383 |
assertEquals(SIZE, j); |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* listIterator only returns those elements after the given index |
|
388 |
*/ |
|
389 |
public void testListIterator2() { |
|
390 |
CopyOnWriteArrayList full = populatedArray(3); |
|
391 |
ListIterator i = full.listIterator(1); |
|
392 |
int j; |
|
393 |
for (j = 0; i.hasNext(); j++) |
|
394 |
assertEquals(j + 1, i.next()); |
|
395 |
assertEquals(2, j); |
|
396 |
} |
|
397 |
||
398 |
/** |
|
399 |
* remove(int) removes and returns the object at the given index |
|
400 |
*/ |
|
401 |
public void testRemove_int() { |
|
402 |
int SIZE = 3; |
|
403 |
for (int i = 0; i < SIZE; i++) { |
|
404 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
|
405 |
assertEquals(i, full.remove(i)); |
|
406 |
assertEquals(SIZE - 1, full.size()); |
|
407 |
assertFalse(full.contains(new Integer(i))); |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
/** |
|
412 |
* remove(Object) removes the object if found and returns true |
|
413 |
*/ |
|
414 |
public void testRemove_Object() { |
|
415 |
int SIZE = 3; |
|
416 |
for (int i = 0; i < SIZE; i++) { |
|
417 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
|
418 |
assertFalse(full.remove(new Integer(-42))); |
|
419 |
assertTrue(full.remove(new Integer(i))); |
|
420 |
assertEquals(SIZE - 1, full.size()); |
|
421 |
assertFalse(full.contains(new Integer(i))); |
|
422 |
} |
|
423 |
CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); |
|
424 |
assertTrue(x.remove(new Integer(6))); |
|
425 |
assertEquals(x, Arrays.asList(4, 5)); |
|
426 |
assertTrue(x.remove(new Integer(4))); |
|
427 |
assertEquals(x, Arrays.asList(5)); |
|
428 |
assertTrue(x.remove(new Integer(5))); |
|
429 |
assertEquals(x, Arrays.asList()); |
|
430 |
assertFalse(x.remove(new Integer(5))); |
|
431 |
} |
|
432 |
||
433 |
/** |
|
434 |
* removeAll removes all elements from the given collection |
|
435 |
*/ |
|
436 |
public void testRemoveAll() { |
|
437 |
CopyOnWriteArrayList full = populatedArray(3); |
|
438 |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
|
439 |
assertEquals(1, full.size()); |
|
440 |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
|
441 |
assertEquals(1, full.size()); |
|
442 |
} |
|
443 |
||
444 |
/** |
|
445 |
* set changes the element at the given index |
|
446 |
*/ |
|
447 |
public void testSet() { |
|
448 |
CopyOnWriteArrayList full = populatedArray(3); |
|
449 |
assertEquals(2, full.set(2, four)); |
|
450 |
assertEquals(4, full.get(2)); |
|
451 |
} |
|
452 |
||
453 |
/** |
|
454 |
* size returns the number of elements |
|
455 |
*/ |
|
456 |
public void testSize() { |
|
457 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
|
458 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
|
459 |
assertEquals(SIZE, full.size()); |
|
460 |
assertEquals(0, empty.size()); |
|
461 |
} |
|
462 |
||
463 |
/** |
|
464 |
* toArray() returns an Object array containing all elements from |
|
465 |
* the list in insertion order |
|
466 |
*/ |
|
467 |
public void testToArray() { |
|
468 |
Object[] a = new CopyOnWriteArrayList().toArray(); |
|
469 |
assertTrue(Arrays.equals(new Object[0], a)); |
|
470 |
assertSame(Object[].class, a.getClass()); |
|
471 |
||
472 |
Integer[] elements = new Integer[SIZE]; |
|
473 |
for (int i = 0; i < SIZE; i++) |
|
474 |
elements[i] = i; |
|
40280
5542e4584972
8162627: Miscellaneous changes imported from jsr166 CVS 2016-08
dl
parents:
35394
diff
changeset
|
475 |
shuffle(elements); |
35394 | 476 |
Collection<Integer> full = populatedArray(elements); |
477 |
||
478 |
assertTrue(Arrays.equals(elements, full.toArray())); |
|
479 |
assertSame(Object[].class, full.toArray().getClass()); |
|
480 |
} |
|
481 |
||
482 |
/** |
|
483 |
* toArray(Integer array) returns an Integer array containing all |
|
484 |
* elements from the list in insertion order |
|
485 |
*/ |
|
486 |
public void testToArray2() { |
|
487 |
Collection empty = new CopyOnWriteArrayList(); |
|
488 |
Integer[] a; |
|
489 |
||
490 |
a = new Integer[0]; |
|
491 |
assertSame(a, empty.toArray(a)); |
|
492 |
||
493 |
a = new Integer[SIZE / 2]; |
|
494 |
Arrays.fill(a, 42); |
|
495 |
assertSame(a, empty.toArray(a)); |
|
496 |
assertNull(a[0]); |
|
497 |
for (int i = 1; i < a.length; i++) |
|
498 |
assertEquals(42, (int) a[i]); |
|
499 |
||
500 |
Integer[] elements = new Integer[SIZE]; |
|
501 |
for (int i = 0; i < SIZE; i++) |
|
502 |
elements[i] = i; |
|
40280
5542e4584972
8162627: Miscellaneous changes imported from jsr166 CVS 2016-08
dl
parents:
35394
diff
changeset
|
503 |
shuffle(elements); |
35394 | 504 |
Collection<Integer> full = populatedArray(elements); |
505 |
||
506 |
Arrays.fill(a, 42); |
|
507 |
assertTrue(Arrays.equals(elements, full.toArray(a))); |
|
508 |
for (int i = 0; i < a.length; i++) |
|
509 |
assertEquals(42, (int) a[i]); |
|
510 |
assertSame(Integer[].class, full.toArray(a).getClass()); |
|
511 |
||
512 |
a = new Integer[SIZE]; |
|
513 |
Arrays.fill(a, 42); |
|
514 |
assertSame(a, full.toArray(a)); |
|
515 |
assertTrue(Arrays.equals(elements, a)); |
|
516 |
||
517 |
a = new Integer[2 * SIZE]; |
|
518 |
Arrays.fill(a, 42); |
|
519 |
assertSame(a, full.toArray(a)); |
|
520 |
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
|
521 |
assertNull(a[SIZE]); |
|
522 |
for (int i = SIZE + 1; i < a.length; i++) |
|
523 |
assertEquals(42, (int) a[i]); |
|
524 |
} |
|
525 |
||
526 |
/** |
|
527 |
* sublists contains elements at indexes offset from their base |
|
528 |
*/ |
|
529 |
public void testSubList() { |
|
530 |
CopyOnWriteArrayList a = populatedArray(10); |
|
531 |
assertTrue(a.subList(1,1).isEmpty()); |
|
532 |
for (int j = 0; j < 9; ++j) { |
|
533 |
for (int i = j ; i < 10; ++i) { |
|
534 |
List b = a.subList(j,i); |
|
535 |
for (int k = j; k < i; ++k) { |
|
536 |
assertEquals(new Integer(k), b.get(k-j)); |
|
537 |
} |
|
538 |
} |
|
539 |
} |
|
540 |
||
541 |
List s = a.subList(2, 5); |
|
542 |
assertEquals(3, s.size()); |
|
543 |
s.set(2, m1); |
|
544 |
assertEquals(a.get(4), m1); |
|
545 |
s.clear(); |
|
546 |
assertEquals(7, a.size()); |
|
547 |
} |
|
548 |
||
549 |
// Exception tests |
|
550 |
||
551 |
/** |
|
552 |
* toArray throws an ArrayStoreException when the given array |
|
553 |
* can not store the objects inside the list |
|
554 |
*/ |
|
555 |
public void testToArray_ArrayStoreException() { |
|
556 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
|
557 |
c.add("zfasdfsdf"); |
|
558 |
c.add("asdadasd"); |
|
559 |
try { |
|
560 |
c.toArray(new Long[5]); |
|
561 |
shouldThrow(); |
|
562 |
} catch (ArrayStoreException success) {} |
|
563 |
} |
|
564 |
||
565 |
/** |
|
566 |
* get throws an IndexOutOfBoundsException on a negative index |
|
567 |
*/ |
|
568 |
public void testGet1_IndexOutOfBoundsException() { |
|
569 |
CopyOnWriteArrayList c = populatedArray(5); |
|
570 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
571 |
for (List list : lists) { |
|
572 |
try { |
|
573 |
list.get(-1); |
|
574 |
shouldThrow(); |
|
575 |
} catch (IndexOutOfBoundsException success) {} |
|
576 |
} |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* get throws an IndexOutOfBoundsException on a too high index |
|
581 |
*/ |
|
582 |
public void testGet2_IndexOutOfBoundsException() { |
|
583 |
CopyOnWriteArrayList c = populatedArray(5); |
|
584 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
585 |
for (List list : lists) { |
|
586 |
try { |
|
587 |
list.get(list.size()); |
|
588 |
shouldThrow(); |
|
589 |
} catch (IndexOutOfBoundsException success) {} |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
/** |
|
594 |
* set throws an IndexOutOfBoundsException on a negative index |
|
595 |
*/ |
|
596 |
public void testSet1_IndexOutOfBoundsException() { |
|
597 |
CopyOnWriteArrayList c = populatedArray(5); |
|
598 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
599 |
for (List list : lists) { |
|
600 |
try { |
|
601 |
list.set(-1, "qwerty"); |
|
602 |
shouldThrow(); |
|
603 |
} catch (IndexOutOfBoundsException success) {} |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
/** |
|
608 |
* set throws an IndexOutOfBoundsException on a too high index |
|
609 |
*/ |
|
610 |
public void testSet2() { |
|
611 |
CopyOnWriteArrayList c = populatedArray(5); |
|
612 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
613 |
for (List list : lists) { |
|
614 |
try { |
|
615 |
list.set(list.size(), "qwerty"); |
|
616 |
shouldThrow(); |
|
617 |
} catch (IndexOutOfBoundsException success) {} |
|
618 |
} |
|
619 |
} |
|
620 |
||
621 |
/** |
|
622 |
* add throws an IndexOutOfBoundsException on a negative index |
|
623 |
*/ |
|
624 |
public void testAdd1_IndexOutOfBoundsException() { |
|
625 |
CopyOnWriteArrayList c = populatedArray(5); |
|
626 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
627 |
for (List list : lists) { |
|
628 |
try { |
|
629 |
list.add(-1, "qwerty"); |
|
630 |
shouldThrow(); |
|
631 |
} catch (IndexOutOfBoundsException success) {} |
|
632 |
} |
|
633 |
} |
|
634 |
||
635 |
/** |
|
636 |
* add throws an IndexOutOfBoundsException on a too high index |
|
637 |
*/ |
|
638 |
public void testAdd2_IndexOutOfBoundsException() { |
|
639 |
CopyOnWriteArrayList c = populatedArray(5); |
|
640 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
641 |
for (List list : lists) { |
|
642 |
try { |
|
643 |
list.add(list.size() + 1, "qwerty"); |
|
644 |
shouldThrow(); |
|
645 |
} catch (IndexOutOfBoundsException success) {} |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
/** |
|
650 |
* remove throws an IndexOutOfBoundsException on a negative index |
|
651 |
*/ |
|
652 |
public void testRemove1_IndexOutOfBounds() { |
|
653 |
CopyOnWriteArrayList c = populatedArray(5); |
|
654 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
655 |
for (List list : lists) { |
|
656 |
try { |
|
657 |
list.remove(-1); |
|
658 |
shouldThrow(); |
|
659 |
} catch (IndexOutOfBoundsException success) {} |
|
660 |
} |
|
661 |
} |
|
662 |
||
663 |
/** |
|
664 |
* remove throws an IndexOutOfBoundsException on a too high index |
|
665 |
*/ |
|
666 |
public void testRemove2_IndexOutOfBounds() { |
|
667 |
CopyOnWriteArrayList c = populatedArray(5); |
|
668 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
669 |
for (List list : lists) { |
|
670 |
try { |
|
671 |
list.remove(list.size()); |
|
672 |
shouldThrow(); |
|
673 |
} catch (IndexOutOfBoundsException success) {} |
|
674 |
} |
|
675 |
} |
|
676 |
||
677 |
/** |
|
678 |
* addAll throws an IndexOutOfBoundsException on a negative index |
|
679 |
*/ |
|
680 |
public void testAddAll1_IndexOutOfBoundsException() { |
|
681 |
CopyOnWriteArrayList c = populatedArray(5); |
|
682 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
683 |
for (List list : lists) { |
|
684 |
try { |
|
685 |
list.addAll(-1, new LinkedList()); |
|
686 |
shouldThrow(); |
|
687 |
} catch (IndexOutOfBoundsException success) {} |
|
688 |
} |
|
689 |
} |
|
690 |
||
691 |
/** |
|
692 |
* addAll throws an IndexOutOfBoundsException on a too high index |
|
693 |
*/ |
|
694 |
public void testAddAll2_IndexOutOfBoundsException() { |
|
695 |
CopyOnWriteArrayList c = populatedArray(5); |
|
696 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
697 |
for (List list : lists) { |
|
698 |
try { |
|
699 |
list.addAll(list.size() + 1, new LinkedList()); |
|
700 |
shouldThrow(); |
|
701 |
} catch (IndexOutOfBoundsException success) {} |
|
702 |
} |
|
703 |
} |
|
704 |
||
705 |
/** |
|
706 |
* listIterator throws an IndexOutOfBoundsException on a negative index |
|
707 |
*/ |
|
708 |
public void testListIterator1_IndexOutOfBoundsException() { |
|
709 |
CopyOnWriteArrayList c = populatedArray(5); |
|
710 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
711 |
for (List list : lists) { |
|
712 |
try { |
|
713 |
list.listIterator(-1); |
|
714 |
shouldThrow(); |
|
715 |
} catch (IndexOutOfBoundsException success) {} |
|
716 |
} |
|
717 |
} |
|
718 |
||
719 |
/** |
|
720 |
* listIterator throws an IndexOutOfBoundsException on a too high index |
|
721 |
*/ |
|
722 |
public void testListIterator2_IndexOutOfBoundsException() { |
|
723 |
CopyOnWriteArrayList c = populatedArray(5); |
|
724 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
725 |
for (List list : lists) { |
|
726 |
try { |
|
727 |
list.listIterator(list.size() + 1); |
|
728 |
shouldThrow(); |
|
729 |
} catch (IndexOutOfBoundsException success) {} |
|
730 |
} |
|
731 |
} |
|
732 |
||
733 |
/** |
|
734 |
* subList throws an IndexOutOfBoundsException on a negative index |
|
735 |
*/ |
|
736 |
public void testSubList1_IndexOutOfBoundsException() { |
|
737 |
CopyOnWriteArrayList c = populatedArray(5); |
|
738 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
739 |
for (List list : lists) { |
|
740 |
try { |
|
741 |
list.subList(-1, list.size()); |
|
742 |
shouldThrow(); |
|
743 |
} catch (IndexOutOfBoundsException success) {} |
|
744 |
} |
|
745 |
} |
|
746 |
||
747 |
/** |
|
748 |
* subList throws an IndexOutOfBoundsException on a too high index |
|
749 |
*/ |
|
750 |
public void testSubList2_IndexOutOfBoundsException() { |
|
751 |
CopyOnWriteArrayList c = populatedArray(5); |
|
752 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
753 |
for (List list : lists) { |
|
754 |
try { |
|
755 |
list.subList(0, list.size() + 1); |
|
756 |
shouldThrow(); |
|
757 |
} catch (IndexOutOfBoundsException success) {} |
|
758 |
} |
|
759 |
} |
|
760 |
||
761 |
/** |
|
762 |
* subList throws IndexOutOfBoundsException when the second index |
|
763 |
* is lower then the first |
|
764 |
*/ |
|
765 |
public void testSubList3_IndexOutOfBoundsException() { |
|
766 |
CopyOnWriteArrayList c = populatedArray(5); |
|
767 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
|
768 |
for (List list : lists) { |
|
769 |
try { |
|
770 |
list.subList(list.size() - 1, 1); |
|
771 |
shouldThrow(); |
|
772 |
} catch (IndexOutOfBoundsException success) {} |
|
773 |
} |
|
774 |
} |
|
775 |
||
776 |
/** |
|
46146
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
44589
diff
changeset
|
777 |
* a deserialized/reserialized list equals original |
35394 | 778 |
*/ |
779 |
public void testSerialization() throws Exception { |
|
780 |
List x = populatedArray(SIZE); |
|
781 |
List y = serialClone(x); |
|
782 |
||
783 |
assertNotSame(x, y); |
|
784 |
assertEquals(x.size(), y.size()); |
|
785 |
assertEquals(x.toString(), y.toString()); |
|
786 |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
|
787 |
assertEquals(x, y); |
|
788 |
assertEquals(y, x); |
|
789 |
while (!x.isEmpty()) { |
|
790 |
assertFalse(y.isEmpty()); |
|
791 |
assertEquals(x.remove(0), y.remove(0)); |
|
792 |
} |
|
793 |
assertTrue(y.isEmpty()); |
|
794 |
} |
|
795 |
||
796 |
} |