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