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.*; |
12 |
+ |
import java.io.*; |
13 |
|
|
14 |
< |
public class CopyOnWriteArraySetTest extends TestCase{ |
13 |
< |
|
14 |
> |
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
17 |
– |
|
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(CopyOnWriteArraySetTest.class); |
19 |
> |
return new TestSuite(CopyOnWriteArraySetTest.class); |
20 |
|
} |
21 |
|
|
22 |
< |
static CopyOnWriteArraySet fullSet(int n){ |
23 |
< |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
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()); |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* Test to verify addAll correctly adds each element from the given collection |
33 |
> |
* Default-constructed set is empty |
34 |
> |
*/ |
35 |
> |
public void testConstructor() { |
36 |
> |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
37 |
> |
assertTrue(a.isEmpty()); |
38 |
> |
} |
39 |
> |
|
40 |
> |
/** |
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) |
49 |
> |
assertTrue(a.contains(ints[i])); |
50 |
> |
} |
51 |
> |
|
52 |
> |
/** |
53 |
> |
* addAll adds each element from the given collection |
54 |
|
*/ |
55 |
< |
public void testAddAll(){ |
56 |
< |
CopyOnWriteArraySet full = fullSet(3); |
57 |
< |
Vector v = new Vector(); |
58 |
< |
v.add(new Integer(3)); |
59 |
< |
v.add(new Integer(4)); |
60 |
< |
v.add(new Integer(5)); |
61 |
< |
full.addAll(v); |
62 |
< |
assertEquals(6, full.size()); |
55 |
> |
public void testAddAll() { |
56 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
57 |
> |
Vector v = new Vector(); |
58 |
> |
v.add(three); |
59 |
> |
v.add(four); |
60 |
> |
v.add(five); |
61 |
> |
full.addAll(v); |
62 |
> |
assertEquals(6, full.size()); |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* Test to verify addAllAbsent adds each element from the given collection that did not |
67 |
< |
* already exist in the List |
66 |
> |
* addAll adds each element from the given collection that did not |
67 |
> |
* already exist in the set |
68 |
|
*/ |
69 |
< |
public void testAddAll2(){ |
70 |
< |
CopyOnWriteArraySet full = fullSet(3); |
71 |
< |
Vector v = new Vector(); |
72 |
< |
v.add(new Integer(3)); |
73 |
< |
v.add(new Integer(4)); |
74 |
< |
v.add(new Integer(1)); // will not add this element |
75 |
< |
full.addAll(v); |
76 |
< |
assertEquals(5, full.size()); |
69 |
> |
public void testAddAll2() { |
70 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
71 |
> |
Vector v = new Vector(); |
72 |
> |
v.add(three); |
73 |
> |
v.add(four); |
74 |
> |
v.add(one); // will not add this element |
75 |
> |
full.addAll(v); |
76 |
> |
assertEquals(5, full.size()); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
< |
* Test to verify addIfAbsent will not add the element if it already exists in the list |
80 |
> |
* add will not add the element if it already exists in the set |
81 |
|
*/ |
82 |
< |
public void testAdd2(){ |
83 |
< |
CopyOnWriteArraySet full = fullSet(3); |
84 |
< |
full.add(new Integer(1)); |
85 |
< |
assertEquals(3, full.size()); |
82 |
> |
public void testAdd2() { |
83 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
84 |
> |
full.add(one); |
85 |
> |
assertEquals(3, full.size()); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* test to verify addIfAbsent correctly adds the element when it does not exist in the list |
89 |
> |
* add adds the element when it does not exist in the set |
90 |
|
*/ |
91 |
< |
public void testAdd3(){ |
92 |
< |
CopyOnWriteArraySet full = fullSet(3); |
93 |
< |
full.add(new Integer(3)); |
94 |
< |
assertTrue(full.contains(new Integer(3))); |
91 |
> |
public void testAdd3() { |
92 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
93 |
> |
full.add(three); |
94 |
> |
assertTrue(full.contains(three)); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* Test to verify clear correctly removes all elements from the list |
98 |
> |
* clear removes all elements from the set |
99 |
|
*/ |
100 |
< |
public void testClear(){ |
101 |
< |
CopyOnWriteArraySet full = fullSet(3); |
102 |
< |
full.clear(); |
103 |
< |
assertEquals(0, full.size()); |
100 |
> |
public void testClear() { |
101 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
102 |
> |
full.clear(); |
103 |
> |
assertEquals(0, full.size()); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
< |
* Test to verify contains returns the correct values |
107 |
> |
* contains returns true for added elements |
108 |
|
*/ |
109 |
< |
public void testContains(){ |
110 |
< |
CopyOnWriteArraySet full = fullSet(3); |
111 |
< |
assertTrue(full.contains(new Integer(1))); |
112 |
< |
assertFalse(full.contains(new Integer(5))); |
109 |
> |
public void testContains() { |
110 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
111 |
> |
assertTrue(full.contains(one)); |
112 |
> |
assertFalse(full.contains(five)); |
113 |
|
} |
114 |
|
|
115 |
+ |
/** |
116 |
+ |
* Sets with equal elements are equal |
117 |
+ |
*/ |
118 |
|
public void testEquals() { |
119 |
< |
CopyOnWriteArraySet a = fullSet(3); |
120 |
< |
CopyOnWriteArraySet b = fullSet(3); |
119 |
> |
CopyOnWriteArraySet a = populatedSet(3); |
120 |
> |
CopyOnWriteArraySet b = populatedSet(3); |
121 |
|
assertTrue(a.equals(b)); |
122 |
|
assertTrue(b.equals(a)); |
123 |
|
assertEquals(a.hashCode(), b.hashCode()); |
124 |
< |
a.add(new Integer(-1)); |
124 |
> |
a.add(m1); |
125 |
|
assertFalse(a.equals(b)); |
126 |
|
assertFalse(b.equals(a)); |
127 |
< |
b.add(new Integer(-1)); |
127 |
> |
b.add(m1); |
128 |
|
assertTrue(a.equals(b)); |
129 |
|
assertTrue(b.equals(a)); |
130 |
|
assertEquals(a.hashCode(), b.hashCode()); |
131 |
|
} |
132 |
|
|
110 |
– |
|
133 |
|
/** |
134 |
< |
* Test to verify containsAll returns the correct values |
134 |
> |
* containsAll returns true for collections with subset of elements |
135 |
|
*/ |
136 |
< |
public void testContainsAll(){ |
137 |
< |
CopyOnWriteArraySet full = fullSet(3); |
138 |
< |
Vector v = new Vector(); |
139 |
< |
v.add(new Integer(1)); |
140 |
< |
v.add(new Integer(2)); |
141 |
< |
assertTrue(full.containsAll(v)); |
142 |
< |
v.add(new Integer(6)); |
143 |
< |
assertFalse(full.containsAll(v)); |
136 |
> |
public void testContainsAll() { |
137 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
138 |
> |
Vector v = new Vector(); |
139 |
> |
v.add(one); |
140 |
> |
v.add(two); |
141 |
> |
assertTrue(full.containsAll(v)); |
142 |
> |
v.add(six); |
143 |
> |
assertFalse(full.containsAll(v)); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
< |
* Test to verify isEmpty returns the correct values |
147 |
> |
* isEmpty is true when empty, else false |
148 |
|
*/ |
149 |
< |
public void testIsEmpty(){ |
150 |
< |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
151 |
< |
CopyOnWriteArraySet full = fullSet(3); |
152 |
< |
assertTrue(empty.isEmpty()); |
153 |
< |
assertFalse(full.isEmpty()); |
149 |
> |
public void testIsEmpty() { |
150 |
> |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
151 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
152 |
> |
assertTrue(empty.isEmpty()); |
153 |
> |
assertFalse(full.isEmpty()); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
< |
* Test to verify iterator() returns an iterator containing the elements of the list |
157 |
> |
* iterator() returns an iterator containing the elements of the set |
158 |
|
*/ |
159 |
< |
public void testIterator(){ |
160 |
< |
CopyOnWriteArraySet full = fullSet(3); |
161 |
< |
Iterator i = full.iterator(); |
162 |
< |
int j; |
163 |
< |
for(j = 0; i.hasNext(); j++) |
164 |
< |
assertEquals(j, ((Integer)i.next()).intValue()); |
165 |
< |
assertEquals(3, j); |
159 |
> |
public void testIterator() { |
160 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
161 |
> |
Iterator i = full.iterator(); |
162 |
> |
int j; |
163 |
> |
for (j = 0; i.hasNext(); j++) |
164 |
> |
assertEquals(j, i.next()); |
165 |
> |
assertEquals(3, j); |
166 |
|
} |
167 |
|
|
168 |
< |
public void testIteratorRemove () { |
169 |
< |
CopyOnWriteArraySet full = fullSet(3); |
168 |
> |
/** |
169 |
> |
* iterator remove is unsupported |
170 |
> |
*/ |
171 |
> |
public void testIteratorRemove() { |
172 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
173 |
|
Iterator it = full.iterator(); |
174 |
|
it.next(); |
175 |
|
try { |
176 |
|
it.remove(); |
177 |
< |
fail("should throw"); |
178 |
< |
} |
154 |
< |
catch (UnsupportedOperationException success) {} |
177 |
> |
shouldThrow(); |
178 |
> |
} catch (UnsupportedOperationException success) {} |
179 |
|
} |
180 |
|
|
181 |
< |
public void testToString(){ |
182 |
< |
CopyOnWriteArraySet full = fullSet(3); |
181 |
> |
/** |
182 |
> |
* toString holds toString of elements |
183 |
> |
*/ |
184 |
> |
public void testToString() { |
185 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
186 |
|
String s = full.toString(); |
187 |
|
for (int i = 0; i < 3; ++i) { |
188 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
188 |
> |
assertTrue(s.contains(String.valueOf(i))); |
189 |
|
} |
190 |
< |
} |
164 |
< |
|
190 |
> |
} |
191 |
|
|
192 |
|
/** |
193 |
< |
* Test to verify removeAll correctly removes all elements from the given collection |
193 |
> |
* removeAll removes all elements from the given collection |
194 |
|
*/ |
195 |
< |
public void testRemoveAll(){ |
196 |
< |
CopyOnWriteArraySet full = fullSet(3); |
197 |
< |
Vector v = new Vector(); |
198 |
< |
v.add(new Integer(1)); |
199 |
< |
v.add(new Integer(2)); |
200 |
< |
full.removeAll(v); |
201 |
< |
assertEquals(1, full.size()); |
195 |
> |
public void testRemoveAll() { |
196 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
197 |
> |
Vector v = new Vector(); |
198 |
> |
v.add(one); |
199 |
> |
v.add(two); |
200 |
> |
full.removeAll(v); |
201 |
> |
assertEquals(1, full.size()); |
202 |
|
} |
203 |
|
|
204 |
< |
|
205 |
< |
public void testRemove(){ |
206 |
< |
CopyOnWriteArraySet full = fullSet(3); |
207 |
< |
full.remove(new Integer(1)); |
208 |
< |
assertFalse(full.contains(new Integer(1))); |
209 |
< |
assertEquals(2, full.size()); |
204 |
> |
/** |
205 |
> |
* remove removes an element |
206 |
> |
*/ |
207 |
> |
public void testRemove() { |
208 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
209 |
> |
full.remove(one); |
210 |
> |
assertFalse(full.contains(one)); |
211 |
> |
assertEquals(2, full.size()); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* Test to verify size returns the correct values |
215 |
> |
* size returns the number of elements |
216 |
|
*/ |
217 |
< |
public void testSize(){ |
218 |
< |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
219 |
< |
CopyOnWriteArraySet full = fullSet(3); |
220 |
< |
assertEquals(3, full.size()); |
221 |
< |
assertEquals(0, empty.size()); |
217 |
> |
public void testSize() { |
218 |
> |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
219 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
220 |
> |
assertEquals(3, full.size()); |
221 |
> |
assertEquals(0, empty.size()); |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
< |
* Test to verify toArray returns an Object array containing all elements from the list |
225 |
> |
* toArray returns an Object array containing all elements from the set |
226 |
|
*/ |
227 |
< |
public void testToArray(){ |
228 |
< |
CopyOnWriteArraySet full = fullSet(3); |
229 |
< |
Object[] o = full.toArray(); |
230 |
< |
assertEquals(3, o.length); |
231 |
< |
assertEquals(0, ((Integer)o[0]).intValue()); |
232 |
< |
assertEquals(1, ((Integer)o[1]).intValue()); |
233 |
< |
assertEquals(2, ((Integer)o[2]).intValue()); |
227 |
> |
public void testToArray() { |
228 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
229 |
> |
Object[] o = full.toArray(); |
230 |
> |
assertEquals(3, o.length); |
231 |
> |
assertEquals(0, o[0]); |
232 |
> |
assertEquals(1, o[1]); |
233 |
> |
assertEquals(2, o[2]); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
< |
* test to verify toArray returns an Integer array containing all elements from the list |
237 |
> |
* toArray returns an Integer array containing all elements from |
238 |
> |
* the set |
239 |
|
*/ |
240 |
< |
public void testToArray2(){ |
241 |
< |
CopyOnWriteArraySet full = fullSet(3); |
242 |
< |
Integer[] i = new Integer[3]; |
243 |
< |
i = (Integer[])full.toArray(i); |
244 |
< |
assertEquals(3, i.length); |
245 |
< |
assertEquals(0, i[0].intValue()); |
246 |
< |
assertEquals(1, i[1].intValue()); |
247 |
< |
assertEquals(2, i[2].intValue()); |
240 |
> |
public void testToArray2() { |
241 |
> |
CopyOnWriteArraySet full = populatedSet(3); |
242 |
> |
Integer[] i = new Integer[3]; |
243 |
> |
i = (Integer[])full.toArray(i); |
244 |
> |
assertEquals(3, i.length); |
245 |
> |
assertEquals(0, (int) i[0]); |
246 |
> |
assertEquals(1, (int) i[1]); |
247 |
> |
assertEquals(2, (int) i[2]); |
248 |
|
} |
249 |
|
|
221 |
– |
|
222 |
– |
|
223 |
– |
// Exception tests |
224 |
– |
|
250 |
|
/** |
251 |
< |
* Test to verify toArray throws an ArrayStoreException when the given array |
252 |
< |
* can not store the objects inside the list |
251 |
> |
* toArray throws an ArrayStoreException when the given array can |
252 |
> |
* not store the objects inside the set |
253 |
|
*/ |
254 |
< |
public void testToArray_ArrayStoreException(){ |
255 |
< |
try{ |
254 |
> |
public void testToArray_ArrayStoreException() { |
255 |
> |
try { |
256 |
|
CopyOnWriteArraySet c = new CopyOnWriteArraySet(); |
257 |
|
c.add("zfasdfsdf"); |
258 |
|
c.add("asdadasd"); |
259 |
|
c.toArray(new Long[5]); |
260 |
< |
fail("Object[] toArray(Object[]) should throw ArrayStoreException"); |
261 |
< |
}catch(ArrayStoreException e){} |
260 |
> |
shouldThrow(); |
261 |
> |
} catch (ArrayStoreException success) {} |
262 |
> |
} |
263 |
> |
|
264 |
> |
/** |
265 |
> |
* A deserialized serialized set is equal |
266 |
> |
*/ |
267 |
> |
public void testSerialization() throws Exception { |
268 |
> |
CopyOnWriteArraySet q = populatedSet(SIZE); |
269 |
> |
|
270 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
271 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
272 |
> |
out.writeObject(q); |
273 |
> |
out.close(); |
274 |
> |
|
275 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
276 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
277 |
> |
CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject(); |
278 |
> |
assertEquals(q.size(), r.size()); |
279 |
> |
assertTrue(q.equals(r)); |
280 |
> |
assertTrue(r.equals(q)); |
281 |
|
} |
282 |
|
|
283 |
|
} |