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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines