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