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

Comparing jsr166/src/test/tck/CopyOnWriteArrayListTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.30 by jsr166, Fri Jan 31 17:58:35 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines