1 |
/* |
2 |
* 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/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import junit.framework.*; |
10 |
import java.util.Arrays; |
11 |
import java.util.Iterator; |
12 |
import java.util.Set; |
13 |
import java.util.Vector; |
14 |
import java.util.concurrent.CopyOnWriteArraySet; |
15 |
|
16 |
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
17 |
public static void main(String[] args) { |
18 |
junit.textui.TestRunner.run(suite()); |
19 |
} |
20 |
public static Test suite() { |
21 |
return new TestSuite(CopyOnWriteArraySetTest.class); |
22 |
} |
23 |
|
24 |
static CopyOnWriteArraySet populatedSet(int n) { |
25 |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
26 |
assertTrue(a.isEmpty()); |
27 |
for (int i = 0; i < n; ++i) |
28 |
a.add(new Integer(i)); |
29 |
assertFalse(a.isEmpty()); |
30 |
assertEquals(n, a.size()); |
31 |
return a; |
32 |
} |
33 |
|
34 |
/** |
35 |
* Default-constructed set is empty |
36 |
*/ |
37 |
public void testConstructor() { |
38 |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
39 |
assertTrue(a.isEmpty()); |
40 |
} |
41 |
|
42 |
/** |
43 |
* Collection-constructed set holds all of its elements |
44 |
*/ |
45 |
public void testConstructor3() { |
46 |
Integer[] ints = new Integer[SIZE]; |
47 |
for (int i = 0; i < SIZE-1; ++i) |
48 |
ints[i] = new Integer(i); |
49 |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints)); |
50 |
for (int i = 0; i < SIZE; ++i) |
51 |
assertTrue(a.contains(ints[i])); |
52 |
} |
53 |
|
54 |
/** |
55 |
* addAll 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 |
* addAll adds each element from the given collection that did not |
69 |
* already exist in the set |
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 |
* add will not add the element if it already exists in the set |
83 |
*/ |
84 |
public void testAdd2() { |
85 |
CopyOnWriteArraySet full = populatedSet(3); |
86 |
full.add(one); |
87 |
assertEquals(3, full.size()); |
88 |
} |
89 |
|
90 |
/** |
91 |
* add adds the element when it does not exist in the set |
92 |
*/ |
93 |
public void testAdd3() { |
94 |
CopyOnWriteArraySet full = populatedSet(3); |
95 |
full.add(three); |
96 |
assertTrue(full.contains(three)); |
97 |
} |
98 |
|
99 |
/** |
100 |
* clear removes all elements from the set |
101 |
*/ |
102 |
public void testClear() { |
103 |
CopyOnWriteArraySet full = populatedSet(3); |
104 |
full.clear(); |
105 |
assertEquals(0, full.size()); |
106 |
} |
107 |
|
108 |
/** |
109 |
* contains returns true for added elements |
110 |
*/ |
111 |
public void testContains() { |
112 |
CopyOnWriteArraySet full = populatedSet(3); |
113 |
assertTrue(full.contains(one)); |
114 |
assertFalse(full.contains(five)); |
115 |
} |
116 |
|
117 |
/** |
118 |
* Sets with equal elements are equal |
119 |
*/ |
120 |
public void testEquals() { |
121 |
CopyOnWriteArraySet a = populatedSet(3); |
122 |
CopyOnWriteArraySet b = populatedSet(3); |
123 |
assertTrue(a.equals(b)); |
124 |
assertTrue(b.equals(a)); |
125 |
assertEquals(a.hashCode(), b.hashCode()); |
126 |
a.add(m1); |
127 |
assertFalse(a.equals(b)); |
128 |
assertFalse(b.equals(a)); |
129 |
b.add(m1); |
130 |
assertTrue(a.equals(b)); |
131 |
assertTrue(b.equals(a)); |
132 |
assertEquals(a.hashCode(), b.hashCode()); |
133 |
} |
134 |
|
135 |
/** |
136 |
* containsAll returns true for collections with subset of elements |
137 |
*/ |
138 |
public void testContainsAll() { |
139 |
CopyOnWriteArraySet full = populatedSet(3); |
140 |
Vector v = new Vector(); |
141 |
v.add(one); |
142 |
v.add(two); |
143 |
assertTrue(full.containsAll(v)); |
144 |
v.add(six); |
145 |
assertFalse(full.containsAll(v)); |
146 |
} |
147 |
|
148 |
/** |
149 |
* isEmpty is true when empty, else false |
150 |
*/ |
151 |
public void testIsEmpty() { |
152 |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
153 |
CopyOnWriteArraySet full = populatedSet(3); |
154 |
assertTrue(empty.isEmpty()); |
155 |
assertFalse(full.isEmpty()); |
156 |
} |
157 |
|
158 |
/** |
159 |
* iterator() returns an iterator containing the elements of the set |
160 |
*/ |
161 |
public void testIterator() { |
162 |
CopyOnWriteArraySet full = populatedSet(3); |
163 |
Iterator i = full.iterator(); |
164 |
int j; |
165 |
for (j = 0; i.hasNext(); j++) |
166 |
assertEquals(j, i.next()); |
167 |
assertEquals(3, j); |
168 |
} |
169 |
|
170 |
/** |
171 |
* iterator remove is unsupported |
172 |
*/ |
173 |
public void testIteratorRemove() { |
174 |
CopyOnWriteArraySet full = populatedSet(3); |
175 |
Iterator it = full.iterator(); |
176 |
it.next(); |
177 |
try { |
178 |
it.remove(); |
179 |
shouldThrow(); |
180 |
} catch (UnsupportedOperationException success) {} |
181 |
} |
182 |
|
183 |
/** |
184 |
* toString holds toString of elements |
185 |
*/ |
186 |
public void testToString() { |
187 |
CopyOnWriteArraySet full = populatedSet(3); |
188 |
String s = full.toString(); |
189 |
for (int i = 0; i < 3; ++i) { |
190 |
assertTrue(s.contains(String.valueOf(i))); |
191 |
} |
192 |
} |
193 |
|
194 |
/** |
195 |
* removeAll removes all elements from the given collection |
196 |
*/ |
197 |
public void testRemoveAll() { |
198 |
CopyOnWriteArraySet full = populatedSet(3); |
199 |
Vector v = new Vector(); |
200 |
v.add(one); |
201 |
v.add(two); |
202 |
full.removeAll(v); |
203 |
assertEquals(1, full.size()); |
204 |
} |
205 |
|
206 |
/** |
207 |
* remove removes an element |
208 |
*/ |
209 |
public void testRemove() { |
210 |
CopyOnWriteArraySet full = populatedSet(3); |
211 |
full.remove(one); |
212 |
assertFalse(full.contains(one)); |
213 |
assertEquals(2, full.size()); |
214 |
} |
215 |
|
216 |
/** |
217 |
* size returns the number of elements |
218 |
*/ |
219 |
public void testSize() { |
220 |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
221 |
CopyOnWriteArraySet full = populatedSet(3); |
222 |
assertEquals(3, full.size()); |
223 |
assertEquals(0, empty.size()); |
224 |
} |
225 |
|
226 |
/** |
227 |
* toArray returns an Object array containing all elements from the set |
228 |
*/ |
229 |
public void testToArray() { |
230 |
CopyOnWriteArraySet<Integer> full = populatedSet(SIZE); |
231 |
Object[] array = full.toArray(); |
232 |
Iterator<Integer> it = full.iterator(); |
233 |
for (int i = 0; i < SIZE; i++) |
234 |
assertSame(array[i], it.next()); |
235 |
assertFalse(it.hasNext()); |
236 |
} |
237 |
|
238 |
/** |
239 |
* toArray returns an Integer array containing all elements from |
240 |
* the set |
241 |
*/ |
242 |
public void testToArray2() { |
243 |
CopyOnWriteArraySet<Integer> full = populatedSet(SIZE); |
244 |
Integer[] ints = new Integer[SIZE]; |
245 |
assertSame(ints, full.toArray(ints)); |
246 |
Iterator<Integer> it = full.iterator(); |
247 |
for (int i = 0; i < SIZE; i++) |
248 |
assertSame(ints[i], it.next()); |
249 |
assertFalse(it.hasNext()); |
250 |
} |
251 |
|
252 |
/** |
253 |
* toArray throws an ArrayStoreException when the given array can |
254 |
* not store the objects inside the set |
255 |
*/ |
256 |
public void testToArray_ArrayStoreException() { |
257 |
try { |
258 |
CopyOnWriteArraySet c = new CopyOnWriteArraySet(); |
259 |
c.add("zfasdfsdf"); |
260 |
c.add("asdadasd"); |
261 |
c.toArray(new Long[5]); |
262 |
shouldThrow(); |
263 |
} catch (ArrayStoreException success) {} |
264 |
} |
265 |
|
266 |
/** |
267 |
* A deserialized serialized set is equal |
268 |
*/ |
269 |
public void testSerialization() throws Exception { |
270 |
Set x = populatedSet(SIZE); |
271 |
Set y = serialClone(x); |
272 |
|
273 |
assertTrue(x != y); |
274 |
assertEquals(x.size(), y.size()); |
275 |
assertEquals(x, y); |
276 |
assertEquals(y, x); |
277 |
} |
278 |
|
279 |
} |