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.10 by jsr166, Mon Nov 16 05:30:07 2009 UTC vs.
Revision 1.32 by jsr166, Sat Jan 17 22:55:06 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines