ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/VectorTest.java
Revision: 1.8
Committed: Tue Apr 3 05:49:44 2018 UTC (6 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.7: +6 -1 lines
Log Message:
add more randomness to sublist testing

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