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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines