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 |
|
|
import junit.framework.*; |
10 |
jsr166 |
1.27 |
import java.util.ArrayList; |
11 |
jsr166 |
1.22 |
import java.util.Arrays; |
12 |
jsr166 |
1.26 |
import java.util.Collection; |
13 |
|
|
import java.util.Collections; |
14 |
jsr166 |
1.22 |
import java.util.Iterator; |
15 |
|
|
import java.util.LinkedList; |
16 |
|
|
import java.util.List; |
17 |
|
|
import java.util.ListIterator; |
18 |
jsr166 |
1.27 |
import java.util.NoSuchElementException; |
19 |
jsr166 |
1.22 |
import java.util.Vector; |
20 |
|
|
import java.util.concurrent.CopyOnWriteArrayList; |
21 |
dl |
1.1 |
|
22 |
jsr166 |
1.10 |
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
23 |
jsr166 |
1.8 |
|
24 |
dl |
1.1 |
public static void main(String[] args) { |
25 |
jsr166 |
1.17 |
junit.textui.TestRunner.run(suite()); |
26 |
dl |
1.1 |
} |
27 |
|
|
|
28 |
|
|
public static Test suite() { |
29 |
jsr166 |
1.11 |
return new TestSuite(CopyOnWriteArrayListTest.class); |
30 |
dl |
1.1 |
} |
31 |
|
|
|
32 |
jsr166 |
1.25 |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
33 |
jsr166 |
1.26 |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
34 |
dl |
1.1 |
assertTrue(a.isEmpty()); |
35 |
jsr166 |
1.26 |
for (int i = 0; i < n; i++) |
36 |
jsr166 |
1.25 |
a.add(i); |
37 |
dl |
1.1 |
assertFalse(a.isEmpty()); |
38 |
|
|
assertEquals(n, a.size()); |
39 |
|
|
return a; |
40 |
|
|
} |
41 |
|
|
|
42 |
jsr166 |
1.26 |
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
43 |
|
|
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
44 |
|
|
assertTrue(a.isEmpty()); |
45 |
|
|
for (int i = 0; i < elements.length; i++) |
46 |
|
|
a.add(elements[i]); |
47 |
|
|
assertFalse(a.isEmpty()); |
48 |
|
|
assertEquals(elements.length, a.size()); |
49 |
|
|
return a; |
50 |
|
|
} |
51 |
|
|
|
52 |
dl |
1.4 |
/** |
53 |
dl |
1.5 |
* a new list is empty |
54 |
dl |
1.4 |
*/ |
55 |
dl |
1.3 |
public void testConstructor() { |
56 |
jsr166 |
1.11 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
57 |
dl |
1.3 |
assertTrue(a.isEmpty()); |
58 |
|
|
} |
59 |
|
|
|
60 |
dl |
1.4 |
/** |
61 |
dl |
1.5 |
* new list contains all elements of initializing array |
62 |
dl |
1.4 |
*/ |
63 |
dl |
1.3 |
public void testConstructor2() { |
64 |
|
|
Integer[] ints = new Integer[SIZE]; |
65 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
66 |
|
|
ints[i] = new Integer(i); |
67 |
jsr166 |
1.11 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
68 |
jsr166 |
1.8 |
for (int i = 0; i < SIZE; ++i) |
69 |
dl |
1.3 |
assertEquals(ints[i], a.get(i)); |
70 |
|
|
} |
71 |
|
|
|
72 |
dl |
1.4 |
/** |
73 |
dl |
1.5 |
* new list contains all elements of initializing collection |
74 |
dl |
1.4 |
*/ |
75 |
dl |
1.3 |
public void testConstructor3() { |
76 |
|
|
Integer[] ints = new Integer[SIZE]; |
77 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
78 |
|
|
ints[i] = new Integer(i); |
79 |
jsr166 |
1.11 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
80 |
jsr166 |
1.8 |
for (int i = 0; i < SIZE; ++i) |
81 |
dl |
1.3 |
assertEquals(ints[i], a.get(i)); |
82 |
|
|
} |
83 |
jsr166 |
1.8 |
|
84 |
dl |
1.1 |
/** |
85 |
jsr166 |
1.18 |
* addAll adds each element from the given collection |
86 |
dl |
1.1 |
*/ |
87 |
dl |
1.4 |
public void testAddAll() { |
88 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
89 |
|
|
Vector v = new Vector(); |
90 |
|
|
v.add(three); |
91 |
|
|
v.add(four); |
92 |
|
|
v.add(five); |
93 |
|
|
full.addAll(v); |
94 |
|
|
assertEquals(6, full.size()); |
95 |
dl |
1.1 |
} |
96 |
|
|
|
97 |
|
|
/** |
98 |
jsr166 |
1.18 |
* addAllAbsent adds each element from the given collection that did not |
99 |
|
|
* already exist in the List |
100 |
dl |
1.1 |
*/ |
101 |
dl |
1.4 |
public void testAddAllAbsent() { |
102 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
103 |
|
|
Vector v = new Vector(); |
104 |
|
|
v.add(three); |
105 |
|
|
v.add(four); |
106 |
|
|
v.add(one); // will not add this element |
107 |
|
|
full.addAllAbsent(v); |
108 |
|
|
assertEquals(5, full.size()); |
109 |
dl |
1.1 |
} |
110 |
|
|
|
111 |
|
|
/** |
112 |
jsr166 |
1.18 |
* addIfAbsent will not add the element if it already exists in the list |
113 |
dl |
1.1 |
*/ |
114 |
dl |
1.4 |
public void testAddIfAbsent() { |
115 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
116 |
|
|
full.addIfAbsent(one); |
117 |
|
|
assertEquals(SIZE, full.size()); |
118 |
dl |
1.1 |
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
jsr166 |
1.18 |
* addIfAbsent adds the element when it does not exist in the list |
122 |
dl |
1.1 |
*/ |
123 |
dl |
1.4 |
public void testAddIfAbsent2() { |
124 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
125 |
dl |
1.3 |
full.addIfAbsent(three); |
126 |
|
|
assertTrue(full.contains(three)); |
127 |
dl |
1.1 |
} |
128 |
|
|
|
129 |
|
|
/** |
130 |
jsr166 |
1.18 |
* clear removes all elements from the list |
131 |
dl |
1.1 |
*/ |
132 |
dl |
1.4 |
public void testClear() { |
133 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
134 |
|
|
full.clear(); |
135 |
|
|
assertEquals(0, full.size()); |
136 |
dl |
1.1 |
} |
137 |
|
|
|
138 |
dl |
1.4 |
/** |
139 |
jsr166 |
1.18 |
* Cloned list is equal |
140 |
dl |
1.4 |
*/ |
141 |
|
|
public void testClone() { |
142 |
jsr166 |
1.11 |
CopyOnWriteArrayList l1 = populatedArray(SIZE); |
143 |
|
|
CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone()); |
144 |
dl |
1.4 |
assertEquals(l1, l2); |
145 |
jsr166 |
1.11 |
l1.clear(); |
146 |
dl |
1.4 |
assertFalse(l1.equals(l2)); |
147 |
|
|
} |
148 |
|
|
|
149 |
dl |
1.1 |
/** |
150 |
jsr166 |
1.18 |
* contains is true for added elements |
151 |
dl |
1.1 |
*/ |
152 |
dl |
1.4 |
public void testContains() { |
153 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
154 |
|
|
assertTrue(full.contains(one)); |
155 |
|
|
assertFalse(full.contains(five)); |
156 |
dl |
1.1 |
} |
157 |
|
|
|
158 |
dl |
1.4 |
/** |
159 |
dl |
1.7 |
* adding at an index places it in the indicated index |
160 |
dl |
1.4 |
*/ |
161 |
dl |
1.1 |
public void testAddIndex() { |
162 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
163 |
dl |
1.3 |
full.add(0, m1); |
164 |
dl |
1.1 |
assertEquals(4, full.size()); |
165 |
dl |
1.3 |
assertEquals(m1, full.get(0)); |
166 |
|
|
assertEquals(zero, full.get(1)); |
167 |
dl |
1.1 |
|
168 |
dl |
1.3 |
full.add(2, m2); |
169 |
dl |
1.1 |
assertEquals(5, full.size()); |
170 |
dl |
1.3 |
assertEquals(m2, full.get(2)); |
171 |
|
|
assertEquals(two, full.get(4)); |
172 |
dl |
1.1 |
} |
173 |
|
|
|
174 |
dl |
1.4 |
/** |
175 |
dl |
1.5 |
* lists with same elements are equal and have same hashCode |
176 |
dl |
1.4 |
*/ |
177 |
dl |
1.1 |
public void testEquals() { |
178 |
jsr166 |
1.11 |
CopyOnWriteArrayList a = populatedArray(3); |
179 |
|
|
CopyOnWriteArrayList b = populatedArray(3); |
180 |
dl |
1.1 |
assertTrue(a.equals(b)); |
181 |
|
|
assertTrue(b.equals(a)); |
182 |
|
|
assertEquals(a.hashCode(), b.hashCode()); |
183 |
dl |
1.3 |
a.add(m1); |
184 |
dl |
1.1 |
assertFalse(a.equals(b)); |
185 |
|
|
assertFalse(b.equals(a)); |
186 |
dl |
1.3 |
b.add(m1); |
187 |
dl |
1.1 |
assertTrue(a.equals(b)); |
188 |
|
|
assertTrue(b.equals(a)); |
189 |
|
|
assertEquals(a.hashCode(), b.hashCode()); |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
/** |
193 |
jsr166 |
1.18 |
* containsAll returns true for collection with subset of elements |
194 |
dl |
1.1 |
*/ |
195 |
dl |
1.4 |
public void testContainsAll() { |
196 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
197 |
|
|
Vector v = new Vector(); |
198 |
|
|
v.add(one); |
199 |
|
|
v.add(two); |
200 |
|
|
assertTrue(full.containsAll(v)); |
201 |
|
|
v.add(six); |
202 |
|
|
assertFalse(full.containsAll(v)); |
203 |
dl |
1.1 |
} |
204 |
|
|
|
205 |
|
|
/** |
206 |
jsr166 |
1.18 |
* get returns the value at the given index |
207 |
dl |
1.1 |
*/ |
208 |
dl |
1.4 |
public void testGet() { |
209 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
210 |
jsr166 |
1.14 |
assertEquals(0, full.get(0)); |
211 |
dl |
1.1 |
} |
212 |
|
|
|
213 |
|
|
/** |
214 |
jsr166 |
1.18 |
* indexOf gives the index for the given object |
215 |
dl |
1.1 |
*/ |
216 |
dl |
1.4 |
public void testIndexOf() { |
217 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
218 |
|
|
assertEquals(1, full.indexOf(one)); |
219 |
|
|
assertEquals(-1, full.indexOf("puppies")); |
220 |
dl |
1.1 |
} |
221 |
|
|
|
222 |
|
|
/** |
223 |
jsr166 |
1.18 |
* indexOf gives the index based on the given index |
224 |
|
|
* at which to start searching |
225 |
dl |
1.1 |
*/ |
226 |
dl |
1.4 |
public void testIndexOf2() { |
227 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
228 |
|
|
assertEquals(1, full.indexOf(one, 0)); |
229 |
|
|
assertEquals(-1, full.indexOf(one, 2)); |
230 |
dl |
1.1 |
} |
231 |
|
|
|
232 |
|
|
/** |
233 |
jsr166 |
1.18 |
* isEmpty returns true when empty, else false |
234 |
dl |
1.1 |
*/ |
235 |
dl |
1.4 |
public void testIsEmpty() { |
236 |
jsr166 |
1.11 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
237 |
|
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
238 |
|
|
assertTrue(empty.isEmpty()); |
239 |
|
|
assertFalse(full.isEmpty()); |
240 |
dl |
1.1 |
} |
241 |
|
|
|
242 |
|
|
/** |
243 |
jsr166 |
1.27 |
* iterator() returns an iterator containing the elements of the |
244 |
|
|
* list in insertion order |
245 |
dl |
1.1 |
*/ |
246 |
dl |
1.4 |
public void testIterator() { |
247 |
jsr166 |
1.27 |
Collection empty = new CopyOnWriteArrayList(); |
248 |
|
|
assertFalse(empty.iterator().hasNext()); |
249 |
|
|
try { |
250 |
|
|
empty.iterator().next(); |
251 |
|
|
shouldThrow(); |
252 |
|
|
} catch (NoSuchElementException success) {} |
253 |
|
|
|
254 |
|
|
Integer[] elements = new Integer[SIZE]; |
255 |
|
|
for (int i = 0; i < SIZE; i++) |
256 |
|
|
elements[i] = i; |
257 |
|
|
Collections.shuffle(Arrays.asList(elements)); |
258 |
|
|
Collection<Integer> full = populatedArray(elements); |
259 |
|
|
|
260 |
|
|
Iterator it = full.iterator(); |
261 |
|
|
for (int j = 0; j < SIZE; j++) { |
262 |
|
|
assertTrue(it.hasNext()); |
263 |
|
|
assertEquals(elements[j], it.next()); |
264 |
|
|
} |
265 |
|
|
assertFalse(it.hasNext()); |
266 |
|
|
try { |
267 |
|
|
it.next(); |
268 |
|
|
shouldThrow(); |
269 |
|
|
} catch (NoSuchElementException success) {} |
270 |
dl |
1.1 |
} |
271 |
|
|
|
272 |
dl |
1.4 |
/** |
273 |
dl |
1.5 |
* iterator.remove throws UnsupportedOperationException |
274 |
dl |
1.4 |
*/ |
275 |
jsr166 |
1.17 |
public void testIteratorRemove() { |
276 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
277 |
dl |
1.1 |
Iterator it = full.iterator(); |
278 |
|
|
it.next(); |
279 |
|
|
try { |
280 |
|
|
it.remove(); |
281 |
dl |
1.4 |
shouldThrow(); |
282 |
jsr166 |
1.12 |
} catch (UnsupportedOperationException success) {} |
283 |
dl |
1.1 |
} |
284 |
|
|
|
285 |
dl |
1.4 |
/** |
286 |
dl |
1.5 |
* toString contains toString of elements |
287 |
dl |
1.4 |
*/ |
288 |
|
|
public void testToString() { |
289 |
jsr166 |
1.27 |
assertEquals("[]", new CopyOnWriteArrayList().toString()); |
290 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
291 |
dl |
1.1 |
String s = full.toString(); |
292 |
jsr166 |
1.27 |
for (int i = 0; i < 3; ++i) |
293 |
jsr166 |
1.20 |
assertTrue(s.contains(String.valueOf(i))); |
294 |
jsr166 |
1.27 |
assertEquals(new ArrayList(full).toString(), |
295 |
|
|
full.toString()); |
296 |
jsr166 |
1.8 |
} |
297 |
dl |
1.1 |
|
298 |
|
|
/** |
299 |
jsr166 |
1.18 |
* lastIndexOf returns the index for the given object |
300 |
dl |
1.1 |
*/ |
301 |
dl |
1.4 |
public void testLastIndexOf1() { |
302 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
303 |
|
|
full.add(one); |
304 |
|
|
full.add(three); |
305 |
|
|
assertEquals(3, full.lastIndexOf(one)); |
306 |
|
|
assertEquals(-1, full.lastIndexOf(six)); |
307 |
dl |
1.1 |
} |
308 |
|
|
|
309 |
|
|
/** |
310 |
jsr166 |
1.18 |
* lastIndexOf returns the index from the given starting point |
311 |
dl |
1.1 |
*/ |
312 |
jsr166 |
1.21 |
public void testLastIndexOf2() { |
313 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
314 |
|
|
full.add(one); |
315 |
|
|
full.add(three); |
316 |
|
|
assertEquals(3, full.lastIndexOf(one, 4)); |
317 |
|
|
assertEquals(-1, full.lastIndexOf(three, 3)); |
318 |
dl |
1.1 |
} |
319 |
|
|
|
320 |
|
|
/** |
321 |
jsr166 |
1.18 |
* listIterator traverses all elements |
322 |
dl |
1.1 |
*/ |
323 |
dl |
1.4 |
public void testListIterator1() { |
324 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
325 |
|
|
ListIterator i = full.listIterator(); |
326 |
|
|
int j; |
327 |
|
|
for (j = 0; i.hasNext(); j++) |
328 |
jsr166 |
1.14 |
assertEquals(j, i.next()); |
329 |
jsr166 |
1.11 |
assertEquals(SIZE, j); |
330 |
dl |
1.1 |
} |
331 |
|
|
|
332 |
|
|
/** |
333 |
jsr166 |
1.18 |
* listIterator only returns those elements after the given index |
334 |
dl |
1.1 |
*/ |
335 |
dl |
1.4 |
public void testListIterator2() { |
336 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
337 |
|
|
ListIterator i = full.listIterator(1); |
338 |
|
|
int j; |
339 |
|
|
for (j = 0; i.hasNext(); j++) |
340 |
jsr166 |
1.14 |
assertEquals(j+1, i.next()); |
341 |
jsr166 |
1.11 |
assertEquals(2, j); |
342 |
dl |
1.1 |
} |
343 |
|
|
|
344 |
|
|
/** |
345 |
jsr166 |
1.28 |
* remove(int) removes and returns the object at the given index |
346 |
dl |
1.1 |
*/ |
347 |
jsr166 |
1.28 |
public void testRemove_int() { |
348 |
|
|
int SIZE = 3; |
349 |
|
|
for (int i = 0; i < SIZE; i++) { |
350 |
|
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
351 |
|
|
assertEquals(i, full.remove(i)); |
352 |
|
|
assertEquals(SIZE - 1, full.size()); |
353 |
|
|
assertFalse(full.contains(new Integer(i))); |
354 |
|
|
} |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
/** |
358 |
|
|
* remove(Object) removes the object if found and returns true |
359 |
|
|
*/ |
360 |
|
|
public void testRemove_Object() { |
361 |
|
|
int SIZE = 3; |
362 |
|
|
for (int i = 0; i < SIZE; i++) { |
363 |
|
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
364 |
|
|
assertFalse(full.remove(new Integer(-42))); |
365 |
|
|
assertTrue(full.remove(new Integer(i))); |
366 |
|
|
assertEquals(SIZE - 1, full.size()); |
367 |
|
|
assertFalse(full.contains(new Integer(i))); |
368 |
|
|
} |
369 |
|
|
CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); |
370 |
|
|
assertTrue(x.remove(new Integer(6))); |
371 |
|
|
assertEquals(x, Arrays.asList(4, 5)); |
372 |
|
|
assertTrue(x.remove(new Integer(4))); |
373 |
|
|
assertEquals(x, Arrays.asList(5)); |
374 |
|
|
assertTrue(x.remove(new Integer(5))); |
375 |
|
|
assertEquals(x, Arrays.asList()); |
376 |
|
|
assertFalse(x.remove(new Integer(5))); |
377 |
dl |
1.1 |
} |
378 |
|
|
|
379 |
|
|
/** |
380 |
jsr166 |
1.18 |
* removeAll removes all elements from the given collection |
381 |
dl |
1.1 |
*/ |
382 |
dl |
1.4 |
public void testRemoveAll() { |
383 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
384 |
|
|
Vector v = new Vector(); |
385 |
|
|
v.add(one); |
386 |
|
|
v.add(two); |
387 |
|
|
full.removeAll(v); |
388 |
|
|
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 |
|
|
a = new Integer[SIZE/2]; |
441 |
|
|
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 |
|
|
a = new Integer[2*SIZE]; |
465 |
|
|
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 |
|
|
try { |
504 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
505 |
|
|
c.add("zfasdfsdf"); |
506 |
|
|
c.add("asdadasd"); |
507 |
|
|
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 |
|
|
try { |
517 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
518 |
|
|
c.get(-1); |
519 |
dl |
1.4 |
shouldThrow(); |
520 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
521 |
dl |
1.1 |
} |
522 |
jsr166 |
1.8 |
|
523 |
dl |
1.1 |
/** |
524 |
jsr166 |
1.18 |
* get throws an IndexOutOfBoundsException on a too high index |
525 |
dl |
1.1 |
*/ |
526 |
dl |
1.4 |
public void testGet2_IndexOutOfBoundsException() { |
527 |
|
|
try { |
528 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
529 |
|
|
c.add("asdasd"); |
530 |
|
|
c.add("asdad"); |
531 |
|
|
c.get(100); |
532 |
dl |
1.4 |
shouldThrow(); |
533 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
534 |
dl |
1.1 |
} |
535 |
|
|
|
536 |
|
|
/** |
537 |
jsr166 |
1.18 |
* set throws an IndexOutOfBoundsException on a negative index |
538 |
dl |
1.1 |
*/ |
539 |
dl |
1.4 |
public void testSet1_IndexOutOfBoundsException() { |
540 |
|
|
try { |
541 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
542 |
|
|
c.set(-1,"qwerty"); |
543 |
dl |
1.4 |
shouldThrow(); |
544 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
545 |
dl |
1.1 |
} |
546 |
jsr166 |
1.8 |
|
547 |
dl |
1.1 |
/** |
548 |
jsr166 |
1.18 |
* set throws an IndexOutOfBoundsException on a too high index |
549 |
dl |
1.1 |
*/ |
550 |
dl |
1.4 |
public void testSet2() { |
551 |
|
|
try { |
552 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
553 |
|
|
c.add("asdasd"); |
554 |
|
|
c.add("asdad"); |
555 |
|
|
c.set(100, "qwerty"); |
556 |
dl |
1.4 |
shouldThrow(); |
557 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
558 |
dl |
1.1 |
} |
559 |
|
|
|
560 |
|
|
/** |
561 |
jsr166 |
1.18 |
* add throws an IndexOutOfBoundsException on a negative index |
562 |
dl |
1.1 |
*/ |
563 |
dl |
1.4 |
public void testAdd1_IndexOutOfBoundsException() { |
564 |
|
|
try { |
565 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
566 |
|
|
c.add(-1,"qwerty"); |
567 |
dl |
1.4 |
shouldThrow(); |
568 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
569 |
dl |
1.1 |
} |
570 |
jsr166 |
1.8 |
|
571 |
dl |
1.1 |
/** |
572 |
jsr166 |
1.18 |
* add throws an IndexOutOfBoundsException on a too high index |
573 |
dl |
1.1 |
*/ |
574 |
dl |
1.4 |
public void testAdd2_IndexOutOfBoundsException() { |
575 |
|
|
try { |
576 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
577 |
|
|
c.add("asdasd"); |
578 |
|
|
c.add("asdasdasd"); |
579 |
|
|
c.add(100, "qwerty"); |
580 |
dl |
1.4 |
shouldThrow(); |
581 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
582 |
dl |
1.1 |
} |
583 |
|
|
|
584 |
|
|
/** |
585 |
jsr166 |
1.18 |
* remove throws an IndexOutOfBoundsException on a negative index |
586 |
dl |
1.1 |
*/ |
587 |
dl |
1.4 |
public void testRemove1_IndexOutOfBounds() { |
588 |
|
|
try { |
589 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
590 |
|
|
c.remove(-1); |
591 |
dl |
1.4 |
shouldThrow(); |
592 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
593 |
dl |
1.1 |
} |
594 |
|
|
|
595 |
|
|
/** |
596 |
jsr166 |
1.18 |
* remove throws an IndexOutOfBoundsException on a too high index |
597 |
dl |
1.1 |
*/ |
598 |
dl |
1.4 |
public void testRemove2_IndexOutOfBounds() { |
599 |
|
|
try { |
600 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
601 |
|
|
c.add("asdasd"); |
602 |
|
|
c.add("adasdasd"); |
603 |
|
|
c.remove(100); |
604 |
dl |
1.4 |
shouldThrow(); |
605 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
606 |
dl |
1.1 |
} |
607 |
jsr166 |
1.8 |
|
608 |
dl |
1.1 |
/** |
609 |
jsr166 |
1.18 |
* addAll throws an IndexOutOfBoundsException on a negative index |
610 |
dl |
1.1 |
*/ |
611 |
dl |
1.4 |
public void testAddAll1_IndexOutOfBoundsException() { |
612 |
|
|
try { |
613 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
614 |
|
|
c.addAll(-1,new LinkedList()); |
615 |
dl |
1.4 |
shouldThrow(); |
616 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
617 |
dl |
1.1 |
} |
618 |
jsr166 |
1.8 |
|
619 |
dl |
1.1 |
/** |
620 |
jsr166 |
1.18 |
* addAll throws an IndexOutOfBoundsException on a too high index |
621 |
dl |
1.1 |
*/ |
622 |
dl |
1.4 |
public void testAddAll2_IndexOutOfBoundsException() { |
623 |
|
|
try { |
624 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
625 |
|
|
c.add("asdasd"); |
626 |
|
|
c.add("asdasdasd"); |
627 |
|
|
c.addAll(100, new LinkedList()); |
628 |
dl |
1.4 |
shouldThrow(); |
629 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
630 |
dl |
1.1 |
} |
631 |
|
|
|
632 |
|
|
/** |
633 |
jsr166 |
1.18 |
* listIterator throws an IndexOutOfBoundsException on a negative index |
634 |
dl |
1.1 |
*/ |
635 |
dl |
1.4 |
public void testListIterator1_IndexOutOfBoundsException() { |
636 |
|
|
try { |
637 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
638 |
|
|
c.listIterator(-1); |
639 |
dl |
1.4 |
shouldThrow(); |
640 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
641 |
dl |
1.1 |
} |
642 |
|
|
|
643 |
|
|
/** |
644 |
jsr166 |
1.18 |
* listIterator throws an IndexOutOfBoundsException on a too high index |
645 |
dl |
1.1 |
*/ |
646 |
dl |
1.4 |
public void testListIterator2_IndexOutOfBoundsException() { |
647 |
|
|
try { |
648 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
649 |
|
|
c.add("adasd"); |
650 |
|
|
c.add("asdasdas"); |
651 |
|
|
c.listIterator(100); |
652 |
dl |
1.4 |
shouldThrow(); |
653 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
654 |
dl |
1.1 |
} |
655 |
|
|
|
656 |
|
|
/** |
657 |
jsr166 |
1.18 |
* subList throws an IndexOutOfBoundsException on a negative index |
658 |
dl |
1.1 |
*/ |
659 |
dl |
1.4 |
public void testSubList1_IndexOutOfBoundsException() { |
660 |
|
|
try { |
661 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
662 |
|
|
c.subList(-1,100); |
663 |
dl |
1.4 |
shouldThrow(); |
664 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
665 |
dl |
1.1 |
} |
666 |
|
|
|
667 |
|
|
/** |
668 |
jsr166 |
1.18 |
* subList throws an IndexOutOfBoundsException on a too high index |
669 |
dl |
1.1 |
*/ |
670 |
dl |
1.4 |
public void testSubList2_IndexOutOfBoundsException() { |
671 |
|
|
try { |
672 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
673 |
|
|
c.add("asdasd"); |
674 |
|
|
c.subList(1,100); |
675 |
dl |
1.4 |
shouldThrow(); |
676 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
677 |
dl |
1.1 |
} |
678 |
|
|
|
679 |
|
|
/** |
680 |
jsr166 |
1.18 |
* subList throws IndexOutOfBoundsException when the second index |
681 |
|
|
* is lower then the first |
682 |
dl |
1.1 |
*/ |
683 |
dl |
1.4 |
public void testSubList3_IndexOutOfBoundsException() { |
684 |
|
|
try { |
685 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
686 |
|
|
c.subList(3,1); |
687 |
dl |
1.4 |
shouldThrow(); |
688 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
689 |
dl |
1.2 |
} |
690 |
|
|
|
691 |
dl |
1.4 |
/** |
692 |
jsr166 |
1.16 |
* a deserialized serialized list is equal |
693 |
dl |
1.4 |
*/ |
694 |
jsr166 |
1.12 |
public void testSerialization() throws Exception { |
695 |
jsr166 |
1.22 |
List x = populatedArray(SIZE); |
696 |
|
|
List y = serialClone(x); |
697 |
dl |
1.2 |
|
698 |
jsr166 |
1.22 |
assertTrue(x != y); |
699 |
|
|
assertEquals(x.size(), y.size()); |
700 |
|
|
assertEquals(x.toString(), y.toString()); |
701 |
|
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
702 |
|
|
assertEquals(x, y); |
703 |
|
|
assertEquals(y, x); |
704 |
|
|
while (!x.isEmpty()) { |
705 |
|
|
assertFalse(y.isEmpty()); |
706 |
|
|
assertEquals(x.remove(0), y.remove(0)); |
707 |
|
|
} |
708 |
|
|
assertTrue(y.isEmpty()); |
709 |
dl |
1.1 |
} |
710 |
jsr166 |
1.8 |
|
711 |
dl |
1.1 |
} |