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