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