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. |
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/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
12 |
|
import java.io.*; |
13 |
|
|
14 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
14 |
– |
|
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
– |
|
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(CopyOnWriteArraySetTest.class); |
20 |
|
} |
22 |
|
static CopyOnWriteArraySet populatedSet(int n){ |
23 |
|
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
24 |
|
assertTrue(a.isEmpty()); |
25 |
< |
for (int i = 0; i < n; ++i) |
25 |
> |
for (int i = 0; i < n; ++i) |
26 |
|
a.add(new Integer(i)); |
27 |
|
assertFalse(a.isEmpty()); |
28 |
|
assertEquals(n, a.size()); |
38 |
|
} |
39 |
|
|
40 |
|
/** |
41 |
< |
* Collection-constructed list holds all of its elements |
41 |
> |
* Collection-constructed set holds all of its elements |
42 |
|
*/ |
43 |
|
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 |
|
CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints)); |
48 |
< |
for (int i = 0; i < SIZE; ++i) |
48 |
> |
for (int i = 0; i < SIZE; ++i) |
49 |
|
assertTrue(a.contains(ints[i])); |
50 |
|
} |
51 |
< |
|
51 |
> |
|
52 |
|
|
53 |
|
/** |
54 |
< |
* addAll correctly adds each element from the given collection |
54 |
> |
* addAll adds each element from the given collection |
55 |
|
*/ |
56 |
|
public void testAddAll() { |
57 |
|
CopyOnWriteArraySet full = populatedSet(3); |
64 |
|
} |
65 |
|
|
66 |
|
/** |
67 |
< |
* addAllAbsent adds each element from the given collection that did not |
68 |
< |
* already exist in the List |
67 |
> |
* addAll adds each element from the given collection that did not |
68 |
> |
* already exist in the set |
69 |
|
*/ |
70 |
|
public void testAddAll2() { |
71 |
|
CopyOnWriteArraySet full = populatedSet(3); |
78 |
|
} |
79 |
|
|
80 |
|
/** |
81 |
< |
* addIfAbsent will not add the element if it already exists in the list |
81 |
> |
* add will not add the element if it already exists in the set |
82 |
|
*/ |
83 |
|
public void testAdd2() { |
84 |
|
CopyOnWriteArraySet full = populatedSet(3); |
87 |
|
} |
88 |
|
|
89 |
|
/** |
90 |
< |
* addIfAbsent correctly adds the element when it does not exist |
91 |
< |
* in the list |
90 |
> |
* add adds the element when it does not exist |
91 |
> |
* in the set |
92 |
|
*/ |
93 |
|
public void testAdd3() { |
94 |
|
CopyOnWriteArraySet full = populatedSet(3); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
< |
* clear correctly removes all elements from the list |
100 |
> |
* clear removes all elements from the set |
101 |
|
*/ |
102 |
|
public void testClear() { |
103 |
|
CopyOnWriteArraySet full = populatedSet(3); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
< |
* contains returns the correct values |
109 |
> |
* contains returns true for added elements |
110 |
|
*/ |
111 |
|
public void testContains() { |
112 |
|
CopyOnWriteArraySet full = populatedSet(3); |
132 |
|
assertEquals(a.hashCode(), b.hashCode()); |
133 |
|
} |
134 |
|
|
135 |
< |
|
135 |
> |
|
136 |
|
/** |
137 |
< |
* containsAll returns the correct values |
137 |
> |
* containsAll returns true for collections with subset of elements |
138 |
|
*/ |
139 |
|
public void testContainsAll() { |
140 |
|
CopyOnWriteArraySet full = populatedSet(3); |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* isEmpty returns the correct values |
150 |
> |
* isEmpty is true when empty, else false |
151 |
|
*/ |
152 |
|
public void testIsEmpty() { |
153 |
|
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
160 |
< |
* iterator() returns an iterator containing the elements of the list |
160 |
> |
* iterator() returns an iterator containing the elements of the set |
161 |
|
*/ |
162 |
|
public void testIterator() { |
163 |
|
CopyOnWriteArraySet full = populatedSet(3); |
191 |
|
for (int i = 0; i < 3; ++i) { |
192 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
193 |
|
} |
194 |
< |
} |
194 |
> |
} |
195 |
|
|
196 |
|
|
197 |
|
/** |
198 |
< |
* removeAll correctly removes all elements from the given collection |
198 |
> |
* removeAll removes all elements from the given collection |
199 |
|
*/ |
200 |
|
public void testRemoveAll() { |
201 |
|
CopyOnWriteArraySet full = populatedSet(3); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* size returns the correct values |
221 |
> |
* size returns the number of elements |
222 |
|
*/ |
223 |
|
public void testSize() { |
224 |
|
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
< |
* toArray returns an Object array containing all elements from the list |
231 |
> |
* toArray returns an Object array containing all elements from the set |
232 |
|
*/ |
233 |
|
public void testToArray() { |
234 |
|
CopyOnWriteArraySet full = populatedSet(3); |
241 |
|
|
242 |
|
/** |
243 |
|
* toArray returns an Integer array containing all elements from |
244 |
< |
* the list |
244 |
> |
* the set |
245 |
|
*/ |
246 |
|
public void testToArray2() { |
247 |
|
CopyOnWriteArraySet full = populatedSet(3); |
256 |
|
|
257 |
|
/** |
258 |
|
* toArray throws an ArrayStoreException when the given array can |
259 |
< |
* not store the objects inside the list |
259 |
> |
* not store the objects inside the set |
260 |
|
*/ |
261 |
|
public void testToArray_ArrayStoreException() { |
262 |
|
try { |
269 |
|
} |
270 |
|
|
271 |
|
/** |
272 |
< |
* |
272 |
> |
* A deserialized serialized set is equal |
273 |
|
*/ |
274 |
|
public void testSerialization() { |
275 |
|
CopyOnWriteArraySet q = populatedSet(SIZE); |