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.27 by jsr166, Wed Nov 30 05:42:38 2011 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.*;
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  
13 public class CopyOnWriteArrayListTest extends TestCase{
14    
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 <     *  Test to verify addAll correctly adds each element from the given collection
53 >     * a new list is empty
54       */
55 <    public void testAddAll(){
56 <        CopyOnWriteArrayList full = fullArray(3);
57 <        Vector v = new Vector();
39 <        v.add(new Integer(3));
40 <        v.add(new Integer(4));
41 <        v.add(new Integer(5));
42 <        full.addAll(v);
43 <        assertEquals(6, full.size());
55 >    public void testConstructor() {
56 >        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
57 >        assertTrue(a.isEmpty());
58      }
59  
60      /**
61 <     *  Test to verify addAllAbsent adds each element from the given collection that did not
48 <     *  already exist in the List
61 >     * new list contains all elements of initializing array
62       */
63 <    public void testAddAllAbsent(){
64 <        CopyOnWriteArrayList full = fullArray(3);
65 <        Vector v = new Vector();
66 <        v.add(new Integer(3));
67 <        v.add(new Integer(4));
68 <        v.add(new Integer(1)); // will not add this element
69 <        full.addAllAbsent(v);
57 <        assertEquals(5, full.size());
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 <     *  Test to verify addIfAbsent will not add the element if it already exists in the list
73 >     * new list contains all elements of initializing collection
74       */
75 <    public void testAddIfAbsent(){
76 <        CopyOnWriteArrayList full = fullArray(3);
77 <        full.addIfAbsent(new Integer(1));
78 <        assertEquals(3, full.size());
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 <     *  test to verify addIfAbsent correctly adds the element when it does not exist in the list
85 >     * addAll adds each element from the given collection
86       */
87 <    public void testAddIfAbsent2(){
88 <        CopyOnWriteArrayList full = fullArray(3);
89 <        full.addIfAbsent(new Integer(3));
90 <        assertTrue(full.contains(new Integer(3)));
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 clear correctly removes all elements from the list
98 >     * addAllAbsent adds each element from the given collection that did not
99 >     * already exist in the List
100       */
101 <    public void testClear(){
102 <        CopyOnWriteArrayList full = fullArray(3);
103 <        full.clear();
104 <        assertEquals(0, 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 contains returns the correct values
112 >     * addIfAbsent will not add the element if it already exists in the list
113       */
114 <    public void testContains(){
115 <        CopyOnWriteArrayList full = fullArray(3);
116 <        assertTrue(full.contains(new Integer(1)));
117 <        assertFalse(full.contains(new Integer(5)));
114 >    public void testAddIfAbsent() {
115 >        CopyOnWriteArrayList full = populatedArray(SIZE);
116 >        full.addIfAbsent(one);
117 >        assertEquals(SIZE, full.size());
118      }
119  
120 +    /**
121 +     * addIfAbsent adds the element when it does not exist in the list
122 +     */
123 +    public void testAddIfAbsent2() {
124 +        CopyOnWriteArrayList full = populatedArray(SIZE);
125 +        full.addIfAbsent(three);
126 +        assertTrue(full.contains(three));
127 +    }
128 +
129 +    /**
130 +     * clear removes all elements from the list
131 +     */
132 +    public void testClear() {
133 +        CopyOnWriteArrayList full = populatedArray(SIZE);
134 +        full.clear();
135 +        assertEquals(0, full.size());
136 +    }
137 +
138 +    /**
139 +     * Cloned list is equal
140 +     */
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 +     * contains is true for added elements
151 +     */
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  
124    
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 <        }
195 <        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
242 <     *  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 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() {
348 >        CopyOnWriteArrayList full = populatedArray(3);
349 >        assertEquals(2, full.remove(2));
350 >        assertEquals(2, full.size());
351      }
352  
353      /**
354 <     *  Test to verify removeAll correctly removes all elements from the given collection
354 >     * removeAll removes all elements from the given collection
355       */
356 <    public void testRemoveAll(){
357 <        CopyOnWriteArrayList full = fullArray(3);
358 <        Vector v = new Vector();
359 <        v.add(new Integer(1));
360 <        v.add(new Integer(2));
361 <        full.removeAll(v);
362 <        assertEquals(1, full.size());
356 >    public void testRemoveAll() {
357 >        CopyOnWriteArrayList full = populatedArray(3);
358 >        Vector v = new Vector();
359 >        v.add(one);
360 >        v.add(two);
361 >        full.removeAll(v);
362 >        assertEquals(1, full.size());
363      }
364  
365      /**
366 <     *  Test to verify set correctly changes the element at the given index
366 >     * set changes the element at the given index
367       */
368 <    public void testSet(){
369 <        CopyOnWriteArrayList full = fullArray(3);
370 <        assertEquals(new Integer(2), full.set(2, new Integer(4)));
371 <        assertEquals(4, ((Integer)full.get(2)).intValue());
368 >    public void testSet() {
369 >        CopyOnWriteArrayList full = populatedArray(3);
370 >        assertEquals(2, full.set(2, four));
371 >        assertEquals(4, full.get(2));
372      }
373  
374      /**
375 <     *  Test to verify size returns the correct values
375 >     * size returns the number of elements
376       */
377 <    public void testSize(){
378 <        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
379 <        CopyOnWriteArrayList full = fullArray(3);
380 <        assertEquals(3, full.size());
381 <        assertEquals(0, empty.size());
377 >    public void testSize() {
378 >        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
379 >        CopyOnWriteArrayList full = populatedArray(SIZE);
380 >        assertEquals(SIZE, full.size());
381 >        assertEquals(0, empty.size());
382      }
383  
384      /**
385 <     *  Test to verify toArray returns an Object array containing all elements from the list
385 >     * toArray() returns an Object array containing all elements from
386 >     * the list in insertion order
387       */
388 <    public void testToArray(){
389 <        CopyOnWriteArrayList full = fullArray(3);
390 <        Object[] o = full.toArray();
391 <        assertEquals(3, o.length);
392 <        assertEquals(0, ((Integer)o[0]).intValue());
393 <        assertEquals(1, ((Integer)o[1]).intValue());
394 <        assertEquals(2, ((Integer)o[2]).intValue());
388 >    public void testToArray() {
389 >        Object[] a = new CopyOnWriteArrayList().toArray();
390 >        assertTrue(Arrays.equals(new Object[0], a));
391 >        assertSame(Object[].class, a.getClass());
392 >
393 >        Integer[] elements = new Integer[SIZE];
394 >        for (int i = 0; i < SIZE; i++)
395 >            elements[i] = i;
396 >        Collections.shuffle(Arrays.asList(elements));
397 >        Collection<Integer> full = populatedArray(elements);
398 >
399 >        assertTrue(Arrays.equals(elements, full.toArray()));
400 >        assertSame(Object[].class, full.toArray().getClass());
401      }
402  
403      /**
404 <     *  test to verify toArray returns an Integer array containing all elements from the list
404 >     * toArray(Integer array) returns an Integer array containing all
405 >     * elements from the list in insertion order
406       */
407 <    public void testToArray2(){
408 <        CopyOnWriteArrayList full = fullArray(3);
409 <        Integer[] i = new Integer[3];
410 <        i = (Integer[])full.toArray(i);
411 <        assertEquals(3, i.length);
412 <        assertEquals(0, i[0].intValue());
413 <        assertEquals(1, i[1].intValue());
414 <        assertEquals(2, i[2].intValue());
415 <    }
407 >    public void testToArray2() {
408 >        Collection empty = new CopyOnWriteArrayList();
409 >        Integer[] a;
410 >
411 >        a = new Integer[0];
412 >        assertSame(a, empty.toArray(a));
413 >
414 >        a = new Integer[SIZE/2];
415 >        Arrays.fill(a, 42);
416 >        assertSame(a, empty.toArray(a));
417 >        assertNull(a[0]);
418 >        for (int i = 1; i < a.length; i++)
419 >            assertEquals(42, (int) a[i]);
420 >
421 >        Integer[] elements = new Integer[SIZE];
422 >        for (int i = 0; i < SIZE; i++)
423 >            elements[i] = i;
424 >        Collections.shuffle(Arrays.asList(elements));
425 >        Collection<Integer> full = populatedArray(elements);
426  
427 +        Arrays.fill(a, 42);
428 +        assertTrue(Arrays.equals(elements, full.toArray(a)));
429 +        for (int i = 0; i < a.length; i++)
430 +            assertEquals(42, (int) a[i]);
431 +        assertSame(Integer[].class, full.toArray(a).getClass());
432  
433 +        a = new Integer[SIZE];
434 +        Arrays.fill(a, 42);
435 +        assertSame(a, full.toArray(a));
436 +        assertTrue(Arrays.equals(elements, a));
437 +
438 +        a = new Integer[2*SIZE];
439 +        Arrays.fill(a, 42);
440 +        assertSame(a, full.toArray(a));
441 +        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
442 +        assertNull(a[SIZE]);
443 +        for (int i = SIZE + 1; i < a.length; i++)
444 +            assertEquals(42, (int) a[i]);
445 +    }
446 +
447 +    /**
448 +     * sublists contains elements at indexes offset from their base
449 +     */
450      public void testSubList() {
451 <        CopyOnWriteArrayList a = fullArray(10);
451 >        CopyOnWriteArrayList a = populatedArray(10);
452          assertTrue(a.subList(1,1).isEmpty());
453 <        for(int j = 0; j < 9; ++j) {
454 <            for(int i = j ; i < 10; ++i) {
455 <                List b = a.subList(j,i);
456 <                for(int k = j; k < i; ++k) {
457 <                    assertEquals(new Integer(k), b.get(k-j));
458 <                }
459 <            }
460 <        }
461 <
462 <        Integer m1 = new Integer(-1);
463 <        List s = a.subList(2, 5);
464 <        assertEquals(s.size(), 3);
334 <        s.set(2, new Integer(m1));
453 >        for (int j = 0; j < 9; ++j) {
454 >            for (int i = j ; i < 10; ++i) {
455 >                List b = a.subList(j,i);
456 >                for (int k = j; k < i; ++k) {
457 >                    assertEquals(new Integer(k), b.get(k-j));
458 >                }
459 >            }
460 >        }
461 >
462 >        List s = a.subList(2, 5);
463 >        assertEquals(3, s.size());
464 >        s.set(2, m1);
465          assertEquals(a.get(4), m1);
466 <        s.clear();
467 <        assertEquals(a.size(), 7);
466 >        s.clear();
467 >        assertEquals(7, a.size());
468      }
469  
470      // Exception tests
471  
472      /**
473 <     *  Test to verify toArray throws an ArrayStoreException when the given array
474 <     *  can not store the objects inside the list
473 >     * toArray throws an ArrayStoreException when the given array
474 >     * can not store the objects inside the list
475       */
476 <    public void testToArray_ArrayStoreException(){
477 <        try{
476 >    public void testToArray_ArrayStoreException() {
477 >        try {
478              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
479              c.add("zfasdfsdf");
480              c.add("asdadasd");
481              c.toArray(new Long[5]);
482 <            fail("Object[] toArray(Object[]) should throw ArrayStoreException");
483 <        }catch(ArrayStoreException e){}
482 >            shouldThrow();
483 >        } catch (ArrayStoreException success) {}
484      }
485  
486      /**
487 <     *  Test to verify get throws an IndexOutOfBoundsException on a negative index
487 >     * get throws an IndexOutOfBoundsException on a negative index
488       */
489 <    public void testGet1_IndexOutOfBoundsException(){
490 <        try{
489 >    public void testGet1_IndexOutOfBoundsException() {
490 >        try {
491              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
492              c.get(-1);
493 <            fail("Object get(int) should throw IndexOutOfBounds exception");
494 <        }catch(IndexOutOfBoundsException e){}
493 >            shouldThrow();
494 >        } catch (IndexOutOfBoundsException success) {}
495      }
496 <    
496 >
497      /**
498 <     *  Test to verify get throws an IndexOutOfBoundsException on a too high index
498 >     * get throws an IndexOutOfBoundsException on a too high index
499       */
500 <    public void testGet2_IndexOutOfBoundsException(){
501 <        try{
500 >    public void testGet2_IndexOutOfBoundsException() {
501 >        try {
502              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
503              c.add("asdasd");
504              c.add("asdad");
505              c.get(100);
506 <            fail("Object get(int) should throw IndexOutOfBounds exception");
507 <        }catch(IndexOutOfBoundsException e){}
506 >            shouldThrow();
507 >        } catch (IndexOutOfBoundsException success) {}
508      }
509  
510      /**
511 <     *  Test to verify set throws an IndexOutOfBoundsException on a negative index
511 >     * set throws an IndexOutOfBoundsException on a negative index
512       */
513 <    public void testSet1_IndexOutOfBoundsException(){
514 <        try{
513 >    public void testSet1_IndexOutOfBoundsException() {
514 >        try {
515              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
516              c.set(-1,"qwerty");
517 <            fail("Object get(int, Object) should throw IndexOutOfBounds exception");
518 <        }catch(IndexOutOfBoundsException e){}
517 >            shouldThrow();
518 >        } catch (IndexOutOfBoundsException success) {}
519      }
520 <    
520 >
521      /**
522 <     *  Test to verify set throws an IndexOutOfBoundsException on a too high index
522 >     * set throws an IndexOutOfBoundsException on a too high index
523       */
524 <    public void testSet2(){
525 <        try{
524 >    public void testSet2() {
525 >        try {
526              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
527              c.add("asdasd");
528              c.add("asdad");
529              c.set(100, "qwerty");
530 <            fail("Object set(int, Object) should throw IndexOutOfBounds exception");
531 <        }catch(IndexOutOfBoundsException e){}
530 >            shouldThrow();
531 >        } catch (IndexOutOfBoundsException success) {}
532      }
533  
534      /**
535 <     *  Test to verify add throws an IndexOutOfBoundsException on a negative index
535 >     * add throws an IndexOutOfBoundsException on a negative index
536       */
537 <    public void testAdd1_IndexOutOfBoundsException(){
538 <        try{
537 >    public void testAdd1_IndexOutOfBoundsException() {
538 >        try {
539              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
540              c.add(-1,"qwerty");
541 <            fail("void add(int, Object) should throw IndexOutOfBounds exception");
542 <        }catch(IndexOutOfBoundsException e){}
541 >            shouldThrow();
542 >        } catch (IndexOutOfBoundsException success) {}
543      }
544 <    
544 >
545      /**
546 <     *  Test to verify add throws an IndexOutOfBoundsException on a too high index
546 >     * add throws an IndexOutOfBoundsException on a too high index
547       */
548 <    public void testAdd2_IndexOutOfBoundsException(){
549 <        try{
548 >    public void testAdd2_IndexOutOfBoundsException() {
549 >        try {
550              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
551              c.add("asdasd");
552              c.add("asdasdasd");
553              c.add(100, "qwerty");
554 <            fail("void add(int, Object) should throw IndexOutOfBounds exception");
555 <        }catch(IndexOutOfBoundsException e){}
554 >            shouldThrow();
555 >        } catch (IndexOutOfBoundsException success) {}
556      }
557  
558      /**
559 <     *  Test to verify remove throws an IndexOutOfBoundsException on a negative index
559 >     * remove throws an IndexOutOfBoundsException on a negative index
560       */
561 <    public void testRemove1_IndexOutOfBounds(){
562 <        try{
561 >    public void testRemove1_IndexOutOfBounds() {
562 >        try {
563              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
564              c.remove(-1);
565 <            fail("Object remove(int) should throw IndexOutOfBounds exception");
566 <        }catch(IndexOutOfBoundsException e){}
565 >            shouldThrow();
566 >        } catch (IndexOutOfBoundsException success) {}
567      }
568  
569      /**
570 <     *  Test to verify remove throws an IndexOutOfBoundsException on a too high index
570 >     * remove throws an IndexOutOfBoundsException on a too high index
571       */
572 <    public void testRemove2_IndexOutOfBounds(){
573 <        try{
572 >    public void testRemove2_IndexOutOfBounds() {
573 >        try {
574              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
575              c.add("asdasd");
576              c.add("adasdasd");
577              c.remove(100);
578 <            fail("Object remove(int) should throw IndexOutOfBounds exception");
579 <        }catch(IndexOutOfBoundsException e){}
578 >            shouldThrow();
579 >        } catch (IndexOutOfBoundsException success) {}
580      }
581 <    
581 >
582      /**
583 <     *  Test to verify addAll throws an IndexOutOfBoundsException on a negative index
583 >     * addAll throws an IndexOutOfBoundsException on a negative index
584       */
585 <    public void testAddAll1_IndexOutOfBoundsException(){
586 <        try{
585 >    public void testAddAll1_IndexOutOfBoundsException() {
586 >        try {
587              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
588              c.addAll(-1,new LinkedList());
589 <            fail("boolean add(int, Collection) should throw IndexOutOfBounds exception");
590 <        }catch(IndexOutOfBoundsException e){}
589 >            shouldThrow();
590 >        } catch (IndexOutOfBoundsException success) {}
591      }
592 <    
592 >
593      /**
594 <     *  Test to verify addAll throws an IndexOutOfBoundsException on a too high index
594 >     * addAll throws an IndexOutOfBoundsException on a too high index
595       */
596 <    public void testAddAll2_IndexOutOfBoundsException(){
597 <        try{
596 >    public void testAddAll2_IndexOutOfBoundsException() {
597 >        try {
598              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
599              c.add("asdasd");
600              c.add("asdasdasd");
601              c.addAll(100, new LinkedList());
602 <            fail("boolean addAll(int, Collection) should throw IndexOutOfBounds exception");
603 <        }catch(IndexOutOfBoundsException e){}
602 >            shouldThrow();
603 >        } catch (IndexOutOfBoundsException success) {}
604      }
605  
606      /**
607 <     *  Test to verify listIterator throws an IndexOutOfBoundsException on a negative index
607 >     * listIterator throws an IndexOutOfBoundsException on a negative index
608       */
609 <    public void testListIterator1_IndexOutOfBoundsException(){
610 <        try{
609 >    public void testListIterator1_IndexOutOfBoundsException() {
610 >        try {
611              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
612              c.listIterator(-1);
613 <            fail("ListIterator listIterator(int) should throw IndexOutOfBounds exceptione");
614 <        }catch(IndexOutOfBoundsException e){}
613 >            shouldThrow();
614 >        } catch (IndexOutOfBoundsException success) {}
615      }
616  
617      /**
618 <     *  Test to verify listIterator throws an IndexOutOfBoundsException on a too high index
618 >     * listIterator throws an IndexOutOfBoundsException on a too high index
619       */
620 <    public void testListIterator2_IndexOutOfBoundsException(){
621 <        try{
620 >    public void testListIterator2_IndexOutOfBoundsException() {
621 >        try {
622              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
623              c.add("adasd");
624              c.add("asdasdas");
625              c.listIterator(100);
626 <            fail("ListIterator listIterator(int) should throw IndexOutOfBounds exception");
627 <        }catch(IndexOutOfBoundsException e){}
626 >            shouldThrow();
627 >        } catch (IndexOutOfBoundsException success) {}
628      }
629  
630      /**
631 <     *  Test to verify subList throws an IndexOutOfBoundsException on a negative index
631 >     * subList throws an IndexOutOfBoundsException on a negative index
632       */
633 <    public void testSubList1_IndexOutOfBoundsException(){
634 <        try{
633 >    public void testSubList1_IndexOutOfBoundsException() {
634 >        try {
635              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
636              c.subList(-1,100);
637 <
638 <            fail("List subList(int, int) should throw IndexOutofBounds exception");
509 <        }catch(IndexOutOfBoundsException e){}
637 >            shouldThrow();
638 >        } catch (IndexOutOfBoundsException success) {}
639      }
640  
641      /**
642 <     *  Test to verify subList throws an IndexOutOfBoundsException on a too high index
642 >     * subList throws an IndexOutOfBoundsException on a too high index
643       */
644 <    public void testSubList2_IndexOutOfBoundsException(){
645 <        try{
644 >    public void testSubList2_IndexOutOfBoundsException() {
645 >        try {
646              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
647              c.add("asdasd");
648              c.subList(1,100);
649 <            fail("List subList(int, int) should throw IndexOutofBounds exception");
650 <        }catch(IndexOutOfBoundsException e){}
649 >            shouldThrow();
650 >        } catch (IndexOutOfBoundsException success) {}
651      }
652  
653      /**
654 <     *  Test to verify subList throws IndexOutOfBoundsException when the second index
655 <     *  is lower then the first
654 >     * subList throws IndexOutOfBoundsException when the second index
655 >     * is lower then the first
656       */
657 <    public void testSubList3_IndexOutOfBoundsException(){
658 <        try{
657 >    public void testSubList3_IndexOutOfBoundsException() {
658 >        try {
659              CopyOnWriteArrayList c = new CopyOnWriteArrayList();
660              c.subList(3,1);
661 <
662 <            fail("List subList(int, int) should throw IndexOutofBounds exception");
534 <        }catch(IndexOutOfBoundsException e){}
661 >            shouldThrow();
662 >        } catch (IndexOutOfBoundsException success) {}
663      }
664  
665 <    public void testSerialization() {
666 <        CopyOnWriteArrayList q = fullArray(10);
667 <
668 <        try {
669 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
670 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
671 <            out.writeObject(q);
672 <            out.close();
673 <
674 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
675 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
676 <            CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
677 <            assertEquals(q.size(), r.size());
678 <            assertTrue(q.equals(r));
679 <            assertTrue(r.equals(q));
680 <        } catch(Exception e){
553 <            e.printStackTrace();
554 <            fail("unexpected exception");
665 >    /**
666 >     * a deserialized serialized list is equal
667 >     */
668 >    public void testSerialization() throws Exception {
669 >        List x = populatedArray(SIZE);
670 >        List y = serialClone(x);
671 >
672 >        assertTrue(x != y);
673 >        assertEquals(x.size(), y.size());
674 >        assertEquals(x.toString(), y.toString());
675 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
676 >        assertEquals(x, y);
677 >        assertEquals(y, x);
678 >        while (!x.isEmpty()) {
679 >            assertFalse(y.isEmpty());
680 >            assertEquals(x.remove(0), y.remove(0));
681          }
682 +        assertTrue(y.isEmpty());
683      }
684 <    
684 >
685   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines