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