ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/VectorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/VectorTest.java (file contents):
Revision 1.3 by jsr166, Tue Nov 29 22:31:56 2016 UTC vs.
Revision 1.13 by jsr166, Wed Jan 27 02:55:18 2021 UTC

# Line 5 | Line 5
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;
13 import junit.framework.TestSuite;
16  
17   public class VectorTest extends JSR166TestCase {
18      public static void main(String[] args) {
# Line 21 | Line 23 | public class VectorTest extends JSR166Te
23          class Implementation implements CollectionImplementation {
24              public Class<?> klazz() { return Vector.class; }
25              public List emptyCollection() { return new Vector(); }
26 <            public Object makeElement(int i) { return i; }
26 >            public Object makeElement(int i) { return JSR166TestCase.itemFor(i); }
27              public boolean isConcurrent() { return false; }
28              public boolean permitsNulls() { return true; }
29          }
30          class SubListImplementation extends Implementation {
31 +            @SuppressWarnings("unchecked")
32              public List emptyCollection() {
33 <                return super.emptyCollection().subList(0, 0);
33 >                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              }
40          }
41          return newTestSuite(
# Line 36 | Line 44 | public class VectorTest extends JSR166Te
44                  CollectionTest.testSuite(new SubListImplementation()));
45      }
46  
47 +    static Vector<Item> populatedList(int n) {
48 +        Vector<Item> list = new Vector<>();
49 +        assertTrue(list.isEmpty());
50 +        for (int i = 0; i < n; i++)
51 +            mustAdd(list, i);
52 +        mustEqual(n <= 0, list.isEmpty());
53 +        mustEqual(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<Item> list = populatedList(3);
62 +        assertTrue(list.addAll(Arrays.asList(three, four, five)));
63 +        mustEqual(6, list.size());
64 +        assertTrue(list.addAll(Arrays.asList(three, four, five)));
65 +        mustEqual(9, list.size());
66 +    }
67 +
68 +    /**
69 +     * clear removes all elements from the list
70 +     */
71 +    public void testClear() {
72 +        List<Item> list = populatedList(SIZE);
73 +        list.clear();
74 +        mustEqual(0, list.size());
75 +    }
76 +
77 +    /**
78 +     * Cloned list is equal
79 +     */
80 +    public void testClone() {
81 +        Vector<Item> l1 = populatedList(SIZE);
82 +        @SuppressWarnings("unchecked")
83 +        Vector<Item> l2 = (Vector<Item>)(l1.clone());
84 +        mustEqual(l1, l2);
85 +        l1.clear();
86 +        assertFalse(l1.equals(l2));
87 +    }
88 +
89 +    /**
90 +     * contains is true for added elements
91 +     */
92 +    public void testContains() {
93 +        List<Item> list = populatedList(3);
94 +        mustContain(list, one);
95 +        mustNotContain(list, five);
96 +    }
97 +
98 +    /**
99 +     * adding at an index places it in the indicated index
100 +     */
101 +    public void testAddIndex() {
102 +        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 +    }
113 +
114 +    /**
115 +     * lists with same elements are equal and have same hashCode
116 +     */
117 +    public void testEquals() {
118 +        List<Item> a = populatedList(3);
119 +        List<Item> b = populatedList(3);
120 +        assertTrue(a.equals(b));
121 +        assertTrue(b.equals(a));
122 +        assertTrue(a.containsAll(b));
123 +        assertTrue(b.containsAll(a));
124 +        mustEqual(a.hashCode(), b.hashCode());
125 +        a.add(minusOne);
126 +        assertFalse(a.equals(b));
127 +        assertFalse(b.equals(a));
128 +        assertTrue(a.containsAll(b));
129 +        assertFalse(b.containsAll(a));
130 +        b.add(minusOne);
131 +        assertTrue(a.equals(b));
132 +        assertTrue(b.equals(a));
133 +        assertTrue(a.containsAll(b));
134 +        assertTrue(b.containsAll(a));
135 +        mustEqual(a.hashCode(), b.hashCode());
136 +
137 +        assertFalse(a.equals(null));
138 +    }
139 +
140 +    /**
141 +     * containsAll returns true for collections with subset of elements
142 +     */
143 +    public void testContainsAll() {
144 +        List<Item> list = populatedList(3);
145 +        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 +        List<Item> list = populatedList(3);
162 +        mustEqual(0, list.get(0));
163 +    }
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 +        List<Item> list = populatedList(3);
172 +        mustEqual(-1, list.indexOf(minusTen));
173 +        int size = list.size();
174 +        for (int i = 0; i < size; i++) {
175 +            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 +        }
183 +
184 +        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 +    }
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 +        Vector<Item> list = populatedList(3);
200 +        int size = list.size();
201 +        mustEqual(-1, list.indexOf(minusTen, 0));
202 +
203 +        // we might expect IOOBE, but spec says otherwise
204 +        mustEqual(-1, list.indexOf(zero, size));
205 +        mustEqual(-1, list.indexOf(zero, Integer.MAX_VALUE));
206 +
207 +        assertThrows(
208 +            IndexOutOfBoundsException.class,
209 +            () -> list.indexOf(zero, -1),
210 +            () -> list.indexOf(zero, Integer.MIN_VALUE));
211 +
212 +        for (int i = 0; i < size; i++) {
213 +            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 +        }
218 +
219 +        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 +    }
225 +
226 +    /**
227 +     * isEmpty returns true when empty, else false
228 +     */
229 +    public void testIsEmpty() {
230 +        List<Item> empty = new Vector<>();
231 +        assertTrue(empty.isEmpty());
232 +        assertTrue(empty.subList(0, 0).isEmpty());
233 +
234 +        List<Item> full = populatedList(SIZE);
235 +        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 +        Collection<Item> c = new Vector<>();
245 +        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 +        List<Item> list = populatedList(3);
255 +        mustEqual(-1, list.lastIndexOf(minusTen));
256 +        int size = list.size();
257 +        for (int i = 0; i < size; i++) {
258 +            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 +        }
266 +
267 +        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 +    }
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 +        Vector<Item> list = populatedList(3);
282 +
283 +        // we might expect IOOBE, but spec says otherwise
284 +        mustEqual(-1, list.lastIndexOf(zero, -1));
285 +
286 +        int size = list.size();
287 +        assertThrows(
288 +            IndexOutOfBoundsException.class,
289 +            () -> list.lastIndexOf(zero, size),
290 +            () -> list.lastIndexOf(zero, Integer.MAX_VALUE));
291 +
292 +        for (int i = 0; i < size; i++) {
293 +            Item I = itemFor(i);
294 +            mustEqual(i, list.lastIndexOf(I, i));
295 +            mustEqual(list.indexOf(I), list.lastIndexOf(I, i));
296 +            if (i > 0)
297 +                mustEqual(-1, list.lastIndexOf(I, i - 1));
298 +        }
299 +        list.add(one);
300 +        list.add(three);
301 +        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 +    }
307 +
308 +    /**
309 +     * size returns the number of elements
310 +     */
311 +    public void testSize() {
312 +        List<Item> empty = new Vector<>();
313 +        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 +    }
321 +
322 +    /**
323 +     * sublists contains elements at indexes offset from their base
324 +     */
325 +    public void testSubList() {
326 +        List<Item> a = populatedList(10);
327 +        assertTrue(a.subList(1,1).isEmpty());
328 +        for (int j = 0; j < 9; ++j) {
329 +            for (int i = j ; i < 10; ++i) {
330 +                List<Item> b = a.subList(j,i);
331 +                for (int k = j; k < i; ++k) {
332 +                    mustEqual(k, b.get(k-j));
333 +                }
334 +            }
335 +        }
336 +
337 +        List<Item> s = a.subList(2, 5);
338 +        mustEqual(3, s.size());
339 +        s.set(2, minusOne);
340 +        mustEqual(a.get(4), minusOne);
341 +        s.clear();
342 +        mustEqual(7, a.size());
343 +
344 +        assertThrows(
345 +            IndexOutOfBoundsException.class,
346 +            () -> s.get(0),
347 +            () -> s.set(0, fortytwo));
348 +    }
349 +
350 +    /**
351 +     * toArray throws an ArrayStoreException when the given array
352 +     * can not store the objects inside the list
353 +     */
354 +    @SuppressWarnings("CollectionToArraySafeParameter")
355 +    public void testToArray_ArrayStoreException() {
356 +        List<Item> list = new Vector<>();
357 +        // Items are not auto-converted to Longs
358 +        list.add(eightysix);
359 +        list.add(ninetynine);
360 +        assertThrows(
361 +            ArrayStoreException.class,
362 +            () -> list.toArray(new Long[0]),
363 +            () -> list.toArray(new Long[5]));
364 +    }
365 +
366 +    @SuppressWarnings("unchecked")
367 +    void testIndexOutOfBoundsException(List list) {
368 +        int size = list.size();
369 +        assertThrows(
370 +            IndexOutOfBoundsException.class,
371 +            () -> list.get(-1),
372 +            () -> list.get(size),
373 +            () -> list.set(-1, "qwerty"),
374 +            () -> list.set(size, "qwerty"),
375 +            () -> list.add(-1, "qwerty"),
376 +            () -> list.add(size + 1, "qwerty"),
377 +            () -> list.remove(-1),
378 +            () -> list.remove(size),
379 +            () -> list.addAll(-1, Collections.emptyList()),
380 +            () -> list.addAll(size + 1, Collections.emptyList()),
381 +            () -> list.listIterator(-1),
382 +            () -> list.listIterator(size + 1),
383 +            () -> list.subList(-1, size),
384 +            () -> list.subList(0, size + 1));
385 +
386 +        // Conversely, operations that must not throw
387 +        list.addAll(0, Collections.emptyList());
388 +        list.addAll(size, Collections.emptyList());
389 +        list.add(0, "qwerty");
390 +        list.add(list.size(), "qwerty");
391 +        list.get(0);
392 +        list.get(list.size() - 1);
393 +        list.set(0, "azerty");
394 +        list.set(list.size() - 1, "azerty");
395 +        list.listIterator(0);
396 +        list.listIterator(list.size());
397 +        list.subList(0, list.size());
398 +        list.remove(list.size() - 1);
399 +    }
400 +
401 +    /**
402 +     * IndexOutOfBoundsException is thrown when specified
403 +     */
404 +    public void testIndexOutOfBoundsException() {
405 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
406 +        List<Item> x = populatedList(rnd.nextInt(5));
407 +        testIndexOutOfBoundsException(x);
408 +
409 +        int start = rnd.nextInt(x.size() + 1);
410 +        int end = rnd.nextInt(start, x.size() + 1);
411 +
412 +        // Vector#subList spec deviates slightly from List#subList spec
413 +        assertThrows(
414 +            IllegalArgumentException.class,
415 +            () -> x.subList(start, start - 1));
416 +
417 +        List<Item> subList = x.subList(start, end);
418 +        testIndexOutOfBoundsException(x);
419 +    }
420 +
421 +    /**
422 +     * a deserialized/reserialized list equals original
423 +     */
424 +    public void testSerialization() throws Exception {
425 +        List<Item> x = populatedList(SIZE);
426 +        List<Item> y = serialClone(x);
427 +
428 +        assertNotSame(x, y);
429 +        mustEqual(x.size(), y.size());
430 +        mustEqual(x.toString(), y.toString());
431 +        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
432 +        mustEqual(x, y);
433 +        mustEqual(y, x);
434 +        while (!x.isEmpty()) {
435 +            assertFalse(y.isEmpty());
436 +            mustEqual(x.remove(0), y.remove(0));
437 +        }
438 +        assertTrue(y.isEmpty());
439 +    }
440 +
441      /**
442       * tests for setSize()
443       */
444      public void testSetSize() {
445 <        Vector v = new Vector();
445 >        final Vector<Item> v = new Vector<>();
446          for (int n : new int[] { 100, 5, 50 }) {
447              v.setSize(n);
448 <            assertEquals(n, v.size());
448 >            mustEqual(n, v.size());
449 >            assertNull(v.get(0));
450 >            assertNull(v.get(n - 1));
451 >            assertThrows(ArrayIndexOutOfBoundsException.class,
452 >                () -> v.setSize(-1));
453 >            mustEqual(n, v.size());
454              assertNull(v.get(0));
455              assertNull(v.get(n - 1));
456          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines