ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.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 CopyOnWriteArraySetTest 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(CopyOnWriteArraySetTest.class);
21     }
22    
23     static CopyOnWriteArraySet fullSet(int n){
24     CopyOnWriteArraySet a = new CopyOnWriteArraySet();
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     CopyOnWriteArraySet full = fullSet(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 testAddAll2(){
51     CopyOnWriteArraySet full = fullSet(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.addAll(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 testAdd2(){
64     CopyOnWriteArraySet full = fullSet(3);
65     full.add(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 testAdd3(){
73     CopyOnWriteArraySet full = fullSet(3);
74     full.add(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     CopyOnWriteArraySet full = fullSet(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     CopyOnWriteArraySet full = fullSet(3);
92     assertTrue(full.contains(new Integer(1)));
93     assertFalse(full.contains(new Integer(5)));
94     }
95    
96     public void testEquals() {
97     CopyOnWriteArraySet a = fullSet(3);
98     CopyOnWriteArraySet b = fullSet(3);
99     assertTrue(a.equals(b));
100     assertTrue(b.equals(a));
101     assertEquals(a.hashCode(), b.hashCode());
102     a.add(new Integer(-1));
103     assertFalse(a.equals(b));
104     assertFalse(b.equals(a));
105     b.add(new Integer(-1));
106     assertTrue(a.equals(b));
107     assertTrue(b.equals(a));
108     assertEquals(a.hashCode(), b.hashCode());
109     }
110    
111    
112     /**
113     * Test to verify containsAll returns the correct values
114     */
115     public void testContainsAll(){
116     CopyOnWriteArraySet full = fullSet(3);
117     Vector v = new Vector();
118     v.add(new Integer(1));
119     v.add(new Integer(2));
120     assertTrue(full.containsAll(v));
121     v.add(new Integer(6));
122     assertFalse(full.containsAll(v));
123     }
124    
125     /**
126     * Test to verify isEmpty returns the correct values
127     */
128     public void testIsEmpty(){
129     CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
130     CopyOnWriteArraySet full = fullSet(3);
131     assertTrue(empty.isEmpty());
132     assertFalse(full.isEmpty());
133     }
134    
135     /**
136     * Test to verify iterator() returns an iterator containing the elements of the list
137     */
138     public void testIterator(){
139     CopyOnWriteArraySet full = fullSet(3);
140     Iterator i = full.iterator();
141     int j;
142     for(j = 0; i.hasNext(); j++)
143     assertEquals(j, ((Integer)i.next()).intValue());
144     assertEquals(3, j);
145     }
146    
147     public void testIteratorRemove () {
148     CopyOnWriteArraySet full = fullSet(3);
149     Iterator it = full.iterator();
150     it.next();
151     try {
152     it.remove();
153     fail("should throw");
154     }
155     catch (UnsupportedOperationException success) {}
156     }
157    
158     public void testToString(){
159     CopyOnWriteArraySet full = fullSet(3);
160     String s = full.toString();
161     for (int i = 0; i < 3; ++i) {
162     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
163     }
164     }
165    
166    
167     /**
168     * Test to verify removeAll correctly removes all elements from the given collection
169     */
170     public void testRemoveAll(){
171     CopyOnWriteArraySet full = fullSet(3);
172     Vector v = new Vector();
173     v.add(new Integer(1));
174     v.add(new Integer(2));
175     full.removeAll(v);
176     assertEquals(1, full.size());
177     }
178    
179    
180     public void testRemove(){
181     CopyOnWriteArraySet full = fullSet(3);
182     full.remove(new Integer(1));
183     assertFalse(full.contains(new Integer(1)));
184     assertEquals(2, full.size());
185     }
186    
187     /**
188     * Test to verify size returns the correct values
189     */
190     public void testSize(){
191     CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
192     CopyOnWriteArraySet full = fullSet(3);
193     assertEquals(3, full.size());
194     assertEquals(0, empty.size());
195     }
196    
197     /**
198     * Test to verify toArray returns an Object array containing all elements from the list
199     */
200     public void testToArray(){
201     CopyOnWriteArraySet full = fullSet(3);
202     Object[] o = full.toArray();
203     assertEquals(3, o.length);
204     assertEquals(0, ((Integer)o[0]).intValue());
205     assertEquals(1, ((Integer)o[1]).intValue());
206     assertEquals(2, ((Integer)o[2]).intValue());
207     }
208    
209     /**
210     * test to verify toArray returns an Integer array containing all elements from the list
211     */
212     public void testToArray2(){
213     CopyOnWriteArraySet full = fullSet(3);
214     Integer[] i = new Integer[3];
215     i = (Integer[])full.toArray(i);
216     assertEquals(3, i.length);
217     assertEquals(0, i[0].intValue());
218     assertEquals(1, i[1].intValue());
219     assertEquals(2, i[2].intValue());
220     }
221    
222    
223    
224     // Exception tests
225    
226     /**
227     * Test to verify toArray throws an ArrayStoreException when the given array
228     * can not store the objects inside the list
229     */
230     public void testToArray_ArrayStoreException(){
231     try{
232     CopyOnWriteArraySet c = new CopyOnWriteArraySet();
233     c.add("zfasdfsdf");
234     c.add("asdadasd");
235     c.toArray(new Long[5]);
236     fail("Object[] toArray(Object[]) should throw ArrayStoreException");
237     }catch(ArrayStoreException e){}
238 dl 1.2 }
239    
240     public void testSerialization() {
241     CopyOnWriteArraySet q = fullSet(10);
242    
243     try {
244     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
245     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
246     out.writeObject(q);
247     out.close();
248    
249     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
250     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
251     CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
252     assertEquals(q.size(), r.size());
253     assertTrue(q.equals(r));
254     assertTrue(r.equals(q));
255     } catch(Exception e){
256     e.printStackTrace();
257     fail("unexpected exception");
258     }
259 dl 1.1 }
260    
261     }