1 |
|
/* |
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. |
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.*; |
9 |
> |
import java.util.ArrayList; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collection; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.LinkedList; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.ListIterator; |
16 |
> |
import java.util.NoSuchElementException; |
17 |
> |
import java.util.concurrent.CopyOnWriteArrayList; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
> |
|
22 |
> |
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
23 |
|
|
14 |
– |
public class CopyOnWriteArrayListTest extends JSR166TestCase{ |
15 |
– |
|
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run (suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
|
28 |
|
public static Test suite() { |
29 |
< |
return new TestSuite(CopyOnWriteArrayListTest.class); |
29 |
> |
return new TestSuite(CopyOnWriteArrayListTest.class); |
30 |
|
} |
31 |
|
|
32 |
< |
static CopyOnWriteArrayList populatedArray(int n){ |
33 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
32 |
> |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
33 |
> |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
34 |
|
assertTrue(a.isEmpty()); |
35 |
< |
for (int i = 0; i < n; ++i) |
36 |
< |
a.add(new Integer(i)); |
35 |
> |
for (int i = 0; i < n; i++) |
36 |
> |
a.add(i); |
37 |
|
assertFalse(a.isEmpty()); |
38 |
|
assertEquals(n, a.size()); |
39 |
|
return a; |
40 |
|
} |
41 |
|
|
42 |
+ |
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
43 |
+ |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
44 |
+ |
assertTrue(a.isEmpty()); |
45 |
+ |
for (int i = 0; i < elements.length; i++) |
46 |
+ |
a.add(elements[i]); |
47 |
+ |
assertFalse(a.isEmpty()); |
48 |
+ |
assertEquals(elements.length, a.size()); |
49 |
+ |
return a; |
50 |
+ |
} |
51 |
|
|
52 |
|
/** |
53 |
|
* a new list is empty |
54 |
|
*/ |
55 |
|
public void testConstructor() { |
56 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
56 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
57 |
|
assertTrue(a.isEmpty()); |
58 |
|
} |
59 |
|
|
62 |
|
*/ |
63 |
|
public void testConstructor2() { |
64 |
|
Integer[] ints = new Integer[SIZE]; |
65 |
< |
for (int i = 0; i < SIZE-1; ++i) |
65 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
66 |
|
ints[i] = new Integer(i); |
67 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
68 |
< |
for (int i = 0; i < SIZE; ++i) |
67 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
68 |
> |
for (int i = 0; i < SIZE; ++i) |
69 |
|
assertEquals(ints[i], a.get(i)); |
70 |
|
} |
71 |
|
|
74 |
|
*/ |
75 |
|
public void testConstructor3() { |
76 |
|
Integer[] ints = new Integer[SIZE]; |
77 |
< |
for (int i = 0; i < SIZE-1; ++i) |
77 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
78 |
|
ints[i] = new Integer(i); |
79 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
80 |
< |
for (int i = 0; i < SIZE; ++i) |
79 |
> |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
80 |
> |
for (int i = 0; i < SIZE; ++i) |
81 |
|
assertEquals(ints[i], a.get(i)); |
82 |
|
} |
66 |
– |
|
83 |
|
|
84 |
|
/** |
85 |
< |
* addAll adds each element from the given collection |
85 |
> |
* addAll adds each element from the given collection, including duplicates |
86 |
|
*/ |
87 |
|
public void testAddAll() { |
88 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
89 |
< |
Vector v = new Vector(); |
90 |
< |
v.add(three); |
91 |
< |
v.add(four); |
92 |
< |
v.add(five); |
77 |
< |
full.addAll(v); |
78 |
< |
assertEquals(6, full.size()); |
88 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
89 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
90 |
> |
assertEquals(6, full.size()); |
91 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
92 |
> |
assertEquals(9, full.size()); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
< |
* addAllAbsent adds each element from the given collection that did not |
97 |
< |
* already exist in the List |
96 |
> |
* addAllAbsent adds each element from the given collection that did not |
97 |
> |
* already exist in the List |
98 |
|
*/ |
99 |
|
public void testAddAllAbsent() { |
100 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
101 |
< |
Vector v = new Vector(); |
102 |
< |
v.add(three); |
103 |
< |
v.add(four); |
104 |
< |
v.add(one); // will not add this element |
105 |
< |
full.addAllAbsent(v); |
92 |
< |
assertEquals(5, full.size()); |
100 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
101 |
> |
// "one" is duplicate and will not be added |
102 |
> |
assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one))); |
103 |
> |
assertEquals(5, full.size()); |
104 |
> |
assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one))); |
105 |
> |
assertEquals(5, full.size()); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
< |
* addIfAbsent will not add the element if it already exists in the list |
109 |
> |
* addIfAbsent will not add the element if it already exists in the list |
110 |
|
*/ |
111 |
|
public void testAddIfAbsent() { |
112 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
113 |
< |
full.addIfAbsent(one); |
114 |
< |
assertEquals(SIZE, full.size()); |
112 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
113 |
> |
full.addIfAbsent(one); |
114 |
> |
assertEquals(SIZE, full.size()); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* addIfAbsent adds the element when it does not exist in the list |
118 |
> |
* addIfAbsent adds the element when it does not exist in the list |
119 |
|
*/ |
120 |
|
public void testAddIfAbsent2() { |
121 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
121 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
122 |
|
full.addIfAbsent(three); |
123 |
|
assertTrue(full.contains(three)); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* clear removes all elements from the list |
127 |
> |
* clear removes all elements from the list |
128 |
|
*/ |
129 |
|
public void testClear() { |
130 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
131 |
< |
full.clear(); |
132 |
< |
assertEquals(0, full.size()); |
130 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
131 |
> |
full.clear(); |
132 |
> |
assertEquals(0, full.size()); |
133 |
|
} |
134 |
|
|
122 |
– |
|
135 |
|
/** |
136 |
< |
* Cloned list is equal |
136 |
> |
* Cloned list is equal |
137 |
|
*/ |
138 |
|
public void testClone() { |
139 |
< |
CopyOnWriteArrayList l1 = populatedArray(SIZE); |
140 |
< |
CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone()); |
139 |
> |
CopyOnWriteArrayList l1 = populatedArray(SIZE); |
140 |
> |
CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone()); |
141 |
|
assertEquals(l1, l2); |
142 |
< |
l1.clear(); |
142 |
> |
l1.clear(); |
143 |
|
assertFalse(l1.equals(l2)); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
< |
* contains is true for added elements |
147 |
> |
* contains is true for added elements |
148 |
|
*/ |
149 |
|
public void testContains() { |
150 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
151 |
< |
assertTrue(full.contains(one)); |
152 |
< |
assertFalse(full.contains(five)); |
150 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
151 |
> |
assertTrue(full.contains(one)); |
152 |
> |
assertFalse(full.contains(five)); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
< |
* adding at an index places it in the indindicated index |
156 |
> |
* adding at an index places it in the indicated index |
157 |
|
*/ |
158 |
|
public void testAddIndex() { |
159 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
159 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
160 |
|
full.add(0, m1); |
161 |
|
assertEquals(4, full.size()); |
162 |
|
assertEquals(m1, full.get(0)); |
172 |
|
* lists with same elements are equal and have same hashCode |
173 |
|
*/ |
174 |
|
public void testEquals() { |
175 |
< |
CopyOnWriteArrayList a = populatedArray(3); |
176 |
< |
CopyOnWriteArrayList b = populatedArray(3); |
175 |
> |
CopyOnWriteArrayList a = populatedArray(3); |
176 |
> |
CopyOnWriteArrayList b = populatedArray(3); |
177 |
|
assertTrue(a.equals(b)); |
178 |
|
assertTrue(b.equals(a)); |
179 |
+ |
assertTrue(a.containsAll(b)); |
180 |
+ |
assertTrue(b.containsAll(a)); |
181 |
|
assertEquals(a.hashCode(), b.hashCode()); |
182 |
|
a.add(m1); |
183 |
|
assertFalse(a.equals(b)); |
184 |
|
assertFalse(b.equals(a)); |
185 |
+ |
assertTrue(a.containsAll(b)); |
186 |
+ |
assertFalse(b.containsAll(a)); |
187 |
|
b.add(m1); |
188 |
|
assertTrue(a.equals(b)); |
189 |
|
assertTrue(b.equals(a)); |
190 |
+ |
assertTrue(a.containsAll(b)); |
191 |
+ |
assertTrue(b.containsAll(a)); |
192 |
|
assertEquals(a.hashCode(), b.hashCode()); |
193 |
+ |
|
194 |
+ |
assertFalse(a.equals(null)); |
195 |
|
} |
196 |
|
|
177 |
– |
|
197 |
|
/** |
198 |
< |
* containsAll returns true for collection with subset of elements |
198 |
> |
* containsAll returns true for collections with subset of elements |
199 |
|
*/ |
200 |
|
public void testContainsAll() { |
201 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
202 |
< |
Vector v = new Vector(); |
203 |
< |
v.add(one); |
204 |
< |
v.add(two); |
205 |
< |
assertTrue(full.containsAll(v)); |
206 |
< |
v.add(six); |
207 |
< |
assertFalse(full.containsAll(v)); |
201 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
202 |
> |
assertTrue(full.containsAll(Arrays.asList())); |
203 |
> |
assertTrue(full.containsAll(Arrays.asList(one))); |
204 |
> |
assertTrue(full.containsAll(Arrays.asList(one, two))); |
205 |
> |
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
206 |
> |
assertFalse(full.containsAll(Arrays.asList(six))); |
207 |
> |
|
208 |
> |
try { |
209 |
> |
full.containsAll(null); |
210 |
> |
shouldThrow(); |
211 |
> |
} catch (NullPointerException success) {} |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* get returns the value at the given index |
215 |
> |
* get returns the value at the given index |
216 |
|
*/ |
217 |
|
public void testGet() { |
218 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
219 |
< |
assertEquals(0, ((Integer)full.get(0)).intValue()); |
218 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
219 |
> |
assertEquals(0, full.get(0)); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
< |
* indexOf gives the index for the given object |
223 |
> |
* indexOf gives the index for the given object |
224 |
|
*/ |
225 |
|
public void testIndexOf() { |
226 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
227 |
< |
assertEquals(1, full.indexOf(one)); |
228 |
< |
assertEquals(-1, full.indexOf("puppies")); |
226 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
227 |
> |
assertEquals(1, full.indexOf(one)); |
228 |
> |
assertEquals(-1, full.indexOf("puppies")); |
229 |
|
} |
230 |
|
|
231 |
|
/** |
232 |
< |
* indexOf gives the index based on the given index |
233 |
< |
* at which to start searching |
232 |
> |
* indexOf gives the index based on the given index |
233 |
> |
* at which to start searching |
234 |
|
*/ |
235 |
|
public void testIndexOf2() { |
236 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
237 |
< |
assertEquals(1, full.indexOf(one, 0)); |
238 |
< |
assertEquals(-1, full.indexOf(one, 2)); |
236 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
237 |
> |
assertEquals(1, full.indexOf(one, 0)); |
238 |
> |
assertEquals(-1, full.indexOf(one, 2)); |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
< |
* isEmpty returns true when empty, else false |
242 |
> |
* isEmpty returns true when empty, else false |
243 |
|
*/ |
244 |
|
public void testIsEmpty() { |
245 |
< |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
246 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
247 |
< |
assertTrue(empty.isEmpty()); |
248 |
< |
assertFalse(full.isEmpty()); |
245 |
> |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
246 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
247 |
> |
assertTrue(empty.isEmpty()); |
248 |
> |
assertFalse(full.isEmpty()); |
249 |
|
} |
250 |
|
|
251 |
|
/** |
252 |
< |
* iterator() returns an iterator containing the elements of the list |
252 |
> |
* iterator() returns an iterator containing the elements of the |
253 |
> |
* list in insertion order |
254 |
|
*/ |
255 |
|
public void testIterator() { |
256 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
257 |
< |
Iterator i = full.iterator(); |
258 |
< |
int j; |
259 |
< |
for(j = 0; i.hasNext(); j++) |
260 |
< |
assertEquals(j, ((Integer)i.next()).intValue()); |
261 |
< |
assertEquals(SIZE, j); |
256 |
> |
Collection empty = new CopyOnWriteArrayList(); |
257 |
> |
assertFalse(empty.iterator().hasNext()); |
258 |
> |
try { |
259 |
> |
empty.iterator().next(); |
260 |
> |
shouldThrow(); |
261 |
> |
} catch (NoSuchElementException success) {} |
262 |
> |
|
263 |
> |
Integer[] elements = new Integer[SIZE]; |
264 |
> |
for (int i = 0; i < SIZE; i++) |
265 |
> |
elements[i] = i; |
266 |
> |
shuffle(elements); |
267 |
> |
Collection<Integer> full = populatedArray(elements); |
268 |
> |
|
269 |
> |
Iterator it = full.iterator(); |
270 |
> |
for (int j = 0; j < SIZE; j++) { |
271 |
> |
assertTrue(it.hasNext()); |
272 |
> |
assertEquals(elements[j], it.next()); |
273 |
> |
} |
274 |
> |
assertIteratorExhausted(it); |
275 |
> |
} |
276 |
> |
|
277 |
> |
/** |
278 |
> |
* iterator of empty collection has no elements |
279 |
> |
*/ |
280 |
> |
public void testEmptyIterator() { |
281 |
> |
Collection c = new CopyOnWriteArrayList(); |
282 |
> |
assertIteratorExhausted(c.iterator()); |
283 |
|
} |
284 |
|
|
285 |
|
/** |
286 |
|
* iterator.remove throws UnsupportedOperationException |
287 |
|
*/ |
288 |
< |
public void testIteratorRemove () { |
289 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
288 |
> |
public void testIteratorRemove() { |
289 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
290 |
|
Iterator it = full.iterator(); |
291 |
|
it.next(); |
292 |
|
try { |
293 |
|
it.remove(); |
294 |
|
shouldThrow(); |
295 |
< |
} |
251 |
< |
catch (UnsupportedOperationException success) {} |
295 |
> |
} catch (UnsupportedOperationException success) {} |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
|
* toString contains toString of elements |
300 |
|
*/ |
301 |
|
public void testToString() { |
302 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
302 |
> |
assertEquals("[]", new CopyOnWriteArrayList().toString()); |
303 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
304 |
|
String s = full.toString(); |
305 |
< |
for (int i = 0; i < 3; ++i) { |
306 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
307 |
< |
} |
308 |
< |
} |
305 |
> |
for (int i = 0; i < 3; ++i) |
306 |
> |
assertTrue(s.contains(String.valueOf(i))); |
307 |
> |
assertEquals(new ArrayList(full).toString(), |
308 |
> |
full.toString()); |
309 |
> |
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* lastIndexOf returns the index for the given object |
312 |
> |
* lastIndexOf returns the index for the given object |
313 |
|
*/ |
314 |
|
public void testLastIndexOf1() { |
315 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
316 |
< |
full.add(one); |
317 |
< |
full.add(three); |
318 |
< |
assertEquals(3, full.lastIndexOf(one)); |
319 |
< |
assertEquals(-1, full.lastIndexOf(six)); |
315 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
316 |
> |
full.add(one); |
317 |
> |
full.add(three); |
318 |
> |
assertEquals(3, full.lastIndexOf(one)); |
319 |
> |
assertEquals(-1, full.lastIndexOf(six)); |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
< |
* lastIndexOf returns the index from the given starting point |
323 |
> |
* lastIndexOf returns the index from the given starting point |
324 |
|
*/ |
325 |
< |
public void testlastIndexOf2() { |
326 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
327 |
< |
full.add(one); |
328 |
< |
full.add(three); |
329 |
< |
assertEquals(3, full.lastIndexOf(one, 4)); |
330 |
< |
assertEquals(-1, full.lastIndexOf(three, 3)); |
325 |
> |
public void testLastIndexOf2() { |
326 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
327 |
> |
full.add(one); |
328 |
> |
full.add(three); |
329 |
> |
assertEquals(3, full.lastIndexOf(one, 4)); |
330 |
> |
assertEquals(-1, full.lastIndexOf(three, 3)); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
< |
* listIterator traverses all elements |
334 |
> |
* listIterator traverses all elements |
335 |
|
*/ |
336 |
|
public void testListIterator1() { |
337 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
338 |
< |
ListIterator i = full.listIterator(); |
339 |
< |
int j; |
340 |
< |
for(j = 0; i.hasNext(); j++) |
341 |
< |
assertEquals(j, ((Integer)i.next()).intValue()); |
342 |
< |
assertEquals(SIZE, j); |
337 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
338 |
> |
ListIterator i = full.listIterator(); |
339 |
> |
int j; |
340 |
> |
for (j = 0; i.hasNext(); j++) |
341 |
> |
assertEquals(j, i.next()); |
342 |
> |
assertEquals(SIZE, j); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
< |
* listIterator only returns those elements after the given index |
346 |
> |
* listIterator only returns those elements after the given index |
347 |
|
*/ |
348 |
|
public void testListIterator2() { |
349 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
350 |
< |
ListIterator i = full.listIterator(1); |
351 |
< |
int j; |
352 |
< |
for(j = 0; i.hasNext(); j++) |
353 |
< |
assertEquals(j+1, ((Integer)i.next()).intValue()); |
354 |
< |
assertEquals(2, j); |
349 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
350 |
> |
ListIterator i = full.listIterator(1); |
351 |
> |
int j; |
352 |
> |
for (j = 0; i.hasNext(); j++) |
353 |
> |
assertEquals(j + 1, i.next()); |
354 |
> |
assertEquals(2, j); |
355 |
> |
} |
356 |
> |
|
357 |
> |
/** |
358 |
> |
* remove(int) removes and returns the object at the given index |
359 |
> |
*/ |
360 |
> |
public void testRemove_int() { |
361 |
> |
int SIZE = 3; |
362 |
> |
for (int i = 0; i < SIZE; i++) { |
363 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
364 |
> |
assertEquals(i, full.remove(i)); |
365 |
> |
assertEquals(SIZE - 1, full.size()); |
366 |
> |
assertFalse(full.contains(new Integer(i))); |
367 |
> |
} |
368 |
|
} |
369 |
|
|
370 |
|
/** |
371 |
< |
* remove removes and returns the object at the given index |
371 |
> |
* remove(Object) removes the object if found and returns true |
372 |
|
*/ |
373 |
< |
public void testRemove() { |
374 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
375 |
< |
assertEquals(two, full.remove(2)); |
376 |
< |
assertEquals(2, full.size()); |
373 |
> |
public void testRemove_Object() { |
374 |
> |
int SIZE = 3; |
375 |
> |
for (int i = 0; i < SIZE; i++) { |
376 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
377 |
> |
assertFalse(full.remove(new Integer(-42))); |
378 |
> |
assertTrue(full.remove(new Integer(i))); |
379 |
> |
assertEquals(SIZE - 1, full.size()); |
380 |
> |
assertFalse(full.contains(new Integer(i))); |
381 |
> |
} |
382 |
> |
CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); |
383 |
> |
assertTrue(x.remove(new Integer(6))); |
384 |
> |
assertEquals(x, Arrays.asList(4, 5)); |
385 |
> |
assertTrue(x.remove(new Integer(4))); |
386 |
> |
assertEquals(x, Arrays.asList(5)); |
387 |
> |
assertTrue(x.remove(new Integer(5))); |
388 |
> |
assertEquals(x, Arrays.asList()); |
389 |
> |
assertFalse(x.remove(new Integer(5))); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
393 |
< |
* removeAll removes all elements from the given collection |
393 |
> |
* removeAll removes all elements from the given collection |
394 |
|
*/ |
395 |
|
public void testRemoveAll() { |
396 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
397 |
< |
Vector v = new Vector(); |
398 |
< |
v.add(one); |
399 |
< |
v.add(two); |
400 |
< |
full.removeAll(v); |
329 |
< |
assertEquals(1, full.size()); |
396 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
397 |
> |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
398 |
> |
assertEquals(1, full.size()); |
399 |
> |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
400 |
> |
assertEquals(1, full.size()); |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
< |
* set changes the element at the given index |
404 |
> |
* set changes the element at the given index |
405 |
|
*/ |
406 |
|
public void testSet() { |
407 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
408 |
< |
assertEquals(two, full.set(2, four)); |
409 |
< |
assertEquals(4, ((Integer)full.get(2)).intValue()); |
407 |
> |
CopyOnWriteArrayList full = populatedArray(3); |
408 |
> |
assertEquals(2, full.set(2, four)); |
409 |
> |
assertEquals(4, full.get(2)); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* size returns the number of elements |
413 |
> |
* size returns the number of elements |
414 |
|
*/ |
415 |
|
public void testSize() { |
416 |
< |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
417 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
418 |
< |
assertEquals(SIZE, full.size()); |
419 |
< |
assertEquals(0, empty.size()); |
416 |
> |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
417 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
418 |
> |
assertEquals(SIZE, full.size()); |
419 |
> |
assertEquals(0, empty.size()); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* toArray returns an Object array containing all elements from the list |
423 |
> |
* toArray() returns an Object array containing all elements from |
424 |
> |
* the list in insertion order |
425 |
|
*/ |
426 |
|
public void testToArray() { |
427 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
428 |
< |
Object[] o = full.toArray(); |
429 |
< |
assertEquals(3, o.length); |
430 |
< |
assertEquals(0, ((Integer)o[0]).intValue()); |
431 |
< |
assertEquals(1, ((Integer)o[1]).intValue()); |
432 |
< |
assertEquals(2, ((Integer)o[2]).intValue()); |
427 |
> |
Object[] a = new CopyOnWriteArrayList().toArray(); |
428 |
> |
assertTrue(Arrays.equals(new Object[0], a)); |
429 |
> |
assertSame(Object[].class, a.getClass()); |
430 |
> |
|
431 |
> |
Integer[] elements = new Integer[SIZE]; |
432 |
> |
for (int i = 0; i < SIZE; i++) |
433 |
> |
elements[i] = i; |
434 |
> |
shuffle(elements); |
435 |
> |
Collection<Integer> full = populatedArray(elements); |
436 |
> |
|
437 |
> |
assertTrue(Arrays.equals(elements, full.toArray())); |
438 |
> |
assertSame(Object[].class, full.toArray().getClass()); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
< |
* toArray returns an Integer array containing all elements from |
443 |
< |
* the list |
442 |
> |
* toArray(Integer array) returns an Integer array containing all |
443 |
> |
* elements from the list in insertion order |
444 |
|
*/ |
445 |
|
public void testToArray2() { |
446 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
447 |
< |
Integer[] i = new Integer[3]; |
448 |
< |
i = (Integer[])full.toArray(i); |
449 |
< |
assertEquals(3, i.length); |
450 |
< |
assertEquals(0, i[0].intValue()); |
373 |
< |
assertEquals(1, i[1].intValue()); |
374 |
< |
assertEquals(2, i[2].intValue()); |
375 |
< |
} |
446 |
> |
Collection empty = new CopyOnWriteArrayList(); |
447 |
> |
Integer[] a; |
448 |
> |
|
449 |
> |
a = new Integer[0]; |
450 |
> |
assertSame(a, empty.toArray(a)); |
451 |
|
|
452 |
+ |
a = new Integer[SIZE / 2]; |
453 |
+ |
Arrays.fill(a, 42); |
454 |
+ |
assertSame(a, empty.toArray(a)); |
455 |
+ |
assertNull(a[0]); |
456 |
+ |
for (int i = 1; i < a.length; i++) |
457 |
+ |
assertEquals(42, (int) a[i]); |
458 |
+ |
|
459 |
+ |
Integer[] elements = new Integer[SIZE]; |
460 |
+ |
for (int i = 0; i < SIZE; i++) |
461 |
+ |
elements[i] = i; |
462 |
+ |
shuffle(elements); |
463 |
+ |
Collection<Integer> full = populatedArray(elements); |
464 |
+ |
|
465 |
+ |
Arrays.fill(a, 42); |
466 |
+ |
assertTrue(Arrays.equals(elements, full.toArray(a))); |
467 |
+ |
for (int i = 0; i < a.length; i++) |
468 |
+ |
assertEquals(42, (int) a[i]); |
469 |
+ |
assertSame(Integer[].class, full.toArray(a).getClass()); |
470 |
+ |
|
471 |
+ |
a = new Integer[SIZE]; |
472 |
+ |
Arrays.fill(a, 42); |
473 |
+ |
assertSame(a, full.toArray(a)); |
474 |
+ |
assertTrue(Arrays.equals(elements, a)); |
475 |
+ |
|
476 |
+ |
a = new Integer[2 * SIZE]; |
477 |
+ |
Arrays.fill(a, 42); |
478 |
+ |
assertSame(a, full.toArray(a)); |
479 |
+ |
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
480 |
+ |
assertNull(a[SIZE]); |
481 |
+ |
for (int i = SIZE + 1; i < a.length; i++) |
482 |
+ |
assertEquals(42, (int) a[i]); |
483 |
+ |
} |
484 |
|
|
485 |
|
/** |
486 |
|
* sublists contains elements at indexes offset from their base |
487 |
|
*/ |
488 |
|
public void testSubList() { |
489 |
< |
CopyOnWriteArrayList a = populatedArray(10); |
489 |
> |
CopyOnWriteArrayList a = populatedArray(10); |
490 |
|
assertTrue(a.subList(1,1).isEmpty()); |
491 |
< |
for(int j = 0; j < 9; ++j) { |
492 |
< |
for(int i = j ; i < 10; ++i) { |
493 |
< |
List b = a.subList(j,i); |
494 |
< |
for(int k = j; k < i; ++k) { |
495 |
< |
assertEquals(new Integer(k), b.get(k-j)); |
496 |
< |
} |
497 |
< |
} |
498 |
< |
} |
491 |
> |
for (int j = 0; j < 9; ++j) { |
492 |
> |
for (int i = j ; i < 10; ++i) { |
493 |
> |
List b = a.subList(j,i); |
494 |
> |
for (int k = j; k < i; ++k) { |
495 |
> |
assertEquals(new Integer(k), b.get(k-j)); |
496 |
> |
} |
497 |
> |
} |
498 |
> |
} |
499 |
|
|
500 |
< |
List s = a.subList(2, 5); |
501 |
< |
assertEquals(s.size(), 3); |
500 |
> |
List s = a.subList(2, 5); |
501 |
> |
assertEquals(3, s.size()); |
502 |
|
s.set(2, m1); |
503 |
|
assertEquals(a.get(4), m1); |
504 |
< |
s.clear(); |
505 |
< |
assertEquals(a.size(), 7); |
504 |
> |
s.clear(); |
505 |
> |
assertEquals(7, a.size()); |
506 |
|
} |
507 |
|
|
508 |
|
// Exception tests |
509 |
|
|
510 |
|
/** |
511 |
< |
* toArray throws an ArrayStoreException when the given array |
512 |
< |
* can not store the objects inside the list |
511 |
> |
* toArray throws an ArrayStoreException when the given array |
512 |
> |
* can not store the objects inside the list |
513 |
|
*/ |
514 |
|
public void testToArray_ArrayStoreException() { |
515 |
+ |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
516 |
+ |
c.add("zfasdfsdf"); |
517 |
+ |
c.add("asdadasd"); |
518 |
|
try { |
409 |
– |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
410 |
– |
c.add("zfasdfsdf"); |
411 |
– |
c.add("asdadasd"); |
519 |
|
c.toArray(new Long[5]); |
520 |
< |
shouldThrow(); |
521 |
< |
} catch(ArrayStoreException e){} |
520 |
> |
shouldThrow(); |
521 |
> |
} catch (ArrayStoreException success) {} |
522 |
|
} |
523 |
|
|
524 |
|
/** |
525 |
< |
* get throws an IndexOutOfBoundsException on a negative index |
525 |
> |
* get throws an IndexOutOfBoundsException on a negative index |
526 |
|
*/ |
527 |
|
public void testGet1_IndexOutOfBoundsException() { |
528 |
< |
try { |
529 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
530 |
< |
c.get(-1); |
531 |
< |
shouldThrow(); |
532 |
< |
} catch(IndexOutOfBoundsException e){} |
528 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
529 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
530 |
> |
for (List list : lists) { |
531 |
> |
try { |
532 |
> |
list.get(-1); |
533 |
> |
shouldThrow(); |
534 |
> |
} catch (IndexOutOfBoundsException success) {} |
535 |
> |
} |
536 |
|
} |
537 |
< |
|
537 |
> |
|
538 |
|
/** |
539 |
< |
* get throws an IndexOutOfBoundsException on a too high index |
539 |
> |
* get throws an IndexOutOfBoundsException on a too high index |
540 |
|
*/ |
541 |
|
public void testGet2_IndexOutOfBoundsException() { |
542 |
< |
try { |
543 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
544 |
< |
c.add("asdasd"); |
545 |
< |
c.add("asdad"); |
546 |
< |
c.get(100); |
547 |
< |
shouldThrow(); |
548 |
< |
} catch(IndexOutOfBoundsException e){} |
542 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
543 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
544 |
> |
for (List list : lists) { |
545 |
> |
try { |
546 |
> |
list.get(list.size()); |
547 |
> |
shouldThrow(); |
548 |
> |
} catch (IndexOutOfBoundsException success) {} |
549 |
> |
} |
550 |
|
} |
551 |
|
|
552 |
|
/** |
553 |
< |
* set throws an IndexOutOfBoundsException on a negative index |
553 |
> |
* set throws an IndexOutOfBoundsException on a negative index |
554 |
|
*/ |
555 |
|
public void testSet1_IndexOutOfBoundsException() { |
556 |
< |
try { |
557 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
558 |
< |
c.set(-1,"qwerty"); |
559 |
< |
shouldThrow(); |
560 |
< |
} catch(IndexOutOfBoundsException e){} |
556 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
557 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
558 |
> |
for (List list : lists) { |
559 |
> |
try { |
560 |
> |
list.set(-1, "qwerty"); |
561 |
> |
shouldThrow(); |
562 |
> |
} catch (IndexOutOfBoundsException success) {} |
563 |
> |
} |
564 |
|
} |
565 |
< |
|
565 |
> |
|
566 |
|
/** |
567 |
< |
* set throws an IndexOutOfBoundsException on a too high index |
567 |
> |
* set throws an IndexOutOfBoundsException on a too high index |
568 |
|
*/ |
569 |
|
public void testSet2() { |
570 |
< |
try { |
571 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
572 |
< |
c.add("asdasd"); |
573 |
< |
c.add("asdad"); |
574 |
< |
c.set(100, "qwerty"); |
575 |
< |
shouldThrow(); |
576 |
< |
} catch(IndexOutOfBoundsException e){} |
570 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
571 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
572 |
> |
for (List list : lists) { |
573 |
> |
try { |
574 |
> |
list.set(list.size(), "qwerty"); |
575 |
> |
shouldThrow(); |
576 |
> |
} catch (IndexOutOfBoundsException success) {} |
577 |
> |
} |
578 |
|
} |
579 |
|
|
580 |
|
/** |
581 |
< |
* add throws an IndexOutOfBoundsException on a negative index |
581 |
> |
* add throws an IndexOutOfBoundsException on a negative index |
582 |
|
*/ |
583 |
|
public void testAdd1_IndexOutOfBoundsException() { |
584 |
< |
try { |
585 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
586 |
< |
c.add(-1,"qwerty"); |
587 |
< |
shouldThrow(); |
588 |
< |
} catch(IndexOutOfBoundsException e){} |
584 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
585 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
586 |
> |
for (List list : lists) { |
587 |
> |
try { |
588 |
> |
list.add(-1, "qwerty"); |
589 |
> |
shouldThrow(); |
590 |
> |
} catch (IndexOutOfBoundsException success) {} |
591 |
> |
} |
592 |
|
} |
593 |
< |
|
593 |
> |
|
594 |
|
/** |
595 |
< |
* add throws an IndexOutOfBoundsException on a too high index |
595 |
> |
* add throws an IndexOutOfBoundsException on a too high index |
596 |
|
*/ |
597 |
|
public void testAdd2_IndexOutOfBoundsException() { |
598 |
< |
try { |
599 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
600 |
< |
c.add("asdasd"); |
601 |
< |
c.add("asdasdasd"); |
602 |
< |
c.add(100, "qwerty"); |
603 |
< |
shouldThrow(); |
604 |
< |
} catch(IndexOutOfBoundsException e){} |
598 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
599 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
600 |
> |
for (List list : lists) { |
601 |
> |
try { |
602 |
> |
list.add(list.size() + 1, "qwerty"); |
603 |
> |
shouldThrow(); |
604 |
> |
} catch (IndexOutOfBoundsException success) {} |
605 |
> |
} |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
< |
* remove throws an IndexOutOfBoundsException on a negative index |
609 |
> |
* remove throws an IndexOutOfBoundsException on a negative index |
610 |
|
*/ |
611 |
|
public void testRemove1_IndexOutOfBounds() { |
612 |
< |
try { |
613 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
614 |
< |
c.remove(-1); |
615 |
< |
shouldThrow(); |
616 |
< |
} catch(IndexOutOfBoundsException e){} |
612 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
613 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
614 |
> |
for (List list : lists) { |
615 |
> |
try { |
616 |
> |
list.remove(-1); |
617 |
> |
shouldThrow(); |
618 |
> |
} catch (IndexOutOfBoundsException success) {} |
619 |
> |
} |
620 |
|
} |
621 |
|
|
622 |
|
/** |
623 |
< |
* remove throws an IndexOutOfBoundsException on a too high index |
623 |
> |
* remove throws an IndexOutOfBoundsException on a too high index |
624 |
|
*/ |
625 |
|
public void testRemove2_IndexOutOfBounds() { |
626 |
< |
try { |
627 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
628 |
< |
c.add("asdasd"); |
629 |
< |
c.add("adasdasd"); |
630 |
< |
c.remove(100); |
631 |
< |
shouldThrow(); |
632 |
< |
} catch(IndexOutOfBoundsException e){} |
626 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
627 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
628 |
> |
for (List list : lists) { |
629 |
> |
try { |
630 |
> |
list.remove(list.size()); |
631 |
> |
shouldThrow(); |
632 |
> |
} catch (IndexOutOfBoundsException success) {} |
633 |
> |
} |
634 |
|
} |
635 |
< |
|
635 |
> |
|
636 |
|
/** |
637 |
< |
* addAll throws an IndexOutOfBoundsException on a negative index |
637 |
> |
* addAll throws an IndexOutOfBoundsException on a negative index |
638 |
|
*/ |
639 |
|
public void testAddAll1_IndexOutOfBoundsException() { |
640 |
< |
try { |
641 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
642 |
< |
c.addAll(-1,new LinkedList()); |
643 |
< |
shouldThrow(); |
644 |
< |
} catch(IndexOutOfBoundsException e){} |
640 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
641 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
642 |
> |
for (List list : lists) { |
643 |
> |
try { |
644 |
> |
list.addAll(-1, new LinkedList()); |
645 |
> |
shouldThrow(); |
646 |
> |
} catch (IndexOutOfBoundsException success) {} |
647 |
> |
} |
648 |
|
} |
649 |
< |
|
649 |
> |
|
650 |
|
/** |
651 |
< |
* addAll throws an IndexOutOfBoundsException on a too high index |
651 |
> |
* addAll throws an IndexOutOfBoundsException on a too high index |
652 |
|
*/ |
653 |
|
public void testAddAll2_IndexOutOfBoundsException() { |
654 |
< |
try { |
655 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
656 |
< |
c.add("asdasd"); |
657 |
< |
c.add("asdasdasd"); |
658 |
< |
c.addAll(100, new LinkedList()); |
659 |
< |
shouldThrow(); |
660 |
< |
} catch(IndexOutOfBoundsException e){} |
654 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
655 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
656 |
> |
for (List list : lists) { |
657 |
> |
try { |
658 |
> |
list.addAll(list.size() + 1, new LinkedList()); |
659 |
> |
shouldThrow(); |
660 |
> |
} catch (IndexOutOfBoundsException success) {} |
661 |
> |
} |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
< |
* listIterator throws an IndexOutOfBoundsException on a negative index |
665 |
> |
* listIterator throws an IndexOutOfBoundsException on a negative index |
666 |
|
*/ |
667 |
|
public void testListIterator1_IndexOutOfBoundsException() { |
668 |
< |
try { |
669 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
670 |
< |
c.listIterator(-1); |
671 |
< |
shouldThrow(); |
672 |
< |
} catch(IndexOutOfBoundsException e){} |
668 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
669 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
670 |
> |
for (List list : lists) { |
671 |
> |
try { |
672 |
> |
list.listIterator(-1); |
673 |
> |
shouldThrow(); |
674 |
> |
} catch (IndexOutOfBoundsException success) {} |
675 |
> |
} |
676 |
|
} |
677 |
|
|
678 |
|
/** |
679 |
< |
* listIterator throws an IndexOutOfBoundsException on a too high index |
679 |
> |
* listIterator throws an IndexOutOfBoundsException on a too high index |
680 |
|
*/ |
681 |
|
public void testListIterator2_IndexOutOfBoundsException() { |
682 |
< |
try { |
683 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
684 |
< |
c.add("adasd"); |
685 |
< |
c.add("asdasdas"); |
686 |
< |
c.listIterator(100); |
687 |
< |
shouldThrow(); |
688 |
< |
} catch(IndexOutOfBoundsException e){} |
682 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
683 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
684 |
> |
for (List list : lists) { |
685 |
> |
try { |
686 |
> |
list.listIterator(list.size() + 1); |
687 |
> |
shouldThrow(); |
688 |
> |
} catch (IndexOutOfBoundsException success) {} |
689 |
> |
} |
690 |
|
} |
691 |
|
|
692 |
|
/** |
693 |
< |
* subList throws an IndexOutOfBoundsException on a negative index |
693 |
> |
* subList throws an IndexOutOfBoundsException on a negative index |
694 |
|
*/ |
695 |
|
public void testSubList1_IndexOutOfBoundsException() { |
696 |
< |
try { |
697 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
698 |
< |
c.subList(-1,100); |
699 |
< |
|
700 |
< |
shouldThrow(); |
701 |
< |
} catch(IndexOutOfBoundsException e){} |
696 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
697 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
698 |
> |
for (List list : lists) { |
699 |
> |
try { |
700 |
> |
list.subList(-1, list.size()); |
701 |
> |
shouldThrow(); |
702 |
> |
} catch (IndexOutOfBoundsException success) {} |
703 |
> |
} |
704 |
|
} |
705 |
|
|
706 |
|
/** |
707 |
< |
* subList throws an IndexOutOfBoundsException on a too high index |
707 |
> |
* subList throws an IndexOutOfBoundsException on a too high index |
708 |
|
*/ |
709 |
|
public void testSubList2_IndexOutOfBoundsException() { |
710 |
< |
try { |
711 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
712 |
< |
c.add("asdasd"); |
713 |
< |
c.subList(1,100); |
714 |
< |
shouldThrow(); |
715 |
< |
} catch(IndexOutOfBoundsException e){} |
710 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
711 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
712 |
> |
for (List list : lists) { |
713 |
> |
try { |
714 |
> |
list.subList(0, list.size() + 1); |
715 |
> |
shouldThrow(); |
716 |
> |
} catch (IndexOutOfBoundsException success) {} |
717 |
> |
} |
718 |
|
} |
719 |
|
|
720 |
|
/** |
721 |
< |
* subList throws IndexOutOfBoundsException when the second index |
722 |
< |
* is lower then the first |
721 |
> |
* subList throws IndexOutOfBoundsException when the second index |
722 |
> |
* is lower then the first |
723 |
|
*/ |
724 |
|
public void testSubList3_IndexOutOfBoundsException() { |
725 |
< |
try { |
726 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
727 |
< |
c.subList(3,1); |
728 |
< |
|
729 |
< |
shouldThrow(); |
730 |
< |
} catch(IndexOutOfBoundsException e){} |
725 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
726 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
727 |
> |
for (List list : lists) { |
728 |
> |
try { |
729 |
> |
list.subList(list.size() - 1, 1); |
730 |
> |
shouldThrow(); |
731 |
> |
} catch (IndexOutOfBoundsException success) {} |
732 |
> |
} |
733 |
|
} |
734 |
|
|
735 |
|
/** |
736 |
< |
* a deserialized serialiszed list is equal |
736 |
> |
* a deserialized serialized list is equal |
737 |
|
*/ |
738 |
< |
public void testSerialization() { |
739 |
< |
CopyOnWriteArrayList q = populatedArray(SIZE); |
740 |
< |
|
741 |
< |
try { |
742 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
743 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
744 |
< |
out.writeObject(q); |
745 |
< |
out.close(); |
746 |
< |
|
747 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
748 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
749 |
< |
CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject(); |
750 |
< |
assertEquals(q.size(), r.size()); |
614 |
< |
assertTrue(q.equals(r)); |
615 |
< |
assertTrue(r.equals(q)); |
616 |
< |
} catch(Exception e){ |
617 |
< |
unexpectedException(); |
738 |
> |
public void testSerialization() throws Exception { |
739 |
> |
List x = populatedArray(SIZE); |
740 |
> |
List y = serialClone(x); |
741 |
> |
|
742 |
> |
assertNotSame(x, y); |
743 |
> |
assertEquals(x.size(), y.size()); |
744 |
> |
assertEquals(x.toString(), y.toString()); |
745 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
746 |
> |
assertEquals(x, y); |
747 |
> |
assertEquals(y, x); |
748 |
> |
while (!x.isEmpty()) { |
749 |
> |
assertFalse(y.isEmpty()); |
750 |
> |
assertEquals(x.remove(0), y.remove(0)); |
751 |
|
} |
752 |
+ |
assertTrue(y.isEmpty()); |
753 |
|
} |
754 |
< |
|
754 |
> |
|
755 |
|
} |