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.14 by jsr166, Sun Nov 22 18:57:17 2009 UTC vs.
Revision 1.29 by jsr166, Thu May 30 03:28:55 2013 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.*;
10 > import java.util.ArrayList;
11 > import java.util.Arrays;
12 > import java.util.Collection;
13 > import java.util.Collections;
14 > import java.util.Iterator;
15 > import java.util.LinkedList;
16 > import java.util.List;
17 > import java.util.ListIterator;
18 > import java.util.NoSuchElementException;
19 > import java.util.Vector;
20 > import java.util.concurrent.CopyOnWriteArrayList;
21  
22   public class CopyOnWriteArrayListTest extends JSR166TestCase {
23  
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run (suite());
25 >        junit.textui.TestRunner.run(suite());
26      }
27  
28      public static Test suite() {
29          return new TestSuite(CopyOnWriteArrayListTest.class);
30      }
31  
32 <    static CopyOnWriteArrayList populatedArray(int n) {
33 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
32 >    static CopyOnWriteArrayList<Integer> populatedArray(int n) {
33 >        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>();
34          assertTrue(a.isEmpty());
35 <        for (int i = 0; i < n; ++i)
36 <            a.add(new Integer(i));
35 >        for (int i = 0; i < n; i++)
36 >            a.add(i);
37          assertFalse(a.isEmpty());
38          assertEquals(n, a.size());
39          return a;
40      }
41  
42 +    static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) {
43 +        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>();
44 +        assertTrue(a.isEmpty());
45 +        for (int i = 0; i < elements.length; i++)
46 +            a.add(elements[i]);
47 +        assertFalse(a.isEmpty());
48 +        assertEquals(elements.length, a.size());
49 +        return a;
50 +    }
51  
52      /**
53       * a new list is empty
# Line 64 | Line 81 | public class CopyOnWriteArrayListTest ex
81              assertEquals(ints[i], a.get(i));
82      }
83  
67
84      /**
85 <     *   addAll  adds each element from the given collection
85 >     * addAll adds each element from the given collection
86       */
87      public void testAddAll() {
88          CopyOnWriteArrayList full = populatedArray(3);
# Line 79 | Line 95 | public class CopyOnWriteArrayListTest ex
95      }
96  
97      /**
98 <     *   addAllAbsent adds each element from the given collection that did not
99 <     *  already exist in the List
98 >     * addAllAbsent adds each element from the given collection that did not
99 >     * already exist in the List
100       */
101      public void testAddAllAbsent() {
102          CopyOnWriteArrayList full = populatedArray(3);
# Line 93 | Line 109 | public class CopyOnWriteArrayListTest ex
109      }
110  
111      /**
112 <     *   addIfAbsent will not add the element if it already exists in the list
112 >     * addIfAbsent will not add the element if it already exists in the list
113       */
114      public void testAddIfAbsent() {
115          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 102 | Line 118 | public class CopyOnWriteArrayListTest ex
118      }
119  
120      /**
121 <     *   addIfAbsent adds the element when it does not exist in the list
121 >     * addIfAbsent adds the element when it does not exist in the list
122       */
123      public void testAddIfAbsent2() {
124          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 111 | Line 127 | public class CopyOnWriteArrayListTest ex
127      }
128  
129      /**
130 <     *   clear removes all elements from the list
130 >     * clear removes all elements from the list
131       */
132      public void testClear() {
133          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 119 | Line 135 | public class CopyOnWriteArrayListTest ex
135          assertEquals(0, full.size());
136      }
137  
122
138      /**
139 <     *  Cloned list is equal
139 >     * Cloned list is equal
140       */
141      public void testClone() {
142          CopyOnWriteArrayList l1 = populatedArray(SIZE);
# Line 132 | Line 147 | public class CopyOnWriteArrayListTest ex
147      }
148  
149      /**
150 <     *   contains is true for added elements
150 >     * contains is true for added elements
151       */
152      public void testContains() {
153          CopyOnWriteArrayList full = populatedArray(3);
# Line 174 | Line 189 | public class CopyOnWriteArrayListTest ex
189          assertEquals(a.hashCode(), b.hashCode());
190      }
191  
177
192      /**
193 <     *   containsAll returns true for collection with subset of elements
193 >     * containsAll returns true for collection with subset of elements
194       */
195      public void testContainsAll() {
196          CopyOnWriteArrayList full = populatedArray(3);
# Line 189 | Line 203 | public class CopyOnWriteArrayListTest ex
203      }
204  
205      /**
206 <     *   get returns the value at the given index
206 >     * get returns the value at the given index
207       */
208      public void testGet() {
209          CopyOnWriteArrayList full = populatedArray(3);
# Line 197 | Line 211 | public class CopyOnWriteArrayListTest ex
211      }
212  
213      /**
214 <     *   indexOf gives the index for the given object
214 >     * indexOf gives the index for the given object
215       */
216      public void testIndexOf() {
217          CopyOnWriteArrayList full = populatedArray(3);
# Line 206 | Line 220 | public class CopyOnWriteArrayListTest ex
220      }
221  
222      /**
223 <     *   indexOf gives the index based on the given index
224 <     *  at which to start searching
223 >     * indexOf gives the index based on the given index
224 >     * at which to start searching
225       */
226      public void testIndexOf2() {
227          CopyOnWriteArrayList full = populatedArray(3);
# Line 216 | Line 230 | public class CopyOnWriteArrayListTest ex
230      }
231  
232      /**
233 <     *   isEmpty returns true when empty, else false
233 >     * isEmpty returns true when empty, else false
234       */
235      public void testIsEmpty() {
236          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
# Line 226 | Line 240 | public class CopyOnWriteArrayListTest ex
240      }
241  
242      /**
243 <     *   iterator() returns an iterator containing the elements of the list
243 >     * iterator() returns an iterator containing the elements of the
244 >     * list in insertion order
245       */
246      public void testIterator() {
247 <        CopyOnWriteArrayList full = populatedArray(SIZE);
248 <        Iterator i = full.iterator();
249 <        int j;
250 <        for (j = 0; i.hasNext(); j++)
251 <            assertEquals(j, i.next());
252 <        assertEquals(SIZE, j);
247 >        Collection empty = new CopyOnWriteArrayList();
248 >        assertFalse(empty.iterator().hasNext());
249 >        try {
250 >            empty.iterator().next();
251 >            shouldThrow();
252 >        } catch (NoSuchElementException success) {}
253 >
254 >        Integer[] elements = new Integer[SIZE];
255 >        for (int i = 0; i < SIZE; i++)
256 >            elements[i] = i;
257 >        Collections.shuffle(Arrays.asList(elements));
258 >        Collection<Integer> full = populatedArray(elements);
259 >
260 >        Iterator it = full.iterator();
261 >        for (int j = 0; j < SIZE; j++) {
262 >            assertTrue(it.hasNext());
263 >            assertEquals(elements[j], it.next());
264 >        }
265 >        assertFalse(it.hasNext());
266 >        try {
267 >            it.next();
268 >            shouldThrow();
269 >        } catch (NoSuchElementException success) {}
270      }
271  
272      /**
273       * iterator.remove throws UnsupportedOperationException
274       */
275 <    public void testIteratorRemove () {
275 >    public void testIteratorRemove() {
276          CopyOnWriteArrayList full = populatedArray(SIZE);
277          Iterator it = full.iterator();
278          it.next();
# Line 254 | Line 286 | public class CopyOnWriteArrayListTest ex
286       * toString contains toString of elements
287       */
288      public void testToString() {
289 +        assertEquals("[]", new CopyOnWriteArrayList().toString());
290          CopyOnWriteArrayList full = populatedArray(3);
291          String s = full.toString();
292 <        for (int i = 0; i < 3; ++i) {
293 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
294 <        }
292 >        for (int i = 0; i < 3; ++i)
293 >            assertTrue(s.contains(String.valueOf(i)));
294 >        assertEquals(new ArrayList(full).toString(),
295 >                     full.toString());
296      }
297  
298      /**
299 <     *   lastIndexOf returns the index for the given object
299 >     * lastIndexOf returns the index for the given object
300       */
301      public void testLastIndexOf1() {
302          CopyOnWriteArrayList full = populatedArray(3);
# Line 273 | Line 307 | public class CopyOnWriteArrayListTest ex
307      }
308  
309      /**
310 <     *   lastIndexOf returns the index from the given starting point
310 >     * lastIndexOf returns the index from the given starting point
311       */
312 <    public void testlastIndexOf2() {
312 >    public void testLastIndexOf2() {
313          CopyOnWriteArrayList full = populatedArray(3);
314          full.add(one);
315          full.add(three);
# Line 284 | Line 318 | public class CopyOnWriteArrayListTest ex
318      }
319  
320      /**
321 <     *  listIterator traverses all elements
321 >     * listIterator traverses all elements
322       */
323      public void testListIterator1() {
324          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 296 | Line 330 | public class CopyOnWriteArrayListTest ex
330      }
331  
332      /**
333 <     *  listIterator only returns those elements after the given index
333 >     * listIterator only returns those elements after the given index
334       */
335      public void testListIterator2() {
336          CopyOnWriteArrayList full = populatedArray(3);
# Line 308 | Line 342 | public class CopyOnWriteArrayListTest ex
342      }
343  
344      /**
345 <     *   remove  removes and returns the object at the given index
345 >     * remove(int) removes and returns the object at the given index
346       */
347 <    public void testRemove() {
348 <        CopyOnWriteArrayList full = populatedArray(3);
349 <        assertEquals(two, full.remove(2));
350 <        assertEquals(2, full.size());
347 >    public void testRemove_int() {
348 >        int SIZE = 3;
349 >        for (int i = 0; i < SIZE; i++) {
350 >            CopyOnWriteArrayList full = populatedArray(SIZE);
351 >            assertEquals(i, full.remove(i));
352 >            assertEquals(SIZE - 1, full.size());
353 >            assertFalse(full.contains(new Integer(i)));
354 >        }
355 >    }
356 >
357 >    /**
358 >     * remove(Object) removes the object if found and returns true
359 >     */
360 >    public void testRemove_Object() {
361 >        int SIZE = 3;
362 >        for (int i = 0; i < SIZE; i++) {
363 >            CopyOnWriteArrayList full = populatedArray(SIZE);
364 >            assertFalse(full.remove(new Integer(-42)));
365 >            assertTrue(full.remove(new Integer(i)));
366 >            assertEquals(SIZE - 1, full.size());
367 >            assertFalse(full.contains(new Integer(i)));
368 >        }
369 >        CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
370 >        assertTrue(x.remove(new Integer(6)));
371 >        assertEquals(x, Arrays.asList(4, 5));
372 >        assertTrue(x.remove(new Integer(4)));
373 >        assertEquals(x, Arrays.asList(5));
374 >        assertTrue(x.remove(new Integer(5)));
375 >        assertEquals(x, Arrays.asList());
376 >        assertFalse(x.remove(new Integer(5)));
377      }
378  
379      /**
380 <     *   removeAll  removes all elements from the given collection
380 >     * removeAll removes all elements from the given collection
381       */
382      public void testRemoveAll() {
383          CopyOnWriteArrayList full = populatedArray(3);
# Line 329 | Line 389 | public class CopyOnWriteArrayListTest ex
389      }
390  
391      /**
392 <     *   set  changes the element at the given index
392 >     * set changes the element at the given index
393       */
394      public void testSet() {
395          CopyOnWriteArrayList full = populatedArray(3);
396 <        assertEquals(two, full.set(2, four));
396 >        assertEquals(2, full.set(2, four));
397          assertEquals(4, full.get(2));
398      }
399  
400      /**
401 <     *   size returns the number of elements
401 >     * size returns the number of elements
402       */
403      public void testSize() {
404          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
# Line 348 | Line 408 | public class CopyOnWriteArrayListTest ex
408      }
409  
410      /**
411 <     *   toArray returns an Object array containing all elements from the list
411 >     * toArray() returns an Object array containing all elements from
412 >     * the list in insertion order
413       */
414      public void testToArray() {
415 <        CopyOnWriteArrayList full = populatedArray(3);
416 <        Object[] o = full.toArray();
417 <        assertEquals(3, o.length);
418 <        assertEquals(0, o[0]);
419 <        assertEquals(1, o[1]);
420 <        assertEquals(2, o[2]);
415 >        Object[] a = new CopyOnWriteArrayList().toArray();
416 >        assertTrue(Arrays.equals(new Object[0], a));
417 >        assertSame(Object[].class, a.getClass());
418 >
419 >        Integer[] elements = new Integer[SIZE];
420 >        for (int i = 0; i < SIZE; i++)
421 >            elements[i] = i;
422 >        Collections.shuffle(Arrays.asList(elements));
423 >        Collection<Integer> full = populatedArray(elements);
424 >
425 >        assertTrue(Arrays.equals(elements, full.toArray()));
426 >        assertSame(Object[].class, full.toArray().getClass());
427      }
428  
429      /**
430 <     *   toArray returns an Integer array containing all elements from
431 <     *   the list
430 >     * toArray(Integer array) returns an Integer array containing all
431 >     * elements from the list in insertion order
432       */
433      public void testToArray2() {
434 <        CopyOnWriteArrayList full = populatedArray(3);
435 <        Integer[] i = new Integer[3];
436 <        i = (Integer[])full.toArray(i);
437 <        assertEquals(3, i.length);
438 <        assertEquals(0, i[0].intValue());
372 <        assertEquals(1, i[1].intValue());
373 <        assertEquals(2, i[2].intValue());
374 <    }
434 >        Collection empty = new CopyOnWriteArrayList();
435 >        Integer[] a;
436 >
437 >        a = new Integer[0];
438 >        assertSame(a, empty.toArray(a));
439  
440 +        a = new Integer[SIZE/2];
441 +        Arrays.fill(a, 42);
442 +        assertSame(a, empty.toArray(a));
443 +        assertNull(a[0]);
444 +        for (int i = 1; i < a.length; i++)
445 +            assertEquals(42, (int) a[i]);
446 +
447 +        Integer[] elements = new Integer[SIZE];
448 +        for (int i = 0; i < SIZE; i++)
449 +            elements[i] = i;
450 +        Collections.shuffle(Arrays.asList(elements));
451 +        Collection<Integer> full = populatedArray(elements);
452 +
453 +        Arrays.fill(a, 42);
454 +        assertTrue(Arrays.equals(elements, full.toArray(a)));
455 +        for (int i = 0; i < a.length; i++)
456 +            assertEquals(42, (int) a[i]);
457 +        assertSame(Integer[].class, full.toArray(a).getClass());
458 +
459 +        a = new Integer[SIZE];
460 +        Arrays.fill(a, 42);
461 +        assertSame(a, full.toArray(a));
462 +        assertTrue(Arrays.equals(elements, a));
463 +
464 +        a = new Integer[2*SIZE];
465 +        Arrays.fill(a, 42);
466 +        assertSame(a, full.toArray(a));
467 +        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
468 +        assertNull(a[SIZE]);
469 +        for (int i = SIZE + 1; i < a.length; i++)
470 +            assertEquals(42, (int) a[i]);
471 +    }
472  
473      /**
474       * sublists contains elements at indexes offset from their base
# Line 390 | Line 486 | public class CopyOnWriteArrayListTest ex
486          }
487  
488          List s = a.subList(2, 5);
489 <        assertEquals(s.size(), 3);
489 >        assertEquals(3, s.size());
490          s.set(2, m1);
491          assertEquals(a.get(4), m1);
492          s.clear();
493 <        assertEquals(a.size(), 7);
493 >        assertEquals(7, a.size());
494      }
495  
496      // Exception tests
497  
498      /**
499 <     *   toArray throws an ArrayStoreException when the given array
500 <     *  can not store the objects inside the list
499 >     * toArray throws an ArrayStoreException when the given array
500 >     * can not store the objects inside the list
501       */
502      public void testToArray_ArrayStoreException() {
503          try {
# Line 414 | Line 510 | public class CopyOnWriteArrayListTest ex
510      }
511  
512      /**
513 <     *   get throws an IndexOutOfBoundsException on a negative index
513 >     * get throws an IndexOutOfBoundsException on a negative index
514       */
515      public void testGet1_IndexOutOfBoundsException() {
516          try {
# Line 425 | Line 521 | public class CopyOnWriteArrayListTest ex
521      }
522  
523      /**
524 <     *   get throws an IndexOutOfBoundsException on a too high index
524 >     * get throws an IndexOutOfBoundsException on a too high index
525       */
526      public void testGet2_IndexOutOfBoundsException() {
527          try {
# Line 438 | Line 534 | public class CopyOnWriteArrayListTest ex
534      }
535  
536      /**
537 <     *   set throws an IndexOutOfBoundsException on a negative index
537 >     * set throws an IndexOutOfBoundsException on a negative index
538       */
539      public void testSet1_IndexOutOfBoundsException() {
540          try {
# Line 449 | Line 545 | public class CopyOnWriteArrayListTest ex
545      }
546  
547      /**
548 <     *   set throws an IndexOutOfBoundsException on a too high index
548 >     * set throws an IndexOutOfBoundsException on a too high index
549       */
550      public void testSet2() {
551          try {
# Line 462 | Line 558 | public class CopyOnWriteArrayListTest ex
558      }
559  
560      /**
561 <     *   add throws an IndexOutOfBoundsException on a negative index
561 >     * add throws an IndexOutOfBoundsException on a negative index
562       */
563      public void testAdd1_IndexOutOfBoundsException() {
564          try {
# Line 473 | Line 569 | public class CopyOnWriteArrayListTest ex
569      }
570  
571      /**
572 <     *   add throws an IndexOutOfBoundsException on a too high index
572 >     * add throws an IndexOutOfBoundsException on a too high index
573       */
574      public void testAdd2_IndexOutOfBoundsException() {
575          try {
# Line 486 | Line 582 | public class CopyOnWriteArrayListTest ex
582      }
583  
584      /**
585 <     *   remove throws an IndexOutOfBoundsException on a negative index
585 >     * remove throws an IndexOutOfBoundsException on a negative index
586       */
587      public void testRemove1_IndexOutOfBounds() {
588          try {
# Line 497 | Line 593 | public class CopyOnWriteArrayListTest ex
593      }
594  
595      /**
596 <     *   remove throws an IndexOutOfBoundsException on a too high index
596 >     * remove throws an IndexOutOfBoundsException on a too high index
597       */
598      public void testRemove2_IndexOutOfBounds() {
599          try {
# Line 510 | Line 606 | public class CopyOnWriteArrayListTest ex
606      }
607  
608      /**
609 <     *   addAll throws an IndexOutOfBoundsException on a negative index
609 >     * addAll throws an IndexOutOfBoundsException on a negative index
610       */
611      public void testAddAll1_IndexOutOfBoundsException() {
612          try {
# Line 521 | Line 617 | public class CopyOnWriteArrayListTest ex
617      }
618  
619      /**
620 <     *   addAll throws an IndexOutOfBoundsException on a too high index
620 >     * addAll throws an IndexOutOfBoundsException on a too high index
621       */
622      public void testAddAll2_IndexOutOfBoundsException() {
623          try {
# Line 534 | Line 630 | public class CopyOnWriteArrayListTest ex
630      }
631  
632      /**
633 <     *   listIterator throws an IndexOutOfBoundsException on a negative index
633 >     * listIterator throws an IndexOutOfBoundsException on a negative index
634       */
635      public void testListIterator1_IndexOutOfBoundsException() {
636          try {
# Line 545 | Line 641 | public class CopyOnWriteArrayListTest ex
641      }
642  
643      /**
644 <     *   listIterator throws an IndexOutOfBoundsException on a too high index
644 >     * listIterator throws an IndexOutOfBoundsException on a too high index
645       */
646      public void testListIterator2_IndexOutOfBoundsException() {
647          try {
# Line 558 | Line 654 | public class CopyOnWriteArrayListTest ex
654      }
655  
656      /**
657 <     *   subList throws an IndexOutOfBoundsException on a negative index
657 >     * subList throws an IndexOutOfBoundsException on a negative index
658       */
659      public void testSubList1_IndexOutOfBoundsException() {
660          try {
# Line 569 | Line 665 | public class CopyOnWriteArrayListTest ex
665      }
666  
667      /**
668 <     *   subList throws an IndexOutOfBoundsException on a too high index
668 >     * subList throws an IndexOutOfBoundsException on a too high index
669       */
670      public void testSubList2_IndexOutOfBoundsException() {
671          try {
# Line 581 | Line 677 | public class CopyOnWriteArrayListTest ex
677      }
678  
679      /**
680 <     *   subList throws IndexOutOfBoundsException when the second index
681 <     *  is lower then the first
680 >     * subList throws IndexOutOfBoundsException when the second index
681 >     * is lower then the first
682       */
683      public void testSubList3_IndexOutOfBoundsException() {
684          try {
# Line 593 | Line 689 | public class CopyOnWriteArrayListTest ex
689      }
690  
691      /**
692 <     * a deserialized serialiszed list is equal
692 >     * a deserialized serialized list is equal
693       */
694      public void testSerialization() throws Exception {
695 <        CopyOnWriteArrayList q = populatedArray(SIZE);
695 >        List x = populatedArray(SIZE);
696 >        List y = serialClone(x);
697  
698 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
699 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
700 <        out.writeObject(q);
701 <        out.close();
702 <
703 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
704 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
705 <        CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
706 <        assertEquals(q.size(), r.size());
707 <        assertTrue(q.equals(r));
708 <        assertTrue(r.equals(q));
698 >        assertNotSame(x, y);
699 >        assertEquals(x.size(), y.size());
700 >        assertEquals(x.toString(), y.toString());
701 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
702 >        assertEquals(x, y);
703 >        assertEquals(y, x);
704 >        while (!x.isEmpty()) {
705 >            assertFalse(y.isEmpty());
706 >            assertEquals(x.remove(0), y.remove(0));
707 >        }
708 >        assertTrue(y.isEmpty());
709      }
710  
711   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines