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