ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/VectorTest.java
Revision: 1.12
Committed: Wed Jan 27 01:57:25 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +5 -5 lines
Log Message:
use diamond <> pervasively

File Contents

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