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.17 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.23 by jsr166, Sat Jun 18 14:32:14 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  
# Line 31 | Line 35 | public class CopyOnWriteArrayListTest ex
35          return a;
36      }
37  
34
38      /**
39       * a new list is empty
40       */
# Line 64 | Line 67 | public class CopyOnWriteArrayListTest ex
67              assertEquals(ints[i], a.get(i));
68      }
69  
67
70      /**
71 <     *   addAll  adds each element from the given collection
71 >     * addAll adds each element from the given collection
72       */
73      public void testAddAll() {
74          CopyOnWriteArrayList full = populatedArray(3);
# Line 79 | Line 81 | public class CopyOnWriteArrayListTest ex
81      }
82  
83      /**
84 <     *   addAllAbsent adds each element from the given collection that did not
85 <     *  already exist in the List
84 >     * addAllAbsent adds each element from the given collection that did not
85 >     * already exist in the List
86       */
87      public void testAddAllAbsent() {
88          CopyOnWriteArrayList full = populatedArray(3);
# Line 93 | Line 95 | public class CopyOnWriteArrayListTest ex
95      }
96  
97      /**
98 <     *   addIfAbsent will not add the element if it already exists in the list
98 >     * addIfAbsent will not add the element if it already exists in the list
99       */
100      public void testAddIfAbsent() {
101          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 102 | Line 104 | public class CopyOnWriteArrayListTest ex
104      }
105  
106      /**
107 <     *   addIfAbsent adds the element when it does not exist in the list
107 >     * addIfAbsent adds the element when it does not exist in the list
108       */
109      public void testAddIfAbsent2() {
110          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 111 | Line 113 | public class CopyOnWriteArrayListTest ex
113      }
114  
115      /**
116 <     *   clear removes all elements from the list
116 >     * clear removes all elements from the list
117       */
118      public void testClear() {
119          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 119 | Line 121 | public class CopyOnWriteArrayListTest ex
121          assertEquals(0, full.size());
122      }
123  
122
124      /**
125 <     *  Cloned list is equal
125 >     * Cloned list is equal
126       */
127      public void testClone() {
128          CopyOnWriteArrayList l1 = populatedArray(SIZE);
# Line 132 | Line 133 | public class CopyOnWriteArrayListTest ex
133      }
134  
135      /**
136 <     *   contains is true for added elements
136 >     * contains is true for added elements
137       */
138      public void testContains() {
139          CopyOnWriteArrayList full = populatedArray(3);
# Line 174 | Line 175 | public class CopyOnWriteArrayListTest ex
175          assertEquals(a.hashCode(), b.hashCode());
176      }
177  
177
178      /**
179 <     *   containsAll returns true for collection with subset of elements
179 >     * containsAll returns true for collection with subset of elements
180       */
181      public void testContainsAll() {
182          CopyOnWriteArrayList full = populatedArray(3);
# Line 189 | Line 189 | public class CopyOnWriteArrayListTest ex
189      }
190  
191      /**
192 <     *   get returns the value at the given index
192 >     * get returns the value at the given index
193       */
194      public void testGet() {
195          CopyOnWriteArrayList full = populatedArray(3);
# Line 197 | Line 197 | public class CopyOnWriteArrayListTest ex
197      }
198  
199      /**
200 <     *   indexOf gives the index for the given object
200 >     * indexOf gives the index for the given object
201       */
202      public void testIndexOf() {
203          CopyOnWriteArrayList full = populatedArray(3);
# Line 206 | Line 206 | public class CopyOnWriteArrayListTest ex
206      }
207  
208      /**
209 <     *   indexOf gives the index based on the given index
210 <     *  at which to start searching
209 >     * indexOf gives the index based on the given index
210 >     * at which to start searching
211       */
212      public void testIndexOf2() {
213          CopyOnWriteArrayList full = populatedArray(3);
# Line 216 | Line 216 | public class CopyOnWriteArrayListTest ex
216      }
217  
218      /**
219 <     *   isEmpty returns true when empty, else false
219 >     * isEmpty returns true when empty, else false
220       */
221      public void testIsEmpty() {
222          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
# Line 226 | Line 226 | public class CopyOnWriteArrayListTest ex
226      }
227  
228      /**
229 <     *   iterator() returns an iterator containing the elements of the list
229 >     * iterator() returns an iterator containing the elements of the list
230       */
231      public void testIterator() {
232          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 257 | Line 257 | public class CopyOnWriteArrayListTest ex
257          CopyOnWriteArrayList full = populatedArray(3);
258          String s = full.toString();
259          for (int i = 0; i < 3; ++i) {
260 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
260 >            assertTrue(s.contains(String.valueOf(i)));
261          }
262      }
263  
264      /**
265 <     *   lastIndexOf returns the index for the given object
265 >     * lastIndexOf returns the index for the given object
266       */
267      public void testLastIndexOf1() {
268          CopyOnWriteArrayList full = populatedArray(3);
# Line 273 | Line 273 | public class CopyOnWriteArrayListTest ex
273      }
274  
275      /**
276 <     *   lastIndexOf returns the index from the given starting point
276 >     * lastIndexOf returns the index from the given starting point
277       */
278 <    public void testlastIndexOf2() {
278 >    public void testLastIndexOf2() {
279          CopyOnWriteArrayList full = populatedArray(3);
280          full.add(one);
281          full.add(three);
# Line 284 | Line 284 | public class CopyOnWriteArrayListTest ex
284      }
285  
286      /**
287 <     *  listIterator traverses all elements
287 >     * listIterator traverses all elements
288       */
289      public void testListIterator1() {
290          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 296 | Line 296 | public class CopyOnWriteArrayListTest ex
296      }
297  
298      /**
299 <     *  listIterator only returns those elements after the given index
299 >     * listIterator only returns those elements after the given index
300       */
301      public void testListIterator2() {
302          CopyOnWriteArrayList full = populatedArray(3);
# Line 308 | Line 308 | public class CopyOnWriteArrayListTest ex
308      }
309  
310      /**
311 <     *   remove  removes and returns the object at the given index
311 >     * remove removes and returns the object at the given index
312       */
313      public void testRemove() {
314          CopyOnWriteArrayList full = populatedArray(3);
# Line 317 | Line 317 | public class CopyOnWriteArrayListTest ex
317      }
318  
319      /**
320 <     *   removeAll  removes all elements from the given collection
320 >     * removeAll removes all elements from the given collection
321       */
322      public void testRemoveAll() {
323          CopyOnWriteArrayList full = populatedArray(3);
# Line 329 | Line 329 | public class CopyOnWriteArrayListTest ex
329      }
330  
331      /**
332 <     *   set  changes the element at the given index
332 >     * set changes the element at the given index
333       */
334      public void testSet() {
335          CopyOnWriteArrayList full = populatedArray(3);
# Line 338 | Line 338 | public class CopyOnWriteArrayListTest ex
338      }
339  
340      /**
341 <     *   size returns the number of elements
341 >     * size returns the number of elements
342       */
343      public void testSize() {
344          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
# Line 348 | Line 348 | public class CopyOnWriteArrayListTest ex
348      }
349  
350      /**
351 <     *   toArray returns an Object array containing all elements from the list
351 >     * toArray returns an Object array containing all elements from the list
352       */
353      public void testToArray() {
354          CopyOnWriteArrayList full = populatedArray(3);
# Line 360 | Line 360 | public class CopyOnWriteArrayListTest ex
360      }
361  
362      /**
363 <     *   toArray returns an Integer array containing all elements from
364 <     *   the list
363 >     * toArray returns an Integer array containing all elements from
364 >     * the list
365       */
366      public void testToArray2() {
367          CopyOnWriteArrayList full = populatedArray(3);
# Line 373 | Line 373 | public class CopyOnWriteArrayListTest ex
373          assertEquals(2, i[2].intValue());
374      }
375  
376
376      /**
377       * sublists contains elements at indexes offset from their base
378       */
# Line 400 | Line 399 | public class CopyOnWriteArrayListTest ex
399      // Exception tests
400  
401      /**
402 <     *   toArray throws an ArrayStoreException when the given array
403 <     *  can not store the objects inside the list
402 >     * toArray throws an ArrayStoreException when the given array
403 >     * can not store the objects inside the list
404       */
405      public void testToArray_ArrayStoreException() {
406          try {
# Line 414 | Line 413 | public class CopyOnWriteArrayListTest ex
413      }
414  
415      /**
416 <     *   get throws an IndexOutOfBoundsException on a negative index
416 >     * get throws an IndexOutOfBoundsException on a negative index
417       */
418      public void testGet1_IndexOutOfBoundsException() {
419          try {
# Line 425 | Line 424 | public class CopyOnWriteArrayListTest ex
424      }
425  
426      /**
427 <     *   get throws an IndexOutOfBoundsException on a too high index
427 >     * get throws an IndexOutOfBoundsException on a too high index
428       */
429      public void testGet2_IndexOutOfBoundsException() {
430          try {
# Line 438 | Line 437 | public class CopyOnWriteArrayListTest ex
437      }
438  
439      /**
440 <     *   set throws an IndexOutOfBoundsException on a negative index
440 >     * set throws an IndexOutOfBoundsException on a negative index
441       */
442      public void testSet1_IndexOutOfBoundsException() {
443          try {
# Line 449 | Line 448 | public class CopyOnWriteArrayListTest ex
448      }
449  
450      /**
451 <     *   set throws an IndexOutOfBoundsException on a too high index
451 >     * set throws an IndexOutOfBoundsException on a too high index
452       */
453      public void testSet2() {
454          try {
# Line 462 | Line 461 | public class CopyOnWriteArrayListTest ex
461      }
462  
463      /**
464 <     *   add throws an IndexOutOfBoundsException on a negative index
464 >     * add throws an IndexOutOfBoundsException on a negative index
465       */
466      public void testAdd1_IndexOutOfBoundsException() {
467          try {
# Line 473 | Line 472 | public class CopyOnWriteArrayListTest ex
472      }
473  
474      /**
475 <     *   add throws an IndexOutOfBoundsException on a too high index
475 >     * add throws an IndexOutOfBoundsException on a too high index
476       */
477      public void testAdd2_IndexOutOfBoundsException() {
478          try {
# Line 486 | Line 485 | public class CopyOnWriteArrayListTest ex
485      }
486  
487      /**
488 <     *   remove throws an IndexOutOfBoundsException on a negative index
488 >     * remove throws an IndexOutOfBoundsException on a negative index
489       */
490      public void testRemove1_IndexOutOfBounds() {
491          try {
# Line 497 | Line 496 | public class CopyOnWriteArrayListTest ex
496      }
497  
498      /**
499 <     *   remove throws an IndexOutOfBoundsException on a too high index
499 >     * remove throws an IndexOutOfBoundsException on a too high index
500       */
501      public void testRemove2_IndexOutOfBounds() {
502          try {
# Line 510 | Line 509 | public class CopyOnWriteArrayListTest ex
509      }
510  
511      /**
512 <     *   addAll throws an IndexOutOfBoundsException on a negative index
512 >     * addAll throws an IndexOutOfBoundsException on a negative index
513       */
514      public void testAddAll1_IndexOutOfBoundsException() {
515          try {
# Line 521 | Line 520 | public class CopyOnWriteArrayListTest ex
520      }
521  
522      /**
523 <     *   addAll throws an IndexOutOfBoundsException on a too high index
523 >     * addAll throws an IndexOutOfBoundsException on a too high index
524       */
525      public void testAddAll2_IndexOutOfBoundsException() {
526          try {
# Line 534 | Line 533 | public class CopyOnWriteArrayListTest ex
533      }
534  
535      /**
536 <     *   listIterator throws an IndexOutOfBoundsException on a negative index
536 >     * listIterator throws an IndexOutOfBoundsException on a negative index
537       */
538      public void testListIterator1_IndexOutOfBoundsException() {
539          try {
# Line 545 | Line 544 | public class CopyOnWriteArrayListTest ex
544      }
545  
546      /**
547 <     *   listIterator throws an IndexOutOfBoundsException on a too high index
547 >     * listIterator throws an IndexOutOfBoundsException on a too high index
548       */
549      public void testListIterator2_IndexOutOfBoundsException() {
550          try {
# Line 558 | Line 557 | public class CopyOnWriteArrayListTest ex
557      }
558  
559      /**
560 <     *   subList throws an IndexOutOfBoundsException on a negative index
560 >     * subList throws an IndexOutOfBoundsException on a negative index
561       */
562      public void testSubList1_IndexOutOfBoundsException() {
563          try {
# Line 569 | Line 568 | public class CopyOnWriteArrayListTest ex
568      }
569  
570      /**
571 <     *   subList throws an IndexOutOfBoundsException on a too high index
571 >     * subList throws an IndexOutOfBoundsException on a too high index
572       */
573      public void testSubList2_IndexOutOfBoundsException() {
574          try {
# Line 581 | Line 580 | public class CopyOnWriteArrayListTest ex
580      }
581  
582      /**
583 <     *   subList throws IndexOutOfBoundsException when the second index
584 <     *  is lower then the first
583 >     * subList throws IndexOutOfBoundsException when the second index
584 >     * is lower then the first
585       */
586      public void testSubList3_IndexOutOfBoundsException() {
587          try {
# Line 596 | Line 595 | public class CopyOnWriteArrayListTest ex
595       * a deserialized serialized list is equal
596       */
597      public void testSerialization() throws Exception {
598 <        CopyOnWriteArrayList q = populatedArray(SIZE);
598 >        List x = populatedArray(SIZE);
599 >        List y = serialClone(x);
600  
601 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
602 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
603 <        out.writeObject(q);
604 <        out.close();
605 <
606 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
607 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
608 <        CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
609 <        assertEquals(q.size(), r.size());
610 <        assertTrue(q.equals(r));
611 <        assertTrue(r.equals(q));
601 >        assertTrue(x != y);
602 >        assertEquals(x.size(), y.size());
603 >        assertEquals(x.toString(), y.toString());
604 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
605 >        assertEquals(x, y);
606 >        assertEquals(y, x);
607 >        while (!x.isEmpty()) {
608 >            assertFalse(y.isEmpty());
609 >            assertEquals(x.remove(0), y.remove(0));
610 >        }
611 >        assertTrue(y.isEmpty());
612      }
613  
614   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines