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

# Content
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 import java.io.*;
12
13 public class CopyOnWriteArraySetTest extends JSR166TestCase {
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 populatedSet(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 * Default-constructed set is empty
35 */
36 public void testConstructor() {
37 CopyOnWriteArraySet a = new CopyOnWriteArraySet();
38 assertTrue(a.isEmpty());
39 }
40
41 /**
42 * Collection-constructed list holds all of its elements
43 */
44 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 /**
55 * addAll correctly adds each element from the given collection
56 */
57 public void testAddAll() {
58 CopyOnWriteArraySet full = populatedSet(3);
59 Vector v = new Vector();
60 v.add(three);
61 v.add(four);
62 v.add(five);
63 full.addAll(v);
64 assertEquals(6, full.size());
65 }
66
67 /**
68 * addAllAbsent adds each element from the given collection that did not
69 * already exist in the List
70 */
71 public void testAddAll2() {
72 CopyOnWriteArraySet full = populatedSet(3);
73 Vector v = new Vector();
74 v.add(three);
75 v.add(four);
76 v.add(one); // will not add this element
77 full.addAll(v);
78 assertEquals(5, full.size());
79 }
80
81 /**
82 * addIfAbsent will not add the element if it already exists in the list
83 */
84 public void testAdd2() {
85 CopyOnWriteArraySet full = populatedSet(3);
86 full.add(one);
87 assertEquals(3, full.size());
88 }
89
90 /**
91 * addIfAbsent correctly adds the element when it does not exist
92 * in the list
93 */
94 public void testAdd3() {
95 CopyOnWriteArraySet full = populatedSet(3);
96 full.add(three);
97 assertTrue(full.contains(three));
98 }
99
100 /**
101 * clear correctly removes all elements from the list
102 */
103 public void testClear() {
104 CopyOnWriteArraySet full = populatedSet(3);
105 full.clear();
106 assertEquals(0, full.size());
107 }
108
109 /**
110 * contains returns the correct values
111 */
112 public void testContains() {
113 CopyOnWriteArraySet full = populatedSet(3);
114 assertTrue(full.contains(one));
115 assertFalse(full.contains(five));
116 }
117
118 /**
119 * Sets with equal elements are equal
120 */
121 public void testEquals() {
122 CopyOnWriteArraySet a = populatedSet(3);
123 CopyOnWriteArraySet b = populatedSet(3);
124 assertTrue(a.equals(b));
125 assertTrue(b.equals(a));
126 assertEquals(a.hashCode(), b.hashCode());
127 a.add(m1);
128 assertFalse(a.equals(b));
129 assertFalse(b.equals(a));
130 b.add(m1);
131 assertTrue(a.equals(b));
132 assertTrue(b.equals(a));
133 assertEquals(a.hashCode(), b.hashCode());
134 }
135
136
137 /**
138 * containsAll returns the correct values
139 */
140 public void testContainsAll() {
141 CopyOnWriteArraySet full = populatedSet(3);
142 Vector v = new Vector();
143 v.add(one);
144 v.add(two);
145 assertTrue(full.containsAll(v));
146 v.add(six);
147 assertFalse(full.containsAll(v));
148 }
149
150 /**
151 * isEmpty returns the correct values
152 */
153 public void testIsEmpty() {
154 CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
155 CopyOnWriteArraySet full = populatedSet(3);
156 assertTrue(empty.isEmpty());
157 assertFalse(full.isEmpty());
158 }
159
160 /**
161 * iterator() returns an iterator containing the elements of the list
162 */
163 public void testIterator() {
164 CopyOnWriteArraySet full = populatedSet(3);
165 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 /**
173 * iterator remove is unsupported
174 */
175 public void testIteratorRemove () {
176 CopyOnWriteArraySet full = populatedSet(3);
177 Iterator it = full.iterator();
178 it.next();
179 try {
180 it.remove();
181 shouldThrow();
182 }
183 catch (UnsupportedOperationException success) {}
184 }
185
186 /**
187 * toString holds toString of elements
188 */
189 public void testToString() {
190 CopyOnWriteArraySet full = populatedSet(3);
191 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 * removeAll correctly removes all elements from the given collection
200 */
201 public void testRemoveAll() {
202 CopyOnWriteArraySet full = populatedSet(3);
203 Vector v = new Vector();
204 v.add(one);
205 v.add(two);
206 full.removeAll(v);
207 assertEquals(1, full.size());
208 }
209
210
211 /**
212 * remove removes an element
213 */
214 public void testRemove() {
215 CopyOnWriteArraySet full = populatedSet(3);
216 full.remove(one);
217 assertFalse(full.contains(one));
218 assertEquals(2, full.size());
219 }
220
221 /**
222 * size returns the correct values
223 */
224 public void testSize() {
225 CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
226 CopyOnWriteArraySet full = populatedSet(3);
227 assertEquals(3, full.size());
228 assertEquals(0, empty.size());
229 }
230
231 /**
232 * toArray returns an Object array containing all elements from the list
233 */
234 public void testToArray() {
235 CopyOnWriteArraySet full = populatedSet(3);
236 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 * toArray returns an Integer array containing all elements from
245 * the list
246 */
247 public void testToArray2() {
248 CopyOnWriteArraySet full = populatedSet(3);
249 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 * toArray throws an ArrayStoreException when the given array can
260 * not store the objects inside the list
261 */
262 public void testToArray_ArrayStoreException() {
263 try {
264 CopyOnWriteArraySet c = new CopyOnWriteArraySet();
265 c.add("zfasdfsdf");
266 c.add("asdadasd");
267 c.toArray(new Long[5]);
268 shouldThrow();
269 } catch(ArrayStoreException e){}
270 }
271
272 /**
273 *
274 */
275 public void testSerialization() {
276 CopyOnWriteArraySet q = populatedSet(SIZE);
277
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 unexpectedException();
292 }
293 }
294
295 }