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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines