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.6 by jsr166, Sat Mar 11 17:33:32 2017 UTC vs.
Revision 1.7 by jsr166, Tue Apr 3 00:59:22 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines