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