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.22 by jsr166, Tue May 31 16:16:23 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() {
# Line 66 | Line 70 | public class CopyOnWriteArrayListTest ex
70  
71  
72      /**
73 <     *   addAll  adds each element from the given collection
73 >     * addAll adds each element from the given collection
74       */
75      public void testAddAll() {
76          CopyOnWriteArrayList full = populatedArray(3);
# Line 79 | Line 83 | public class CopyOnWriteArrayListTest ex
83      }
84  
85      /**
86 <     *   addAllAbsent adds each element from the given collection that did not
87 <     *  already exist in the List
86 >     * addAllAbsent adds each element from the given collection that did not
87 >     * already exist in the List
88       */
89      public void testAddAllAbsent() {
90          CopyOnWriteArrayList full = populatedArray(3);
# Line 93 | Line 97 | public class CopyOnWriteArrayListTest ex
97      }
98  
99      /**
100 <     *   addIfAbsent will not add the element if it already exists in the list
100 >     * addIfAbsent will not add the element if it already exists in the list
101       */
102      public void testAddIfAbsent() {
103          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 102 | Line 106 | public class CopyOnWriteArrayListTest ex
106      }
107  
108      /**
109 <     *   addIfAbsent adds the element when it does not exist in the list
109 >     * addIfAbsent adds the element when it does not exist in the list
110       */
111      public void testAddIfAbsent2() {
112          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 111 | Line 115 | public class CopyOnWriteArrayListTest ex
115      }
116  
117      /**
118 <     *   clear removes all elements from the list
118 >     * clear removes all elements from the list
119       */
120      public void testClear() {
121          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 121 | Line 125 | public class CopyOnWriteArrayListTest ex
125  
126  
127      /**
128 <     *  Cloned list is equal
128 >     * Cloned list is equal
129       */
130      public void testClone() {
131          CopyOnWriteArrayList l1 = populatedArray(SIZE);
# Line 132 | Line 136 | public class CopyOnWriteArrayListTest ex
136      }
137  
138      /**
139 <     *   contains is true for added elements
139 >     * contains is true for added elements
140       */
141      public void testContains() {
142          CopyOnWriteArrayList full = populatedArray(3);
# Line 176 | Line 180 | public class CopyOnWriteArrayListTest ex
180  
181  
182      /**
183 <     *   containsAll returns true for collection with subset of elements
183 >     * containsAll returns true for collection with subset of elements
184       */
185      public void testContainsAll() {
186          CopyOnWriteArrayList full = populatedArray(3);
# Line 189 | Line 193 | public class CopyOnWriteArrayListTest ex
193      }
194  
195      /**
196 <     *   get returns the value at the given index
196 >     * get returns the value at the given index
197       */
198      public void testGet() {
199          CopyOnWriteArrayList full = populatedArray(3);
# Line 197 | Line 201 | public class CopyOnWriteArrayListTest ex
201      }
202  
203      /**
204 <     *   indexOf gives the index for the given object
204 >     * indexOf gives the index for the given object
205       */
206      public void testIndexOf() {
207          CopyOnWriteArrayList full = populatedArray(3);
# Line 206 | Line 210 | public class CopyOnWriteArrayListTest ex
210      }
211  
212      /**
213 <     *   indexOf gives the index based on the given index
214 <     *  at which to start searching
213 >     * indexOf gives the index based on the given index
214 >     * at which to start searching
215       */
216      public void testIndexOf2() {
217          CopyOnWriteArrayList full = populatedArray(3);
# Line 216 | Line 220 | public class CopyOnWriteArrayListTest ex
220      }
221  
222      /**
223 <     *   isEmpty returns true when empty, else false
223 >     * isEmpty returns true when empty, else false
224       */
225      public void testIsEmpty() {
226          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
# Line 226 | Line 230 | public class CopyOnWriteArrayListTest ex
230      }
231  
232      /**
233 <     *   iterator() returns an iterator containing the elements of the list
233 >     * iterator() returns an iterator containing the elements of the list
234       */
235      public void testIterator() {
236          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 240 | Line 244 | public class CopyOnWriteArrayListTest ex
244      /**
245       * iterator.remove throws UnsupportedOperationException
246       */
247 <    public void testIteratorRemove () {
247 >    public void testIteratorRemove() {
248          CopyOnWriteArrayList full = populatedArray(SIZE);
249          Iterator it = full.iterator();
250          it.next();
# Line 257 | Line 261 | public class CopyOnWriteArrayListTest ex
261          CopyOnWriteArrayList full = populatedArray(3);
262          String s = full.toString();
263          for (int i = 0; i < 3; ++i) {
264 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
264 >            assertTrue(s.contains(String.valueOf(i)));
265          }
266      }
267  
268      /**
269 <     *   lastIndexOf returns the index for the given object
269 >     * lastIndexOf returns the index for the given object
270       */
271      public void testLastIndexOf1() {
272          CopyOnWriteArrayList full = populatedArray(3);
# Line 273 | Line 277 | public class CopyOnWriteArrayListTest ex
277      }
278  
279      /**
280 <     *   lastIndexOf returns the index from the given starting point
280 >     * lastIndexOf returns the index from the given starting point
281       */
282 <    public void testlastIndexOf2() {
282 >    public void testLastIndexOf2() {
283          CopyOnWriteArrayList full = populatedArray(3);
284          full.add(one);
285          full.add(three);
# Line 284 | Line 288 | public class CopyOnWriteArrayListTest ex
288      }
289  
290      /**
291 <     *  listIterator traverses all elements
291 >     * listIterator traverses all elements
292       */
293      public void testListIterator1() {
294          CopyOnWriteArrayList full = populatedArray(SIZE);
# Line 296 | Line 300 | public class CopyOnWriteArrayListTest ex
300      }
301  
302      /**
303 <     *  listIterator only returns those elements after the given index
303 >     * listIterator only returns those elements after the given index
304       */
305      public void testListIterator2() {
306          CopyOnWriteArrayList full = populatedArray(3);
# Line 308 | Line 312 | public class CopyOnWriteArrayListTest ex
312      }
313  
314      /**
315 <     *   remove  removes and returns the object at the given index
315 >     * remove removes and returns the object at the given index
316       */
317      public void testRemove() {
318          CopyOnWriteArrayList full = populatedArray(3);
319 <        assertEquals(two, full.remove(2));
319 >        assertEquals(2, full.remove(2));
320          assertEquals(2, full.size());
321      }
322  
323      /**
324 <     *   removeAll  removes all elements from the given collection
324 >     * removeAll removes all elements from the given collection
325       */
326      public void testRemoveAll() {
327          CopyOnWriteArrayList full = populatedArray(3);
# Line 329 | Line 333 | public class CopyOnWriteArrayListTest ex
333      }
334  
335      /**
336 <     *   set  changes the element at the given index
336 >     * set changes the element at the given index
337       */
338      public void testSet() {
339          CopyOnWriteArrayList full = populatedArray(3);
340 <        assertEquals(two, full.set(2, four));
340 >        assertEquals(2, full.set(2, four));
341          assertEquals(4, full.get(2));
342      }
343  
344      /**
345 <     *   size returns the number of elements
345 >     * size returns the number of elements
346       */
347      public void testSize() {
348          CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
# Line 348 | Line 352 | public class CopyOnWriteArrayListTest ex
352      }
353  
354      /**
355 <     *   toArray returns an Object array containing all elements from the list
355 >     * toArray returns an Object array containing all elements from the list
356       */
357      public void testToArray() {
358          CopyOnWriteArrayList full = populatedArray(3);
# Line 360 | Line 364 | public class CopyOnWriteArrayListTest ex
364      }
365  
366      /**
367 <     *   toArray returns an Integer array containing all elements from
368 <     *   the list
367 >     * toArray returns an Integer array containing all elements from
368 >     * the list
369       */
370      public void testToArray2() {
371          CopyOnWriteArrayList full = populatedArray(3);
# Line 400 | Line 404 | public class CopyOnWriteArrayListTest ex
404      // Exception tests
405  
406      /**
407 <     *   toArray throws an ArrayStoreException when the given array
408 <     *  can not store the objects inside the list
407 >     * toArray throws an ArrayStoreException when the given array
408 >     * can not store the objects inside the list
409       */
410      public void testToArray_ArrayStoreException() {
411          try {
# Line 414 | Line 418 | public class CopyOnWriteArrayListTest ex
418      }
419  
420      /**
421 <     *   get throws an IndexOutOfBoundsException on a negative index
421 >     * get throws an IndexOutOfBoundsException on a negative index
422       */
423      public void testGet1_IndexOutOfBoundsException() {
424          try {
# Line 425 | Line 429 | public class CopyOnWriteArrayListTest ex
429      }
430  
431      /**
432 <     *   get throws an IndexOutOfBoundsException on a too high index
432 >     * get throws an IndexOutOfBoundsException on a too high index
433       */
434      public void testGet2_IndexOutOfBoundsException() {
435          try {
# Line 438 | Line 442 | public class CopyOnWriteArrayListTest ex
442      }
443  
444      /**
445 <     *   set throws an IndexOutOfBoundsException on a negative index
445 >     * set throws an IndexOutOfBoundsException on a negative index
446       */
447      public void testSet1_IndexOutOfBoundsException() {
448          try {
# Line 449 | Line 453 | public class CopyOnWriteArrayListTest ex
453      }
454  
455      /**
456 <     *   set throws an IndexOutOfBoundsException on a too high index
456 >     * set throws an IndexOutOfBoundsException on a too high index
457       */
458      public void testSet2() {
459          try {
# Line 462 | Line 466 | public class CopyOnWriteArrayListTest ex
466      }
467  
468      /**
469 <     *   add throws an IndexOutOfBoundsException on a negative index
469 >     * add throws an IndexOutOfBoundsException on a negative index
470       */
471      public void testAdd1_IndexOutOfBoundsException() {
472          try {
# Line 473 | Line 477 | public class CopyOnWriteArrayListTest ex
477      }
478  
479      /**
480 <     *   add throws an IndexOutOfBoundsException on a too high index
480 >     * add throws an IndexOutOfBoundsException on a too high index
481       */
482      public void testAdd2_IndexOutOfBoundsException() {
483          try {
# Line 486 | Line 490 | public class CopyOnWriteArrayListTest ex
490      }
491  
492      /**
493 <     *   remove throws an IndexOutOfBoundsException on a negative index
493 >     * remove throws an IndexOutOfBoundsException on a negative index
494       */
495      public void testRemove1_IndexOutOfBounds() {
496          try {
# Line 497 | Line 501 | public class CopyOnWriteArrayListTest ex
501      }
502  
503      /**
504 <     *   remove throws an IndexOutOfBoundsException on a too high index
504 >     * remove throws an IndexOutOfBoundsException on a too high index
505       */
506      public void testRemove2_IndexOutOfBounds() {
507          try {
# Line 510 | Line 514 | public class CopyOnWriteArrayListTest ex
514      }
515  
516      /**
517 <     *   addAll throws an IndexOutOfBoundsException on a negative index
517 >     * addAll throws an IndexOutOfBoundsException on a negative index
518       */
519      public void testAddAll1_IndexOutOfBoundsException() {
520          try {
# Line 521 | Line 525 | public class CopyOnWriteArrayListTest ex
525      }
526  
527      /**
528 <     *   addAll throws an IndexOutOfBoundsException on a too high index
528 >     * addAll throws an IndexOutOfBoundsException on a too high index
529       */
530      public void testAddAll2_IndexOutOfBoundsException() {
531          try {
# Line 534 | Line 538 | public class CopyOnWriteArrayListTest ex
538      }
539  
540      /**
541 <     *   listIterator throws an IndexOutOfBoundsException on a negative index
541 >     * listIterator throws an IndexOutOfBoundsException on a negative index
542       */
543      public void testListIterator1_IndexOutOfBoundsException() {
544          try {
# Line 545 | Line 549 | public class CopyOnWriteArrayListTest ex
549      }
550  
551      /**
552 <     *   listIterator throws an IndexOutOfBoundsException on a too high index
552 >     * listIterator throws an IndexOutOfBoundsException on a too high index
553       */
554      public void testListIterator2_IndexOutOfBoundsException() {
555          try {
# Line 558 | Line 562 | public class CopyOnWriteArrayListTest ex
562      }
563  
564      /**
565 <     *   subList throws an IndexOutOfBoundsException on a negative index
565 >     * subList throws an IndexOutOfBoundsException on a negative index
566       */
567      public void testSubList1_IndexOutOfBoundsException() {
568          try {
# Line 569 | Line 573 | public class CopyOnWriteArrayListTest ex
573      }
574  
575      /**
576 <     *   subList throws an IndexOutOfBoundsException on a too high index
576 >     * subList throws an IndexOutOfBoundsException on a too high index
577       */
578      public void testSubList2_IndexOutOfBoundsException() {
579          try {
# Line 581 | Line 585 | public class CopyOnWriteArrayListTest ex
585      }
586  
587      /**
588 <     *   subList throws IndexOutOfBoundsException when the second index
589 <     *  is lower then the first
588 >     * subList throws IndexOutOfBoundsException when the second index
589 >     * is lower then the first
590       */
591      public void testSubList3_IndexOutOfBoundsException() {
592          try {
# Line 593 | Line 597 | public class CopyOnWriteArrayListTest ex
597      }
598  
599      /**
600 <     * a deserialized serialiszed list is equal
600 >     * a deserialized serialized list is equal
601       */
602      public void testSerialization() throws Exception {
603 <        CopyOnWriteArrayList q = populatedArray(SIZE);
603 >        List x = populatedArray(SIZE);
604 >        List y = serialClone(x);
605  
606 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
607 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
608 <        out.writeObject(q);
609 <        out.close();
610 <
611 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
612 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
613 <        CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
614 <        assertEquals(q.size(), r.size());
615 <        assertTrue(q.equals(r));
616 <        assertTrue(r.equals(q));
606 >        assertTrue(x != y);
607 >        assertEquals(x.size(), y.size());
608 >        assertEquals(x.toString(), y.toString());
609 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
610 >        assertEquals(x, y);
611 >        assertEquals(y, x);
612 >        while (!x.isEmpty()) {
613 >            assertFalse(y.isEmpty());
614 >            assertEquals(x.remove(0), y.remove(0));
615 >        }
616 >        assertTrue(y.isEmpty());
617      }
618  
619   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines