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/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.*; |
11 |
< |
import java.util.concurrent.*; |
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 TestCase{ |
13 |
< |
|
16 |
> |
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
17 |
– |
|
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(CopyOnWriteArraySetTest.class); |
21 |
> |
return new TestSuite(CopyOnWriteArraySetTest.class); |
22 |
|
} |
23 |
|
|
24 |
< |
static CopyOnWriteArraySet fullSet(int n){ |
25 |
< |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
24 |
> |
static CopyOnWriteArraySet populatedSet(int n) { |
25 |
> |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
26 |
|
assertTrue(a.isEmpty()); |
27 |
< |
for (int i = 0; i < n; ++i) |
27 |
> |
for (int i = 0; i < n; ++i) |
28 |
|
a.add(new Integer(i)); |
29 |
|
assertFalse(a.isEmpty()); |
30 |
|
assertEquals(n, a.size()); |
32 |
|
} |
33 |
|
|
34 |
|
/** |
35 |
< |
* Test to verify addAll correctly adds each element from the given collection |
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 = fullSet(3); |
59 |
< |
Vector v = new Vector(); |
60 |
< |
v.add(new Integer(3)); |
61 |
< |
v.add(new Integer(4)); |
62 |
< |
v.add(new Integer(5)); |
63 |
< |
full.addAll(v); |
64 |
< |
assertEquals(6, full.size()); |
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 |
< |
* Test to verify addAllAbsent adds each element from the given collection that did not |
69 |
< |
* already exist in the List |
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 = fullSet(3); |
73 |
< |
Vector v = new Vector(); |
74 |
< |
v.add(new Integer(3)); |
75 |
< |
v.add(new Integer(4)); |
76 |
< |
v.add(new Integer(1)); // will not add this element |
77 |
< |
full.addAll(v); |
78 |
< |
assertEquals(5, full.size()); |
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 |
< |
* Test to verify addIfAbsent will not add the element if it already exists in the list |
82 |
> |
* add will not add the element if it already exists in the set |
83 |
|
*/ |
84 |
< |
public void testAdd2(){ |
85 |
< |
CopyOnWriteArraySet full = fullSet(3); |
86 |
< |
full.add(new Integer(1)); |
87 |
< |
assertEquals(3, full.size()); |
84 |
> |
public void testAdd2() { |
85 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
86 |
> |
full.add(one); |
87 |
> |
assertEquals(3, full.size()); |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* test to verify addIfAbsent correctly adds the element when it does not exist in the list |
91 |
> |
* add adds the element when it does not exist in the set |
92 |
|
*/ |
93 |
< |
public void testAdd3(){ |
94 |
< |
CopyOnWriteArraySet full = fullSet(3); |
95 |
< |
full.add(new Integer(3)); |
96 |
< |
assertTrue(full.contains(new Integer(3))); |
93 |
> |
public void testAdd3() { |
94 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
95 |
> |
full.add(three); |
96 |
> |
assertTrue(full.contains(three)); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
< |
* Test to verify clear correctly removes all elements from the list |
100 |
> |
* clear removes all elements from the set |
101 |
|
*/ |
102 |
< |
public void testClear(){ |
103 |
< |
CopyOnWriteArraySet full = fullSet(3); |
104 |
< |
full.clear(); |
105 |
< |
assertEquals(0, full.size()); |
102 |
> |
public void testClear() { |
103 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
104 |
> |
full.clear(); |
105 |
> |
assertEquals(0, full.size()); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
< |
* Test to verify contains returns the correct values |
109 |
> |
* contains returns true for added elements |
110 |
|
*/ |
111 |
< |
public void testContains(){ |
112 |
< |
CopyOnWriteArraySet full = fullSet(3); |
113 |
< |
assertTrue(full.contains(new Integer(1))); |
114 |
< |
assertFalse(full.contains(new Integer(5))); |
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 = fullSet(3); |
122 |
< |
CopyOnWriteArraySet b = fullSet(3); |
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(new Integer(-1)); |
126 |
> |
a.add(m1); |
127 |
|
assertFalse(a.equals(b)); |
128 |
|
assertFalse(b.equals(a)); |
129 |
< |
b.add(new Integer(-1)); |
129 |
> |
b.add(m1); |
130 |
|
assertTrue(a.equals(b)); |
131 |
|
assertTrue(b.equals(a)); |
132 |
|
assertEquals(a.hashCode(), b.hashCode()); |
133 |
|
} |
134 |
|
|
110 |
– |
|
135 |
|
/** |
136 |
< |
* Test to verify containsAll returns the correct values |
136 |
> |
* containsAll returns true for collections with subset of elements |
137 |
|
*/ |
138 |
< |
public void testContainsAll(){ |
139 |
< |
CopyOnWriteArraySet full = fullSet(3); |
140 |
< |
Vector v = new Vector(); |
141 |
< |
v.add(new Integer(1)); |
142 |
< |
v.add(new Integer(2)); |
143 |
< |
assertTrue(full.containsAll(v)); |
144 |
< |
v.add(new Integer(6)); |
145 |
< |
assertFalse(full.containsAll(v)); |
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 |
< |
* Test to verify isEmpty returns the correct values |
149 |
> |
* isEmpty is true when empty, else false |
150 |
|
*/ |
151 |
< |
public void testIsEmpty(){ |
152 |
< |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
153 |
< |
CopyOnWriteArraySet full = fullSet(3); |
154 |
< |
assertTrue(empty.isEmpty()); |
155 |
< |
assertFalse(full.isEmpty()); |
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 |
< |
* Test to verify iterator() returns an iterator containing the elements of the list |
159 |
> |
* iterator() returns an iterator containing the elements of the set |
160 |
|
*/ |
161 |
< |
public void testIterator(){ |
162 |
< |
CopyOnWriteArraySet full = fullSet(3); |
163 |
< |
Iterator i = full.iterator(); |
164 |
< |
int j; |
165 |
< |
for(j = 0; i.hasNext(); j++) |
166 |
< |
assertEquals(j, ((Integer)i.next()).intValue()); |
167 |
< |
assertEquals(3, j); |
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 |
< |
public void testIteratorRemove () { |
171 |
< |
CopyOnWriteArraySet full = fullSet(3); |
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 |
< |
fail("should throw"); |
180 |
< |
} |
154 |
< |
catch (UnsupportedOperationException success) {} |
179 |
> |
shouldThrow(); |
180 |
> |
} catch (UnsupportedOperationException success) {} |
181 |
|
} |
182 |
|
|
183 |
< |
public void testToString(){ |
184 |
< |
CopyOnWriteArraySet full = fullSet(3); |
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.indexOf(String.valueOf(i)) >= 0); |
190 |
> |
assertTrue(s.contains(String.valueOf(i))); |
191 |
|
} |
192 |
< |
} |
164 |
< |
|
192 |
> |
} |
193 |
|
|
194 |
|
/** |
195 |
< |
* Test to verify removeAll correctly removes all elements from the given collection |
195 |
> |
* removeAll removes all elements from the given collection |
196 |
|
*/ |
197 |
< |
public void testRemoveAll(){ |
198 |
< |
CopyOnWriteArraySet full = fullSet(3); |
199 |
< |
Vector v = new Vector(); |
200 |
< |
v.add(new Integer(1)); |
201 |
< |
v.add(new Integer(2)); |
202 |
< |
full.removeAll(v); |
203 |
< |
assertEquals(1, full.size()); |
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 |
< |
public void testRemove(){ |
208 |
< |
CopyOnWriteArraySet full = fullSet(3); |
209 |
< |
full.remove(new Integer(1)); |
210 |
< |
assertFalse(full.contains(new Integer(1))); |
211 |
< |
assertEquals(2, full.size()); |
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 |
< |
* Test to verify size returns the correct values |
217 |
> |
* size returns the number of elements |
218 |
|
*/ |
219 |
< |
public void testSize(){ |
220 |
< |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
221 |
< |
CopyOnWriteArraySet full = fullSet(3); |
222 |
< |
assertEquals(3, full.size()); |
223 |
< |
assertEquals(0, empty.size()); |
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 |
< |
* Test to verify toArray returns an Object array containing all elements from the list |
227 |
> |
* toArray returns an Object array containing all elements from the set |
228 |
|
*/ |
229 |
< |
public void testToArray(){ |
230 |
< |
CopyOnWriteArraySet full = fullSet(3); |
231 |
< |
Object[] o = full.toArray(); |
232 |
< |
assertEquals(3, o.length); |
233 |
< |
assertEquals(0, ((Integer)o[0]).intValue()); |
234 |
< |
assertEquals(1, ((Integer)o[1]).intValue()); |
235 |
< |
assertEquals(2, ((Integer)o[2]).intValue()); |
229 |
> |
public void testToArray() { |
230 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
231 |
> |
Object[] o = full.toArray(); |
232 |
> |
assertEquals(3, o.length); |
233 |
> |
assertEquals(0, o[0]); |
234 |
> |
assertEquals(1, o[1]); |
235 |
> |
assertEquals(2, o[2]); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
< |
* test to verify toArray returns an Integer array containing all elements from the list |
239 |
> |
* toArray returns an Integer array containing all elements from |
240 |
> |
* the set |
241 |
|
*/ |
242 |
< |
public void testToArray2(){ |
243 |
< |
CopyOnWriteArraySet full = fullSet(3); |
244 |
< |
Integer[] i = new Integer[3]; |
245 |
< |
i = (Integer[])full.toArray(i); |
246 |
< |
assertEquals(3, i.length); |
247 |
< |
assertEquals(0, i[0].intValue()); |
248 |
< |
assertEquals(1, i[1].intValue()); |
249 |
< |
assertEquals(2, i[2].intValue()); |
242 |
> |
public void testToArray2() { |
243 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
244 |
> |
Integer[] i = new Integer[3]; |
245 |
> |
i = (Integer[])full.toArray(i); |
246 |
> |
assertEquals(3, i.length); |
247 |
> |
assertEquals(0, (int) i[0]); |
248 |
> |
assertEquals(1, (int) i[1]); |
249 |
> |
assertEquals(2, (int) i[2]); |
250 |
|
} |
251 |
|
|
221 |
– |
|
222 |
– |
|
223 |
– |
// Exception tests |
224 |
– |
|
252 |
|
/** |
253 |
< |
* Test to verify toArray throws an ArrayStoreException when the given array |
254 |
< |
* can not store the objects inside the list |
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{ |
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 |
< |
fail("Object[] toArray(Object[]) should throw ArrayStoreException"); |
263 |
< |
}catch(ArrayStoreException e){} |
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 |
|
} |