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.15 by jsr166, Tue Dec 1 09:56:28 2009 UTC vs.
Revision 1.25 by jsr166, Sat Nov 26 07:39:02 2011 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.Arrays;
11 > import java.util.Iterator;
12 > import java.util.LinkedList;
13 > import java.util.List;
14 > import java.util.ListIterator;
15 > import java.util.Vector;
16 > import java.util.concurrent.CopyOnWriteArrayList;
17  
18   public class CopyOnWriteArrayListTest extends JSR166TestCase {
19  
20      public static void main(String[] args) {
21 <        junit.textui.TestRunner.run (suite());
21 >        junit.textui.TestRunner.run(suite());
22      }
23  
24      public static Test suite() {
25          return new TestSuite(CopyOnWriteArrayListTest.class);
26      }
27  
28 <    static CopyOnWriteArrayList populatedArray(int n) {
29 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
28 >    static CopyOnWriteArrayList<Integer> populatedArray(int n) {
29 >        CopyOnWriteArrayList<Integer> a
30 >            = new CopyOnWriteArrayList<Integer>();
31          assertTrue(a.isEmpty());
32          for (int i = 0; i < n; ++i)
33 <            a.add(new Integer(i));
33 >            a.add(i);
34          assertFalse(a.isEmpty());
35          assertEquals(n, a.size());
36          return a;
37      }
38  
34
39      /**
40       * a new list is empty
41       */
# Line 64 | Line 68 | public class CopyOnWriteArrayListTest ex
68              assertEquals(ints[i], a.get(i));
69      }
70  
67
71      /**
72 <     *   addAll  adds each element from the given collection
72 >     * addAll adds each element from the given collection
73       */
74      public void testAddAll() {
75          CopyOnWriteArrayList full = populatedArray(3);
# Line 79 | Line 82 | public class CopyOnWriteArrayListTest ex
82      }
83  
84      /**
85 <     *   addAllAbsent adds each element from the given collection that did not
86 <     *  already exist in the List
85 >     * addAllAbsent adds each element from the given collection that did not
86 >     * already exist in the List
87       */
88      public void testAddAllAbsent() {
89          CopyOnWriteArrayList full = populatedArray(3);
# Line 93 | Line 96 | public class CopyOnWriteArrayListTest ex
96      }
97  
98      /**
99 <     *   addIfAbsent will not add the element if it already exists in the list
99 >     * addIfAbsent will not add the element if it already exists in the list
100       */
101      public void testAddIfAbsent() {
102          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 102 | Line 105 | public class CopyOnWriteArrayListTest ex
105      }
106  
107      /**
108 <     *   addIfAbsent adds the element when it does not exist in the list
108 >     * addIfAbsent adds the element when it does not exist in the list
109       */
110      public void testAddIfAbsent2() {
111          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 111 | Line 114 | public class CopyOnWriteArrayListTest ex
114      }
115  
116      /**
117 <     *   clear removes all elements from the list
117 >     * clear removes all elements from the list
118       */
119      public void testClear() {
120          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 119 | Line 122 | public class CopyOnWriteArrayListTest ex
122          assertEquals(0, full.size());
123      }
124  
122
125      /**
126 <     *  Cloned list is equal
126 >     * Cloned list is equal
127       */
128      public void testClone() {
129          CopyOnWriteArrayList l1 = populatedArray(SIZE);
# Line 132 | Line 134 | public class CopyOnWriteArrayListTest ex
134      }
135  
136      /**
137 <     *   contains is true for added elements
137 >     * contains is true for added elements
138       */
139      public void testContains() {
140          CopyOnWriteArrayList full = populatedArray(3);
# Line 174 | Line 176 | public class CopyOnWriteArrayListTest ex
176          assertEquals(a.hashCode(), b.hashCode());
177      }
178  
177
179      /**
180 <     *   containsAll returns true for collection with subset of elements
180 >     * containsAll returns true for collection with subset of elements
181       */
182      public void testContainsAll() {
183          CopyOnWriteArrayList full = populatedArray(3);
# Line 189 | Line 190 | public class CopyOnWriteArrayListTest ex
190      }
191  
192      /**
193 <     *   get returns the value at the given index
193 >     * get returns the value at the given index
194       */
195      public void testGet() {
196          CopyOnWriteArrayList full = populatedArray(3);
# Line 197 | Line 198 | public class CopyOnWriteArrayListTest ex
198      }
199  
200      /**
201 <     *   indexOf gives the index for the given object
201 >     * indexOf gives the index for the given object
202       */
203      public void testIndexOf() {
204          CopyOnWriteArrayList full = populatedArray(3);
# Line 206 | Line 207 | public class CopyOnWriteArrayListTest ex
207      }
208  
209      /**
210 <     *   indexOf gives the index based on the given index
211 <     *  at which to start searching
210 >     * indexOf gives the index based on the given index
211 >     * at which to start searching
212       */
213      public void testIndexOf2() {
214          CopyOnWriteArrayList full = populatedArray(3);
# Line 216 | Line 217 | public class CopyOnWriteArrayListTest ex
217      }
218  
219      /**
220 <     *   isEmpty returns true when empty, else false
220 >     * isEmpty returns true when empty, else false
221       */
222      public void testIsEmpty() {
223          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
# Line 226 | Line 227 | public class CopyOnWriteArrayListTest ex
227      }
228  
229      /**
230 <     *   iterator() returns an iterator containing the elements of the list
230 >     * iterator() returns an iterator containing the elements of the list
231       */
232      public void testIterator() {
233          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 240 | Line 241 | public class CopyOnWriteArrayListTest ex
241      /**
242       * iterator.remove throws UnsupportedOperationException
243       */
244 <    public void testIteratorRemove () {
244 >    public void testIteratorRemove() {
245          CopyOnWriteArrayList full = populatedArray(SIZE);
246          Iterator it = full.iterator();
247          it.next();
# Line 257 | Line 258 | public class CopyOnWriteArrayListTest ex
258          CopyOnWriteArrayList full = populatedArray(3);
259          String s = full.toString();
260          for (int i = 0; i < 3; ++i) {
261 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
261 >            assertTrue(s.contains(String.valueOf(i)));
262          }
263      }
264  
265      /**
266 <     *   lastIndexOf returns the index for the given object
266 >     * lastIndexOf returns the index for the given object
267       */
268      public void testLastIndexOf1() {
269          CopyOnWriteArrayList full = populatedArray(3);
# Line 273 | Line 274 | public class CopyOnWriteArrayListTest ex
274      }
275  
276      /**
277 <     *   lastIndexOf returns the index from the given starting point
277 >     * lastIndexOf returns the index from the given starting point
278       */
279 <    public void testlastIndexOf2() {
279 >    public void testLastIndexOf2() {
280          CopyOnWriteArrayList full = populatedArray(3);
281          full.add(one);
282          full.add(three);
# Line 284 | Line 285 | public class CopyOnWriteArrayListTest ex
285      }
286  
287      /**
288 <     *  listIterator traverses all elements
288 >     * listIterator traverses all elements
289       */
290      public void testListIterator1() {
291          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 296 | Line 297 | public class CopyOnWriteArrayListTest ex
297      }
298  
299      /**
300 <     *  listIterator only returns those elements after the given index
300 >     * listIterator only returns those elements after the given index
301       */
302      public void testListIterator2() {
303          CopyOnWriteArrayList full = populatedArray(3);
# Line 308 | Line 309 | public class CopyOnWriteArrayListTest ex
309      }
310  
311      /**
312 <     *   remove  removes and returns the object at the given index
312 >     * remove removes and returns the object at the given index
313       */
314      public void testRemove() {
315          CopyOnWriteArrayList full = populatedArray(3);
# Line 317 | Line 318 | public class CopyOnWriteArrayListTest ex
318      }
319  
320      /**
321 <     *   removeAll  removes all elements from the given collection
321 >     * removeAll removes all elements from the given collection
322       */
323      public void testRemoveAll() {
324          CopyOnWriteArrayList full = populatedArray(3);
# Line 329 | Line 330 | public class CopyOnWriteArrayListTest ex
330      }
331  
332      /**
333 <     *   set  changes the element at the given index
333 >     * set changes the element at the given index
334       */
335      public void testSet() {
336          CopyOnWriteArrayList full = populatedArray(3);
# Line 338 | Line 339 | public class CopyOnWriteArrayListTest ex
339      }
340  
341      /**
342 <     *   size returns the number of elements
342 >     * size returns the number of elements
343       */
344      public void testSize() {
345          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
# Line 348 | Line 349 | public class CopyOnWriteArrayListTest ex
349      }
350  
351      /**
352 <     *   toArray returns an Object array containing all elements from the list
352 >     * toArray returns an Object array containing all elements from the list
353       */
354      public void testToArray() {
355          CopyOnWriteArrayList full = populatedArray(3);
# Line 360 | Line 361 | public class CopyOnWriteArrayListTest ex
361      }
362  
363      /**
364 <     *   toArray returns an Integer array containing all elements from
365 <     *   the list
364 >     * toArray returns an Integer array containing all elements from
365 >     * the list
366       */
367      public void testToArray2() {
368 <        CopyOnWriteArrayList full = populatedArray(3);
369 <        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());
368 >        final int size = 3;
369 >        CopyOnWriteArrayList<Integer> full = populatedArray(size);
370 >        Integer[] ints = new Integer[size];
371 >        assertSame(ints, full.toArray(ints));
372 >        Iterator<Integer> it = full.iterator();
373 >        for (int i = 0; i < size; i++)
374 >            assertSame(ints[i], it.next());
375 >        assertFalse(it.hasNext());
376      }
377  
376
378      /**
379       * sublists contains elements at indexes offset from their base
380       */
# Line 390 | Line 391 | public class CopyOnWriteArrayListTest ex
391          }
392  
393          List s = a.subList(2, 5);
394 <        assertEquals(s.size(), 3);
394 >        assertEquals(3, s.size());
395          s.set(2, m1);
396          assertEquals(a.get(4), m1);
397          s.clear();
398 <        assertEquals(a.size(), 7);
398 >        assertEquals(7, a.size());
399      }
400  
401      // Exception tests
402  
403      /**
404 <     *   toArray throws an ArrayStoreException when the given array
405 <     *  can not store the objects inside the list
404 >     * toArray throws an ArrayStoreException when the given array
405 >     * can not store the objects inside the list
406       */
407      public void testToArray_ArrayStoreException() {
408          try {
# Line 414 | Line 415 | public class CopyOnWriteArrayListTest ex
415      }
416  
417      /**
418 <     *   get throws an IndexOutOfBoundsException on a negative index
418 >     * get throws an IndexOutOfBoundsException on a negative index
419       */
420      public void testGet1_IndexOutOfBoundsException() {
421          try {
# Line 425 | Line 426 | public class CopyOnWriteArrayListTest ex
426      }
427  
428      /**
429 <     *   get throws an IndexOutOfBoundsException on a too high index
429 >     * get throws an IndexOutOfBoundsException on a too high index
430       */
431      public void testGet2_IndexOutOfBoundsException() {
432          try {
# Line 438 | Line 439 | public class CopyOnWriteArrayListTest ex
439      }
440  
441      /**
442 <     *   set throws an IndexOutOfBoundsException on a negative index
442 >     * set throws an IndexOutOfBoundsException on a negative index
443       */
444      public void testSet1_IndexOutOfBoundsException() {
445          try {
# Line 449 | Line 450 | public class CopyOnWriteArrayListTest ex
450      }
451  
452      /**
453 <     *   set throws an IndexOutOfBoundsException on a too high index
453 >     * set throws an IndexOutOfBoundsException on a too high index
454       */
455      public void testSet2() {
456          try {
# Line 462 | Line 463 | public class CopyOnWriteArrayListTest ex
463      }
464  
465      /**
466 <     *   add throws an IndexOutOfBoundsException on a negative index
466 >     * add throws an IndexOutOfBoundsException on a negative index
467       */
468      public void testAdd1_IndexOutOfBoundsException() {
469          try {
# Line 473 | Line 474 | public class CopyOnWriteArrayListTest ex
474      }
475  
476      /**
477 <     *   add throws an IndexOutOfBoundsException on a too high index
477 >     * add throws an IndexOutOfBoundsException on a too high index
478       */
479      public void testAdd2_IndexOutOfBoundsException() {
480          try {
# Line 486 | Line 487 | public class CopyOnWriteArrayListTest ex
487      }
488  
489      /**
490 <     *   remove throws an IndexOutOfBoundsException on a negative index
490 >     * remove throws an IndexOutOfBoundsException on a negative index
491       */
492      public void testRemove1_IndexOutOfBounds() {
493          try {
# Line 497 | Line 498 | public class CopyOnWriteArrayListTest ex
498      }
499  
500      /**
501 <     *   remove throws an IndexOutOfBoundsException on a too high index
501 >     * remove throws an IndexOutOfBoundsException on a too high index
502       */
503      public void testRemove2_IndexOutOfBounds() {
504          try {
# Line 510 | Line 511 | public class CopyOnWriteArrayListTest ex
511      }
512  
513      /**
514 <     *   addAll throws an IndexOutOfBoundsException on a negative index
514 >     * addAll throws an IndexOutOfBoundsException on a negative index
515       */
516      public void testAddAll1_IndexOutOfBoundsException() {
517          try {
# Line 521 | Line 522 | public class CopyOnWriteArrayListTest ex
522      }
523  
524      /**
525 <     *   addAll throws an IndexOutOfBoundsException on a too high index
525 >     * addAll throws an IndexOutOfBoundsException on a too high index
526       */
527      public void testAddAll2_IndexOutOfBoundsException() {
528          try {
# Line 534 | Line 535 | public class CopyOnWriteArrayListTest ex
535      }
536  
537      /**
538 <     *   listIterator throws an IndexOutOfBoundsException on a negative index
538 >     * listIterator throws an IndexOutOfBoundsException on a negative index
539       */
540      public void testListIterator1_IndexOutOfBoundsException() {
541          try {
# Line 545 | Line 546 | public class CopyOnWriteArrayListTest ex
546      }
547  
548      /**
549 <     *   listIterator throws an IndexOutOfBoundsException on a too high index
549 >     * listIterator throws an IndexOutOfBoundsException on a too high index
550       */
551      public void testListIterator2_IndexOutOfBoundsException() {
552          try {
# Line 558 | Line 559 | public class CopyOnWriteArrayListTest ex
559      }
560  
561      /**
562 <     *   subList throws an IndexOutOfBoundsException on a negative index
562 >     * subList throws an IndexOutOfBoundsException on a negative index
563       */
564      public void testSubList1_IndexOutOfBoundsException() {
565          try {
# Line 569 | Line 570 | public class CopyOnWriteArrayListTest ex
570      }
571  
572      /**
573 <     *   subList throws an IndexOutOfBoundsException on a too high index
573 >     * subList throws an IndexOutOfBoundsException on a too high index
574       */
575      public void testSubList2_IndexOutOfBoundsException() {
576          try {
# Line 581 | Line 582 | public class CopyOnWriteArrayListTest ex
582      }
583  
584      /**
585 <     *   subList throws IndexOutOfBoundsException when the second index
586 <     *  is lower then the first
585 >     * subList throws IndexOutOfBoundsException when the second index
586 >     * is lower then the first
587       */
588      public void testSubList3_IndexOutOfBoundsException() {
589          try {
# Line 593 | Line 594 | public class CopyOnWriteArrayListTest ex
594      }
595  
596      /**
597 <     * a deserialized serialiszed list is equal
597 >     * a deserialized serialized list is equal
598       */
599      public void testSerialization() throws Exception {
600 <        CopyOnWriteArrayList q = populatedArray(SIZE);
600 >        List x = populatedArray(SIZE);
601 >        List y = serialClone(x);
602  
603 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
604 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
605 <        out.writeObject(q);
606 <        out.close();
607 <
608 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
609 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
610 <        CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
611 <        assertEquals(q.size(), r.size());
612 <        assertTrue(q.equals(r));
613 <        assertTrue(r.equals(q));
603 >        assertTrue(x != y);
604 >        assertEquals(x.size(), y.size());
605 >        assertEquals(x.toString(), y.toString());
606 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
607 >        assertEquals(x, y);
608 >        assertEquals(y, x);
609 >        while (!x.isEmpty()) {
610 >            assertFalse(y.isEmpty());
611 >            assertEquals(x.remove(0), y.remove(0));
612 >        }
613 >        assertTrue(y.isEmpty());
614      }
615  
616   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines