ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
Revision: 1.2
Committed: Sun Sep 7 20:39:11 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.1: +22 -0 lines
Log Message:
Added serialization and lock tests

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