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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines