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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines