11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
14 |
– |
|
14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
18 |
– |
|
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(CopyOnWriteArraySetTest.class); |
19 |
|
} |
28 |
|
return a; |
29 |
|
} |
30 |
|
|
31 |
+ |
/** |
32 |
+ |
* Default-constructed set is empty |
33 |
+ |
*/ |
34 |
|
public void testConstructor() { |
35 |
|
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
36 |
|
assertTrue(a.isEmpty()); |
37 |
|
} |
38 |
|
|
39 |
+ |
/** |
40 |
+ |
* Collection-constructed set holds all of its elements |
41 |
+ |
*/ |
42 |
|
public void testConstructor3() { |
43 |
|
Integer[] ints = new Integer[SIZE]; |
44 |
|
for (int i = 0; i < SIZE-1; ++i) |
50 |
|
|
51 |
|
|
52 |
|
/** |
53 |
< |
* addAll correctly adds each element from the given collection |
53 |
> |
* addAll adds each element from the given collection |
54 |
|
*/ |
55 |
< |
public void testAddAll(){ |
55 |
> |
public void testAddAll() { |
56 |
|
CopyOnWriteArraySet full = populatedSet(3); |
57 |
|
Vector v = new Vector(); |
58 |
|
v.add(three); |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* 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(){ |
69 |
> |
public void testAddAll2() { |
70 |
|
CopyOnWriteArraySet full = populatedSet(3); |
71 |
|
Vector v = new Vector(); |
72 |
|
v.add(three); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
< |
* 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(){ |
82 |
> |
public void testAdd2() { |
83 |
|
CopyOnWriteArraySet full = populatedSet(3); |
84 |
|
full.add(one); |
85 |
|
assertEquals(3, full.size()); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* addIfAbsent correctly adds the element when it does not exist in the list |
89 |
> |
* add adds the element when it does not exist |
90 |
> |
* in the set |
91 |
|
*/ |
92 |
< |
public void testAdd3(){ |
92 |
> |
public void testAdd3() { |
93 |
|
CopyOnWriteArraySet full = populatedSet(3); |
94 |
|
full.add(three); |
95 |
|
assertTrue(full.contains(three)); |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
< |
* clear correctly removes all elements from the list |
99 |
> |
* clear removes all elements from the set |
100 |
|
*/ |
101 |
< |
public void testClear(){ |
101 |
> |
public void testClear() { |
102 |
|
CopyOnWriteArraySet full = populatedSet(3); |
103 |
|
full.clear(); |
104 |
|
assertEquals(0, full.size()); |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
< |
* contains returns the correct values |
108 |
> |
* contains returns true for added elements |
109 |
|
*/ |
110 |
< |
public void testContains(){ |
110 |
> |
public void testContains() { |
111 |
|
CopyOnWriteArraySet full = populatedSet(3); |
112 |
|
assertTrue(full.contains(one)); |
113 |
|
assertFalse(full.contains(five)); |
114 |
|
} |
115 |
|
|
116 |
+ |
/** |
117 |
+ |
* Sets with equal elements are equal |
118 |
+ |
*/ |
119 |
|
public void testEquals() { |
120 |
|
CopyOnWriteArraySet a = populatedSet(3); |
121 |
|
CopyOnWriteArraySet b = populatedSet(3); |
133 |
|
|
134 |
|
|
135 |
|
/** |
136 |
< |
* containsAll returns the correct values |
136 |
> |
* containsAll returns true for collections with subset of elements |
137 |
|
*/ |
138 |
< |
public void testContainsAll(){ |
138 |
> |
public void testContainsAll() { |
139 |
|
CopyOnWriteArraySet full = populatedSet(3); |
140 |
|
Vector v = new Vector(); |
141 |
|
v.add(one); |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
< |
* isEmpty returns the correct values |
149 |
> |
* isEmpty is true when empty, else false |
150 |
|
*/ |
151 |
< |
public void testIsEmpty(){ |
151 |
> |
public void testIsEmpty() { |
152 |
|
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
153 |
|
CopyOnWriteArraySet full = populatedSet(3); |
154 |
|
assertTrue(empty.isEmpty()); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
< |
* 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(){ |
161 |
> |
public void testIterator() { |
162 |
|
CopyOnWriteArraySet full = populatedSet(3); |
163 |
|
Iterator i = full.iterator(); |
164 |
|
int j; |
167 |
|
assertEquals(3, j); |
168 |
|
} |
169 |
|
|
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"); |
179 |
> |
shouldThrow(); |
180 |
|
} |
181 |
|
catch (UnsupportedOperationException success) {} |
182 |
|
} |
183 |
|
|
184 |
< |
public void testToString(){ |
184 |
> |
/** |
185 |
> |
* toString holds toString of elements |
186 |
> |
*/ |
187 |
> |
public void testToString() { |
188 |
|
CopyOnWriteArraySet full = populatedSet(3); |
189 |
|
String s = full.toString(); |
190 |
|
for (int i = 0; i < 3; ++i) { |
194 |
|
|
195 |
|
|
196 |
|
/** |
197 |
< |
* removeAll correctly removes all elements from the given collection |
197 |
> |
* removeAll removes all elements from the given collection |
198 |
|
*/ |
199 |
< |
public void testRemoveAll(){ |
199 |
> |
public void testRemoveAll() { |
200 |
|
CopyOnWriteArraySet full = populatedSet(3); |
201 |
|
Vector v = new Vector(); |
202 |
|
v.add(one); |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
< |
public void testRemove(){ |
209 |
> |
/** |
210 |
> |
* remove removes an element |
211 |
> |
*/ |
212 |
> |
public void testRemove() { |
213 |
|
CopyOnWriteArraySet full = populatedSet(3); |
214 |
|
full.remove(one); |
215 |
|
assertFalse(full.contains(one)); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* size returns the correct values |
220 |
> |
* size returns the number of elements |
221 |
|
*/ |
222 |
< |
public void testSize(){ |
222 |
> |
public void testSize() { |
223 |
|
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
224 |
|
CopyOnWriteArraySet full = populatedSet(3); |
225 |
|
assertEquals(3, full.size()); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
< |
* toArray returns an Object array containing all elements from the list |
230 |
> |
* toArray returns an Object array containing all elements from the set |
231 |
|
*/ |
232 |
< |
public void testToArray(){ |
232 |
> |
public void testToArray() { |
233 |
|
CopyOnWriteArraySet full = populatedSet(3); |
234 |
|
Object[] o = full.toArray(); |
235 |
|
assertEquals(3, o.length); |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
< |
* toArray returns an Integer array containing all elements from the list |
242 |
> |
* toArray returns an Integer array containing all elements from |
243 |
> |
* the set |
244 |
|
*/ |
245 |
< |
public void testToArray2(){ |
245 |
> |
public void testToArray2() { |
246 |
|
CopyOnWriteArraySet full = populatedSet(3); |
247 |
|
Integer[] i = new Integer[3]; |
248 |
|
i = (Integer[])full.toArray(i); |
253 |
|
} |
254 |
|
|
255 |
|
|
238 |
– |
|
239 |
– |
// Exception tests |
240 |
– |
|
256 |
|
/** |
257 |
< |
* toArray throws an ArrayStoreException when the given array |
258 |
< |
* can not store the objects inside the list |
257 |
> |
* toArray throws an ArrayStoreException when the given array can |
258 |
> |
* not store the objects inside the set |
259 |
|
*/ |
260 |
< |
public void testToArray_ArrayStoreException(){ |
261 |
< |
try{ |
260 |
> |
public void testToArray_ArrayStoreException() { |
261 |
> |
try { |
262 |
|
CopyOnWriteArraySet c = new CopyOnWriteArraySet(); |
263 |
|
c.add("zfasdfsdf"); |
264 |
|
c.add("asdadasd"); |
265 |
|
c.toArray(new Long[5]); |
266 |
< |
fail("Object[] toArray(Object[]) should throw ArrayStoreException"); |
267 |
< |
}catch(ArrayStoreException e){} |
266 |
> |
shouldThrow(); |
267 |
> |
} catch(ArrayStoreException e){} |
268 |
|
} |
269 |
|
|
270 |
+ |
/** |
271 |
+ |
* A deserialized seriealized set is equal |
272 |
+ |
*/ |
273 |
|
public void testSerialization() { |
274 |
|
CopyOnWriteArraySet q = populatedSet(SIZE); |
275 |
|
|
286 |
|
assertTrue(q.equals(r)); |
287 |
|
assertTrue(r.equals(q)); |
288 |
|
} catch(Exception e){ |
289 |
< |
e.printStackTrace(); |
272 |
< |
fail("unexpected exception"); |
289 |
> |
unexpectedException(); |
290 |
|
} |
291 |
|
} |
292 |
|
|