ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CopyOnWriteArrayListTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.39 by jsr166, Wed Aug 24 22:22:39 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines