ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.3
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +70 -55 lines
Log Message:
New base class JSR166TestCase

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 CopyOnWriteArraySetTest 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(CopyOnWriteArraySetTest.class);
21     }
22    
23 dl 1.3 static CopyOnWriteArraySet populatedSet(int n){
24 dl 1.1 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 dl 1.3 public void testConstructor() {
34     CopyOnWriteArraySet a = new CopyOnWriteArraySet();
35     assertTrue(a.isEmpty());
36     }
37    
38     public void testConstructor3() {
39     Integer[] ints = new Integer[SIZE];
40     for (int i = 0; i < SIZE-1; ++i)
41     ints[i] = new Integer(i);
42     CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
43     for (int i = 0; i < SIZE; ++i)
44     assertTrue(a.contains(ints[i]));
45     }
46    
47    
48 dl 1.1 /**
49 dl 1.3 * addAll correctly adds each element from the given collection
50 dl 1.1 */
51     public void testAddAll(){
52 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
53 dl 1.1 Vector v = new Vector();
54 dl 1.3 v.add(three);
55     v.add(four);
56     v.add(five);
57 dl 1.1 full.addAll(v);
58     assertEquals(6, full.size());
59     }
60    
61     /**
62 dl 1.3 * addAllAbsent adds each element from the given collection that did not
63 dl 1.1 * already exist in the List
64     */
65     public void testAddAll2(){
66 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
67 dl 1.1 Vector v = new Vector();
68 dl 1.3 v.add(three);
69     v.add(four);
70     v.add(one); // will not add this element
71 dl 1.1 full.addAll(v);
72     assertEquals(5, full.size());
73     }
74    
75     /**
76 dl 1.3 * addIfAbsent will not add the element if it already exists in the list
77 dl 1.1 */
78     public void testAdd2(){
79 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
80     full.add(one);
81 dl 1.1 assertEquals(3, full.size());
82     }
83    
84     /**
85 dl 1.3 * addIfAbsent correctly adds the element when it does not exist in the list
86 dl 1.1 */
87     public void testAdd3(){
88 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
89     full.add(three);
90     assertTrue(full.contains(three));
91 dl 1.1 }
92    
93     /**
94 dl 1.3 * clear correctly removes all elements from the list
95 dl 1.1 */
96     public void testClear(){
97 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
98 dl 1.1 full.clear();
99     assertEquals(0, full.size());
100     }
101    
102     /**
103 dl 1.3 * contains returns the correct values
104 dl 1.1 */
105     public void testContains(){
106 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
107     assertTrue(full.contains(one));
108     assertFalse(full.contains(five));
109 dl 1.1 }
110    
111     public void testEquals() {
112 dl 1.3 CopyOnWriteArraySet a = populatedSet(3);
113     CopyOnWriteArraySet b = populatedSet(3);
114 dl 1.1 assertTrue(a.equals(b));
115     assertTrue(b.equals(a));
116     assertEquals(a.hashCode(), b.hashCode());
117 dl 1.3 a.add(m1);
118 dl 1.1 assertFalse(a.equals(b));
119     assertFalse(b.equals(a));
120 dl 1.3 b.add(m1);
121 dl 1.1 assertTrue(a.equals(b));
122     assertTrue(b.equals(a));
123     assertEquals(a.hashCode(), b.hashCode());
124     }
125    
126    
127     /**
128 dl 1.3 * containsAll returns the correct values
129 dl 1.1 */
130     public void testContainsAll(){
131 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
132 dl 1.1 Vector v = new Vector();
133 dl 1.3 v.add(one);
134     v.add(two);
135 dl 1.1 assertTrue(full.containsAll(v));
136 dl 1.3 v.add(six);
137 dl 1.1 assertFalse(full.containsAll(v));
138     }
139    
140     /**
141 dl 1.3 * isEmpty returns the correct values
142 dl 1.1 */
143     public void testIsEmpty(){
144     CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
145 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
146 dl 1.1 assertTrue(empty.isEmpty());
147     assertFalse(full.isEmpty());
148     }
149    
150     /**
151 dl 1.3 * iterator() returns an iterator containing the elements of the list
152 dl 1.1 */
153     public void testIterator(){
154 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
155 dl 1.1 Iterator i = full.iterator();
156     int j;
157     for(j = 0; i.hasNext(); j++)
158     assertEquals(j, ((Integer)i.next()).intValue());
159     assertEquals(3, j);
160     }
161    
162     public void testIteratorRemove () {
163 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
164 dl 1.1 Iterator it = full.iterator();
165     it.next();
166     try {
167     it.remove();
168     fail("should throw");
169     }
170     catch (UnsupportedOperationException success) {}
171     }
172    
173     public void testToString(){
174 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
175 dl 1.1 String s = full.toString();
176     for (int i = 0; i < 3; ++i) {
177     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
178     }
179     }
180    
181    
182     /**
183 dl 1.3 * removeAll correctly removes all elements from the given collection
184 dl 1.1 */
185     public void testRemoveAll(){
186 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
187 dl 1.1 Vector v = new Vector();
188 dl 1.3 v.add(one);
189     v.add(two);
190 dl 1.1 full.removeAll(v);
191     assertEquals(1, full.size());
192     }
193    
194    
195     public void testRemove(){
196 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
197     full.remove(one);
198     assertFalse(full.contains(one));
199 dl 1.1 assertEquals(2, full.size());
200     }
201    
202     /**
203 dl 1.3 * size returns the correct values
204 dl 1.1 */
205     public void testSize(){
206     CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
207 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
208 dl 1.1 assertEquals(3, full.size());
209     assertEquals(0, empty.size());
210     }
211    
212     /**
213 dl 1.3 * toArray returns an Object array containing all elements from the list
214 dl 1.1 */
215     public void testToArray(){
216 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
217 dl 1.1 Object[] o = full.toArray();
218     assertEquals(3, o.length);
219     assertEquals(0, ((Integer)o[0]).intValue());
220     assertEquals(1, ((Integer)o[1]).intValue());
221     assertEquals(2, ((Integer)o[2]).intValue());
222     }
223    
224     /**
225 dl 1.3 * toArray returns an Integer array containing all elements from the list
226 dl 1.1 */
227     public void testToArray2(){
228 dl 1.3 CopyOnWriteArraySet full = populatedSet(3);
229 dl 1.1 Integer[] i = new Integer[3];
230     i = (Integer[])full.toArray(i);
231     assertEquals(3, i.length);
232     assertEquals(0, i[0].intValue());
233     assertEquals(1, i[1].intValue());
234     assertEquals(2, i[2].intValue());
235     }
236    
237    
238    
239     // Exception tests
240    
241     /**
242 dl 1.3 * toArray throws an ArrayStoreException when the given array
243 dl 1.1 * can not store the objects inside the list
244     */
245     public void testToArray_ArrayStoreException(){
246     try{
247     CopyOnWriteArraySet c = new CopyOnWriteArraySet();
248     c.add("zfasdfsdf");
249     c.add("asdadasd");
250     c.toArray(new Long[5]);
251     fail("Object[] toArray(Object[]) should throw ArrayStoreException");
252     }catch(ArrayStoreException e){}
253 dl 1.2 }
254    
255     public void testSerialization() {
256 dl 1.3 CopyOnWriteArraySet q = populatedSet(SIZE);
257 dl 1.2
258     try {
259     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
260     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
261     out.writeObject(q);
262     out.close();
263    
264     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
265     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
266     CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
267     assertEquals(q.size(), r.size());
268     assertTrue(q.equals(r));
269     assertTrue(r.equals(q));
270     } catch(Exception e){
271     e.printStackTrace();
272     fail("unexpected exception");
273     }
274 dl 1.1 }
275    
276     }