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.*; |
11 |
|
import java.util.concurrent.*; |
12 |
|
import java.io.*; |
13 |
|
|
14 |
< |
public class CopyOnWriteArrayListTest extends JSR166TestCase{ |
15 |
< |
|
14 |
> |
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
15 |
> |
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
|
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(CopyOnWriteArrayListTest.class); |
21 |
> |
return new TestSuite(CopyOnWriteArrayListTest.class); |
22 |
|
} |
23 |
|
|
24 |
< |
static CopyOnWriteArrayList populatedArray(int n){ |
25 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
24 |
> |
static CopyOnWriteArrayList populatedArray(int n) { |
25 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
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 |
+ |
/** |
36 |
+ |
* a new list is empty |
37 |
+ |
*/ |
38 |
|
public void testConstructor() { |
39 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
39 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
40 |
|
assertTrue(a.isEmpty()); |
41 |
|
} |
42 |
|
|
43 |
+ |
/** |
44 |
+ |
* new list contains all elements of initializing array |
45 |
+ |
*/ |
46 |
|
public void testConstructor2() { |
47 |
|
Integer[] ints = new Integer[SIZE]; |
48 |
|
for (int i = 0; i < SIZE-1; ++i) |
49 |
|
ints[i] = new Integer(i); |
50 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
51 |
< |
for (int i = 0; i < SIZE; ++i) |
50 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
51 |
> |
for (int i = 0; i < SIZE; ++i) |
52 |
|
assertEquals(ints[i], a.get(i)); |
53 |
|
} |
54 |
|
|
55 |
+ |
/** |
56 |
+ |
* new list contains all elements of initializing collection |
57 |
+ |
*/ |
58 |
|
public void testConstructor3() { |
59 |
|
Integer[] ints = new Integer[SIZE]; |
60 |
|
for (int i = 0; i < SIZE-1; ++i) |
61 |
|
ints[i] = new Integer(i); |
62 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
63 |
< |
for (int i = 0; i < SIZE; ++i) |
62 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
63 |
> |
for (int i = 0; i < SIZE; ++i) |
64 |
|
assertEquals(ints[i], a.get(i)); |
65 |
|
} |
66 |
< |
|
66 |
> |
|
67 |
|
|
68 |
|
/** |
69 |
< |
* addAll correctly adds each element from the given collection |
69 |
> |
* addAll adds each element from the given collection |
70 |
|
*/ |
71 |
< |
public void testAddAll(){ |
72 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
73 |
< |
Vector v = new Vector(); |
74 |
< |
v.add(three); |
75 |
< |
v.add(four); |
76 |
< |
v.add(five); |
77 |
< |
full.addAll(v); |
78 |
< |
assertEquals(6, full.size()); |
71 |
> |
public void testAddAll() { |
72 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
73 |
> |
Vector v = new Vector(); |
74 |
> |
v.add(three); |
75 |
> |
v.add(four); |
76 |
> |
v.add(five); |
77 |
> |
full.addAll(v); |
78 |
> |
assertEquals(6, full.size()); |
79 |
|
} |
80 |
|
|
81 |
|
/** |
82 |
|
* addAllAbsent adds each element from the given collection that did not |
83 |
|
* already exist in the List |
84 |
|
*/ |
85 |
< |
public void testAddAllAbsent(){ |
86 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
87 |
< |
Vector v = new Vector(); |
88 |
< |
v.add(three); |
89 |
< |
v.add(four); |
90 |
< |
v.add(one); // will not add this element |
91 |
< |
full.addAllAbsent(v); |
92 |
< |
assertEquals(5, full.size()); |
85 |
> |
public void testAddAllAbsent() { |
86 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
87 |
> |
Vector v = new Vector(); |
88 |
> |
v.add(three); |
89 |
> |
v.add(four); |
90 |
> |
v.add(one); // will not add this element |
91 |
> |
full.addAllAbsent(v); |
92 |
> |
assertEquals(5, full.size()); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
|
* addIfAbsent will not add the element if it already exists in the list |
97 |
|
*/ |
98 |
< |
public void testAddIfAbsent(){ |
99 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
100 |
< |
full.addIfAbsent(one); |
101 |
< |
assertEquals(3, full.size()); |
98 |
> |
public void testAddIfAbsent() { |
99 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
100 |
> |
full.addIfAbsent(one); |
101 |
> |
assertEquals(SIZE, full.size()); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* addIfAbsent correctly adds the element when it does not exist in the list |
105 |
> |
* addIfAbsent adds the element when it does not exist in the list |
106 |
|
*/ |
107 |
< |
public void testAddIfAbsent2(){ |
108 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
107 |
> |
public void testAddIfAbsent2() { |
108 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
109 |
|
full.addIfAbsent(three); |
110 |
|
assertTrue(full.contains(three)); |
111 |
|
} |
112 |
|
|
113 |
|
/** |
114 |
< |
* clear correctly removes all elements from the list |
114 |
> |
* clear removes all elements from the list |
115 |
> |
*/ |
116 |
> |
public void testClear() { |
117 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
118 |
> |
full.clear(); |
119 |
> |
assertEquals(0, full.size()); |
120 |
> |
} |
121 |
> |
|
122 |
> |
|
123 |
> |
/** |
124 |
> |
* Cloned list is equal |
125 |
|
*/ |
126 |
< |
public void testClear(){ |
127 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
128 |
< |
full.clear(); |
129 |
< |
assertEquals(0, full.size()); |
126 |
> |
public void testClone() { |
127 |
> |
CopyOnWriteArrayList l1 = populatedArray(SIZE); |
128 |
> |
CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone()); |
129 |
> |
assertEquals(l1, l2); |
130 |
> |
l1.clear(); |
131 |
> |
assertFalse(l1.equals(l2)); |
132 |
|
} |
133 |
|
|
134 |
|
/** |
135 |
< |
* contains returns the correct values |
135 |
> |
* contains is true for added elements |
136 |
|
*/ |
137 |
< |
public void testContains(){ |
138 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
139 |
< |
assertTrue(full.contains(one)); |
140 |
< |
assertFalse(full.contains(five)); |
137 |
> |
public void testContains() { |
138 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
139 |
> |
assertTrue(full.contains(one)); |
140 |
> |
assertFalse(full.contains(five)); |
141 |
|
} |
142 |
|
|
143 |
+ |
/** |
144 |
+ |
* adding at an index places it in the indicated index |
145 |
+ |
*/ |
146 |
|
public void testAddIndex() { |
147 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
147 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
148 |
|
full.add(0, m1); |
149 |
|
assertEquals(4, full.size()); |
150 |
|
assertEquals(m1, full.get(0)); |
156 |
|
assertEquals(two, full.get(4)); |
157 |
|
} |
158 |
|
|
159 |
+ |
/** |
160 |
+ |
* lists with same elements are equal and have same hashCode |
161 |
+ |
*/ |
162 |
|
public void testEquals() { |
163 |
< |
CopyOnWriteArrayList a = populatedArray(3); |
164 |
< |
CopyOnWriteArrayList b = populatedArray(3); |
163 |
> |
CopyOnWriteArrayList a = populatedArray(3); |
164 |
> |
CopyOnWriteArrayList b = populatedArray(3); |
165 |
|
assertTrue(a.equals(b)); |
166 |
|
assertTrue(b.equals(a)); |
167 |
|
assertEquals(a.hashCode(), b.hashCode()); |
174 |
|
assertEquals(a.hashCode(), b.hashCode()); |
175 |
|
} |
176 |
|
|
177 |
< |
|
177 |
> |
|
178 |
|
/** |
179 |
< |
* containsAll returns the correct values |
179 |
> |
* containsAll returns true for collection with subset of elements |
180 |
|
*/ |
181 |
< |
public void testContainsAll(){ |
182 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
183 |
< |
Vector v = new Vector(); |
184 |
< |
v.add(one); |
185 |
< |
v.add(two); |
186 |
< |
assertTrue(full.containsAll(v)); |
187 |
< |
v.add(six); |
188 |
< |
assertFalse(full.containsAll(v)); |
181 |
> |
public void testContainsAll() { |
182 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
183 |
> |
Vector v = new Vector(); |
184 |
> |
v.add(one); |
185 |
> |
v.add(two); |
186 |
> |
assertTrue(full.containsAll(v)); |
187 |
> |
v.add(six); |
188 |
> |
assertFalse(full.containsAll(v)); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* get returns the correct value for the given index |
192 |
> |
* get returns the value at the given index |
193 |
|
*/ |
194 |
< |
public void testGet(){ |
195 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
196 |
< |
assertEquals(0, ((Integer)full.get(0)).intValue()); |
194 |
> |
public void testGet() { |
195 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
196 |
> |
assertEquals(0, ((Integer)full.get(0)).intValue()); |
197 |
|
} |
198 |
|
|
199 |
|
/** |
200 |
< |
* indexOf gives the correct index for the given object |
200 |
> |
* indexOf gives the index for the given object |
201 |
|
*/ |
202 |
< |
public void testIndexOf(){ |
203 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
204 |
< |
assertEquals(1, full.indexOf(one)); |
205 |
< |
assertEquals(-1, full.indexOf("puppies")); |
202 |
> |
public void testIndexOf() { |
203 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
204 |
> |
assertEquals(1, full.indexOf(one)); |
205 |
> |
assertEquals(-1, full.indexOf("puppies")); |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
< |
* indexOf gives the correct index based on the given index |
209 |
> |
* indexOf gives the index based on the given index |
210 |
|
* at which to start searching |
211 |
|
*/ |
212 |
< |
public void testIndexOf2(){ |
213 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
214 |
< |
assertEquals(1, full.indexOf(one, 0)); |
215 |
< |
assertEquals(-1, full.indexOf(one, 2)); |
212 |
> |
public void testIndexOf2() { |
213 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
214 |
> |
assertEquals(1, full.indexOf(one, 0)); |
215 |
> |
assertEquals(-1, full.indexOf(one, 2)); |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* isEmpty returns the correct values |
219 |
> |
* isEmpty returns true when empty, else false |
220 |
|
*/ |
221 |
< |
public void testIsEmpty(){ |
222 |
< |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
223 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
224 |
< |
assertTrue(empty.isEmpty()); |
225 |
< |
assertFalse(full.isEmpty()); |
221 |
> |
public void testIsEmpty() { |
222 |
> |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
223 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
224 |
> |
assertTrue(empty.isEmpty()); |
225 |
> |
assertFalse(full.isEmpty()); |
226 |
|
} |
227 |
|
|
228 |
|
/** |
229 |
< |
* iterator() returns an iterator containing the elements of the list |
229 |
> |
* iterator() returns an iterator containing the elements of the list |
230 |
|
*/ |
231 |
< |
public void testIterator(){ |
232 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
233 |
< |
Iterator i = full.iterator(); |
234 |
< |
int j; |
235 |
< |
for(j = 0; i.hasNext(); j++) |
236 |
< |
assertEquals(j, ((Integer)i.next()).intValue()); |
237 |
< |
assertEquals(3, j); |
231 |
> |
public void testIterator() { |
232 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
233 |
> |
Iterator i = full.iterator(); |
234 |
> |
int j; |
235 |
> |
for (j = 0; i.hasNext(); j++) |
236 |
> |
assertEquals(j, ((Integer)i.next()).intValue()); |
237 |
> |
assertEquals(SIZE, j); |
238 |
|
} |
239 |
|
|
240 |
+ |
/** |
241 |
+ |
* iterator.remove throws UnsupportedOperationException |
242 |
+ |
*/ |
243 |
|
public void testIteratorRemove () { |
244 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
244 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
245 |
|
Iterator it = full.iterator(); |
246 |
|
it.next(); |
247 |
|
try { |
248 |
|
it.remove(); |
249 |
< |
fail("should throw"); |
250 |
< |
} |
220 |
< |
catch (UnsupportedOperationException success) {} |
249 |
> |
shouldThrow(); |
250 |
> |
} catch (UnsupportedOperationException success) {} |
251 |
|
} |
252 |
|
|
253 |
< |
public void testToString(){ |
254 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
253 |
> |
/** |
254 |
> |
* toString contains toString of elements |
255 |
> |
*/ |
256 |
> |
public void testToString() { |
257 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
258 |
|
String s = full.toString(); |
259 |
|
for (int i = 0; i < 3; ++i) { |
260 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
261 |
|
} |
262 |
< |
} |
262 |
> |
} |
263 |
|
|
264 |
|
/** |
265 |
< |
* lastIndexOf returns the correct index for the given object |
265 |
> |
* lastIndexOf returns the index for the given object |
266 |
|
*/ |
267 |
< |
public void testLastIndexOf1(){ |
268 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
269 |
< |
full.add(one); |
270 |
< |
full.add(three); |
271 |
< |
assertEquals(3, full.lastIndexOf(one)); |
272 |
< |
assertEquals(-1, full.lastIndexOf(six)); |
267 |
> |
public void testLastIndexOf1() { |
268 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
269 |
> |
full.add(one); |
270 |
> |
full.add(three); |
271 |
> |
assertEquals(3, full.lastIndexOf(one)); |
272 |
> |
assertEquals(-1, full.lastIndexOf(six)); |
273 |
|
} |
274 |
|
|
275 |
|
/** |
276 |
< |
* lastIndexOf returns the correct index from the given starting point |
276 |
> |
* lastIndexOf returns the index from the given starting point |
277 |
|
*/ |
278 |
< |
public void testlastIndexOf2(){ |
279 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
280 |
< |
full.add(one); |
281 |
< |
full.add(three); |
282 |
< |
assertEquals(3, full.lastIndexOf(one, 4)); |
283 |
< |
assertEquals(-1, full.lastIndexOf(three, 3)); |
278 |
> |
public void testlastIndexOf2() { |
279 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
280 |
> |
full.add(one); |
281 |
> |
full.add(three); |
282 |
> |
assertEquals(3, full.lastIndexOf(one, 4)); |
283 |
> |
assertEquals(-1, full.lastIndexOf(three, 3)); |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
< |
* Identical to testIterator, except ListInterator has more functionality |
287 |
> |
* listIterator traverses all elements |
288 |
|
*/ |
289 |
< |
public void testListIterator1(){ |
290 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
291 |
< |
ListIterator i = full.listIterator(); |
292 |
< |
int j; |
293 |
< |
for(j = 0; i.hasNext(); j++) |
294 |
< |
assertEquals(j, ((Integer)i.next()).intValue()); |
295 |
< |
assertEquals(3, j); |
289 |
> |
public void testListIterator1() { |
290 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
291 |
> |
ListIterator i = full.listIterator(); |
292 |
> |
int j; |
293 |
> |
for (j = 0; i.hasNext(); j++) |
294 |
> |
assertEquals(j, ((Integer)i.next()).intValue()); |
295 |
> |
assertEquals(SIZE, j); |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* Identical to testIterator and testListIterator1, but only returns those elements |
267 |
< |
* after the given index |
299 |
> |
* listIterator only returns those elements after the given index |
300 |
|
*/ |
301 |
< |
public void testListIterator2(){ |
302 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
303 |
< |
ListIterator i = full.listIterator(1); |
304 |
< |
int j; |
305 |
< |
for(j = 0; i.hasNext(); j++) |
306 |
< |
assertEquals(j+1, ((Integer)i.next()).intValue()); |
307 |
< |
assertEquals(2, j); |
301 |
> |
public void testListIterator2() { |
302 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
303 |
> |
ListIterator i = full.listIterator(1); |
304 |
> |
int j; |
305 |
> |
for (j = 0; i.hasNext(); j++) |
306 |
> |
assertEquals(j+1, ((Integer)i.next()).intValue()); |
307 |
> |
assertEquals(2, j); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
< |
* remove correctly removes and returns the object at the given index |
311 |
> |
* remove removes and returns the object at the given index |
312 |
|
*/ |
313 |
< |
public void testRemove(){ |
314 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
315 |
< |
assertEquals(two, full.remove(2)); |
316 |
< |
assertEquals(2, full.size()); |
313 |
> |
public void testRemove() { |
314 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
315 |
> |
assertEquals(two, full.remove(2)); |
316 |
> |
assertEquals(2, full.size()); |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* removeAll correctly removes all elements from the given collection |
320 |
> |
* removeAll removes all elements from the given collection |
321 |
|
*/ |
322 |
< |
public void testRemoveAll(){ |
323 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
324 |
< |
Vector v = new Vector(); |
325 |
< |
v.add(one); |
326 |
< |
v.add(two); |
327 |
< |
full.removeAll(v); |
328 |
< |
assertEquals(1, full.size()); |
322 |
> |
public void testRemoveAll() { |
323 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
324 |
> |
Vector v = new Vector(); |
325 |
> |
v.add(one); |
326 |
> |
v.add(two); |
327 |
> |
full.removeAll(v); |
328 |
> |
assertEquals(1, full.size()); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
< |
* set correctly changes the element at the given index |
332 |
> |
* set changes the element at the given index |
333 |
|
*/ |
334 |
< |
public void testSet(){ |
335 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
336 |
< |
assertEquals(two, full.set(2, four)); |
337 |
< |
assertEquals(4, ((Integer)full.get(2)).intValue()); |
334 |
> |
public void testSet() { |
335 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
336 |
> |
assertEquals(two, full.set(2, four)); |
337 |
> |
assertEquals(4, ((Integer)full.get(2)).intValue()); |
338 |
|
} |
339 |
|
|
340 |
|
/** |
341 |
< |
* size returns the correct values |
341 |
> |
* size returns the number of elements |
342 |
|
*/ |
343 |
< |
public void testSize(){ |
344 |
< |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
345 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
346 |
< |
assertEquals(3, full.size()); |
347 |
< |
assertEquals(0, empty.size()); |
343 |
> |
public void testSize() { |
344 |
> |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
345 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
346 |
> |
assertEquals(SIZE, full.size()); |
347 |
> |
assertEquals(0, empty.size()); |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
|
* toArray returns an Object array containing all elements from the list |
352 |
|
*/ |
353 |
< |
public void testToArray(){ |
354 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
355 |
< |
Object[] o = full.toArray(); |
356 |
< |
assertEquals(3, o.length); |
357 |
< |
assertEquals(0, ((Integer)o[0]).intValue()); |
358 |
< |
assertEquals(1, ((Integer)o[1]).intValue()); |
359 |
< |
assertEquals(2, ((Integer)o[2]).intValue()); |
353 |
> |
public void testToArray() { |
354 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
355 |
> |
Object[] o = full.toArray(); |
356 |
> |
assertEquals(3, o.length); |
357 |
> |
assertEquals(0, ((Integer)o[0]).intValue()); |
358 |
> |
assertEquals(1, ((Integer)o[1]).intValue()); |
359 |
> |
assertEquals(2, ((Integer)o[2]).intValue()); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
< |
* test to verify toArray returns an Integer array containing all elements from the list |
364 |
< |
*/ |
365 |
< |
public void testToArray2(){ |
366 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
367 |
< |
Integer[] i = new Integer[3]; |
368 |
< |
i = (Integer[])full.toArray(i); |
369 |
< |
assertEquals(3, i.length); |
370 |
< |
assertEquals(0, i[0].intValue()); |
371 |
< |
assertEquals(1, i[1].intValue()); |
372 |
< |
assertEquals(2, i[2].intValue()); |
363 |
> |
* toArray returns an Integer array containing all elements from |
364 |
> |
* the list |
365 |
> |
*/ |
366 |
> |
public void testToArray2() { |
367 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
368 |
> |
Integer[] i = new Integer[3]; |
369 |
> |
i = (Integer[])full.toArray(i); |
370 |
> |
assertEquals(3, i.length); |
371 |
> |
assertEquals(0, i[0].intValue()); |
372 |
> |
assertEquals(1, i[1].intValue()); |
373 |
> |
assertEquals(2, i[2].intValue()); |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
+ |
/** |
378 |
+ |
* sublists contains elements at indexes offset from their base |
379 |
+ |
*/ |
380 |
|
public void testSubList() { |
381 |
< |
CopyOnWriteArrayList a = populatedArray(10); |
381 |
> |
CopyOnWriteArrayList a = populatedArray(10); |
382 |
|
assertTrue(a.subList(1,1).isEmpty()); |
383 |
< |
for(int j = 0; j < 9; ++j) { |
384 |
< |
for(int i = j ; i < 10; ++i) { |
385 |
< |
List b = a.subList(j,i); |
386 |
< |
for(int k = j; k < i; ++k) { |
387 |
< |
assertEquals(new Integer(k), b.get(k-j)); |
388 |
< |
} |
389 |
< |
} |
390 |
< |
} |
383 |
> |
for (int j = 0; j < 9; ++j) { |
384 |
> |
for (int i = j ; i < 10; ++i) { |
385 |
> |
List b = a.subList(j,i); |
386 |
> |
for (int k = j; k < i; ++k) { |
387 |
> |
assertEquals(new Integer(k), b.get(k-j)); |
388 |
> |
} |
389 |
> |
} |
390 |
> |
} |
391 |
|
|
392 |
< |
List s = a.subList(2, 5); |
392 |
> |
List s = a.subList(2, 5); |
393 |
|
assertEquals(s.size(), 3); |
394 |
|
s.set(2, m1); |
395 |
|
assertEquals(a.get(4), m1); |
396 |
< |
s.clear(); |
396 |
> |
s.clear(); |
397 |
|
assertEquals(a.size(), 7); |
398 |
|
} |
399 |
|
|
403 |
|
* toArray throws an ArrayStoreException when the given array |
404 |
|
* can not store the objects inside the list |
405 |
|
*/ |
406 |
< |
public void testToArray_ArrayStoreException(){ |
407 |
< |
try{ |
406 |
> |
public void testToArray_ArrayStoreException() { |
407 |
> |
try { |
408 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
409 |
|
c.add("zfasdfsdf"); |
410 |
|
c.add("asdadasd"); |
411 |
|
c.toArray(new Long[5]); |
412 |
< |
fail("Object[] toArray(Object[]) should throw ArrayStoreException"); |
413 |
< |
}catch(ArrayStoreException e){} |
412 |
> |
shouldThrow(); |
413 |
> |
} catch (ArrayStoreException e) {} |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
|
* get throws an IndexOutOfBoundsException on a negative index |
418 |
|
*/ |
419 |
< |
public void testGet1_IndexOutOfBoundsException(){ |
420 |
< |
try{ |
419 |
> |
public void testGet1_IndexOutOfBoundsException() { |
420 |
> |
try { |
421 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
422 |
|
c.get(-1); |
423 |
< |
fail("Object get(int) should throw IndexOutOfBounds exception"); |
424 |
< |
}catch(IndexOutOfBoundsException e){} |
423 |
> |
shouldThrow(); |
424 |
> |
} catch (IndexOutOfBoundsException e) {} |
425 |
|
} |
426 |
< |
|
426 |
> |
|
427 |
|
/** |
428 |
|
* get throws an IndexOutOfBoundsException on a too high index |
429 |
|
*/ |
430 |
< |
public void testGet2_IndexOutOfBoundsException(){ |
431 |
< |
try{ |
430 |
> |
public void testGet2_IndexOutOfBoundsException() { |
431 |
> |
try { |
432 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
433 |
|
c.add("asdasd"); |
434 |
|
c.add("asdad"); |
435 |
|
c.get(100); |
436 |
< |
fail("Object get(int) should throw IndexOutOfBounds exception"); |
437 |
< |
}catch(IndexOutOfBoundsException e){} |
436 |
> |
shouldThrow(); |
437 |
> |
} catch (IndexOutOfBoundsException e) {} |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
|
* set throws an IndexOutOfBoundsException on a negative index |
442 |
|
*/ |
443 |
< |
public void testSet1_IndexOutOfBoundsException(){ |
444 |
< |
try{ |
443 |
> |
public void testSet1_IndexOutOfBoundsException() { |
444 |
> |
try { |
445 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
446 |
|
c.set(-1,"qwerty"); |
447 |
< |
fail("Object get(int, Object) should throw IndexOutOfBounds exception"); |
448 |
< |
}catch(IndexOutOfBoundsException e){} |
447 |
> |
shouldThrow(); |
448 |
> |
} catch (IndexOutOfBoundsException e) {} |
449 |
|
} |
450 |
< |
|
450 |
> |
|
451 |
|
/** |
452 |
|
* set throws an IndexOutOfBoundsException on a too high index |
453 |
|
*/ |
454 |
< |
public void testSet2(){ |
455 |
< |
try{ |
454 |
> |
public void testSet2() { |
455 |
> |
try { |
456 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
457 |
|
c.add("asdasd"); |
458 |
|
c.add("asdad"); |
459 |
|
c.set(100, "qwerty"); |
460 |
< |
fail("Object set(int, Object) should throw IndexOutOfBounds exception"); |
461 |
< |
}catch(IndexOutOfBoundsException e){} |
460 |
> |
shouldThrow(); |
461 |
> |
} catch (IndexOutOfBoundsException e) {} |
462 |
|
} |
463 |
|
|
464 |
|
/** |
465 |
|
* add throws an IndexOutOfBoundsException on a negative index |
466 |
|
*/ |
467 |
< |
public void testAdd1_IndexOutOfBoundsException(){ |
468 |
< |
try{ |
467 |
> |
public void testAdd1_IndexOutOfBoundsException() { |
468 |
> |
try { |
469 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
470 |
|
c.add(-1,"qwerty"); |
471 |
< |
fail("void add(int, Object) should throw IndexOutOfBounds exception"); |
472 |
< |
}catch(IndexOutOfBoundsException e){} |
471 |
> |
shouldThrow(); |
472 |
> |
} catch (IndexOutOfBoundsException e) {} |
473 |
|
} |
474 |
< |
|
474 |
> |
|
475 |
|
/** |
476 |
|
* add throws an IndexOutOfBoundsException on a too high index |
477 |
|
*/ |
478 |
< |
public void testAdd2_IndexOutOfBoundsException(){ |
479 |
< |
try{ |
478 |
> |
public void testAdd2_IndexOutOfBoundsException() { |
479 |
> |
try { |
480 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
481 |
|
c.add("asdasd"); |
482 |
|
c.add("asdasdasd"); |
483 |
|
c.add(100, "qwerty"); |
484 |
< |
fail("void add(int, Object) should throw IndexOutOfBounds exception"); |
485 |
< |
}catch(IndexOutOfBoundsException e){} |
484 |
> |
shouldThrow(); |
485 |
> |
} catch (IndexOutOfBoundsException e) {} |
486 |
|
} |
487 |
|
|
488 |
|
/** |
489 |
|
* remove throws an IndexOutOfBoundsException on a negative index |
490 |
|
*/ |
491 |
< |
public void testRemove1_IndexOutOfBounds(){ |
492 |
< |
try{ |
491 |
> |
public void testRemove1_IndexOutOfBounds() { |
492 |
> |
try { |
493 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
494 |
|
c.remove(-1); |
495 |
< |
fail("Object remove(int) should throw IndexOutOfBounds exception"); |
496 |
< |
}catch(IndexOutOfBoundsException e){} |
495 |
> |
shouldThrow(); |
496 |
> |
} catch (IndexOutOfBoundsException e) {} |
497 |
|
} |
498 |
|
|
499 |
|
/** |
500 |
|
* remove throws an IndexOutOfBoundsException on a too high index |
501 |
|
*/ |
502 |
< |
public void testRemove2_IndexOutOfBounds(){ |
503 |
< |
try{ |
502 |
> |
public void testRemove2_IndexOutOfBounds() { |
503 |
> |
try { |
504 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
505 |
|
c.add("asdasd"); |
506 |
|
c.add("adasdasd"); |
507 |
|
c.remove(100); |
508 |
< |
fail("Object remove(int) should throw IndexOutOfBounds exception"); |
509 |
< |
}catch(IndexOutOfBoundsException e){} |
508 |
> |
shouldThrow(); |
509 |
> |
} catch (IndexOutOfBoundsException e) {} |
510 |
|
} |
511 |
< |
|
511 |
> |
|
512 |
|
/** |
513 |
|
* addAll throws an IndexOutOfBoundsException on a negative index |
514 |
|
*/ |
515 |
< |
public void testAddAll1_IndexOutOfBoundsException(){ |
516 |
< |
try{ |
515 |
> |
public void testAddAll1_IndexOutOfBoundsException() { |
516 |
> |
try { |
517 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
518 |
|
c.addAll(-1,new LinkedList()); |
519 |
< |
fail("boolean add(int, Collection) should throw IndexOutOfBounds exception"); |
520 |
< |
}catch(IndexOutOfBoundsException e){} |
519 |
> |
shouldThrow(); |
520 |
> |
} catch (IndexOutOfBoundsException e) {} |
521 |
|
} |
522 |
< |
|
522 |
> |
|
523 |
|
/** |
524 |
|
* addAll throws an IndexOutOfBoundsException on a too high index |
525 |
|
*/ |
526 |
< |
public void testAddAll2_IndexOutOfBoundsException(){ |
527 |
< |
try{ |
526 |
> |
public void testAddAll2_IndexOutOfBoundsException() { |
527 |
> |
try { |
528 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
529 |
|
c.add("asdasd"); |
530 |
|
c.add("asdasdasd"); |
531 |
|
c.addAll(100, new LinkedList()); |
532 |
< |
fail("boolean addAll(int, Collection) should throw IndexOutOfBounds exception"); |
533 |
< |
}catch(IndexOutOfBoundsException e){} |
532 |
> |
shouldThrow(); |
533 |
> |
} catch (IndexOutOfBoundsException e) {} |
534 |
|
} |
535 |
|
|
536 |
|
/** |
537 |
|
* listIterator throws an IndexOutOfBoundsException on a negative index |
538 |
|
*/ |
539 |
< |
public void testListIterator1_IndexOutOfBoundsException(){ |
540 |
< |
try{ |
539 |
> |
public void testListIterator1_IndexOutOfBoundsException() { |
540 |
> |
try { |
541 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
542 |
|
c.listIterator(-1); |
543 |
< |
fail("ListIterator listIterator(int) should throw IndexOutOfBounds exceptione"); |
544 |
< |
}catch(IndexOutOfBoundsException e){} |
543 |
> |
shouldThrow(); |
544 |
> |
} catch (IndexOutOfBoundsException e) {} |
545 |
|
} |
546 |
|
|
547 |
|
/** |
548 |
|
* listIterator throws an IndexOutOfBoundsException on a too high index |
549 |
|
*/ |
550 |
< |
public void testListIterator2_IndexOutOfBoundsException(){ |
551 |
< |
try{ |
550 |
> |
public void testListIterator2_IndexOutOfBoundsException() { |
551 |
> |
try { |
552 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
553 |
|
c.add("adasd"); |
554 |
|
c.add("asdasdas"); |
555 |
|
c.listIterator(100); |
556 |
< |
fail("ListIterator listIterator(int) should throw IndexOutOfBounds exception"); |
557 |
< |
}catch(IndexOutOfBoundsException e){} |
556 |
> |
shouldThrow(); |
557 |
> |
} catch (IndexOutOfBoundsException e) {} |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
|
* subList throws an IndexOutOfBoundsException on a negative index |
562 |
|
*/ |
563 |
< |
public void testSubList1_IndexOutOfBoundsException(){ |
564 |
< |
try{ |
563 |
> |
public void testSubList1_IndexOutOfBoundsException() { |
564 |
> |
try { |
565 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
566 |
|
c.subList(-1,100); |
567 |
|
|
568 |
< |
fail("List subList(int, int) should throw IndexOutofBounds exception"); |
569 |
< |
}catch(IndexOutOfBoundsException e){} |
568 |
> |
shouldThrow(); |
569 |
> |
} catch (IndexOutOfBoundsException e) {} |
570 |
|
} |
571 |
|
|
572 |
|
/** |
573 |
|
* subList throws an IndexOutOfBoundsException on a too high index |
574 |
|
*/ |
575 |
< |
public void testSubList2_IndexOutOfBoundsException(){ |
576 |
< |
try{ |
575 |
> |
public void testSubList2_IndexOutOfBoundsException() { |
576 |
> |
try { |
577 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
578 |
|
c.add("asdasd"); |
579 |
|
c.subList(1,100); |
580 |
< |
fail("List subList(int, int) should throw IndexOutofBounds exception"); |
581 |
< |
}catch(IndexOutOfBoundsException e){} |
580 |
> |
shouldThrow(); |
581 |
> |
} catch (IndexOutOfBoundsException e) {} |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
|
* subList throws IndexOutOfBoundsException when the second index |
586 |
< |
* is lower then the first |
586 |
> |
* is lower then the first |
587 |
|
*/ |
588 |
< |
public void testSubList3_IndexOutOfBoundsException(){ |
589 |
< |
try{ |
588 |
> |
public void testSubList3_IndexOutOfBoundsException() { |
589 |
> |
try { |
590 |
|
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
591 |
|
c.subList(3,1); |
592 |
|
|
593 |
< |
fail("List subList(int, int) should throw IndexOutofBounds exception"); |
594 |
< |
}catch(IndexOutOfBoundsException e){} |
593 |
> |
shouldThrow(); |
594 |
> |
} catch (IndexOutOfBoundsException e) {} |
595 |
|
} |
596 |
|
|
597 |
< |
public void testSerialization() { |
597 |
> |
/** |
598 |
> |
* a deserialized serialiszed list is equal |
599 |
> |
*/ |
600 |
> |
public void testSerialization() throws Exception { |
601 |
|
CopyOnWriteArrayList q = populatedArray(SIZE); |
602 |
|
|
603 |
< |
try { |
604 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
605 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
606 |
< |
out.writeObject(q); |
607 |
< |
out.close(); |
608 |
< |
|
609 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
610 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
611 |
< |
CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject(); |
612 |
< |
assertEquals(q.size(), r.size()); |
613 |
< |
assertTrue(q.equals(r)); |
575 |
< |
assertTrue(r.equals(q)); |
576 |
< |
} catch(Exception e){ |
577 |
< |
e.printStackTrace(); |
578 |
< |
fail("unexpected exception"); |
579 |
< |
} |
603 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
604 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
605 |
> |
out.writeObject(q); |
606 |
> |
out.close(); |
607 |
> |
|
608 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
609 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
610 |
> |
CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject(); |
611 |
> |
assertEquals(q.size(), r.size()); |
612 |
> |
assertTrue(q.equals(r)); |
613 |
> |
assertTrue(r.equals(q)); |
614 |
|
} |
615 |
< |
|
615 |
> |
|
616 |
|
} |