1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* 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 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
12 |
|
import java.io.*; |
13 |
|
|
14 |
|
public class CopyOnWriteArrayListTest extends JSR166TestCase{ |
15 |
< |
|
15 |
> |
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
|
20 |
|
public static Test suite() { |
24 |
|
static CopyOnWriteArrayList populatedArray(int n){ |
25 |
|
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
26 |
|
assertTrue(a.isEmpty()); |
27 |
< |
for (int i = 0; i < n; ++i) |
27 |
> |
for (int i = 0; i < n; ++i) |
28 |
|
a.add(new Integer(i)); |
29 |
|
assertFalse(a.isEmpty()); |
30 |
|
assertEquals(n, a.size()); |
33 |
|
|
34 |
|
|
35 |
|
/** |
36 |
< |
* |
36 |
> |
* a new list is empty |
37 |
|
*/ |
38 |
|
public void testConstructor() { |
39 |
|
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
41 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
< |
* |
44 |
> |
* new list contains all elements of initializing array |
45 |
|
*/ |
46 |
|
public void testConstructor2() { |
47 |
|
Integer[] ints = new Integer[SIZE]; |
48 |
|
for (int i = 0; i < SIZE-1; ++i) |
49 |
|
ints[i] = new Integer(i); |
50 |
|
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
51 |
< |
for (int i = 0; i < SIZE; ++i) |
51 |
> |
for (int i = 0; i < SIZE; ++i) |
52 |
|
assertEquals(ints[i], a.get(i)); |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* |
56 |
> |
* new list contains all elements of initializing collection |
57 |
|
*/ |
58 |
|
public void testConstructor3() { |
59 |
|
Integer[] ints = new Integer[SIZE]; |
60 |
|
for (int i = 0; i < SIZE-1; ++i) |
61 |
|
ints[i] = new Integer(i); |
62 |
|
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
63 |
< |
for (int i = 0; i < SIZE; ++i) |
63 |
> |
for (int i = 0; i < SIZE; ++i) |
64 |
|
assertEquals(ints[i], a.get(i)); |
65 |
|
} |
66 |
< |
|
66 |
> |
|
67 |
|
|
68 |
|
/** |
69 |
< |
* addAll correctly adds each element from the given collection |
69 |
> |
* addAll adds each element from the given collection |
70 |
|
*/ |
71 |
|
public void testAddAll() { |
72 |
|
CopyOnWriteArrayList full = populatedArray(3); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* addIfAbsent correctly adds the element when it does not exist in the list |
105 |
> |
* addIfAbsent adds the element when it does not exist in the list |
106 |
|
*/ |
107 |
|
public void testAddIfAbsent2() { |
108 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
111 |
|
} |
112 |
|
|
113 |
|
/** |
114 |
< |
* clear correctly removes all elements from the list |
114 |
> |
* clear removes all elements from the list |
115 |
|
*/ |
116 |
|
public void testClear() { |
117 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
132 |
|
} |
133 |
|
|
134 |
|
/** |
135 |
< |
* contains returns the correct values |
135 |
> |
* contains is true for added elements |
136 |
|
*/ |
137 |
|
public void testContains() { |
138 |
|
CopyOnWriteArrayList full = populatedArray(3); |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
< |
* |
144 |
> |
* adding at an index places it in the indicated index |
145 |
|
*/ |
146 |
|
public void testAddIndex() { |
147 |
|
CopyOnWriteArrayList full = populatedArray(3); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
160 |
< |
* |
160 |
> |
* lists with same elements are equal and have same hashCode |
161 |
|
*/ |
162 |
|
public void testEquals() { |
163 |
|
CopyOnWriteArrayList a = populatedArray(3); |
174 |
|
assertEquals(a.hashCode(), b.hashCode()); |
175 |
|
} |
176 |
|
|
177 |
< |
|
177 |
> |
|
178 |
|
/** |
179 |
< |
* containsAll returns the correct values |
179 |
> |
* containsAll returns true for collection with subset of elements |
180 |
|
*/ |
181 |
|
public void testContainsAll() { |
182 |
|
CopyOnWriteArrayList full = populatedArray(3); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* get returns the correct value for the given index |
192 |
> |
* get returns the value at the given index |
193 |
|
*/ |
194 |
|
public void testGet() { |
195 |
|
CopyOnWriteArrayList full = populatedArray(3); |
197 |
|
} |
198 |
|
|
199 |
|
/** |
200 |
< |
* indexOf gives the correct index for the given object |
200 |
> |
* indexOf gives the index for the given object |
201 |
|
*/ |
202 |
|
public void testIndexOf() { |
203 |
|
CopyOnWriteArrayList full = populatedArray(3); |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
< |
* indexOf gives the correct index based on the given index |
209 |
> |
* indexOf gives the index based on the given index |
210 |
|
* at which to start searching |
211 |
|
*/ |
212 |
|
public void testIndexOf2() { |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* isEmpty returns the correct values |
219 |
> |
* isEmpty returns true when empty, else false |
220 |
|
*/ |
221 |
|
public void testIsEmpty() { |
222 |
|
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
226 |
|
} |
227 |
|
|
228 |
|
/** |
229 |
< |
* iterator() returns an iterator containing the elements of the list |
229 |
> |
* iterator() returns an iterator containing the elements of the list |
230 |
|
*/ |
231 |
|
public void testIterator() { |
232 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
238 |
|
} |
239 |
|
|
240 |
|
/** |
241 |
< |
* |
241 |
> |
* iterator.remove throws UnsupportedOperationException |
242 |
|
*/ |
243 |
|
public void testIteratorRemove () { |
244 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* |
255 |
> |
* toString contains toString of elements |
256 |
|
*/ |
257 |
|
public void testToString() { |
258 |
|
CopyOnWriteArrayList full = populatedArray(3); |
260 |
|
for (int i = 0; i < 3; ++i) { |
261 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
262 |
|
} |
263 |
< |
} |
263 |
> |
} |
264 |
|
|
265 |
|
/** |
266 |
< |
* lastIndexOf returns the correct index for the given object |
266 |
> |
* lastIndexOf returns the index for the given object |
267 |
|
*/ |
268 |
|
public void testLastIndexOf1() { |
269 |
|
CopyOnWriteArrayList full = populatedArray(3); |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
< |
* lastIndexOf returns the correct index from the given starting point |
277 |
> |
* lastIndexOf returns the index from the given starting point |
278 |
|
*/ |
279 |
|
public void testlastIndexOf2() { |
280 |
|
CopyOnWriteArrayList full = populatedArray(3); |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
< |
* Identical to testIterator, except ListInterator has more functionality |
288 |
> |
* listIterator traverses all elements |
289 |
|
*/ |
290 |
|
public void testListIterator1() { |
291 |
|
CopyOnWriteArrayList full = populatedArray(SIZE); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* remove correctly removes and returns the object at the given index |
312 |
> |
* remove removes and returns the object at the given index |
313 |
|
*/ |
314 |
|
public void testRemove() { |
315 |
|
CopyOnWriteArrayList full = populatedArray(3); |
318 |
|
} |
319 |
|
|
320 |
|
/** |
321 |
< |
* removeAll correctly removes all elements from the given collection |
321 |
> |
* removeAll removes all elements from the given collection |
322 |
|
*/ |
323 |
|
public void testRemoveAll() { |
324 |
|
CopyOnWriteArrayList full = populatedArray(3); |
330 |
|
} |
331 |
|
|
332 |
|
/** |
333 |
< |
* set correctly changes the element at the given index |
333 |
> |
* set changes the element at the given index |
334 |
|
*/ |
335 |
|
public void testSet() { |
336 |
|
CopyOnWriteArrayList full = populatedArray(3); |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
< |
* size returns the correct values |
342 |
> |
* size returns the number of elements |
343 |
|
*/ |
344 |
|
public void testSize() { |
345 |
|
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
376 |
|
|
377 |
|
|
378 |
|
/** |
379 |
< |
* |
379 |
> |
* sublists contains elements at indexes offset from their base |
380 |
|
*/ |
381 |
|
public void testSubList() { |
382 |
|
CopyOnWriteArrayList a = populatedArray(10); |
424 |
|
shouldThrow(); |
425 |
|
} catch(IndexOutOfBoundsException e){} |
426 |
|
} |
427 |
< |
|
427 |
> |
|
428 |
|
/** |
429 |
|
* get throws an IndexOutOfBoundsException on a too high index |
430 |
|
*/ |
448 |
|
shouldThrow(); |
449 |
|
} catch(IndexOutOfBoundsException e){} |
450 |
|
} |
451 |
< |
|
451 |
> |
|
452 |
|
/** |
453 |
|
* set throws an IndexOutOfBoundsException on a too high index |
454 |
|
*/ |
472 |
|
shouldThrow(); |
473 |
|
} catch(IndexOutOfBoundsException e){} |
474 |
|
} |
475 |
< |
|
475 |
> |
|
476 |
|
/** |
477 |
|
* add throws an IndexOutOfBoundsException on a too high index |
478 |
|
*/ |
509 |
|
shouldThrow(); |
510 |
|
} catch(IndexOutOfBoundsException e){} |
511 |
|
} |
512 |
< |
|
512 |
> |
|
513 |
|
/** |
514 |
|
* addAll throws an IndexOutOfBoundsException on a negative index |
515 |
|
*/ |
520 |
|
shouldThrow(); |
521 |
|
} catch(IndexOutOfBoundsException e){} |
522 |
|
} |
523 |
< |
|
523 |
> |
|
524 |
|
/** |
525 |
|
* addAll throws an IndexOutOfBoundsException on a too high index |
526 |
|
*/ |
584 |
|
|
585 |
|
/** |
586 |
|
* subList throws IndexOutOfBoundsException when the second index |
587 |
< |
* is lower then the first |
587 |
> |
* is lower then the first |
588 |
|
*/ |
589 |
|
public void testSubList3_IndexOutOfBoundsException() { |
590 |
|
try { |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* |
599 |
> |
* a deserialized serialiszed list is equal |
600 |
|
*/ |
601 |
|
public void testSerialization() { |
602 |
|
CopyOnWriteArrayList q = populatedArray(SIZE); |
617 |
|
unexpectedException(); |
618 |
|
} |
619 |
|
} |
620 |
< |
|
620 |
> |
|
621 |
|
} |