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 |
jsr166 |
1.37 |
assertTrue(a.containsAll(b)); |
181 |
|
|
assertTrue(b.containsAll(a)); |
182 |
dl |
1.1 |
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 |
jsr166 |
1.37 |
assertTrue(a.containsAll(b)); |
187 |
|
|
assertFalse(b.containsAll(a)); |
188 |
dl |
1.3 |
b.add(m1); |
189 |
dl |
1.1 |
assertTrue(a.equals(b)); |
190 |
|
|
assertTrue(b.equals(a)); |
191 |
jsr166 |
1.37 |
assertTrue(a.containsAll(b)); |
192 |
|
|
assertTrue(b.containsAll(a)); |
193 |
dl |
1.1 |
assertEquals(a.hashCode(), b.hashCode()); |
194 |
jsr166 |
1.37 |
|
195 |
|
|
assertFalse(a.equals(null)); |
196 |
dl |
1.1 |
} |
197 |
|
|
|
198 |
|
|
/** |
199 |
jsr166 |
1.37 |
* containsAll returns true for collections with subset of elements |
200 |
dl |
1.1 |
*/ |
201 |
dl |
1.4 |
public void testContainsAll() { |
202 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
203 |
jsr166 |
1.33 |
assertTrue(full.containsAll(Arrays.asList())); |
204 |
|
|
assertTrue(full.containsAll(Arrays.asList(one))); |
205 |
|
|
assertTrue(full.containsAll(Arrays.asList(one, two))); |
206 |
|
|
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
207 |
|
|
assertFalse(full.containsAll(Arrays.asList(six))); |
208 |
jsr166 |
1.37 |
|
209 |
|
|
try { |
210 |
|
|
full.containsAll(null); |
211 |
|
|
shouldThrow(); |
212 |
|
|
} catch (NullPointerException success) {} |
213 |
dl |
1.1 |
} |
214 |
|
|
|
215 |
|
|
/** |
216 |
jsr166 |
1.18 |
* get returns the value at the given index |
217 |
dl |
1.1 |
*/ |
218 |
dl |
1.4 |
public void testGet() { |
219 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
220 |
jsr166 |
1.14 |
assertEquals(0, full.get(0)); |
221 |
dl |
1.1 |
} |
222 |
|
|
|
223 |
|
|
/** |
224 |
jsr166 |
1.18 |
* indexOf gives the index for the given object |
225 |
dl |
1.1 |
*/ |
226 |
dl |
1.4 |
public void testIndexOf() { |
227 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
228 |
|
|
assertEquals(1, full.indexOf(one)); |
229 |
|
|
assertEquals(-1, full.indexOf("puppies")); |
230 |
dl |
1.1 |
} |
231 |
|
|
|
232 |
|
|
/** |
233 |
jsr166 |
1.18 |
* indexOf gives the index based on the given index |
234 |
|
|
* at which to start searching |
235 |
dl |
1.1 |
*/ |
236 |
dl |
1.4 |
public void testIndexOf2() { |
237 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
238 |
|
|
assertEquals(1, full.indexOf(one, 0)); |
239 |
|
|
assertEquals(-1, full.indexOf(one, 2)); |
240 |
dl |
1.1 |
} |
241 |
|
|
|
242 |
|
|
/** |
243 |
jsr166 |
1.18 |
* isEmpty returns true when empty, else false |
244 |
dl |
1.1 |
*/ |
245 |
dl |
1.4 |
public void testIsEmpty() { |
246 |
jsr166 |
1.11 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
247 |
|
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
248 |
|
|
assertTrue(empty.isEmpty()); |
249 |
|
|
assertFalse(full.isEmpty()); |
250 |
dl |
1.1 |
} |
251 |
|
|
|
252 |
|
|
/** |
253 |
jsr166 |
1.27 |
* iterator() returns an iterator containing the elements of the |
254 |
|
|
* list in insertion order |
255 |
dl |
1.1 |
*/ |
256 |
dl |
1.4 |
public void testIterator() { |
257 |
jsr166 |
1.27 |
Collection empty = new CopyOnWriteArrayList(); |
258 |
|
|
assertFalse(empty.iterator().hasNext()); |
259 |
|
|
try { |
260 |
|
|
empty.iterator().next(); |
261 |
|
|
shouldThrow(); |
262 |
|
|
} catch (NoSuchElementException success) {} |
263 |
|
|
|
264 |
|
|
Integer[] elements = new Integer[SIZE]; |
265 |
|
|
for (int i = 0; i < SIZE; i++) |
266 |
|
|
elements[i] = i; |
267 |
jsr166 |
1.38 |
shuffle(elements); |
268 |
jsr166 |
1.27 |
Collection<Integer> full = populatedArray(elements); |
269 |
|
|
|
270 |
|
|
Iterator it = full.iterator(); |
271 |
|
|
for (int j = 0; j < SIZE; j++) { |
272 |
|
|
assertTrue(it.hasNext()); |
273 |
|
|
assertEquals(elements[j], it.next()); |
274 |
|
|
} |
275 |
jsr166 |
1.32 |
assertIteratorExhausted(it); |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
/** |
279 |
|
|
* iterator of empty collection has no elements |
280 |
|
|
*/ |
281 |
|
|
public void testEmptyIterator() { |
282 |
|
|
Collection c = new CopyOnWriteArrayList(); |
283 |
|
|
assertIteratorExhausted(c.iterator()); |
284 |
dl |
1.1 |
} |
285 |
|
|
|
286 |
dl |
1.4 |
/** |
287 |
dl |
1.5 |
* iterator.remove throws UnsupportedOperationException |
288 |
dl |
1.4 |
*/ |
289 |
jsr166 |
1.17 |
public void testIteratorRemove() { |
290 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
291 |
dl |
1.1 |
Iterator it = full.iterator(); |
292 |
|
|
it.next(); |
293 |
|
|
try { |
294 |
|
|
it.remove(); |
295 |
dl |
1.4 |
shouldThrow(); |
296 |
jsr166 |
1.12 |
} catch (UnsupportedOperationException success) {} |
297 |
dl |
1.1 |
} |
298 |
|
|
|
299 |
dl |
1.4 |
/** |
300 |
dl |
1.5 |
* toString contains toString of elements |
301 |
dl |
1.4 |
*/ |
302 |
|
|
public void testToString() { |
303 |
jsr166 |
1.27 |
assertEquals("[]", new CopyOnWriteArrayList().toString()); |
304 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
305 |
dl |
1.1 |
String s = full.toString(); |
306 |
jsr166 |
1.27 |
for (int i = 0; i < 3; ++i) |
307 |
jsr166 |
1.20 |
assertTrue(s.contains(String.valueOf(i))); |
308 |
jsr166 |
1.27 |
assertEquals(new ArrayList(full).toString(), |
309 |
|
|
full.toString()); |
310 |
jsr166 |
1.8 |
} |
311 |
dl |
1.1 |
|
312 |
|
|
/** |
313 |
jsr166 |
1.18 |
* lastIndexOf returns the index for the given object |
314 |
dl |
1.1 |
*/ |
315 |
dl |
1.4 |
public void testLastIndexOf1() { |
316 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
317 |
|
|
full.add(one); |
318 |
|
|
full.add(three); |
319 |
|
|
assertEquals(3, full.lastIndexOf(one)); |
320 |
|
|
assertEquals(-1, full.lastIndexOf(six)); |
321 |
dl |
1.1 |
} |
322 |
|
|
|
323 |
|
|
/** |
324 |
jsr166 |
1.18 |
* lastIndexOf returns the index from the given starting point |
325 |
dl |
1.1 |
*/ |
326 |
jsr166 |
1.21 |
public void testLastIndexOf2() { |
327 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
328 |
|
|
full.add(one); |
329 |
|
|
full.add(three); |
330 |
|
|
assertEquals(3, full.lastIndexOf(one, 4)); |
331 |
|
|
assertEquals(-1, full.lastIndexOf(three, 3)); |
332 |
dl |
1.1 |
} |
333 |
|
|
|
334 |
|
|
/** |
335 |
jsr166 |
1.18 |
* listIterator traverses all elements |
336 |
dl |
1.1 |
*/ |
337 |
dl |
1.4 |
public void testListIterator1() { |
338 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
339 |
|
|
ListIterator i = full.listIterator(); |
340 |
|
|
int j; |
341 |
|
|
for (j = 0; i.hasNext(); j++) |
342 |
jsr166 |
1.14 |
assertEquals(j, i.next()); |
343 |
jsr166 |
1.11 |
assertEquals(SIZE, j); |
344 |
dl |
1.1 |
} |
345 |
|
|
|
346 |
|
|
/** |
347 |
jsr166 |
1.18 |
* listIterator only returns those elements after the given index |
348 |
dl |
1.1 |
*/ |
349 |
dl |
1.4 |
public void testListIterator2() { |
350 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
351 |
|
|
ListIterator i = full.listIterator(1); |
352 |
|
|
int j; |
353 |
|
|
for (j = 0; i.hasNext(); j++) |
354 |
jsr166 |
1.36 |
assertEquals(j + 1, i.next()); |
355 |
jsr166 |
1.11 |
assertEquals(2, j); |
356 |
dl |
1.1 |
} |
357 |
|
|
|
358 |
|
|
/** |
359 |
jsr166 |
1.28 |
* remove(int) removes and returns the object at the given index |
360 |
dl |
1.1 |
*/ |
361 |
jsr166 |
1.28 |
public void testRemove_int() { |
362 |
|
|
int SIZE = 3; |
363 |
|
|
for (int i = 0; i < SIZE; i++) { |
364 |
|
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
365 |
|
|
assertEquals(i, full.remove(i)); |
366 |
|
|
assertEquals(SIZE - 1, full.size()); |
367 |
|
|
assertFalse(full.contains(new Integer(i))); |
368 |
|
|
} |
369 |
|
|
} |
370 |
|
|
|
371 |
|
|
/** |
372 |
|
|
* remove(Object) removes the object if found and returns true |
373 |
|
|
*/ |
374 |
|
|
public void testRemove_Object() { |
375 |
|
|
int SIZE = 3; |
376 |
|
|
for (int i = 0; i < SIZE; i++) { |
377 |
|
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
378 |
|
|
assertFalse(full.remove(new Integer(-42))); |
379 |
|
|
assertTrue(full.remove(new Integer(i))); |
380 |
|
|
assertEquals(SIZE - 1, full.size()); |
381 |
|
|
assertFalse(full.contains(new Integer(i))); |
382 |
|
|
} |
383 |
|
|
CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); |
384 |
|
|
assertTrue(x.remove(new Integer(6))); |
385 |
|
|
assertEquals(x, Arrays.asList(4, 5)); |
386 |
|
|
assertTrue(x.remove(new Integer(4))); |
387 |
|
|
assertEquals(x, Arrays.asList(5)); |
388 |
|
|
assertTrue(x.remove(new Integer(5))); |
389 |
|
|
assertEquals(x, Arrays.asList()); |
390 |
|
|
assertFalse(x.remove(new Integer(5))); |
391 |
dl |
1.1 |
} |
392 |
|
|
|
393 |
|
|
/** |
394 |
jsr166 |
1.18 |
* removeAll removes all elements from the given collection |
395 |
dl |
1.1 |
*/ |
396 |
dl |
1.4 |
public void testRemoveAll() { |
397 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
398 |
jsr166 |
1.33 |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
399 |
|
|
assertEquals(1, full.size()); |
400 |
|
|
assertFalse(full.removeAll(Arrays.asList(one, two))); |
401 |
jsr166 |
1.11 |
assertEquals(1, full.size()); |
402 |
dl |
1.1 |
} |
403 |
|
|
|
404 |
|
|
/** |
405 |
jsr166 |
1.18 |
* set changes the element at the given index |
406 |
dl |
1.1 |
*/ |
407 |
dl |
1.4 |
public void testSet() { |
408 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
409 |
jsr166 |
1.15 |
assertEquals(2, full.set(2, four)); |
410 |
jsr166 |
1.14 |
assertEquals(4, full.get(2)); |
411 |
dl |
1.1 |
} |
412 |
|
|
|
413 |
|
|
/** |
414 |
jsr166 |
1.18 |
* size returns the number of elements |
415 |
dl |
1.1 |
*/ |
416 |
dl |
1.4 |
public void testSize() { |
417 |
jsr166 |
1.11 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
418 |
|
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
419 |
|
|
assertEquals(SIZE, full.size()); |
420 |
|
|
assertEquals(0, empty.size()); |
421 |
dl |
1.1 |
} |
422 |
|
|
|
423 |
|
|
/** |
424 |
jsr166 |
1.26 |
* toArray() returns an Object array containing all elements from |
425 |
|
|
* the list in insertion order |
426 |
dl |
1.1 |
*/ |
427 |
dl |
1.4 |
public void testToArray() { |
428 |
jsr166 |
1.26 |
Object[] a = new CopyOnWriteArrayList().toArray(); |
429 |
|
|
assertTrue(Arrays.equals(new Object[0], a)); |
430 |
|
|
assertSame(Object[].class, a.getClass()); |
431 |
|
|
|
432 |
|
|
Integer[] elements = new Integer[SIZE]; |
433 |
|
|
for (int i = 0; i < SIZE; i++) |
434 |
|
|
elements[i] = i; |
435 |
jsr166 |
1.38 |
shuffle(elements); |
436 |
jsr166 |
1.26 |
Collection<Integer> full = populatedArray(elements); |
437 |
|
|
|
438 |
|
|
assertTrue(Arrays.equals(elements, full.toArray())); |
439 |
|
|
assertSame(Object[].class, full.toArray().getClass()); |
440 |
dl |
1.1 |
} |
441 |
|
|
|
442 |
|
|
/** |
443 |
jsr166 |
1.26 |
* toArray(Integer array) returns an Integer array containing all |
444 |
|
|
* elements from the list in insertion order |
445 |
dl |
1.1 |
*/ |
446 |
dl |
1.4 |
public void testToArray2() { |
447 |
jsr166 |
1.26 |
Collection empty = new CopyOnWriteArrayList(); |
448 |
|
|
Integer[] a; |
449 |
|
|
|
450 |
|
|
a = new Integer[0]; |
451 |
|
|
assertSame(a, empty.toArray(a)); |
452 |
|
|
|
453 |
jsr166 |
1.35 |
a = new Integer[SIZE / 2]; |
454 |
jsr166 |
1.26 |
Arrays.fill(a, 42); |
455 |
|
|
assertSame(a, empty.toArray(a)); |
456 |
|
|
assertNull(a[0]); |
457 |
|
|
for (int i = 1; i < a.length; i++) |
458 |
|
|
assertEquals(42, (int) a[i]); |
459 |
|
|
|
460 |
|
|
Integer[] elements = new Integer[SIZE]; |
461 |
|
|
for (int i = 0; i < SIZE; i++) |
462 |
|
|
elements[i] = i; |
463 |
jsr166 |
1.38 |
shuffle(elements); |
464 |
jsr166 |
1.26 |
Collection<Integer> full = populatedArray(elements); |
465 |
|
|
|
466 |
|
|
Arrays.fill(a, 42); |
467 |
|
|
assertTrue(Arrays.equals(elements, full.toArray(a))); |
468 |
|
|
for (int i = 0; i < a.length; i++) |
469 |
|
|
assertEquals(42, (int) a[i]); |
470 |
|
|
assertSame(Integer[].class, full.toArray(a).getClass()); |
471 |
|
|
|
472 |
|
|
a = new Integer[SIZE]; |
473 |
|
|
Arrays.fill(a, 42); |
474 |
|
|
assertSame(a, full.toArray(a)); |
475 |
|
|
assertTrue(Arrays.equals(elements, a)); |
476 |
|
|
|
477 |
jsr166 |
1.35 |
a = new Integer[2 * SIZE]; |
478 |
jsr166 |
1.26 |
Arrays.fill(a, 42); |
479 |
|
|
assertSame(a, full.toArray(a)); |
480 |
|
|
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
481 |
|
|
assertNull(a[SIZE]); |
482 |
|
|
for (int i = SIZE + 1; i < a.length; i++) |
483 |
|
|
assertEquals(42, (int) a[i]); |
484 |
dl |
1.1 |
} |
485 |
|
|
|
486 |
dl |
1.4 |
/** |
487 |
dl |
1.5 |
* sublists contains elements at indexes offset from their base |
488 |
dl |
1.4 |
*/ |
489 |
dl |
1.1 |
public void testSubList() { |
490 |
jsr166 |
1.11 |
CopyOnWriteArrayList a = populatedArray(10); |
491 |
dl |
1.1 |
assertTrue(a.subList(1,1).isEmpty()); |
492 |
jsr166 |
1.11 |
for (int j = 0; j < 9; ++j) { |
493 |
|
|
for (int i = j ; i < 10; ++i) { |
494 |
|
|
List b = a.subList(j,i); |
495 |
|
|
for (int k = j; k < i; ++k) { |
496 |
|
|
assertEquals(new Integer(k), b.get(k-j)); |
497 |
|
|
} |
498 |
|
|
} |
499 |
|
|
} |
500 |
dl |
1.1 |
|
501 |
jsr166 |
1.11 |
List s = a.subList(2, 5); |
502 |
jsr166 |
1.24 |
assertEquals(3, s.size()); |
503 |
dl |
1.3 |
s.set(2, m1); |
504 |
dl |
1.1 |
assertEquals(a.get(4), m1); |
505 |
jsr166 |
1.11 |
s.clear(); |
506 |
jsr166 |
1.24 |
assertEquals(7, a.size()); |
507 |
dl |
1.1 |
} |
508 |
|
|
|
509 |
|
|
// Exception tests |
510 |
|
|
|
511 |
|
|
/** |
512 |
jsr166 |
1.18 |
* toArray throws an ArrayStoreException when the given array |
513 |
|
|
* can not store the objects inside the list |
514 |
dl |
1.1 |
*/ |
515 |
dl |
1.4 |
public void testToArray_ArrayStoreException() { |
516 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
517 |
|
|
c.add("zfasdfsdf"); |
518 |
|
|
c.add("asdadasd"); |
519 |
dl |
1.4 |
try { |
520 |
dl |
1.1 |
c.toArray(new Long[5]); |
521 |
jsr166 |
1.11 |
shouldThrow(); |
522 |
jsr166 |
1.13 |
} catch (ArrayStoreException success) {} |
523 |
dl |
1.1 |
} |
524 |
|
|
|
525 |
|
|
/** |
526 |
jsr166 |
1.18 |
* get throws an IndexOutOfBoundsException on a negative index |
527 |
dl |
1.1 |
*/ |
528 |
dl |
1.4 |
public void testGet1_IndexOutOfBoundsException() { |
529 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
530 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
531 |
|
|
for (List list : lists) { |
532 |
|
|
try { |
533 |
|
|
list.get(-1); |
534 |
|
|
shouldThrow(); |
535 |
|
|
} catch (IndexOutOfBoundsException success) {} |
536 |
|
|
} |
537 |
dl |
1.1 |
} |
538 |
jsr166 |
1.8 |
|
539 |
dl |
1.1 |
/** |
540 |
jsr166 |
1.18 |
* get throws an IndexOutOfBoundsException on a too high index |
541 |
dl |
1.1 |
*/ |
542 |
dl |
1.4 |
public void testGet2_IndexOutOfBoundsException() { |
543 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
544 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
545 |
|
|
for (List list : lists) { |
546 |
|
|
try { |
547 |
|
|
list.get(list.size()); |
548 |
|
|
shouldThrow(); |
549 |
|
|
} catch (IndexOutOfBoundsException success) {} |
550 |
|
|
} |
551 |
dl |
1.1 |
} |
552 |
|
|
|
553 |
|
|
/** |
554 |
jsr166 |
1.18 |
* set throws an IndexOutOfBoundsException on a negative index |
555 |
dl |
1.1 |
*/ |
556 |
dl |
1.4 |
public void testSet1_IndexOutOfBoundsException() { |
557 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
558 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
559 |
|
|
for (List list : lists) { |
560 |
|
|
try { |
561 |
|
|
list.set(-1, "qwerty"); |
562 |
|
|
shouldThrow(); |
563 |
|
|
} catch (IndexOutOfBoundsException success) {} |
564 |
|
|
} |
565 |
dl |
1.1 |
} |
566 |
jsr166 |
1.8 |
|
567 |
dl |
1.1 |
/** |
568 |
jsr166 |
1.18 |
* set throws an IndexOutOfBoundsException on a too high index |
569 |
dl |
1.1 |
*/ |
570 |
dl |
1.4 |
public void testSet2() { |
571 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
572 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
573 |
|
|
for (List list : lists) { |
574 |
|
|
try { |
575 |
|
|
list.set(list.size(), "qwerty"); |
576 |
|
|
shouldThrow(); |
577 |
|
|
} catch (IndexOutOfBoundsException success) {} |
578 |
|
|
} |
579 |
dl |
1.1 |
} |
580 |
|
|
|
581 |
|
|
/** |
582 |
jsr166 |
1.18 |
* add throws an IndexOutOfBoundsException on a negative index |
583 |
dl |
1.1 |
*/ |
584 |
dl |
1.4 |
public void testAdd1_IndexOutOfBoundsException() { |
585 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
586 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
587 |
|
|
for (List list : lists) { |
588 |
|
|
try { |
589 |
|
|
list.add(-1, "qwerty"); |
590 |
|
|
shouldThrow(); |
591 |
|
|
} catch (IndexOutOfBoundsException success) {} |
592 |
|
|
} |
593 |
dl |
1.1 |
} |
594 |
jsr166 |
1.8 |
|
595 |
dl |
1.1 |
/** |
596 |
jsr166 |
1.18 |
* add throws an IndexOutOfBoundsException on a too high index |
597 |
dl |
1.1 |
*/ |
598 |
dl |
1.4 |
public void testAdd2_IndexOutOfBoundsException() { |
599 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
600 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
601 |
|
|
for (List list : lists) { |
602 |
|
|
try { |
603 |
|
|
list.add(list.size() + 1, "qwerty"); |
604 |
|
|
shouldThrow(); |
605 |
|
|
} catch (IndexOutOfBoundsException success) {} |
606 |
|
|
} |
607 |
dl |
1.1 |
} |
608 |
|
|
|
609 |
|
|
/** |
610 |
jsr166 |
1.18 |
* remove throws an IndexOutOfBoundsException on a negative index |
611 |
dl |
1.1 |
*/ |
612 |
dl |
1.4 |
public void testRemove1_IndexOutOfBounds() { |
613 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
614 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
615 |
|
|
for (List list : lists) { |
616 |
|
|
try { |
617 |
|
|
list.remove(-1); |
618 |
|
|
shouldThrow(); |
619 |
|
|
} catch (IndexOutOfBoundsException success) {} |
620 |
|
|
} |
621 |
dl |
1.1 |
} |
622 |
|
|
|
623 |
|
|
/** |
624 |
jsr166 |
1.18 |
* remove throws an IndexOutOfBoundsException on a too high index |
625 |
dl |
1.1 |
*/ |
626 |
dl |
1.4 |
public void testRemove2_IndexOutOfBounds() { |
627 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
628 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
629 |
|
|
for (List list : lists) { |
630 |
|
|
try { |
631 |
|
|
list.remove(list.size()); |
632 |
|
|
shouldThrow(); |
633 |
|
|
} catch (IndexOutOfBoundsException success) {} |
634 |
|
|
} |
635 |
dl |
1.1 |
} |
636 |
jsr166 |
1.8 |
|
637 |
dl |
1.1 |
/** |
638 |
jsr166 |
1.18 |
* addAll throws an IndexOutOfBoundsException on a negative index |
639 |
dl |
1.1 |
*/ |
640 |
dl |
1.4 |
public void testAddAll1_IndexOutOfBoundsException() { |
641 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
642 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
643 |
|
|
for (List list : lists) { |
644 |
|
|
try { |
645 |
|
|
list.addAll(-1, new LinkedList()); |
646 |
|
|
shouldThrow(); |
647 |
|
|
} catch (IndexOutOfBoundsException success) {} |
648 |
|
|
} |
649 |
dl |
1.1 |
} |
650 |
jsr166 |
1.8 |
|
651 |
dl |
1.1 |
/** |
652 |
jsr166 |
1.18 |
* addAll throws an IndexOutOfBoundsException on a too high index |
653 |
dl |
1.1 |
*/ |
654 |
dl |
1.4 |
public void testAddAll2_IndexOutOfBoundsException() { |
655 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
656 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
657 |
|
|
for (List list : lists) { |
658 |
|
|
try { |
659 |
|
|
list.addAll(list.size() + 1, new LinkedList()); |
660 |
|
|
shouldThrow(); |
661 |
|
|
} catch (IndexOutOfBoundsException success) {} |
662 |
|
|
} |
663 |
dl |
1.1 |
} |
664 |
|
|
|
665 |
|
|
/** |
666 |
jsr166 |
1.18 |
* listIterator throws an IndexOutOfBoundsException on a negative index |
667 |
dl |
1.1 |
*/ |
668 |
dl |
1.4 |
public void testListIterator1_IndexOutOfBoundsException() { |
669 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
670 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
671 |
|
|
for (List list : lists) { |
672 |
|
|
try { |
673 |
|
|
list.listIterator(-1); |
674 |
|
|
shouldThrow(); |
675 |
|
|
} catch (IndexOutOfBoundsException success) {} |
676 |
|
|
} |
677 |
dl |
1.1 |
} |
678 |
|
|
|
679 |
|
|
/** |
680 |
jsr166 |
1.18 |
* listIterator throws an IndexOutOfBoundsException on a too high index |
681 |
dl |
1.1 |
*/ |
682 |
dl |
1.4 |
public void testListIterator2_IndexOutOfBoundsException() { |
683 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
684 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
685 |
|
|
for (List list : lists) { |
686 |
|
|
try { |
687 |
|
|
list.listIterator(list.size() + 1); |
688 |
|
|
shouldThrow(); |
689 |
|
|
} catch (IndexOutOfBoundsException success) {} |
690 |
|
|
} |
691 |
dl |
1.1 |
} |
692 |
|
|
|
693 |
|
|
/** |
694 |
jsr166 |
1.18 |
* subList throws an IndexOutOfBoundsException on a negative index |
695 |
dl |
1.1 |
*/ |
696 |
dl |
1.4 |
public void testSubList1_IndexOutOfBoundsException() { |
697 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
698 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
699 |
|
|
for (List list : lists) { |
700 |
|
|
try { |
701 |
|
|
list.subList(-1, list.size()); |
702 |
|
|
shouldThrow(); |
703 |
|
|
} catch (IndexOutOfBoundsException success) {} |
704 |
|
|
} |
705 |
dl |
1.1 |
} |
706 |
|
|
|
707 |
|
|
/** |
708 |
jsr166 |
1.18 |
* subList throws an IndexOutOfBoundsException on a too high index |
709 |
dl |
1.1 |
*/ |
710 |
dl |
1.4 |
public void testSubList2_IndexOutOfBoundsException() { |
711 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
712 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
713 |
|
|
for (List list : lists) { |
714 |
|
|
try { |
715 |
|
|
list.subList(0, list.size() + 1); |
716 |
|
|
shouldThrow(); |
717 |
|
|
} catch (IndexOutOfBoundsException success) {} |
718 |
|
|
} |
719 |
dl |
1.1 |
} |
720 |
|
|
|
721 |
|
|
/** |
722 |
jsr166 |
1.18 |
* subList throws IndexOutOfBoundsException when the second index |
723 |
|
|
* is lower then the first |
724 |
dl |
1.1 |
*/ |
725 |
dl |
1.4 |
public void testSubList3_IndexOutOfBoundsException() { |
726 |
jsr166 |
1.30 |
CopyOnWriteArrayList c = populatedArray(5); |
727 |
|
|
List[] lists = { c, c.subList(1, c.size() - 1) }; |
728 |
|
|
for (List list : lists) { |
729 |
|
|
try { |
730 |
|
|
list.subList(list.size() - 1, 1); |
731 |
|
|
shouldThrow(); |
732 |
|
|
} catch (IndexOutOfBoundsException success) {} |
733 |
|
|
} |
734 |
dl |
1.2 |
} |
735 |
|
|
|
736 |
dl |
1.4 |
/** |
737 |
jsr166 |
1.16 |
* a deserialized serialized list is equal |
738 |
dl |
1.4 |
*/ |
739 |
jsr166 |
1.12 |
public void testSerialization() throws Exception { |
740 |
jsr166 |
1.22 |
List x = populatedArray(SIZE); |
741 |
|
|
List y = serialClone(x); |
742 |
dl |
1.2 |
|
743 |
jsr166 |
1.29 |
assertNotSame(x, y); |
744 |
jsr166 |
1.22 |
assertEquals(x.size(), y.size()); |
745 |
|
|
assertEquals(x.toString(), y.toString()); |
746 |
|
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
747 |
|
|
assertEquals(x, y); |
748 |
|
|
assertEquals(y, x); |
749 |
|
|
while (!x.isEmpty()) { |
750 |
|
|
assertFalse(y.isEmpty()); |
751 |
|
|
assertEquals(x.remove(0), y.remove(0)); |
752 |
|
|
} |
753 |
|
|
assertTrue(y.isEmpty()); |
754 |
dl |
1.1 |
} |
755 |
jsr166 |
1.8 |
|
756 |
dl |
1.1 |
} |