ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +143 -105 lines
Log Message:
Documentation scaffolding

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.*;
10     import java.util.concurrent.*;
11 dl 1.2 import java.io.*;
12 dl 1.1
13 dl 1.3 public class CopyOnWriteArrayListTest extends JSR166TestCase{
14 dl 1.1
15     public static void main(String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18    
19     public static Test suite() {
20     return new TestSuite(CopyOnWriteArrayListTest.class);
21     }
22    
23 dl 1.3 static CopyOnWriteArrayList populatedArray(int n){
24 dl 1.1 CopyOnWriteArrayList a = new CopyOnWriteArrayList();
25     assertTrue(a.isEmpty());
26     for (int i = 0; i < n; ++i)
27     a.add(new Integer(i));
28     assertFalse(a.isEmpty());
29     assertEquals(n, a.size());
30     return a;
31     }
32    
33 dl 1.3
34 dl 1.4 /**
35     *
36     */
37 dl 1.3 public void testConstructor() {
38     CopyOnWriteArrayList a = new CopyOnWriteArrayList();
39     assertTrue(a.isEmpty());
40     }
41    
42 dl 1.4 /**
43     *
44     */
45 dl 1.3 public void testConstructor2() {
46     Integer[] ints = new Integer[SIZE];
47     for (int i = 0; i < SIZE-1; ++i)
48     ints[i] = new Integer(i);
49     CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
50     for (int i = 0; i < SIZE; ++i)
51     assertEquals(ints[i], a.get(i));
52     }
53    
54 dl 1.4 /**
55     *
56     */
57 dl 1.3 public void testConstructor3() {
58     Integer[] ints = new Integer[SIZE];
59     for (int i = 0; i < SIZE-1; ++i)
60     ints[i] = new Integer(i);
61     CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
62     for (int i = 0; i < SIZE; ++i)
63     assertEquals(ints[i], a.get(i));
64     }
65    
66    
67 dl 1.1 /**
68 dl 1.3 * addAll correctly adds each element from the given collection
69 dl 1.1 */
70 dl 1.4 public void testAddAll() {
71 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
72 dl 1.1 Vector v = new Vector();
73 dl 1.3 v.add(three);
74     v.add(four);
75     v.add(five);
76 dl 1.1 full.addAll(v);
77     assertEquals(6, full.size());
78     }
79    
80     /**
81 dl 1.3 * addAllAbsent adds each element from the given collection that did not
82 dl 1.1 * already exist in the List
83     */
84 dl 1.4 public void testAddAllAbsent() {
85 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
86 dl 1.1 Vector v = new Vector();
87 dl 1.3 v.add(three);
88     v.add(four);
89     v.add(one); // will not add this element
90 dl 1.1 full.addAllAbsent(v);
91     assertEquals(5, full.size());
92     }
93    
94     /**
95 dl 1.3 * addIfAbsent will not add the element if it already exists in the list
96 dl 1.1 */
97 dl 1.4 public void testAddIfAbsent() {
98     CopyOnWriteArrayList full = populatedArray(SIZE);
99 dl 1.3 full.addIfAbsent(one);
100 dl 1.4 assertEquals(SIZE, full.size());
101 dl 1.1 }
102    
103     /**
104 dl 1.3 * addIfAbsent correctly adds the element when it does not exist in the list
105 dl 1.1 */
106 dl 1.4 public void testAddIfAbsent2() {
107     CopyOnWriteArrayList full = populatedArray(SIZE);
108 dl 1.3 full.addIfAbsent(three);
109     assertTrue(full.contains(three));
110 dl 1.1 }
111    
112     /**
113 dl 1.3 * clear correctly removes all elements from the list
114 dl 1.1 */
115 dl 1.4 public void testClear() {
116     CopyOnWriteArrayList full = populatedArray(SIZE);
117 dl 1.1 full.clear();
118     assertEquals(0, full.size());
119     }
120    
121 dl 1.4
122     /**
123     * Cloned list is equal
124     */
125     public void testClone() {
126     CopyOnWriteArrayList l1 = populatedArray(SIZE);
127     CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
128     assertEquals(l1, l2);
129     l1.clear();
130     assertFalse(l1.equals(l2));
131     }
132    
133 dl 1.1 /**
134 dl 1.3 * contains returns the correct values
135 dl 1.1 */
136 dl 1.4 public void testContains() {
137 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
138     assertTrue(full.contains(one));
139     assertFalse(full.contains(five));
140 dl 1.1 }
141    
142 dl 1.4 /**
143     *
144     */
145 dl 1.1 public void testAddIndex() {
146 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
147     full.add(0, m1);
148 dl 1.1 assertEquals(4, full.size());
149 dl 1.3 assertEquals(m1, full.get(0));
150     assertEquals(zero, full.get(1));
151 dl 1.1
152 dl 1.3 full.add(2, m2);
153 dl 1.1 assertEquals(5, full.size());
154 dl 1.3 assertEquals(m2, full.get(2));
155     assertEquals(two, full.get(4));
156 dl 1.1 }
157    
158 dl 1.4 /**
159     *
160     */
161 dl 1.1 public void testEquals() {
162 dl 1.3 CopyOnWriteArrayList a = populatedArray(3);
163     CopyOnWriteArrayList b = populatedArray(3);
164 dl 1.1 assertTrue(a.equals(b));
165     assertTrue(b.equals(a));
166     assertEquals(a.hashCode(), b.hashCode());
167 dl 1.3 a.add(m1);
168 dl 1.1 assertFalse(a.equals(b));
169     assertFalse(b.equals(a));
170 dl 1.3 b.add(m1);
171 dl 1.1 assertTrue(a.equals(b));
172     assertTrue(b.equals(a));
173     assertEquals(a.hashCode(), b.hashCode());
174     }
175    
176    
177     /**
178 dl 1.3 * containsAll returns the correct values
179 dl 1.1 */
180 dl 1.4 public void testContainsAll() {
181 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
182 dl 1.1 Vector v = new Vector();
183 dl 1.3 v.add(one);
184     v.add(two);
185 dl 1.1 assertTrue(full.containsAll(v));
186 dl 1.3 v.add(six);
187 dl 1.1 assertFalse(full.containsAll(v));
188     }
189    
190     /**
191 dl 1.3 * get returns the correct value for the given index
192 dl 1.1 */
193 dl 1.4 public void testGet() {
194 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
195 dl 1.1 assertEquals(0, ((Integer)full.get(0)).intValue());
196     }
197    
198     /**
199 dl 1.3 * indexOf gives the correct index for the given object
200 dl 1.1 */
201 dl 1.4 public void testIndexOf() {
202 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
203     assertEquals(1, full.indexOf(one));
204 dl 1.1 assertEquals(-1, full.indexOf("puppies"));
205     }
206    
207     /**
208 dl 1.3 * indexOf gives the correct index based on the given index
209 dl 1.1 * at which to start searching
210     */
211 dl 1.4 public void testIndexOf2() {
212 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
213     assertEquals(1, full.indexOf(one, 0));
214     assertEquals(-1, full.indexOf(one, 2));
215 dl 1.1 }
216    
217     /**
218 dl 1.3 * isEmpty returns the correct values
219 dl 1.1 */
220 dl 1.4 public void testIsEmpty() {
221 dl 1.1 CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
222 dl 1.4 CopyOnWriteArrayList full = populatedArray(SIZE);
223 dl 1.1 assertTrue(empty.isEmpty());
224     assertFalse(full.isEmpty());
225     }
226    
227     /**
228 dl 1.3 * iterator() returns an iterator containing the elements of the list
229 dl 1.1 */
230 dl 1.4 public void testIterator() {
231     CopyOnWriteArrayList full = populatedArray(SIZE);
232 dl 1.1 Iterator i = full.iterator();
233     int j;
234     for(j = 0; i.hasNext(); j++)
235     assertEquals(j, ((Integer)i.next()).intValue());
236 dl 1.4 assertEquals(SIZE, j);
237 dl 1.1 }
238    
239 dl 1.4 /**
240     *
241     */
242 dl 1.1 public void testIteratorRemove () {
243 dl 1.4 CopyOnWriteArrayList full = populatedArray(SIZE);
244 dl 1.1 Iterator it = full.iterator();
245     it.next();
246     try {
247     it.remove();
248 dl 1.4 shouldThrow();
249 dl 1.1 }
250     catch (UnsupportedOperationException success) {}
251     }
252    
253 dl 1.4 /**
254     *
255     */
256     public void testToString() {
257 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
258 dl 1.1 String s = full.toString();
259     for (int i = 0; i < 3; ++i) {
260     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
261     }
262     }
263    
264     /**
265 dl 1.3 * lastIndexOf returns the correct index for the given object
266 dl 1.1 */
267 dl 1.4 public void testLastIndexOf1() {
268 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
269     full.add(one);
270     full.add(three);
271     assertEquals(3, full.lastIndexOf(one));
272     assertEquals(-1, full.lastIndexOf(six));
273 dl 1.1 }
274    
275     /**
276 dl 1.3 * lastIndexOf returns the correct index from the given starting point
277 dl 1.1 */
278 dl 1.4 public void testlastIndexOf2() {
279 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
280     full.add(one);
281     full.add(three);
282     assertEquals(3, full.lastIndexOf(one, 4));
283     assertEquals(-1, full.lastIndexOf(three, 3));
284 dl 1.1 }
285    
286     /**
287     * Identical to testIterator, except ListInterator has more functionality
288     */
289 dl 1.4 public void testListIterator1() {
290     CopyOnWriteArrayList full = populatedArray(SIZE);
291 dl 1.1 ListIterator i = full.listIterator();
292     int j;
293     for(j = 0; i.hasNext(); j++)
294     assertEquals(j, ((Integer)i.next()).intValue());
295 dl 1.4 assertEquals(SIZE, j);
296 dl 1.1 }
297    
298     /**
299 dl 1.4 * listIterator only returns those elements after the given index
300 dl 1.1 */
301 dl 1.4 public void testListIterator2() {
302 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
303 dl 1.1 ListIterator i = full.listIterator(1);
304     int j;
305     for(j = 0; i.hasNext(); j++)
306     assertEquals(j+1, ((Integer)i.next()).intValue());
307     assertEquals(2, j);
308     }
309    
310     /**
311 dl 1.3 * remove correctly removes and returns the object at the given index
312 dl 1.1 */
313 dl 1.4 public void testRemove() {
314 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
315     assertEquals(two, full.remove(2));
316 dl 1.1 assertEquals(2, full.size());
317     }
318    
319     /**
320 dl 1.3 * removeAll correctly removes all elements from the given collection
321 dl 1.1 */
322 dl 1.4 public void testRemoveAll() {
323 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
324 dl 1.1 Vector v = new Vector();
325 dl 1.3 v.add(one);
326     v.add(two);
327 dl 1.1 full.removeAll(v);
328     assertEquals(1, full.size());
329     }
330    
331     /**
332 dl 1.3 * set correctly changes the element at the given index
333 dl 1.1 */
334 dl 1.4 public void testSet() {
335 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
336     assertEquals(two, full.set(2, four));
337 dl 1.1 assertEquals(4, ((Integer)full.get(2)).intValue());
338     }
339    
340     /**
341 dl 1.3 * size returns the correct values
342 dl 1.1 */
343 dl 1.4 public void testSize() {
344 dl 1.1 CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
345 dl 1.4 CopyOnWriteArrayList full = populatedArray(SIZE);
346     assertEquals(SIZE, full.size());
347 dl 1.1 assertEquals(0, empty.size());
348     }
349    
350     /**
351 dl 1.3 * toArray returns an Object array containing all elements from the list
352 dl 1.1 */
353 dl 1.4 public void testToArray() {
354 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
355 dl 1.1 Object[] o = full.toArray();
356     assertEquals(3, o.length);
357     assertEquals(0, ((Integer)o[0]).intValue());
358     assertEquals(1, ((Integer)o[1]).intValue());
359     assertEquals(2, ((Integer)o[2]).intValue());
360     }
361    
362     /**
363 dl 1.4 * toArray returns an Integer array containing all elements from
364     * the list
365 dl 1.1 */
366 dl 1.4 public void testToArray2() {
367 dl 1.3 CopyOnWriteArrayList full = populatedArray(3);
368 dl 1.1 Integer[] i = new Integer[3];
369     i = (Integer[])full.toArray(i);
370     assertEquals(3, i.length);
371     assertEquals(0, i[0].intValue());
372     assertEquals(1, i[1].intValue());
373     assertEquals(2, i[2].intValue());
374     }
375    
376    
377 dl 1.4 /**
378     *
379     */
380 dl 1.1 public void testSubList() {
381 dl 1.3 CopyOnWriteArrayList a = populatedArray(10);
382 dl 1.1 assertTrue(a.subList(1,1).isEmpty());
383     for(int j = 0; j < 9; ++j) {
384     for(int i = j ; i < 10; ++i) {
385     List b = a.subList(j,i);
386     for(int k = j; k < i; ++k) {
387     assertEquals(new Integer(k), b.get(k-j));
388     }
389     }
390     }
391    
392     List s = a.subList(2, 5);
393     assertEquals(s.size(), 3);
394 dl 1.3 s.set(2, m1);
395 dl 1.1 assertEquals(a.get(4), m1);
396     s.clear();
397     assertEquals(a.size(), 7);
398     }
399    
400     // Exception tests
401    
402     /**
403 dl 1.3 * toArray throws an ArrayStoreException when the given array
404 dl 1.1 * can not store the objects inside the list
405     */
406 dl 1.4 public void testToArray_ArrayStoreException() {
407     try {
408 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
409     c.add("zfasdfsdf");
410     c.add("asdadasd");
411     c.toArray(new Long[5]);
412 dl 1.4 shouldThrow();
413     } catch(ArrayStoreException e){}
414 dl 1.1 }
415    
416     /**
417 dl 1.3 * get throws an IndexOutOfBoundsException on a negative index
418 dl 1.1 */
419 dl 1.4 public void testGet1_IndexOutOfBoundsException() {
420     try {
421 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
422     c.get(-1);
423 dl 1.4 shouldThrow();
424     } catch(IndexOutOfBoundsException e){}
425 dl 1.1 }
426    
427     /**
428 dl 1.3 * get throws an IndexOutOfBoundsException on a too high index
429 dl 1.1 */
430 dl 1.4 public void testGet2_IndexOutOfBoundsException() {
431     try {
432 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
433     c.add("asdasd");
434     c.add("asdad");
435     c.get(100);
436 dl 1.4 shouldThrow();
437     } catch(IndexOutOfBoundsException e){}
438 dl 1.1 }
439    
440     /**
441 dl 1.3 * set throws an IndexOutOfBoundsException on a negative index
442 dl 1.1 */
443 dl 1.4 public void testSet1_IndexOutOfBoundsException() {
444     try {
445 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
446     c.set(-1,"qwerty");
447 dl 1.4 shouldThrow();
448     } catch(IndexOutOfBoundsException e){}
449 dl 1.1 }
450    
451     /**
452 dl 1.3 * set throws an IndexOutOfBoundsException on a too high index
453 dl 1.1 */
454 dl 1.4 public void testSet2() {
455     try {
456 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
457     c.add("asdasd");
458     c.add("asdad");
459     c.set(100, "qwerty");
460 dl 1.4 shouldThrow();
461     } catch(IndexOutOfBoundsException e){}
462 dl 1.1 }
463    
464     /**
465 dl 1.3 * add throws an IndexOutOfBoundsException on a negative index
466 dl 1.1 */
467 dl 1.4 public void testAdd1_IndexOutOfBoundsException() {
468     try {
469 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
470     c.add(-1,"qwerty");
471 dl 1.4 shouldThrow();
472     } catch(IndexOutOfBoundsException e){}
473 dl 1.1 }
474    
475     /**
476 dl 1.3 * add throws an IndexOutOfBoundsException on a too high index
477 dl 1.1 */
478 dl 1.4 public void testAdd2_IndexOutOfBoundsException() {
479     try {
480 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
481     c.add("asdasd");
482     c.add("asdasdasd");
483     c.add(100, "qwerty");
484 dl 1.4 shouldThrow();
485     } catch(IndexOutOfBoundsException e){}
486 dl 1.1 }
487    
488     /**
489 dl 1.3 * remove throws an IndexOutOfBoundsException on a negative index
490 dl 1.1 */
491 dl 1.4 public void testRemove1_IndexOutOfBounds() {
492     try {
493 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
494     c.remove(-1);
495 dl 1.4 shouldThrow();
496     } catch(IndexOutOfBoundsException e){}
497 dl 1.1 }
498    
499     /**
500 dl 1.3 * remove throws an IndexOutOfBoundsException on a too high index
501 dl 1.1 */
502 dl 1.4 public void testRemove2_IndexOutOfBounds() {
503     try {
504 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
505     c.add("asdasd");
506     c.add("adasdasd");
507     c.remove(100);
508 dl 1.4 shouldThrow();
509     } catch(IndexOutOfBoundsException e){}
510 dl 1.1 }
511    
512     /**
513 dl 1.3 * addAll throws an IndexOutOfBoundsException on a negative index
514 dl 1.1 */
515 dl 1.4 public void testAddAll1_IndexOutOfBoundsException() {
516     try {
517 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
518     c.addAll(-1,new LinkedList());
519 dl 1.4 shouldThrow();
520     } catch(IndexOutOfBoundsException e){}
521 dl 1.1 }
522    
523     /**
524 dl 1.3 * addAll throws an IndexOutOfBoundsException on a too high index
525 dl 1.1 */
526 dl 1.4 public void testAddAll2_IndexOutOfBoundsException() {
527     try {
528 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
529     c.add("asdasd");
530     c.add("asdasdasd");
531     c.addAll(100, new LinkedList());
532 dl 1.4 shouldThrow();
533     } catch(IndexOutOfBoundsException e){}
534 dl 1.1 }
535    
536     /**
537 dl 1.3 * listIterator throws an IndexOutOfBoundsException on a negative index
538 dl 1.1 */
539 dl 1.4 public void testListIterator1_IndexOutOfBoundsException() {
540     try {
541 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
542     c.listIterator(-1);
543 dl 1.4 shouldThrow();
544     } catch(IndexOutOfBoundsException e){}
545 dl 1.1 }
546    
547     /**
548 dl 1.3 * listIterator throws an IndexOutOfBoundsException on a too high index
549 dl 1.1 */
550 dl 1.4 public void testListIterator2_IndexOutOfBoundsException() {
551     try {
552 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
553     c.add("adasd");
554     c.add("asdasdas");
555     c.listIterator(100);
556 dl 1.4 shouldThrow();
557     } catch(IndexOutOfBoundsException e){}
558 dl 1.1 }
559    
560     /**
561 dl 1.3 * subList throws an IndexOutOfBoundsException on a negative index
562 dl 1.1 */
563 dl 1.4 public void testSubList1_IndexOutOfBoundsException() {
564     try {
565 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
566     c.subList(-1,100);
567    
568 dl 1.4 shouldThrow();
569     } catch(IndexOutOfBoundsException e){}
570 dl 1.1 }
571    
572     /**
573 dl 1.3 * subList throws an IndexOutOfBoundsException on a too high index
574 dl 1.1 */
575 dl 1.4 public void testSubList2_IndexOutOfBoundsException() {
576     try {
577 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
578     c.add("asdasd");
579     c.subList(1,100);
580 dl 1.4 shouldThrow();
581     } catch(IndexOutOfBoundsException e){}
582 dl 1.1 }
583    
584     /**
585 dl 1.3 * subList throws IndexOutOfBoundsException when the second index
586 dl 1.1 * is lower then the first
587     */
588 dl 1.4 public void testSubList3_IndexOutOfBoundsException() {
589     try {
590 dl 1.1 CopyOnWriteArrayList c = new CopyOnWriteArrayList();
591     c.subList(3,1);
592    
593 dl 1.4 shouldThrow();
594     } catch(IndexOutOfBoundsException e){}
595 dl 1.2 }
596    
597 dl 1.4 /**
598     *
599     */
600 dl 1.2 public void testSerialization() {
601 dl 1.3 CopyOnWriteArrayList q = populatedArray(SIZE);
602 dl 1.2
603     try {
604     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
605     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
606     out.writeObject(q);
607     out.close();
608    
609     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
610     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
611     CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
612     assertEquals(q.size(), r.size());
613     assertTrue(q.equals(r));
614     assertTrue(r.equals(q));
615     } catch(Exception e){
616 dl 1.4 unexpectedException();
617 dl 1.2 }
618 dl 1.1 }
619    
620     }