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