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.47 by jsr166, Mon Mar 26 21:28:22 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines