5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import java.util.Vector; |
8 |
> |
import java.util.Arrays; |
9 |
|
import java.util.Collection; |
10 |
+ |
import java.util.Collections; |
11 |
+ |
import java.util.List; |
12 |
+ |
import java.util.Vector; |
13 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
14 |
|
|
15 |
|
import junit.framework.Test; |
12 |
– |
import junit.framework.TestSuite; |
16 |
|
|
17 |
|
public class VectorTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
22 |
|
public static Test suite() { |
23 |
|
class Implementation implements CollectionImplementation { |
24 |
|
public Class<?> klazz() { return Vector.class; } |
25 |
< |
public Collection emptyCollection() { return new Vector(); } |
25 |
> |
public List emptyCollection() { return new Vector(); } |
26 |
|
public Object makeElement(int i) { return i; } |
27 |
|
public boolean isConcurrent() { return false; } |
28 |
|
public boolean permitsNulls() { return true; } |
29 |
|
} |
30 |
< |
return newTestSuite(// VectorTest.class, |
31 |
< |
CollectionTest.testSuite(new Implementation())); |
30 |
> |
class SubListImplementation extends Implementation { |
31 |
> |
public List emptyCollection() { |
32 |
> |
List list = super.emptyCollection(); |
33 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
34 |
> |
if (rnd.nextBoolean()) |
35 |
> |
list.add(makeElement(rnd.nextInt())); |
36 |
> |
int i = rnd.nextInt(list.size() + 1); |
37 |
> |
return list.subList(i, i); |
38 |
> |
} |
39 |
> |
} |
40 |
> |
return newTestSuite( |
41 |
> |
VectorTest.class, |
42 |
> |
CollectionTest.testSuite(new Implementation()), |
43 |
> |
CollectionTest.testSuite(new SubListImplementation())); |
44 |
> |
} |
45 |
> |
|
46 |
> |
static Vector<Integer> populatedList(int n) { |
47 |
> |
Vector<Integer> list = new Vector<>(); |
48 |
> |
assertTrue(list.isEmpty()); |
49 |
> |
for (int i = 0; i < n; i++) |
50 |
> |
list.add(i); |
51 |
> |
assertEquals(n <= 0, list.isEmpty()); |
52 |
> |
assertEquals(n, list.size()); |
53 |
> |
return list; |
54 |
> |
} |
55 |
> |
|
56 |
> |
/** |
57 |
> |
* addAll adds each element from the given collection, including duplicates |
58 |
> |
*/ |
59 |
> |
public void testAddAll() { |
60 |
> |
List list = populatedList(3); |
61 |
> |
assertTrue(list.addAll(Arrays.asList(three, four, five))); |
62 |
> |
assertEquals(6, list.size()); |
63 |
> |
assertTrue(list.addAll(Arrays.asList(three, four, five))); |
64 |
> |
assertEquals(9, list.size()); |
65 |
> |
} |
66 |
> |
|
67 |
> |
/** |
68 |
> |
* clear removes all elements from the list |
69 |
> |
*/ |
70 |
> |
public void testClear() { |
71 |
> |
List list = populatedList(SIZE); |
72 |
> |
list.clear(); |
73 |
> |
assertEquals(0, list.size()); |
74 |
> |
} |
75 |
> |
|
76 |
> |
/** |
77 |
> |
* Cloned list is equal |
78 |
> |
*/ |
79 |
> |
public void testClone() { |
80 |
> |
Vector l1 = populatedList(SIZE); |
81 |
> |
Vector l2 = (Vector)(l1.clone()); |
82 |
> |
assertEquals(l1, l2); |
83 |
> |
l1.clear(); |
84 |
> |
assertFalse(l1.equals(l2)); |
85 |
> |
} |
86 |
> |
|
87 |
> |
/** |
88 |
> |
* contains is true for added elements |
89 |
> |
*/ |
90 |
> |
public void testContains() { |
91 |
> |
List list = populatedList(3); |
92 |
> |
assertTrue(list.contains(one)); |
93 |
> |
assertFalse(list.contains(five)); |
94 |
> |
} |
95 |
> |
|
96 |
> |
/** |
97 |
> |
* adding at an index places it in the indicated index |
98 |
> |
*/ |
99 |
> |
public void testAddIndex() { |
100 |
> |
List list = populatedList(3); |
101 |
> |
list.add(0, m1); |
102 |
> |
assertEquals(4, list.size()); |
103 |
> |
assertEquals(m1, list.get(0)); |
104 |
> |
assertEquals(zero, list.get(1)); |
105 |
> |
|
106 |
> |
list.add(2, m2); |
107 |
> |
assertEquals(5, list.size()); |
108 |
> |
assertEquals(m2, list.get(2)); |
109 |
> |
assertEquals(two, list.get(4)); |
110 |
> |
} |
111 |
> |
|
112 |
> |
/** |
113 |
> |
* lists with same elements are equal and have same hashCode |
114 |
> |
*/ |
115 |
> |
public void testEquals() { |
116 |
> |
List a = populatedList(3); |
117 |
> |
List b = populatedList(3); |
118 |
> |
assertTrue(a.equals(b)); |
119 |
> |
assertTrue(b.equals(a)); |
120 |
> |
assertTrue(a.containsAll(b)); |
121 |
> |
assertTrue(b.containsAll(a)); |
122 |
> |
assertEquals(a.hashCode(), b.hashCode()); |
123 |
> |
a.add(m1); |
124 |
> |
assertFalse(a.equals(b)); |
125 |
> |
assertFalse(b.equals(a)); |
126 |
> |
assertTrue(a.containsAll(b)); |
127 |
> |
assertFalse(b.containsAll(a)); |
128 |
> |
b.add(m1); |
129 |
> |
assertTrue(a.equals(b)); |
130 |
> |
assertTrue(b.equals(a)); |
131 |
> |
assertTrue(a.containsAll(b)); |
132 |
> |
assertTrue(b.containsAll(a)); |
133 |
> |
assertEquals(a.hashCode(), b.hashCode()); |
134 |
> |
|
135 |
> |
assertFalse(a.equals(null)); |
136 |
> |
} |
137 |
> |
|
138 |
> |
/** |
139 |
> |
* containsAll returns true for collections with subset of elements |
140 |
> |
*/ |
141 |
> |
public void testContainsAll() { |
142 |
> |
List list = populatedList(3); |
143 |
> |
assertTrue(list.containsAll(Arrays.asList())); |
144 |
> |
assertTrue(list.containsAll(Arrays.asList(one))); |
145 |
> |
assertTrue(list.containsAll(Arrays.asList(one, two))); |
146 |
> |
assertFalse(list.containsAll(Arrays.asList(one, two, six))); |
147 |
> |
assertFalse(list.containsAll(Arrays.asList(six))); |
148 |
> |
|
149 |
> |
try { |
150 |
> |
list.containsAll(null); |
151 |
> |
shouldThrow(); |
152 |
> |
} catch (NullPointerException success) {} |
153 |
> |
} |
154 |
> |
|
155 |
> |
/** |
156 |
> |
* get returns the value at the given index |
157 |
> |
*/ |
158 |
> |
public void testGet() { |
159 |
> |
List list = populatedList(3); |
160 |
> |
assertEquals(0, list.get(0)); |
161 |
> |
} |
162 |
> |
|
163 |
> |
/** |
164 |
> |
* indexOf(Object) returns the index of the first occurrence of the |
165 |
> |
* specified element in this list, or -1 if this list does not |
166 |
> |
* contain the element |
167 |
> |
*/ |
168 |
> |
public void testIndexOf() { |
169 |
> |
List list = populatedList(3); |
170 |
> |
assertEquals(-1, list.indexOf(-42)); |
171 |
> |
int size = list.size(); |
172 |
> |
for (int i = 0; i < size; i++) { |
173 |
> |
assertEquals(i, list.indexOf(i)); |
174 |
> |
assertEquals(i, list.subList(0, size).indexOf(i)); |
175 |
> |
assertEquals(i, list.subList(0, i + 1).indexOf(i)); |
176 |
> |
assertEquals(-1, list.subList(0, i).indexOf(i)); |
177 |
> |
assertEquals(0, list.subList(i, size).indexOf(i)); |
178 |
> |
assertEquals(-1, list.subList(i + 1, size).indexOf(i)); |
179 |
> |
} |
180 |
> |
|
181 |
> |
list.add(1); |
182 |
> |
assertEquals(1, list.indexOf(1)); |
183 |
> |
assertEquals(1, list.subList(0, size + 1).indexOf(1)); |
184 |
> |
assertEquals(0, list.subList(1, size + 1).indexOf(1)); |
185 |
> |
assertEquals(size - 2, list.subList(2, size + 1).indexOf(1)); |
186 |
> |
assertEquals(0, list.subList(size, size + 1).indexOf(1)); |
187 |
> |
assertEquals(-1, list.subList(size + 1, size + 1).indexOf(1)); |
188 |
> |
} |
189 |
> |
|
190 |
> |
/** |
191 |
> |
* indexOf(E, int) returns the index of the first occurrence of the |
192 |
> |
* specified element in this list, searching forwards from index, |
193 |
> |
* or returns -1 if the element is not found |
194 |
> |
*/ |
195 |
> |
public void testIndexOf2() { |
196 |
> |
Vector list = populatedList(3); |
197 |
> |
int size = list.size(); |
198 |
> |
assertEquals(-1, list.indexOf(-42, 0)); |
199 |
> |
|
200 |
> |
// we might expect IOOBE, but spec says otherwise |
201 |
> |
assertEquals(-1, list.indexOf(0, size)); |
202 |
> |
assertEquals(-1, list.indexOf(0, Integer.MAX_VALUE)); |
203 |
> |
|
204 |
> |
assertThrows( |
205 |
> |
IndexOutOfBoundsException.class, |
206 |
> |
() -> list.indexOf(0, -1), |
207 |
> |
() -> list.indexOf(0, Integer.MIN_VALUE)); |
208 |
> |
|
209 |
> |
for (int i = 0; i < size; i++) { |
210 |
> |
assertEquals(i, list.indexOf(i, 0)); |
211 |
> |
assertEquals(i, list.indexOf(i, i)); |
212 |
> |
assertEquals(-1, list.indexOf(i, i + 1)); |
213 |
> |
} |
214 |
> |
|
215 |
> |
list.add(1); |
216 |
> |
assertEquals(1, list.indexOf(1, 0)); |
217 |
> |
assertEquals(1, list.indexOf(1, 1)); |
218 |
> |
assertEquals(size, list.indexOf(1, 2)); |
219 |
> |
assertEquals(size, list.indexOf(1, size)); |
220 |
> |
} |
221 |
> |
|
222 |
> |
/** |
223 |
> |
* isEmpty returns true when empty, else false |
224 |
> |
*/ |
225 |
> |
public void testIsEmpty() { |
226 |
> |
List empty = new Vector(); |
227 |
> |
assertTrue(empty.isEmpty()); |
228 |
> |
assertTrue(empty.subList(0, 0).isEmpty()); |
229 |
> |
|
230 |
> |
List full = populatedList(SIZE); |
231 |
> |
assertFalse(full.isEmpty()); |
232 |
> |
assertTrue(full.subList(0, 0).isEmpty()); |
233 |
> |
assertTrue(full.subList(SIZE, SIZE).isEmpty()); |
234 |
> |
} |
235 |
> |
|
236 |
> |
/** |
237 |
> |
* iterator of empty collection has no elements |
238 |
> |
*/ |
239 |
> |
public void testEmptyIterator() { |
240 |
> |
Collection c = new Vector(); |
241 |
> |
assertIteratorExhausted(c.iterator()); |
242 |
> |
} |
243 |
> |
|
244 |
> |
/** |
245 |
> |
* lastIndexOf(Object) returns the index of the last occurrence of |
246 |
> |
* the specified element in this list, or -1 if this list does not |
247 |
> |
* contain the element |
248 |
> |
*/ |
249 |
> |
public void testLastIndexOf1() { |
250 |
> |
List list = populatedList(3); |
251 |
> |
assertEquals(-1, list.lastIndexOf(-42)); |
252 |
> |
int size = list.size(); |
253 |
> |
for (int i = 0; i < size; i++) { |
254 |
> |
assertEquals(i, list.lastIndexOf(i)); |
255 |
> |
assertEquals(i, list.subList(0, size).lastIndexOf(i)); |
256 |
> |
assertEquals(i, list.subList(0, i + 1).lastIndexOf(i)); |
257 |
> |
assertEquals(-1, list.subList(0, i).lastIndexOf(i)); |
258 |
> |
assertEquals(0, list.subList(i, size).lastIndexOf(i)); |
259 |
> |
assertEquals(-1, list.subList(i + 1, size).lastIndexOf(i)); |
260 |
> |
} |
261 |
> |
|
262 |
> |
list.add(1); |
263 |
> |
assertEquals(size, list.lastIndexOf(1)); |
264 |
> |
assertEquals(size, list.subList(0, size + 1).lastIndexOf(1)); |
265 |
> |
assertEquals(1, list.subList(0, size).lastIndexOf(1)); |
266 |
> |
assertEquals(0, list.subList(1, 2).lastIndexOf(1)); |
267 |
> |
assertEquals(-1, list.subList(0, 1).indexOf(1)); |
268 |
> |
} |
269 |
> |
|
270 |
> |
/** |
271 |
> |
* lastIndexOf(E, int) returns the index of the last occurrence of the |
272 |
> |
* specified element in this list, searching backwards from index, or |
273 |
> |
* returns -1 if the element is not found |
274 |
> |
*/ |
275 |
> |
public void testLastIndexOf2() { |
276 |
> |
Vector list = populatedList(3); |
277 |
> |
|
278 |
> |
// we might expect IOOBE, but spec says otherwise |
279 |
> |
assertEquals(-1, list.lastIndexOf(0, -1)); |
280 |
> |
|
281 |
> |
int size = list.size(); |
282 |
> |
assertThrows( |
283 |
> |
IndexOutOfBoundsException.class, |
284 |
> |
() -> list.lastIndexOf(0, size), |
285 |
> |
() -> list.lastIndexOf(0, Integer.MAX_VALUE)); |
286 |
> |
|
287 |
> |
for (int i = 0; i < size; i++) { |
288 |
> |
assertEquals(i, list.lastIndexOf(i, i)); |
289 |
> |
assertEquals(list.indexOf(i), list.lastIndexOf(i, i)); |
290 |
> |
if (i > 0) |
291 |
> |
assertEquals(-1, list.lastIndexOf(i, i - 1)); |
292 |
> |
} |
293 |
> |
list.add(one); |
294 |
> |
list.add(three); |
295 |
> |
assertEquals(1, list.lastIndexOf(one, 1)); |
296 |
> |
assertEquals(1, list.lastIndexOf(one, 2)); |
297 |
> |
assertEquals(3, list.lastIndexOf(one, 3)); |
298 |
> |
assertEquals(3, list.lastIndexOf(one, 4)); |
299 |
> |
assertEquals(-1, list.lastIndexOf(three, 3)); |
300 |
> |
} |
301 |
> |
|
302 |
> |
/** |
303 |
> |
* size returns the number of elements |
304 |
> |
*/ |
305 |
> |
public void testSize() { |
306 |
> |
List empty = new Vector(); |
307 |
> |
assertEquals(0, empty.size()); |
308 |
> |
assertEquals(0, empty.subList(0, 0).size()); |
309 |
> |
|
310 |
> |
List full = populatedList(SIZE); |
311 |
> |
assertEquals(SIZE, full.size()); |
312 |
> |
assertEquals(0, full.subList(0, 0).size()); |
313 |
> |
assertEquals(0, full.subList(SIZE, SIZE).size()); |
314 |
> |
} |
315 |
> |
|
316 |
> |
/** |
317 |
> |
* sublists contains elements at indexes offset from their base |
318 |
> |
*/ |
319 |
> |
public void testSubList() { |
320 |
> |
List a = populatedList(10); |
321 |
> |
assertTrue(a.subList(1,1).isEmpty()); |
322 |
> |
for (int j = 0; j < 9; ++j) { |
323 |
> |
for (int i = j ; i < 10; ++i) { |
324 |
> |
List b = a.subList(j,i); |
325 |
> |
for (int k = j; k < i; ++k) { |
326 |
> |
assertEquals(new Integer(k), b.get(k-j)); |
327 |
> |
} |
328 |
> |
} |
329 |
> |
} |
330 |
> |
|
331 |
> |
List s = a.subList(2, 5); |
332 |
> |
assertEquals(3, s.size()); |
333 |
> |
s.set(2, m1); |
334 |
> |
assertEquals(a.get(4), m1); |
335 |
> |
s.clear(); |
336 |
> |
assertEquals(7, a.size()); |
337 |
> |
|
338 |
> |
assertThrows( |
339 |
> |
IndexOutOfBoundsException.class, |
340 |
> |
() -> s.get(0), |
341 |
> |
() -> s.set(0, 42)); |
342 |
> |
} |
343 |
> |
|
344 |
> |
/** |
345 |
> |
* toArray throws an ArrayStoreException when the given array |
346 |
> |
* can not store the objects inside the list |
347 |
> |
*/ |
348 |
> |
public void testToArray_ArrayStoreException() { |
349 |
> |
List list = new Vector(); |
350 |
> |
// Integers are not auto-converted to Longs |
351 |
> |
list.add(86); |
352 |
> |
list.add(99); |
353 |
> |
assertThrows( |
354 |
> |
ArrayStoreException.class, |
355 |
> |
() -> list.toArray(new Long[0]), |
356 |
> |
() -> list.toArray(new Long[5])); |
357 |
> |
} |
358 |
> |
|
359 |
> |
void testIndexOutOfBoundsException(List list) { |
360 |
> |
int size = list.size(); |
361 |
> |
assertThrows( |
362 |
> |
IndexOutOfBoundsException.class, |
363 |
> |
() -> list.get(-1), |
364 |
> |
() -> list.get(size), |
365 |
> |
() -> list.set(-1, "qwerty"), |
366 |
> |
() -> list.set(size, "qwerty"), |
367 |
> |
() -> list.add(-1, "qwerty"), |
368 |
> |
() -> list.add(size + 1, "qwerty"), |
369 |
> |
() -> list.remove(-1), |
370 |
> |
() -> list.remove(size), |
371 |
> |
() -> list.addAll(-1, Collections.emptyList()), |
372 |
> |
() -> list.addAll(size + 1, Collections.emptyList()), |
373 |
> |
() -> list.listIterator(-1), |
374 |
> |
() -> list.listIterator(size + 1), |
375 |
> |
() -> list.subList(-1, size), |
376 |
> |
() -> list.subList(0, size + 1)); |
377 |
> |
|
378 |
> |
// Conversely, operations that must not throw |
379 |
> |
list.addAll(0, Collections.emptyList()); |
380 |
> |
list.addAll(size, Collections.emptyList()); |
381 |
> |
list.add(0, "qwerty"); |
382 |
> |
list.add(list.size(), "qwerty"); |
383 |
> |
list.get(0); |
384 |
> |
list.get(list.size() - 1); |
385 |
> |
list.set(0, "azerty"); |
386 |
> |
list.set(list.size() - 1, "azerty"); |
387 |
> |
list.listIterator(0); |
388 |
> |
list.listIterator(list.size()); |
389 |
> |
list.subList(0, list.size()); |
390 |
> |
list.remove(list.size() - 1); |
391 |
> |
} |
392 |
> |
|
393 |
> |
/** |
394 |
> |
* IndexOutOfBoundsException is thrown when specified |
395 |
> |
*/ |
396 |
> |
public void testIndexOutOfBoundsException() { |
397 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
398 |
> |
List x = populatedList(rnd.nextInt(5)); |
399 |
> |
testIndexOutOfBoundsException(x); |
400 |
> |
|
401 |
> |
int start = rnd.nextInt(x.size() + 1); |
402 |
> |
int end = rnd.nextInt(start, x.size() + 1); |
403 |
> |
|
404 |
> |
// Vector#subList spec deviates slightly from List#subList spec |
405 |
> |
assertThrows( |
406 |
> |
IllegalArgumentException.class, |
407 |
> |
() -> x.subList(start, start - 1)); |
408 |
> |
|
409 |
> |
List subList = x.subList(start, end); |
410 |
> |
testIndexOutOfBoundsException(x); |
411 |
> |
} |
412 |
> |
|
413 |
> |
/** |
414 |
> |
* a deserialized/reserialized list equals original |
415 |
> |
*/ |
416 |
> |
public void testSerialization() throws Exception { |
417 |
> |
List x = populatedList(SIZE); |
418 |
> |
List y = serialClone(x); |
419 |
> |
|
420 |
> |
assertNotSame(x, y); |
421 |
> |
assertEquals(x.size(), y.size()); |
422 |
> |
assertEquals(x.toString(), y.toString()); |
423 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
424 |
> |
assertEquals(x, y); |
425 |
> |
assertEquals(y, x); |
426 |
> |
while (!x.isEmpty()) { |
427 |
> |
assertFalse(y.isEmpty()); |
428 |
> |
assertEquals(x.remove(0), y.remove(0)); |
429 |
> |
} |
430 |
> |
assertTrue(y.isEmpty()); |
431 |
> |
} |
432 |
> |
|
433 |
> |
/** |
434 |
> |
* tests for setSize() |
435 |
> |
*/ |
436 |
> |
public void testSetSize() { |
437 |
> |
final Vector v = new Vector(); |
438 |
> |
for (int n : new int[] { 100, 5, 50 }) { |
439 |
> |
v.setSize(n); |
440 |
> |
assertEquals(n, v.size()); |
441 |
> |
assertNull(v.get(0)); |
442 |
> |
assertNull(v.get(n - 1)); |
443 |
> |
assertThrows(ArrayIndexOutOfBoundsException.class, |
444 |
> |
() -> v.setSize(-1)); |
445 |
> |
assertEquals(n, v.size()); |
446 |
> |
assertNull(v.get(0)); |
447 |
> |
assertNull(v.get(n - 1)); |
448 |
> |
} |
449 |
|
} |
450 |
|
|
451 |
|
} |