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