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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines