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.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 |
|
|
import java.util.Vector; |
18 |
|
|
import java.util.concurrent.CopyOnWriteArrayList; |
19 |
dl |
1.1 |
|
20 |
jsr166 |
1.10 |
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
21 |
jsr166 |
1.8 |
|
22 |
dl |
1.1 |
public static void main(String[] args) { |
23 |
jsr166 |
1.17 |
junit.textui.TestRunner.run(suite()); |
24 |
dl |
1.1 |
} |
25 |
|
|
|
26 |
|
|
public static Test suite() { |
27 |
jsr166 |
1.11 |
return new TestSuite(CopyOnWriteArrayListTest.class); |
28 |
dl |
1.1 |
} |
29 |
|
|
|
30 |
jsr166 |
1.25 |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
31 |
jsr166 |
1.26 |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
32 |
dl |
1.1 |
assertTrue(a.isEmpty()); |
33 |
jsr166 |
1.26 |
for (int i = 0; i < n; i++) |
34 |
jsr166 |
1.25 |
a.add(i); |
35 |
dl |
1.1 |
assertFalse(a.isEmpty()); |
36 |
|
|
assertEquals(n, a.size()); |
37 |
|
|
return a; |
38 |
|
|
} |
39 |
|
|
|
40 |
jsr166 |
1.26 |
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
41 |
|
|
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
42 |
|
|
assertTrue(a.isEmpty()); |
43 |
|
|
for (int i = 0; i < elements.length; i++) |
44 |
|
|
a.add(elements[i]); |
45 |
|
|
assertFalse(a.isEmpty()); |
46 |
|
|
assertEquals(elements.length, a.size()); |
47 |
|
|
return a; |
48 |
|
|
} |
49 |
|
|
|
50 |
dl |
1.4 |
/** |
51 |
dl |
1.5 |
* a new list is empty |
52 |
dl |
1.4 |
*/ |
53 |
dl |
1.3 |
public void testConstructor() { |
54 |
jsr166 |
1.11 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
55 |
dl |
1.3 |
assertTrue(a.isEmpty()); |
56 |
|
|
} |
57 |
|
|
|
58 |
dl |
1.4 |
/** |
59 |
dl |
1.5 |
* new list contains all elements of initializing array |
60 |
dl |
1.4 |
*/ |
61 |
dl |
1.3 |
public void testConstructor2() { |
62 |
|
|
Integer[] ints = new Integer[SIZE]; |
63 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
64 |
|
|
ints[i] = new Integer(i); |
65 |
jsr166 |
1.11 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
66 |
jsr166 |
1.8 |
for (int i = 0; i < SIZE; ++i) |
67 |
dl |
1.3 |
assertEquals(ints[i], a.get(i)); |
68 |
|
|
} |
69 |
|
|
|
70 |
dl |
1.4 |
/** |
71 |
dl |
1.5 |
* new list contains all elements of initializing collection |
72 |
dl |
1.4 |
*/ |
73 |
dl |
1.3 |
public void testConstructor3() { |
74 |
|
|
Integer[] ints = new Integer[SIZE]; |
75 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
76 |
|
|
ints[i] = new Integer(i); |
77 |
jsr166 |
1.11 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
78 |
jsr166 |
1.8 |
for (int i = 0; i < SIZE; ++i) |
79 |
dl |
1.3 |
assertEquals(ints[i], a.get(i)); |
80 |
|
|
} |
81 |
jsr166 |
1.8 |
|
82 |
dl |
1.1 |
/** |
83 |
jsr166 |
1.18 |
* addAll adds each element from the given collection |
84 |
dl |
1.1 |
*/ |
85 |
dl |
1.4 |
public void testAddAll() { |
86 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
87 |
|
|
Vector v = new Vector(); |
88 |
|
|
v.add(three); |
89 |
|
|
v.add(four); |
90 |
|
|
v.add(five); |
91 |
|
|
full.addAll(v); |
92 |
|
|
assertEquals(6, full.size()); |
93 |
dl |
1.1 |
} |
94 |
|
|
|
95 |
|
|
/** |
96 |
jsr166 |
1.18 |
* addAllAbsent adds each element from the given collection that did not |
97 |
|
|
* already exist in the List |
98 |
dl |
1.1 |
*/ |
99 |
dl |
1.4 |
public void testAddAllAbsent() { |
100 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
101 |
|
|
Vector v = new Vector(); |
102 |
|
|
v.add(three); |
103 |
|
|
v.add(four); |
104 |
|
|
v.add(one); // will not add this element |
105 |
|
|
full.addAllAbsent(v); |
106 |
|
|
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 |
|
|
Vector v = new Vector(); |
196 |
|
|
v.add(one); |
197 |
|
|
v.add(two); |
198 |
|
|
assertTrue(full.containsAll(v)); |
199 |
|
|
v.add(six); |
200 |
|
|
assertFalse(full.containsAll(v)); |
201 |
dl |
1.1 |
} |
202 |
|
|
|
203 |
|
|
/** |
204 |
jsr166 |
1.18 |
* get returns the value at the given index |
205 |
dl |
1.1 |
*/ |
206 |
dl |
1.4 |
public void testGet() { |
207 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
208 |
jsr166 |
1.14 |
assertEquals(0, full.get(0)); |
209 |
dl |
1.1 |
} |
210 |
|
|
|
211 |
|
|
/** |
212 |
jsr166 |
1.18 |
* indexOf gives the index for the given object |
213 |
dl |
1.1 |
*/ |
214 |
dl |
1.4 |
public void testIndexOf() { |
215 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
216 |
|
|
assertEquals(1, full.indexOf(one)); |
217 |
|
|
assertEquals(-1, full.indexOf("puppies")); |
218 |
dl |
1.1 |
} |
219 |
|
|
|
220 |
|
|
/** |
221 |
jsr166 |
1.18 |
* indexOf gives the index based on the given index |
222 |
|
|
* at which to start searching |
223 |
dl |
1.1 |
*/ |
224 |
dl |
1.4 |
public void testIndexOf2() { |
225 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
226 |
|
|
assertEquals(1, full.indexOf(one, 0)); |
227 |
|
|
assertEquals(-1, full.indexOf(one, 2)); |
228 |
dl |
1.1 |
} |
229 |
|
|
|
230 |
|
|
/** |
231 |
jsr166 |
1.18 |
* isEmpty returns true when empty, else false |
232 |
dl |
1.1 |
*/ |
233 |
dl |
1.4 |
public void testIsEmpty() { |
234 |
jsr166 |
1.11 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
235 |
|
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
236 |
|
|
assertTrue(empty.isEmpty()); |
237 |
|
|
assertFalse(full.isEmpty()); |
238 |
dl |
1.1 |
} |
239 |
|
|
|
240 |
|
|
/** |
241 |
jsr166 |
1.18 |
* iterator() returns an iterator containing the elements of the list |
242 |
dl |
1.1 |
*/ |
243 |
dl |
1.4 |
public void testIterator() { |
244 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
245 |
|
|
Iterator i = full.iterator(); |
246 |
|
|
int j; |
247 |
|
|
for (j = 0; i.hasNext(); j++) |
248 |
jsr166 |
1.14 |
assertEquals(j, i.next()); |
249 |
jsr166 |
1.11 |
assertEquals(SIZE, j); |
250 |
dl |
1.1 |
} |
251 |
|
|
|
252 |
dl |
1.4 |
/** |
253 |
dl |
1.5 |
* iterator.remove throws UnsupportedOperationException |
254 |
dl |
1.4 |
*/ |
255 |
jsr166 |
1.17 |
public void testIteratorRemove() { |
256 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
257 |
dl |
1.1 |
Iterator it = full.iterator(); |
258 |
|
|
it.next(); |
259 |
|
|
try { |
260 |
|
|
it.remove(); |
261 |
dl |
1.4 |
shouldThrow(); |
262 |
jsr166 |
1.12 |
} catch (UnsupportedOperationException success) {} |
263 |
dl |
1.1 |
} |
264 |
|
|
|
265 |
dl |
1.4 |
/** |
266 |
dl |
1.5 |
* toString contains toString of elements |
267 |
dl |
1.4 |
*/ |
268 |
|
|
public void testToString() { |
269 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
270 |
dl |
1.1 |
String s = full.toString(); |
271 |
|
|
for (int i = 0; i < 3; ++i) { |
272 |
jsr166 |
1.20 |
assertTrue(s.contains(String.valueOf(i))); |
273 |
dl |
1.1 |
} |
274 |
jsr166 |
1.8 |
} |
275 |
dl |
1.1 |
|
276 |
|
|
/** |
277 |
jsr166 |
1.18 |
* lastIndexOf returns the index for the given object |
278 |
dl |
1.1 |
*/ |
279 |
dl |
1.4 |
public void testLastIndexOf1() { |
280 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
281 |
|
|
full.add(one); |
282 |
|
|
full.add(three); |
283 |
|
|
assertEquals(3, full.lastIndexOf(one)); |
284 |
|
|
assertEquals(-1, full.lastIndexOf(six)); |
285 |
dl |
1.1 |
} |
286 |
|
|
|
287 |
|
|
/** |
288 |
jsr166 |
1.18 |
* lastIndexOf returns the index from the given starting point |
289 |
dl |
1.1 |
*/ |
290 |
jsr166 |
1.21 |
public void testLastIndexOf2() { |
291 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
292 |
|
|
full.add(one); |
293 |
|
|
full.add(three); |
294 |
|
|
assertEquals(3, full.lastIndexOf(one, 4)); |
295 |
|
|
assertEquals(-1, full.lastIndexOf(three, 3)); |
296 |
dl |
1.1 |
} |
297 |
|
|
|
298 |
|
|
/** |
299 |
jsr166 |
1.18 |
* listIterator traverses all elements |
300 |
dl |
1.1 |
*/ |
301 |
dl |
1.4 |
public void testListIterator1() { |
302 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
303 |
|
|
ListIterator i = full.listIterator(); |
304 |
|
|
int j; |
305 |
|
|
for (j = 0; i.hasNext(); j++) |
306 |
jsr166 |
1.14 |
assertEquals(j, i.next()); |
307 |
jsr166 |
1.11 |
assertEquals(SIZE, j); |
308 |
dl |
1.1 |
} |
309 |
|
|
|
310 |
|
|
/** |
311 |
jsr166 |
1.18 |
* listIterator only returns those elements after the given index |
312 |
dl |
1.1 |
*/ |
313 |
dl |
1.4 |
public void testListIterator2() { |
314 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
315 |
|
|
ListIterator i = full.listIterator(1); |
316 |
|
|
int j; |
317 |
|
|
for (j = 0; i.hasNext(); j++) |
318 |
jsr166 |
1.14 |
assertEquals(j+1, i.next()); |
319 |
jsr166 |
1.11 |
assertEquals(2, j); |
320 |
dl |
1.1 |
} |
321 |
|
|
|
322 |
|
|
/** |
323 |
jsr166 |
1.18 |
* remove removes and returns the object at the given index |
324 |
dl |
1.1 |
*/ |
325 |
dl |
1.4 |
public void testRemove() { |
326 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
327 |
jsr166 |
1.15 |
assertEquals(2, full.remove(2)); |
328 |
jsr166 |
1.11 |
assertEquals(2, full.size()); |
329 |
dl |
1.1 |
} |
330 |
|
|
|
331 |
|
|
/** |
332 |
jsr166 |
1.18 |
* removeAll removes all elements from the given collection |
333 |
dl |
1.1 |
*/ |
334 |
dl |
1.4 |
public void testRemoveAll() { |
335 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
336 |
|
|
Vector v = new Vector(); |
337 |
|
|
v.add(one); |
338 |
|
|
v.add(two); |
339 |
|
|
full.removeAll(v); |
340 |
|
|
assertEquals(1, full.size()); |
341 |
dl |
1.1 |
} |
342 |
|
|
|
343 |
|
|
/** |
344 |
jsr166 |
1.18 |
* set changes the element at the given index |
345 |
dl |
1.1 |
*/ |
346 |
dl |
1.4 |
public void testSet() { |
347 |
jsr166 |
1.11 |
CopyOnWriteArrayList full = populatedArray(3); |
348 |
jsr166 |
1.15 |
assertEquals(2, full.set(2, four)); |
349 |
jsr166 |
1.14 |
assertEquals(4, full.get(2)); |
350 |
dl |
1.1 |
} |
351 |
|
|
|
352 |
|
|
/** |
353 |
jsr166 |
1.18 |
* size returns the number of elements |
354 |
dl |
1.1 |
*/ |
355 |
dl |
1.4 |
public void testSize() { |
356 |
jsr166 |
1.11 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
357 |
|
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
358 |
|
|
assertEquals(SIZE, full.size()); |
359 |
|
|
assertEquals(0, empty.size()); |
360 |
dl |
1.1 |
} |
361 |
|
|
|
362 |
|
|
/** |
363 |
jsr166 |
1.26 |
* toArray() returns an Object array containing all elements from |
364 |
|
|
* the list in insertion order |
365 |
dl |
1.1 |
*/ |
366 |
dl |
1.4 |
public void testToArray() { |
367 |
jsr166 |
1.26 |
Object[] a = new CopyOnWriteArrayList().toArray(); |
368 |
|
|
assertTrue(Arrays.equals(new Object[0], a)); |
369 |
|
|
assertSame(Object[].class, a.getClass()); |
370 |
|
|
|
371 |
|
|
Integer[] elements = new Integer[SIZE]; |
372 |
|
|
for (int i = 0; i < SIZE; i++) |
373 |
|
|
elements[i] = i; |
374 |
|
|
Collections.shuffle(Arrays.asList(elements)); |
375 |
|
|
Collection<Integer> full = populatedArray(elements); |
376 |
|
|
|
377 |
|
|
assertTrue(Arrays.equals(elements, full.toArray())); |
378 |
|
|
assertSame(Object[].class, full.toArray().getClass()); |
379 |
dl |
1.1 |
} |
380 |
|
|
|
381 |
|
|
/** |
382 |
jsr166 |
1.26 |
* toArray(Integer array) returns an Integer array containing all |
383 |
|
|
* elements from the list in insertion order |
384 |
dl |
1.1 |
*/ |
385 |
dl |
1.4 |
public void testToArray2() { |
386 |
jsr166 |
1.26 |
Collection empty = new CopyOnWriteArrayList(); |
387 |
|
|
Integer[] a; |
388 |
|
|
|
389 |
|
|
a = new Integer[0]; |
390 |
|
|
assertSame(a, empty.toArray(a)); |
391 |
|
|
|
392 |
|
|
a = new Integer[SIZE/2]; |
393 |
|
|
Arrays.fill(a, 42); |
394 |
|
|
assertSame(a, empty.toArray(a)); |
395 |
|
|
assertNull(a[0]); |
396 |
|
|
for (int i = 1; i < a.length; i++) |
397 |
|
|
assertEquals(42, (int) a[i]); |
398 |
|
|
|
399 |
|
|
Integer[] elements = new Integer[SIZE]; |
400 |
|
|
for (int i = 0; i < SIZE; i++) |
401 |
|
|
elements[i] = i; |
402 |
|
|
Collections.shuffle(Arrays.asList(elements)); |
403 |
|
|
Collection<Integer> full = populatedArray(elements); |
404 |
|
|
|
405 |
|
|
Arrays.fill(a, 42); |
406 |
|
|
assertTrue(Arrays.equals(elements, full.toArray(a))); |
407 |
|
|
for (int i = 0; i < a.length; i++) |
408 |
|
|
assertEquals(42, (int) a[i]); |
409 |
|
|
assertSame(Integer[].class, full.toArray(a).getClass()); |
410 |
|
|
|
411 |
|
|
a = new Integer[SIZE]; |
412 |
|
|
Arrays.fill(a, 42); |
413 |
|
|
assertSame(a, full.toArray(a)); |
414 |
|
|
assertTrue(Arrays.equals(elements, a)); |
415 |
|
|
|
416 |
|
|
a = new Integer[2*SIZE]; |
417 |
|
|
Arrays.fill(a, 42); |
418 |
|
|
assertSame(a, full.toArray(a)); |
419 |
|
|
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
420 |
|
|
assertNull(a[SIZE]); |
421 |
|
|
for (int i = SIZE + 1; i < a.length; i++) |
422 |
|
|
assertEquals(42, (int) a[i]); |
423 |
dl |
1.1 |
} |
424 |
|
|
|
425 |
dl |
1.4 |
/** |
426 |
dl |
1.5 |
* sublists contains elements at indexes offset from their base |
427 |
dl |
1.4 |
*/ |
428 |
dl |
1.1 |
public void testSubList() { |
429 |
jsr166 |
1.11 |
CopyOnWriteArrayList a = populatedArray(10); |
430 |
dl |
1.1 |
assertTrue(a.subList(1,1).isEmpty()); |
431 |
jsr166 |
1.11 |
for (int j = 0; j < 9; ++j) { |
432 |
|
|
for (int i = j ; i < 10; ++i) { |
433 |
|
|
List b = a.subList(j,i); |
434 |
|
|
for (int k = j; k < i; ++k) { |
435 |
|
|
assertEquals(new Integer(k), b.get(k-j)); |
436 |
|
|
} |
437 |
|
|
} |
438 |
|
|
} |
439 |
dl |
1.1 |
|
440 |
jsr166 |
1.11 |
List s = a.subList(2, 5); |
441 |
jsr166 |
1.24 |
assertEquals(3, s.size()); |
442 |
dl |
1.3 |
s.set(2, m1); |
443 |
dl |
1.1 |
assertEquals(a.get(4), m1); |
444 |
jsr166 |
1.11 |
s.clear(); |
445 |
jsr166 |
1.24 |
assertEquals(7, a.size()); |
446 |
dl |
1.1 |
} |
447 |
|
|
|
448 |
|
|
// Exception tests |
449 |
|
|
|
450 |
|
|
/** |
451 |
jsr166 |
1.18 |
* toArray throws an ArrayStoreException when the given array |
452 |
|
|
* can not store the objects inside the list |
453 |
dl |
1.1 |
*/ |
454 |
dl |
1.4 |
public void testToArray_ArrayStoreException() { |
455 |
|
|
try { |
456 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
457 |
|
|
c.add("zfasdfsdf"); |
458 |
|
|
c.add("asdadasd"); |
459 |
|
|
c.toArray(new Long[5]); |
460 |
jsr166 |
1.11 |
shouldThrow(); |
461 |
jsr166 |
1.13 |
} catch (ArrayStoreException success) {} |
462 |
dl |
1.1 |
} |
463 |
|
|
|
464 |
|
|
/** |
465 |
jsr166 |
1.18 |
* get throws an IndexOutOfBoundsException on a negative index |
466 |
dl |
1.1 |
*/ |
467 |
dl |
1.4 |
public void testGet1_IndexOutOfBoundsException() { |
468 |
|
|
try { |
469 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
470 |
|
|
c.get(-1); |
471 |
dl |
1.4 |
shouldThrow(); |
472 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
473 |
dl |
1.1 |
} |
474 |
jsr166 |
1.8 |
|
475 |
dl |
1.1 |
/** |
476 |
jsr166 |
1.18 |
* get throws an IndexOutOfBoundsException on a too high index |
477 |
dl |
1.1 |
*/ |
478 |
dl |
1.4 |
public void testGet2_IndexOutOfBoundsException() { |
479 |
|
|
try { |
480 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
481 |
|
|
c.add("asdasd"); |
482 |
|
|
c.add("asdad"); |
483 |
|
|
c.get(100); |
484 |
dl |
1.4 |
shouldThrow(); |
485 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
486 |
dl |
1.1 |
} |
487 |
|
|
|
488 |
|
|
/** |
489 |
jsr166 |
1.18 |
* set throws an IndexOutOfBoundsException on a negative index |
490 |
dl |
1.1 |
*/ |
491 |
dl |
1.4 |
public void testSet1_IndexOutOfBoundsException() { |
492 |
|
|
try { |
493 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
494 |
|
|
c.set(-1,"qwerty"); |
495 |
dl |
1.4 |
shouldThrow(); |
496 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
497 |
dl |
1.1 |
} |
498 |
jsr166 |
1.8 |
|
499 |
dl |
1.1 |
/** |
500 |
jsr166 |
1.18 |
* set throws an IndexOutOfBoundsException on a too high index |
501 |
dl |
1.1 |
*/ |
502 |
dl |
1.4 |
public void testSet2() { |
503 |
|
|
try { |
504 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
505 |
|
|
c.add("asdasd"); |
506 |
|
|
c.add("asdad"); |
507 |
|
|
c.set(100, "qwerty"); |
508 |
dl |
1.4 |
shouldThrow(); |
509 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
510 |
dl |
1.1 |
} |
511 |
|
|
|
512 |
|
|
/** |
513 |
jsr166 |
1.18 |
* add throws an IndexOutOfBoundsException on a negative index |
514 |
dl |
1.1 |
*/ |
515 |
dl |
1.4 |
public void testAdd1_IndexOutOfBoundsException() { |
516 |
|
|
try { |
517 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
518 |
|
|
c.add(-1,"qwerty"); |
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 |
* add throws an IndexOutOfBoundsException on a too high index |
525 |
dl |
1.1 |
*/ |
526 |
dl |
1.4 |
public void testAdd2_IndexOutOfBoundsException() { |
527 |
|
|
try { |
528 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
529 |
|
|
c.add("asdasd"); |
530 |
|
|
c.add("asdasdasd"); |
531 |
|
|
c.add(100, "qwerty"); |
532 |
dl |
1.4 |
shouldThrow(); |
533 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
534 |
dl |
1.1 |
} |
535 |
|
|
|
536 |
|
|
/** |
537 |
jsr166 |
1.18 |
* remove throws an IndexOutOfBoundsException on a negative index |
538 |
dl |
1.1 |
*/ |
539 |
dl |
1.4 |
public void testRemove1_IndexOutOfBounds() { |
540 |
|
|
try { |
541 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
542 |
|
|
c.remove(-1); |
543 |
dl |
1.4 |
shouldThrow(); |
544 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
545 |
dl |
1.1 |
} |
546 |
|
|
|
547 |
|
|
/** |
548 |
jsr166 |
1.18 |
* remove throws an IndexOutOfBoundsException on a too high index |
549 |
dl |
1.1 |
*/ |
550 |
dl |
1.4 |
public void testRemove2_IndexOutOfBounds() { |
551 |
|
|
try { |
552 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
553 |
|
|
c.add("asdasd"); |
554 |
|
|
c.add("adasdasd"); |
555 |
|
|
c.remove(100); |
556 |
dl |
1.4 |
shouldThrow(); |
557 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
558 |
dl |
1.1 |
} |
559 |
jsr166 |
1.8 |
|
560 |
dl |
1.1 |
/** |
561 |
jsr166 |
1.18 |
* addAll throws an IndexOutOfBoundsException on a negative index |
562 |
dl |
1.1 |
*/ |
563 |
dl |
1.4 |
public void testAddAll1_IndexOutOfBoundsException() { |
564 |
|
|
try { |
565 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
566 |
|
|
c.addAll(-1,new LinkedList()); |
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 |
* addAll throws an IndexOutOfBoundsException on a too high index |
573 |
dl |
1.1 |
*/ |
574 |
dl |
1.4 |
public void testAddAll2_IndexOutOfBoundsException() { |
575 |
|
|
try { |
576 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
577 |
|
|
c.add("asdasd"); |
578 |
|
|
c.add("asdasdasd"); |
579 |
|
|
c.addAll(100, new LinkedList()); |
580 |
dl |
1.4 |
shouldThrow(); |
581 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
582 |
dl |
1.1 |
} |
583 |
|
|
|
584 |
|
|
/** |
585 |
jsr166 |
1.18 |
* listIterator throws an IndexOutOfBoundsException on a negative index |
586 |
dl |
1.1 |
*/ |
587 |
dl |
1.4 |
public void testListIterator1_IndexOutOfBoundsException() { |
588 |
|
|
try { |
589 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
590 |
|
|
c.listIterator(-1); |
591 |
dl |
1.4 |
shouldThrow(); |
592 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
593 |
dl |
1.1 |
} |
594 |
|
|
|
595 |
|
|
/** |
596 |
jsr166 |
1.18 |
* listIterator throws an IndexOutOfBoundsException on a too high index |
597 |
dl |
1.1 |
*/ |
598 |
dl |
1.4 |
public void testListIterator2_IndexOutOfBoundsException() { |
599 |
|
|
try { |
600 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
601 |
|
|
c.add("adasd"); |
602 |
|
|
c.add("asdasdas"); |
603 |
|
|
c.listIterator(100); |
604 |
dl |
1.4 |
shouldThrow(); |
605 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
606 |
dl |
1.1 |
} |
607 |
|
|
|
608 |
|
|
/** |
609 |
jsr166 |
1.18 |
* subList throws an IndexOutOfBoundsException on a negative index |
610 |
dl |
1.1 |
*/ |
611 |
dl |
1.4 |
public void testSubList1_IndexOutOfBoundsException() { |
612 |
|
|
try { |
613 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
614 |
|
|
c.subList(-1,100); |
615 |
dl |
1.4 |
shouldThrow(); |
616 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
617 |
dl |
1.1 |
} |
618 |
|
|
|
619 |
|
|
/** |
620 |
jsr166 |
1.18 |
* subList throws an IndexOutOfBoundsException on a too high index |
621 |
dl |
1.1 |
*/ |
622 |
dl |
1.4 |
public void testSubList2_IndexOutOfBoundsException() { |
623 |
|
|
try { |
624 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
625 |
|
|
c.add("asdasd"); |
626 |
|
|
c.subList(1,100); |
627 |
dl |
1.4 |
shouldThrow(); |
628 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
629 |
dl |
1.1 |
} |
630 |
|
|
|
631 |
|
|
/** |
632 |
jsr166 |
1.18 |
* subList throws IndexOutOfBoundsException when the second index |
633 |
|
|
* is lower then the first |
634 |
dl |
1.1 |
*/ |
635 |
dl |
1.4 |
public void testSubList3_IndexOutOfBoundsException() { |
636 |
|
|
try { |
637 |
dl |
1.1 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
638 |
|
|
c.subList(3,1); |
639 |
dl |
1.4 |
shouldThrow(); |
640 |
jsr166 |
1.13 |
} catch (IndexOutOfBoundsException success) {} |
641 |
dl |
1.2 |
} |
642 |
|
|
|
643 |
dl |
1.4 |
/** |
644 |
jsr166 |
1.16 |
* a deserialized serialized list is equal |
645 |
dl |
1.4 |
*/ |
646 |
jsr166 |
1.12 |
public void testSerialization() throws Exception { |
647 |
jsr166 |
1.22 |
List x = populatedArray(SIZE); |
648 |
|
|
List y = serialClone(x); |
649 |
dl |
1.2 |
|
650 |
jsr166 |
1.22 |
assertTrue(x != y); |
651 |
|
|
assertEquals(x.size(), y.size()); |
652 |
|
|
assertEquals(x.toString(), y.toString()); |
653 |
|
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
654 |
|
|
assertEquals(x, y); |
655 |
|
|
assertEquals(y, x); |
656 |
|
|
while (!x.isEmpty()) { |
657 |
|
|
assertFalse(y.isEmpty()); |
658 |
|
|
assertEquals(x.remove(0), y.remove(0)); |
659 |
|
|
} |
660 |
|
|
assertTrue(y.isEmpty()); |
661 |
dl |
1.1 |
} |
662 |
jsr166 |
1.8 |
|
663 |
dl |
1.1 |
} |