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