9 |
|
import java.util.ArrayList; |
10 |
|
import java.util.Arrays; |
11 |
|
import java.util.Collection; |
12 |
– |
import java.util.Collections; |
12 |
|
import java.util.Iterator; |
13 |
|
import java.util.NoSuchElementException; |
14 |
|
import java.util.Set; |
15 |
|
import java.util.concurrent.CopyOnWriteArraySet; |
16 |
|
|
17 |
|
import junit.framework.Test; |
19 |
– |
import junit.framework.TestSuite; |
18 |
|
|
19 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
20 |
|
public static void main(String[] args) { |
21 |
|
main(suite(), args); |
22 |
|
} |
23 |
|
public static Test suite() { |
24 |
< |
return new TestSuite(CopyOnWriteArraySetTest.class); |
24 |
> |
class Implementation implements CollectionImplementation { |
25 |
> |
public Class<?> klazz() { return CopyOnWriteArraySet.class; } |
26 |
> |
public Set emptyCollection() { return new CopyOnWriteArraySet(); } |
27 |
> |
public Object makeElement(int i) { return i; } |
28 |
> |
public boolean isConcurrent() { return true; } |
29 |
> |
public boolean permitsNulls() { return true; } |
30 |
> |
} |
31 |
> |
return newTestSuite( |
32 |
> |
CopyOnWriteArraySetTest.class, |
33 |
> |
CollectionTest.testSuite(new Implementation())); |
34 |
|
} |
35 |
|
|
36 |
|
static CopyOnWriteArraySet<Integer> populatedSet(int n) { |
37 |
< |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
37 |
> |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>(); |
38 |
|
assertTrue(a.isEmpty()); |
39 |
|
for (int i = 0; i < n; i++) |
40 |
|
a.add(i); |
44 |
|
} |
45 |
|
|
46 |
|
static CopyOnWriteArraySet populatedSet(Integer[] elements) { |
47 |
< |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
47 |
> |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>(); |
48 |
|
assertTrue(a.isEmpty()); |
49 |
|
for (int i = 0; i < elements.length; i++) |
50 |
|
a.add(elements[i]); |
66 |
|
*/ |
67 |
|
public void testConstructor3() { |
68 |
|
Integer[] ints = new Integer[SIZE]; |
69 |
< |
for (int i = 0; i < SIZE-1; ++i) |
69 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
70 |
|
ints[i] = new Integer(i); |
71 |
|
CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints)); |
72 |
|
for (int i = 0; i < SIZE; ++i) |
141 |
|
CopyOnWriteArraySet b = populatedSet(3); |
142 |
|
assertTrue(a.equals(b)); |
143 |
|
assertTrue(b.equals(a)); |
144 |
+ |
assertTrue(a.containsAll(b)); |
145 |
+ |
assertTrue(b.containsAll(a)); |
146 |
|
assertEquals(a.hashCode(), b.hashCode()); |
147 |
+ |
assertEquals(a.size(), b.size()); |
148 |
+ |
|
149 |
|
a.add(m1); |
150 |
|
assertFalse(a.equals(b)); |
151 |
|
assertFalse(b.equals(a)); |
152 |
+ |
assertTrue(a.containsAll(b)); |
153 |
+ |
assertFalse(b.containsAll(a)); |
154 |
|
b.add(m1); |
155 |
|
assertTrue(a.equals(b)); |
156 |
|
assertTrue(b.equals(a)); |
157 |
+ |
assertTrue(a.containsAll(b)); |
158 |
+ |
assertTrue(b.containsAll(a)); |
159 |
+ |
assertEquals(a.hashCode(), b.hashCode()); |
160 |
+ |
|
161 |
+ |
Object x = a.iterator().next(); |
162 |
+ |
a.remove(x); |
163 |
+ |
assertFalse(a.equals(b)); |
164 |
+ |
assertFalse(b.equals(a)); |
165 |
+ |
assertFalse(a.containsAll(b)); |
166 |
+ |
assertTrue(b.containsAll(a)); |
167 |
+ |
a.add(x); |
168 |
+ |
assertTrue(a.equals(b)); |
169 |
+ |
assertTrue(b.equals(a)); |
170 |
+ |
assertTrue(a.containsAll(b)); |
171 |
+ |
assertTrue(b.containsAll(a)); |
172 |
|
assertEquals(a.hashCode(), b.hashCode()); |
173 |
+ |
assertEquals(a.size(), b.size()); |
174 |
+ |
|
175 |
+ |
CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList()); |
176 |
+ |
CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList()); |
177 |
+ |
assertTrue(empty1.equals(empty1)); |
178 |
+ |
assertTrue(empty1.equals(empty2)); |
179 |
+ |
|
180 |
+ |
assertFalse(empty1.equals(a)); |
181 |
+ |
assertFalse(a.equals(empty1)); |
182 |
+ |
|
183 |
+ |
assertFalse(a.equals(null)); |
184 |
|
} |
185 |
|
|
186 |
|
/** |
188 |
|
*/ |
189 |
|
public void testContainsAll() { |
190 |
|
Collection full = populatedSet(3); |
191 |
+ |
assertTrue(full.containsAll(full)); |
192 |
|
assertTrue(full.containsAll(Arrays.asList())); |
193 |
|
assertTrue(full.containsAll(Arrays.asList(one))); |
194 |
|
assertTrue(full.containsAll(Arrays.asList(one, two))); |
195 |
|
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
196 |
|
assertFalse(full.containsAll(Arrays.asList(six))); |
197 |
+ |
|
198 |
+ |
CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList()); |
199 |
+ |
CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList()); |
200 |
+ |
assertTrue(empty1.containsAll(empty2)); |
201 |
+ |
assertTrue(empty1.containsAll(empty1)); |
202 |
+ |
assertFalse(empty1.containsAll(full)); |
203 |
+ |
assertTrue(full.containsAll(empty1)); |
204 |
+ |
|
205 |
+ |
try { |
206 |
+ |
full.containsAll(null); |
207 |
+ |
shouldThrow(); |
208 |
+ |
} catch (NullPointerException success) {} |
209 |
|
} |
210 |
|
|
211 |
|
/** |
231 |
|
Integer[] elements = new Integer[SIZE]; |
232 |
|
for (int i = 0; i < SIZE; i++) |
233 |
|
elements[i] = i; |
234 |
< |
Collections.shuffle(Arrays.asList(elements)); |
234 |
> |
shuffle(elements); |
235 |
|
Collection<Integer> full = populatedSet(elements); |
236 |
|
|
237 |
|
Iterator it = full.iterator(); |
318 |
|
Integer[] elements = new Integer[SIZE]; |
319 |
|
for (int i = 0; i < SIZE; i++) |
320 |
|
elements[i] = i; |
321 |
< |
Collections.shuffle(Arrays.asList(elements)); |
321 |
> |
shuffle(elements); |
322 |
|
Collection<Integer> full = populatedSet(elements); |
323 |
|
|
324 |
|
assertTrue(Arrays.equals(elements, full.toArray())); |
336 |
|
a = new Integer[0]; |
337 |
|
assertSame(a, empty.toArray(a)); |
338 |
|
|
339 |
< |
a = new Integer[SIZE/2]; |
339 |
> |
a = new Integer[SIZE / 2]; |
340 |
|
Arrays.fill(a, 42); |
341 |
|
assertSame(a, empty.toArray(a)); |
342 |
|
assertNull(a[0]); |
346 |
|
Integer[] elements = new Integer[SIZE]; |
347 |
|
for (int i = 0; i < SIZE; i++) |
348 |
|
elements[i] = i; |
349 |
< |
Collections.shuffle(Arrays.asList(elements)); |
349 |
> |
shuffle(elements); |
350 |
|
Collection<Integer> full = populatedSet(elements); |
351 |
|
|
352 |
|
Arrays.fill(a, 42); |
360 |
|
assertSame(a, full.toArray(a)); |
361 |
|
assertTrue(Arrays.equals(elements, a)); |
362 |
|
|
363 |
< |
a = new Integer[2*SIZE]; |
363 |
> |
a = new Integer[2 * SIZE]; |
364 |
|
Arrays.fill(a, 42); |
365 |
|
assertSame(a, full.toArray(a)); |
366 |
|
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
374 |
|
* not store the objects inside the set |
375 |
|
*/ |
376 |
|
public void testToArray_ArrayStoreException() { |
377 |
+ |
CopyOnWriteArraySet c = new CopyOnWriteArraySet(); |
378 |
+ |
c.add("zfasdfsdf"); |
379 |
+ |
c.add("asdadasd"); |
380 |
|
try { |
326 |
– |
CopyOnWriteArraySet c = new CopyOnWriteArraySet(); |
327 |
– |
c.add("zfasdfsdf"); |
328 |
– |
c.add("asdadasd"); |
381 |
|
c.toArray(new Long[5]); |
382 |
|
shouldThrow(); |
383 |
|
} catch (ArrayStoreException success) {} |
384 |
|
} |
385 |
|
|
386 |
|
/** |
387 |
< |
* A deserialized serialized set is equal |
387 |
> |
* A deserialized/reserialized set equals original |
388 |
|
*/ |
389 |
|
public void testSerialization() throws Exception { |
390 |
|
Set x = populatedSet(SIZE); |