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

Comparing jsr166/src/test/tck/CopyOnWriteArrayListTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.48 by jsr166, Tue Apr 3 00:58:28 2018 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
9 > import java.util.ArrayList;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Collections;
13 > import java.util.Iterator;
14 > import java.util.List;
15 > import java.util.ListIterator;
16 > import java.util.NoSuchElementException;
17 > import java.util.concurrent.CopyOnWriteArrayList;
18 > import java.util.concurrent.ThreadLocalRandom;
19 >
20 > import junit.framework.Test;
21 >
22 > public class CopyOnWriteArrayListTest extends JSR166TestCase {
23  
12 public class CopyOnWriteArrayListTest extends TestCase{
13    
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run (suite());  
25 >        main(suite(), args);
26      }
27  
28      public static Test suite() {
29 <        return new TestSuite(CopyOnWriteArrayListTest.class);
29 >        class Implementation implements CollectionImplementation {
30 >            public Class<?> klazz() { return CopyOnWriteArrayList.class; }
31 >            public List emptyCollection() { return new CopyOnWriteArrayList(); }
32 >            public Object makeElement(int i) { return i; }
33 >            public boolean isConcurrent() { return true; }
34 >            public boolean permitsNulls() { return true; }
35 >        }
36 >        class SubListImplementation extends Implementation {
37 >            public List emptyCollection() {
38 >                return super.emptyCollection().subList(0, 0);
39 >            }
40 >        }
41 >        return newTestSuite(
42 >                CopyOnWriteArrayListTest.class,
43 >                CollectionTest.testSuite(new Implementation()),
44 >                CollectionTest.testSuite(new SubListImplementation()));
45      }
46  
47 <    static CopyOnWriteArrayList fullArray(int n){
48 <        CopyOnWriteArrayList a = new CopyOnWriteArrayList();
49 <        assertTrue(a.isEmpty());
50 <        for (int i = 0; i < n; ++i)
51 <            a.add(new Integer(i));
52 <        assertFalse(a.isEmpty());
53 <        assertEquals(n, a.size());
54 <        return a;
47 >    static CopyOnWriteArrayList<Integer> populatedList(int n) {
48 >        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
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 <     *  Test to verify addAll correctly adds each element from the given collection
59 <     */
60 <    public void testAddAll(){
61 <        CopyOnWriteArrayList full = fullArray(3);
62 <        Vector v = new Vector();
63 <        v.add(new Integer(3));
64 <        v.add(new Integer(4));
40 <        v.add(new Integer(5));
41 <        full.addAll(v);
42 <        assertEquals(6, full.size());
57 >    static CopyOnWriteArrayList<Integer> populatedList(Integer[] elements) {
58 >        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
59 >        assertTrue(list.isEmpty());
60 >        for (Integer element : elements)
61 >            list.add(element);
62 >        assertFalse(list.isEmpty());
63 >        assertEquals(elements.length, list.size());
64 >        return list;
65      }
66  
67      /**
68 <     *  Test to verify addAllAbsent adds each element from the given collection that did not
47 <     *  already exist in the List
68 >     * a new list is empty
69       */
70 <    public void testAddAllAbsent(){
71 <        CopyOnWriteArrayList full = fullArray(3);
72 <        Vector v = new Vector();
52 <        v.add(new Integer(3));
53 <        v.add(new Integer(4));
54 <        v.add(new Integer(1)); // will not add this element
55 <        full.addAllAbsent(v);
56 <        assertEquals(5, full.size());
70 >    public void testConstructor() {
71 >        List list = new CopyOnWriteArrayList();
72 >        assertTrue(list.isEmpty());
73      }
74  
75      /**
76 <     *  Test to verify addIfAbsent will not add the element if it already exists in the list
76 >     * new list contains all elements of initializing array
77       */
78 <    public void testAddIfAbsent(){
79 <        CopyOnWriteArrayList full = fullArray(3);
80 <        full.addIfAbsent(new Integer(1));
81 <        assertEquals(3, full.size());
78 >    public void testConstructor2() {
79 >        Integer[] elts = new Integer[SIZE];
80 >        for (int i = 0; i < SIZE - 1; ++i)
81 >            elts[i] = i;
82 >        List list = new CopyOnWriteArrayList(elts);
83 >        for (int i = 0; i < SIZE; ++i)
84 >            assertEquals(elts[i], list.get(i));
85      }
86  
87      /**
88 <     *  test to verify addIfAbsent correctly adds the element when it does not exist in the list
88 >     * new list contains all elements of initializing collection
89       */
90 <    public void testAddIfAbsent2(){
91 <        CopyOnWriteArrayList full = fullArray(3);
92 <        full.addIfAbsent(new Integer(3));
93 <        assertTrue(full.contains(new Integer(3)));
90 >    public void testConstructor3() {
91 >        Integer[] elts = new Integer[SIZE];
92 >        for (int i = 0; i < SIZE - 1; ++i)
93 >            elts[i] = i;
94 >        List list = new CopyOnWriteArrayList(Arrays.asList(elts));
95 >        for (int i = 0; i < SIZE; ++i)
96 >            assertEquals(elts[i], list.get(i));
97      }
98  
99      /**
100 <     *  Test to verify clear correctly removes all elements from the list
100 >     * addAll adds each element from the given collection, including duplicates
101       */
102 <    public void testClear(){
103 <        CopyOnWriteArrayList full = fullArray(3);
104 <        full.clear();
105 <        assertEquals(0, full.size());
102 >    public void testAddAll() {
103 >        List list = populatedList(3);
104 >        assertTrue(list.addAll(Arrays.asList(three, four, five)));
105 >        assertEquals(6, list.size());
106 >        assertTrue(list.addAll(Arrays.asList(three, four, five)));
107 >        assertEquals(9, list.size());
108      }
109  
110      /**
111 <     *  Test to verify contains returns the correct values
111 >     * addAllAbsent adds each element from the given collection that did not
112 >     * already exist in the List
113       */
114 <    public void testContains(){
115 <        CopyOnWriteArrayList full = fullArray(3);
116 <        assertTrue(full.contains(new Integer(1)));
117 <        assertFalse(full.contains(new Integer(5)));
114 >    public void testAddAllAbsent() {
115 >        CopyOnWriteArrayList list = populatedList(3);
116 >        // "one" is duplicate and will not be added
117 >        assertEquals(2, list.addAllAbsent(Arrays.asList(three, four, one)));
118 >        assertEquals(5, list.size());
119 >        assertEquals(0, list.addAllAbsent(Arrays.asList(three, four, one)));
120 >        assertEquals(5, list.size());
121      }
122  
95    public void testAddIndex() {
96        CopyOnWriteArrayList full = fullArray(3);
97        full.add(0, new Integer(-1));
98        assertEquals(4, full.size());
99        assertEquals(new Integer(-1), full.get(0));
100        assertEquals(new Integer(0), full.get(1));
101
102        full.add(2, new Integer(-2));
103        assertEquals(5, full.size());
104        assertEquals(new Integer(-2), full.get(2));
105        assertEquals(new Integer(2), full.get(4));
106    }
107
108    public void testEquals() {
109        CopyOnWriteArrayList a = fullArray(3);
110        CopyOnWriteArrayList b = fullArray(3);
111        assertTrue(a.equals(b));
112        assertTrue(b.equals(a));
113        assertEquals(a.hashCode(), b.hashCode());
114        a.add(new Integer(-1));
115        assertFalse(a.equals(b));
116        assertFalse(b.equals(a));
117        b.add(new Integer(-1));
118        assertTrue(a.equals(b));
119        assertTrue(b.equals(a));
120        assertEquals(a.hashCode(), b.hashCode());
121    }
122
123    
123      /**
124 <     *  Test to verify containsAll returns the correct values
124 >     * addIfAbsent will not add the element if it already exists in the list
125       */
126 <    public void testContainsAll(){
127 <        CopyOnWriteArrayList full = fullArray(3);
128 <        Vector v = new Vector();
129 <        v.add(new Integer(1));
131 <        v.add(new Integer(2));
132 <        assertTrue(full.containsAll(v));
133 <        v.add(new Integer(6));
134 <        assertFalse(full.containsAll(v));
126 >    public void testAddIfAbsent() {
127 >        CopyOnWriteArrayList list = populatedList(SIZE);
128 >        list.addIfAbsent(one);
129 >        assertEquals(SIZE, list.size());
130      }
131  
132      /**
133 <     *  Test to verify get returns the correct value for the given index
133 >     * addIfAbsent adds the element when it does not exist in the list
134       */
135 <    public void testGet(){
136 <        CopyOnWriteArrayList full = fullArray(3);
137 <        assertEquals(0, ((Integer)full.get(0)).intValue());
135 >    public void testAddIfAbsent2() {
136 >        CopyOnWriteArrayList list = populatedList(SIZE);
137 >        list.addIfAbsent(three);
138 >        assertTrue(list.contains(three));
139      }
140  
141      /**
142 <     *  Test to verify indexOf gives the correct index for the given object
142 >     * clear removes all elements from the list
143       */
144 <    public void testIndexOf(){
145 <        CopyOnWriteArrayList full = fullArray(3);
146 <        assertEquals(1, full.indexOf(new Integer(1)));
147 <        assertEquals(-1, full.indexOf("puppies"));
144 >    public void testClear() {
145 >        List list = populatedList(SIZE);
146 >        list.clear();
147 >        assertEquals(0, list.size());
148      }
149  
150      /**
151 <     *  Test to verify indexOf gives the correct index based on the given index
156 <     *  at which to start searching
151 >     * Cloned list is equal
152       */
153 <    public void testIndexOf2(){
154 <        CopyOnWriteArrayList full = fullArray(3);
155 <        assertEquals(1, full.indexOf(new Integer(1), 0));
156 <        assertEquals(-1, full.indexOf(new Integer(1), 2));
153 >    public void testClone() {
154 >        CopyOnWriteArrayList l1 = populatedList(SIZE);
155 >        CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone());
156 >        assertEquals(l1, l2);
157 >        l1.clear();
158 >        assertFalse(l1.equals(l2));
159      }
160  
161      /**
162 <     *  Test to verify isEmpty returns the correct values
162 >     * contains is true for added elements
163       */
164 <    public void testIsEmpty(){
165 <        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
166 <        CopyOnWriteArrayList full = fullArray(3);
167 <        assertTrue(empty.isEmpty());
171 <        assertFalse(full.isEmpty());
164 >    public void testContains() {
165 >        List list = populatedList(3);
166 >        assertTrue(list.contains(one));
167 >        assertFalse(list.contains(five));
168      }
169  
170      /**
171 <     *  Test to verify iterator() returns an iterator containing the elements of the list
171 >     * adding at an index places it in the indicated index
172       */
173 <    public void testIterator(){
174 <        CopyOnWriteArrayList full = fullArray(3);
175 <        Iterator i = full.iterator();
176 <        int j;
177 <        for(j = 0; i.hasNext(); j++)
178 <            assertEquals(j, ((Integer)i.next()).intValue());
183 <        assertEquals(3, j);
184 <    }
173 >    public void testAddIndex() {
174 >        List list = populatedList(3);
175 >        list.add(0, m1);
176 >        assertEquals(4, list.size());
177 >        assertEquals(m1, list.get(0));
178 >        assertEquals(zero, list.get(1));
179  
180 <    public void testIteratorRemove () {
181 <        CopyOnWriteArrayList full = fullArray(3);
182 <        Iterator it = full.iterator();
183 <        it.next();
190 <        try {
191 <            it.remove();
192 <            fail("should throw");
193 <        }
194 <        catch (UnsupportedOperationException success) {}
180 >        list.add(2, m2);
181 >        assertEquals(5, list.size());
182 >        assertEquals(m2, list.get(2));
183 >        assertEquals(two, list.get(4));
184      }
185  
197    public void testToString(){
198        CopyOnWriteArrayList full = fullArray(3);
199        String s = full.toString();
200        for (int i = 0; i < 3; ++i) {
201            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
202        }
203    }        
204
186      /**
187 <     *  Test to verify lastIndexOf returns the correct index for the given object
187 >     * lists with same elements are equal and have same hashCode
188       */
189 <    public void testLastIndexOf1(){
190 <        CopyOnWriteArrayList full = fullArray(3);
191 <        full.add(new Integer(1));
192 <        full.add(new Integer(3));
193 <        assertEquals(3, full.lastIndexOf(new Integer(1)));
194 <        assertEquals(-1, full.lastIndexOf(new Integer(6)));
195 <    }
189 >    public void testEquals() {
190 >        List a = populatedList(3);
191 >        List b = populatedList(3);
192 >        assertTrue(a.equals(b));
193 >        assertTrue(b.equals(a));
194 >        assertTrue(a.containsAll(b));
195 >        assertTrue(b.containsAll(a));
196 >        assertEquals(a.hashCode(), b.hashCode());
197 >        a.add(m1);
198 >        assertFalse(a.equals(b));
199 >        assertFalse(b.equals(a));
200 >        assertTrue(a.containsAll(b));
201 >        assertFalse(b.containsAll(a));
202 >        b.add(m1);
203 >        assertTrue(a.equals(b));
204 >        assertTrue(b.equals(a));
205 >        assertTrue(a.containsAll(b));
206 >        assertTrue(b.containsAll(a));
207 >        assertEquals(a.hashCode(), b.hashCode());
208  
209 <    /**
217 <     *  Test to verify lastIndexOf returns the correct index from the given starting point
218 <     */
219 <    public void testlastIndexOf2(){
220 <        CopyOnWriteArrayList full = fullArray(3);
221 <        full.add(new Integer(1));
222 <        full.add(new Integer(3));
223 <        assertEquals(3, full.lastIndexOf(new Integer(1), 4));
224 <        assertEquals(-1, full.lastIndexOf(new Integer(3), 3));
209 >        assertFalse(a.equals(null));
210      }
211  
212      /**
213 <     *  Identical to testIterator, except ListInterator has more functionality
213 >     * containsAll returns true for collections with subset of elements
214       */
215 <    public void testListIterator1(){
216 <        CopyOnWriteArrayList full = fullArray(3);
217 <        ListIterator i = full.listIterator();
218 <        int j;
219 <        for(j = 0; i.hasNext(); j++)
220 <            assertEquals(j, ((Integer)i.next()).intValue());
221 <        assertEquals(3, j);
237 <    }
215 >    public void testContainsAll() {
216 >        List list = populatedList(3);
217 >        assertTrue(list.containsAll(Arrays.asList()));
218 >        assertTrue(list.containsAll(Arrays.asList(one)));
219 >        assertTrue(list.containsAll(Arrays.asList(one, two)));
220 >        assertFalse(list.containsAll(Arrays.asList(one, two, six)));
221 >        assertFalse(list.containsAll(Arrays.asList(six)));
222  
223 <    /**
224 <     *  Identical to testIterator and testListIterator1, but only returns those elements
225 <     *  after the given index
226 <     */
243 <    public void testListIterator2(){
244 <        CopyOnWriteArrayList full = fullArray(3);
245 <        ListIterator i = full.listIterator(1);
246 <        int j;
247 <        for(j = 0; i.hasNext(); j++)
248 <            assertEquals(j+1, ((Integer)i.next()).intValue());
249 <        assertEquals(2, j);
223 >        try {
224 >            list.containsAll(null);
225 >            shouldThrow();
226 >        } catch (NullPointerException success) {}
227      }
228  
229      /**
230 <     *  Test to verify remove correctly removes and returns the object at the given index
230 >     * get returns the value at the given index
231       */
232 <    public void testRemove(){
233 <        CopyOnWriteArrayList full = fullArray(3);
234 <        assertEquals(new Integer(2), full.remove(2));
258 <        assertEquals(2, full.size());
232 >    public void testGet() {
233 >        List list = populatedList(3);
234 >        assertEquals(0, list.get(0));
235      }
236  
237      /**
238 <     *  Test to verify removeAll correctly removes all elements from the given collection
238 >     * indexOf(Object) returns the index of the first occurrence of the
239 >     * specified element in this list, or -1 if this list does not
240 >     * contain the element
241       */
242 <    public void testRemoveAll(){
243 <        CopyOnWriteArrayList full = fullArray(3);
244 <        Vector v = new Vector();
245 <        v.add(new Integer(1));
246 <        v.add(new Integer(2));
247 <        full.removeAll(v);
248 <        assertEquals(1, full.size());
249 <    }
242 >    public void testIndexOf() {
243 >        List list = populatedList(3);
244 >        assertEquals(-1, list.indexOf(-42));
245 >        int size = list.size();
246 >        for (int i = 0; i < size; i++) {
247 >            assertEquals(i, list.indexOf(i));
248 >            assertEquals(i, list.subList(0, size).indexOf(i));
249 >            assertEquals(i, list.subList(0, i + 1).indexOf(i));
250 >            assertEquals(-1, list.subList(0, i).indexOf(i));
251 >            assertEquals(0, list.subList(i, size).indexOf(i));
252 >            assertEquals(-1, list.subList(i + 1, size).indexOf(i));
253 >        }
254  
255 <    /**
256 <     *  Test to verify set correctly changes the element at the given index
257 <     */
258 <    public void testSet(){
259 <        CopyOnWriteArrayList full = fullArray(3);
260 <        assertEquals(new Integer(2), full.set(2, new Integer(4)));
261 <        assertEquals(4, ((Integer)full.get(2)).intValue());
255 >        list.add(1);
256 >        assertEquals(1, list.indexOf(1));
257 >        assertEquals(1, list.subList(0, size + 1).indexOf(1));
258 >        assertEquals(0, list.subList(1, size + 1).indexOf(1));
259 >        assertEquals(size - 2, list.subList(2, size + 1).indexOf(1));
260 >        assertEquals(0, list.subList(size, size + 1).indexOf(1));
261 >        assertEquals(-1, list.subList(size + 1, size + 1).indexOf(1));
262      }
263  
264      /**
265 <     *  Test to verify size returns the correct values
266 <     */
267 <    public void testSize(){
268 <        CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
269 <        CopyOnWriteArrayList full = fullArray(3);
270 <        assertEquals(3, full.size());
271 <        assertEquals(0, empty.size());
265 >     * indexOf(E, int) returns the index of the first occurrence of the
266 >     * specified element in this list, searching forwards from index,
267 >     * or returns -1 if the element is not found
268 >     */
269 >    public void testIndexOf2() {
270 >        CopyOnWriteArrayList list = populatedList(3);
271 >        int size = list.size();
272 >        assertEquals(-1, list.indexOf(-42, 0));
273 >
274 >        // we might expect IOOBE, but spec says otherwise
275 >        assertEquals(-1, list.indexOf(0, size));
276 >        assertEquals(-1, list.indexOf(0, Integer.MAX_VALUE));
277 >
278 >        assertThrows(
279 >            IndexOutOfBoundsException.class,
280 >            () -> list.indexOf(0, -1),
281 >            () -> list.indexOf(0, Integer.MIN_VALUE));
282 >
283 >        for (int i = 0; i < size; i++) {
284 >            assertEquals(i, list.indexOf(i, 0));
285 >            assertEquals(i, list.indexOf(i, i));
286 >            assertEquals(-1, list.indexOf(i, i + 1));
287 >        }
288 >
289 >        list.add(1);
290 >        assertEquals(1, list.indexOf(1, 0));
291 >        assertEquals(1, list.indexOf(1, 1));
292 >        assertEquals(size, list.indexOf(1, 2));
293 >        assertEquals(size, list.indexOf(1, size));
294      }
295  
296      /**
297 <     *  Test to verify toArray returns an Object array containing all elements from the list
297 >     * isEmpty returns true when empty, else false
298       */
299 <    public void testToArray(){
300 <        CopyOnWriteArrayList full = fullArray(3);
301 <        Object[] o = full.toArray();
302 <        assertEquals(3, o.length);
303 <        assertEquals(0, ((Integer)o[0]).intValue());
304 <        assertEquals(1, ((Integer)o[1]).intValue());
305 <        assertEquals(2, ((Integer)o[2]).intValue());
299 >    public void testIsEmpty() {
300 >        List empty = new CopyOnWriteArrayList();
301 >        assertTrue(empty.isEmpty());
302 >        assertTrue(empty.subList(0, 0).isEmpty());
303 >
304 >        List full = populatedList(SIZE);
305 >        assertFalse(full.isEmpty());
306 >        assertTrue(full.subList(0, 0).isEmpty());
307 >        assertTrue(full.subList(SIZE, SIZE).isEmpty());
308      }
309  
310      /**
311 <     *  test to verify toArray returns an Integer array containing all elements from the list
311 >     * iterator() returns an iterator containing the elements of the
312 >     * list in insertion order
313       */
314 <    public void testToArray2(){
315 <        CopyOnWriteArrayList full = fullArray(3);
316 <        Integer[] i = new Integer[3];
317 <        i = (Integer[])full.toArray(i);
318 <        assertEquals(3, i.length);
319 <        assertEquals(0, i[0].intValue());
320 <        assertEquals(1, i[1].intValue());
321 <        assertEquals(2, i[2].intValue());
322 <    }
323 <
314 >    public void testIterator() {
315 >        Collection empty = new CopyOnWriteArrayList();
316 >        assertFalse(empty.iterator().hasNext());
317 >        try {
318 >            empty.iterator().next();
319 >            shouldThrow();
320 >        } catch (NoSuchElementException success) {}
321 >
322 >        Integer[] elements = new Integer[SIZE];
323 >        for (int i = 0; i < SIZE; i++)
324 >            elements[i] = i;
325 >        shuffle(elements);
326 >        Collection<Integer> full = populatedList(elements);
327  
328 <    public void testSubList() {
329 <        CopyOnWriteArrayList a = fullArray(10);
330 <        assertTrue(a.subList(1,1).isEmpty());
331 <        for(int j = 0; j < 9; ++j) {
332 <            for(int i = j ; i < 10; ++i) {
333 <                List b = a.subList(j,i);
324 <                for(int k = j; k < i; ++k) {
325 <                    assertEquals(new Integer(k), b.get(k-j));
326 <                }
327 <            }
328 <        }
329 <
330 <        Integer m1 = new Integer(-1);
331 <        List s = a.subList(2, 5);
332 <        assertEquals(s.size(), 3);
333 <        s.set(2, new Integer(m1));
334 <        assertEquals(a.get(4), m1);
335 <        s.clear();
336 <        assertEquals(a.size(), 7);
328 >        Iterator it = full.iterator();
329 >        for (int j = 0; j < SIZE; j++) {
330 >            assertTrue(it.hasNext());
331 >            assertEquals(elements[j], it.next());
332 >        }
333 >        assertIteratorExhausted(it);
334      }
335  
339    // Exception tests
340
336      /**
337 <     *  Test to verify toArray throws an ArrayStoreException when the given array
343 <     *  can not store the objects inside the list
337 >     * iterator of empty collection has no elements
338       */
339 <    public void testToArray_ArrayStoreException(){
340 <        try{
341 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
348 <            c.add("zfasdfsdf");
349 <            c.add("asdadasd");
350 <            c.toArray(new Long[5]);
351 <            fail("Object[] toArray(Object[]) should throw ArrayStoreException");
352 <        }catch(ArrayStoreException e){}
339 >    public void testEmptyIterator() {
340 >        Collection c = new CopyOnWriteArrayList();
341 >        assertIteratorExhausted(c.iterator());
342      }
343  
344      /**
345 <     *  Test to verify get throws an IndexOutOfBoundsException on a negative index
345 >     * iterator.remove throws UnsupportedOperationException
346       */
347 <    public void testGet1_IndexOutOfBoundsException(){
348 <        try{
349 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
350 <            c.get(-1);
351 <            fail("Object get(int) should throw IndexOutOfBounds exception");
352 <        }catch(IndexOutOfBoundsException e){}
347 >    public void testIteratorRemove() {
348 >        CopyOnWriteArrayList list = populatedList(SIZE);
349 >        Iterator it = list.iterator();
350 >        it.next();
351 >        try {
352 >            it.remove();
353 >            shouldThrow();
354 >        } catch (UnsupportedOperationException success) {}
355      }
356 <    
356 >
357      /**
358 <     *  Test to verify get throws an IndexOutOfBoundsException on a too high index
358 >     * toString contains toString of elements
359       */
360 <    public void testGet2_IndexOutOfBoundsException(){
361 <        try{
362 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
363 <            c.add("asdasd");
364 <            c.add("asdad");
365 <            c.get(100);
366 <            fail("Object get(int) should throw IndexOutOfBounds exception");
367 <        }catch(IndexOutOfBoundsException e){}
360 >    public void testToString() {
361 >        assertEquals("[]", new CopyOnWriteArrayList().toString());
362 >        List list = populatedList(3);
363 >        String s = list.toString();
364 >        for (int i = 0; i < 3; ++i)
365 >            assertTrue(s.contains(String.valueOf(i)));
366 >        assertEquals(new ArrayList(list).toString(),
367 >                     list.toString());
368      }
369  
370      /**
371 <     *  Test to verify set throws an IndexOutOfBoundsException on a negative index
372 <     */
373 <    public void testSet1_IndexOutOfBoundsException(){
374 <        try{
375 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
376 <            c.set(-1,"qwerty");
377 <            fail("Object get(int, Object) should throw IndexOutOfBounds exception");
378 <        }catch(IndexOutOfBoundsException e){}
371 >     * lastIndexOf(Object) returns the index of the last occurrence of
372 >     * the specified element in this list, or -1 if this list does not
373 >     * contain the element
374 >     */
375 >    public void testLastIndexOf1() {
376 >        List list = populatedList(3);
377 >        assertEquals(-1, list.lastIndexOf(-42));
378 >        int size = list.size();
379 >        for (int i = 0; i < size; i++) {
380 >            assertEquals(i, list.lastIndexOf(i));
381 >            assertEquals(i, list.subList(0, size).lastIndexOf(i));
382 >            assertEquals(i, list.subList(0, i + 1).lastIndexOf(i));
383 >            assertEquals(-1, list.subList(0, i).lastIndexOf(i));
384 >            assertEquals(0, list.subList(i, size).lastIndexOf(i));
385 >            assertEquals(-1, list.subList(i + 1, size).lastIndexOf(i));
386 >        }
387 >
388 >        list.add(1);
389 >        assertEquals(size, list.lastIndexOf(1));
390 >        assertEquals(size, list.subList(0, size + 1).lastIndexOf(1));
391 >        assertEquals(1, list.subList(0, size).lastIndexOf(1));
392 >        assertEquals(0, list.subList(1, 2).lastIndexOf(1));
393 >        assertEquals(-1, list.subList(0, 1).indexOf(1));
394      }
395 <    
395 >
396      /**
397 <     *  Test to verify set throws an IndexOutOfBoundsException on a too high index
398 <     */
399 <    public void testSet2(){
400 <        try{
401 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
402 <            c.add("asdasd");
403 <            c.add("asdad");
404 <            c.set(100, "qwerty");
405 <            fail("Object set(int, Object) should throw IndexOutOfBounds exception");
406 <        }catch(IndexOutOfBoundsException e){}
397 >     * lastIndexOf(E, int) returns the index of the last occurrence of the
398 >     * specified element in this list, searching backwards from index, or
399 >     * returns -1 if the element is not found
400 >     */
401 >    public void testLastIndexOf2() {
402 >        CopyOnWriteArrayList list = populatedList(3);
403 >
404 >        // we might expect IOOBE, but spec says otherwise
405 >        assertEquals(-1, list.lastIndexOf(0, -1));
406 >
407 >        int size = list.size();
408 >        assertThrows(
409 >            IndexOutOfBoundsException.class,
410 >            () -> list.lastIndexOf(0, size),
411 >            () -> list.lastIndexOf(0, Integer.MAX_VALUE));
412 >
413 >        for (int i = 0; i < size; i++) {
414 >            assertEquals(i, list.lastIndexOf(i, i));
415 >            assertEquals(list.indexOf(i), list.lastIndexOf(i, i));
416 >            if (i > 0)
417 >                assertEquals(-1, list.lastIndexOf(i, i - 1));
418 >        }
419 >        list.add(one);
420 >        list.add(three);
421 >        assertEquals(1, list.lastIndexOf(one, 1));
422 >        assertEquals(1, list.lastIndexOf(one, 2));
423 >        assertEquals(3, list.lastIndexOf(one, 3));
424 >        assertEquals(3, list.lastIndexOf(one, 4));
425 >        assertEquals(-1, list.lastIndexOf(three, 3));
426      }
427  
428      /**
429 <     *  Test to verify add throws an IndexOutOfBoundsException on a negative index
429 >     * listIterator traverses all elements
430       */
431 <    public void testAdd1_IndexOutOfBoundsException(){
432 <        try{
433 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
434 <            c.add(-1,"qwerty");
435 <            fail("void add(int, Object) should throw IndexOutOfBounds exception");
436 <        }catch(IndexOutOfBoundsException e){}
431 >    public void testListIterator1() {
432 >        List list = populatedList(SIZE);
433 >        ListIterator i = list.listIterator();
434 >        int j;
435 >        for (j = 0; i.hasNext(); j++)
436 >            assertEquals(j, i.next());
437 >        assertEquals(SIZE, j);
438      }
439 <    
439 >
440      /**
441 <     *  Test to verify add throws an IndexOutOfBoundsException on a too high index
441 >     * listIterator only returns those elements after the given index
442       */
443 <    public void testAdd2_IndexOutOfBoundsException(){
444 <        try{
445 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
446 <            c.add("asdasd");
447 <            c.add("asdasdasd");
448 <            c.add(100, "qwerty");
449 <            fail("void add(int, Object) should throw IndexOutOfBounds exception");
424 <        }catch(IndexOutOfBoundsException e){}
443 >    public void testListIterator2() {
444 >        List list = populatedList(3);
445 >        ListIterator i = list.listIterator(1);
446 >        int j;
447 >        for (j = 0; i.hasNext(); j++)
448 >            assertEquals(j + 1, i.next());
449 >        assertEquals(2, j);
450      }
451  
452      /**
453 <     *  Test to verify remove throws an IndexOutOfBoundsException on a negative index
453 >     * remove(int) removes and returns the object at the given index
454       */
455 <    public void testRemove1_IndexOutOfBounds(){
456 <        try{
457 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
458 <            c.remove(-1);
459 <            fail("Object remove(int) should throw IndexOutOfBounds exception");
460 <        }catch(IndexOutOfBoundsException e){}
455 >    public void testRemove_int() {
456 >        int SIZE = 3;
457 >        for (int i = 0; i < SIZE; i++) {
458 >            List list = populatedList(SIZE);
459 >            assertEquals(i, list.remove(i));
460 >            assertEquals(SIZE - 1, list.size());
461 >            assertFalse(list.contains(new Integer(i)));
462 >        }
463      }
464  
465      /**
466 <     *  Test to verify remove throws an IndexOutOfBoundsException on a too high index
466 >     * remove(Object) removes the object if found and returns true
467       */
468 <    public void testRemove2_IndexOutOfBounds(){
469 <        try{
470 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
471 <            c.add("asdasd");
472 <            c.add("adasdasd");
473 <            c.remove(100);
474 <            fail("Object remove(int) should throw IndexOutOfBounds exception");
475 <        }catch(IndexOutOfBoundsException e){}
468 >    public void testRemove_Object() {
469 >        int SIZE = 3;
470 >        for (int i = 0; i < SIZE; i++) {
471 >            List list = populatedList(SIZE);
472 >            assertFalse(list.remove(new Integer(-42)));
473 >            assertTrue(list.remove(new Integer(i)));
474 >            assertEquals(SIZE - 1, list.size());
475 >            assertFalse(list.contains(new Integer(i)));
476 >        }
477 >        CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6));
478 >        assertTrue(x.remove(new Integer(6)));
479 >        assertEquals(x, Arrays.asList(4, 5));
480 >        assertTrue(x.remove(new Integer(4)));
481 >        assertEquals(x, Arrays.asList(5));
482 >        assertTrue(x.remove(new Integer(5)));
483 >        assertEquals(x, Arrays.asList());
484 >        assertFalse(x.remove(new Integer(5)));
485      }
486 <    
486 >
487      /**
488 <     *  Test to verify addAll throws an IndexOutOfBoundsException on a negative index
488 >     * removeAll removes all elements from the given collection
489       */
490 <    public void testAddAll1_IndexOutOfBoundsException(){
491 <        try{
492 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
493 <            c.addAll(-1,new LinkedList());
494 <            fail("boolean add(int, Collection) should throw IndexOutOfBounds exception");
495 <        }catch(IndexOutOfBoundsException e){}
490 >    public void testRemoveAll() {
491 >        List list = populatedList(3);
492 >        assertTrue(list.removeAll(Arrays.asList(one, two)));
493 >        assertEquals(1, list.size());
494 >        assertFalse(list.removeAll(Arrays.asList(one, two)));
495 >        assertEquals(1, list.size());
496      }
497 <    
497 >
498      /**
499 <     *  Test to verify addAll throws an IndexOutOfBoundsException on a too high index
499 >     * set changes the element at the given index
500       */
501 <    public void testAddAll2_IndexOutOfBoundsException(){
502 <        try{
503 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
504 <            c.add("asdasd");
469 <            c.add("asdasdasd");
470 <            c.addAll(100, new LinkedList());
471 <            fail("boolean addAll(int, Collection) should throw IndexOutOfBounds exception");
472 <        }catch(IndexOutOfBoundsException e){}
501 >    public void testSet() {
502 >        List list = populatedList(3);
503 >        assertEquals(2, list.set(2, four));
504 >        assertEquals(4, list.get(2));
505      }
506  
507      /**
508 <     *  Test to verify listIterator throws an IndexOutOfBoundsException on a negative index
508 >     * size returns the number of elements
509       */
510 <    public void testListIterator1_IndexOutOfBoundsException(){
511 <        try{
512 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
513 <            c.listIterator(-1);
514 <            fail("ListIterator listIterator(int) should throw IndexOutOfBounds exceptione");
515 <        }catch(IndexOutOfBoundsException e){}
510 >    public void testSize() {
511 >        List empty = new CopyOnWriteArrayList();
512 >        assertEquals(0, empty.size());
513 >        assertEquals(0, empty.subList(0, 0).size());
514 >
515 >        List full = populatedList(SIZE);
516 >        assertEquals(SIZE, full.size());
517 >        assertEquals(0, full.subList(0, 0).size());
518 >        assertEquals(0, full.subList(SIZE, SIZE).size());
519      }
520  
521      /**
522 <     *  Test to verify listIterator throws an IndexOutOfBoundsException on a too high index
522 >     * toArray() returns an Object array containing all elements from
523 >     * the list in insertion order
524       */
525 <    public void testListIterator2_IndexOutOfBoundsException(){
526 <        try{
527 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
528 <            c.add("adasd");
529 <            c.add("asdasdas");
530 <            c.listIterator(100);
531 <            fail("ListIterator listIterator(int) should throw IndexOutOfBounds exception");
532 <        }catch(IndexOutOfBoundsException e){}
525 >    public void testToArray() {
526 >        Object[] a = new CopyOnWriteArrayList().toArray();
527 >        assertTrue(Arrays.equals(new Object[0], a));
528 >        assertSame(Object[].class, a.getClass());
529 >
530 >        Integer[] elements = new Integer[SIZE];
531 >        for (int i = 0; i < SIZE; i++)
532 >            elements[i] = i;
533 >        shuffle(elements);
534 >        Collection<Integer> full = populatedList(elements);
535 >
536 >        assertTrue(Arrays.equals(elements, full.toArray()));
537 >        assertSame(Object[].class, full.toArray().getClass());
538      }
539  
540      /**
541 <     *  Test to verify subList throws an IndexOutOfBoundsException on a negative index
541 >     * toArray(Integer array) returns an Integer array containing all
542 >     * elements from the list in insertion order
543       */
544 <    public void testSubList1_IndexOutOfBoundsException(){
545 <        try{
546 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
547 <            c.subList(-1,100);
544 >    public void testToArray2() {
545 >        Collection empty = new CopyOnWriteArrayList();
546 >        Integer[] a;
547 >
548 >        a = new Integer[0];
549 >        assertSame(a, empty.toArray(a));
550 >
551 >        a = new Integer[SIZE / 2];
552 >        Arrays.fill(a, 42);
553 >        assertSame(a, empty.toArray(a));
554 >        assertNull(a[0]);
555 >        for (int i = 1; i < a.length; i++)
556 >            assertEquals(42, (int) a[i]);
557 >
558 >        Integer[] elements = new Integer[SIZE];
559 >        for (int i = 0; i < SIZE; i++)
560 >            elements[i] = i;
561 >        shuffle(elements);
562 >        Collection<Integer> full = populatedList(elements);
563 >
564 >        Arrays.fill(a, 42);
565 >        assertTrue(Arrays.equals(elements, full.toArray(a)));
566 >        for (int i = 0; i < a.length; i++)
567 >            assertEquals(42, (int) a[i]);
568 >        assertSame(Integer[].class, full.toArray(a).getClass());
569  
570 <            fail("List subList(int, int) should throw IndexOutofBounds exception");
571 <        }catch(IndexOutOfBoundsException e){}
570 >        a = new Integer[SIZE];
571 >        Arrays.fill(a, 42);
572 >        assertSame(a, full.toArray(a));
573 >        assertTrue(Arrays.equals(elements, a));
574 >
575 >        a = new Integer[2 * SIZE];
576 >        Arrays.fill(a, 42);
577 >        assertSame(a, full.toArray(a));
578 >        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
579 >        assertNull(a[SIZE]);
580 >        for (int i = SIZE + 1; i < a.length; i++)
581 >            assertEquals(42, (int) a[i]);
582      }
583  
584      /**
585 <     *  Test to verify subList throws an IndexOutOfBoundsException on a too high index
585 >     * sublists contains elements at indexes offset from their base
586       */
587 <    public void testSubList2_IndexOutOfBoundsException(){
588 <        try{
589 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
590 <            c.add("asdasd");
591 <            c.subList(1,100);
592 <            fail("List subList(int, int) should throw IndexOutofBounds exception");
593 <        }catch(IndexOutOfBoundsException e){}
587 >    public void testSubList() {
588 >        List a = populatedList(10);
589 >        assertTrue(a.subList(1,1).isEmpty());
590 >        for (int j = 0; j < 9; ++j) {
591 >            for (int i = j ; i < 10; ++i) {
592 >                List b = a.subList(j,i);
593 >                for (int k = j; k < i; ++k) {
594 >                    assertEquals(new Integer(k), b.get(k-j));
595 >                }
596 >            }
597 >        }
598 >
599 >        List s = a.subList(2, 5);
600 >        assertEquals(3, s.size());
601 >        s.set(2, m1);
602 >        assertEquals(a.get(4), m1);
603 >        s.clear();
604 >        assertEquals(7, a.size());
605 >
606 >        assertThrows(
607 >            IndexOutOfBoundsException.class,
608 >            () -> s.get(0),
609 >            () -> s.set(0, 42));
610      }
611  
612 +    // Exception tests
613 +
614      /**
615 <     *  Test to verify subList throws IndexOutOfBoundsException when the second index
616 <     *  is lower then the first
615 >     * toArray throws an ArrayStoreException when the given array
616 >     * can not store the objects inside the list
617       */
618 <    public void testSubList3_IndexOutOfBoundsException(){
619 <        try{
620 <            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
621 <            c.subList(3,1);
622 <
623 <            fail("List subList(int, int) should throw IndexOutofBounds exception");
624 <        }catch(IndexOutOfBoundsException e){}
618 >    public void testToArray_ArrayStoreException() {
619 >        List list = new CopyOnWriteArrayList();
620 >        // Integers are not auto-converted to Longs
621 >        list.add(86);
622 >        list.add(99);
623 >        assertThrows(
624 >            ArrayStoreException.class,
625 >            () -> list.toArray(new Long[0]),
626 >            () -> list.toArray(new Long[5]));
627 >    }
628 >
629 >    void testIndexOutOfBoundsException(List list) {
630 >        int size = list.size();
631 >        assertThrows(
632 >            IndexOutOfBoundsException.class,
633 >            () -> list.get(-1),
634 >            () -> list.get(size),
635 >            () -> list.set(-1, "qwerty"),
636 >            () -> list.set(size, "qwerty"),
637 >            () -> list.add(-1, "qwerty"),
638 >            () -> list.add(size + 1, "qwerty"),
639 >            () -> list.remove(-1),
640 >            () -> list.remove(size),
641 >            () -> list.addAll(-1, Collections.emptyList()),
642 >            () -> list.addAll(size + 1, Collections.emptyList()),
643 >            () -> list.listIterator(-1),
644 >            () -> list.listIterator(size + 1),
645 >            () -> list.subList(-1, size),
646 >            () -> list.subList(0, size + 1));
647 >
648 >        // Conversely, operations that must not throw
649 >        list.addAll(0, Collections.emptyList());
650 >        list.addAll(size, Collections.emptyList());
651 >        list.add(0, "qwerty");
652 >        list.add(list.size(), "qwerty");
653 >        list.get(0);
654 >        list.get(list.size() - 1);
655 >        list.set(0, "azerty");
656 >        list.set(list.size() - 1, "azerty");
657 >        list.listIterator(0);
658 >        list.listIterator(list.size());
659 >        list.subList(0, list.size());
660 >        list.remove(list.size() - 1);
661 >    }
662 >
663 >    /**
664 >     * IndexOutOfBoundsException is thrown when specified
665 >     */
666 >    public void testIndexOutOfBoundsException() {
667 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
668 >        List x = populatedList(rnd.nextInt(5));
669 >        testIndexOutOfBoundsException(x);
670 >
671 >        int start = rnd.nextInt(x.size() + 1);
672 >        int end = rnd.nextInt(start, x.size() + 1);
673 >        assertThrows(
674 >            IndexOutOfBoundsException.class,
675 >            () -> x.subList(start, start - 1));
676 >        List subList = x.subList(start, end);
677 >        testIndexOutOfBoundsException(x);
678 >    }
679 >
680 >    /**
681 >     * a deserialized/reserialized list equals original
682 >     */
683 >    public void testSerialization() throws Exception {
684 >        List x = populatedList(SIZE);
685 >        List y = serialClone(x);
686 >
687 >        assertNotSame(x, y);
688 >        assertEquals(x.size(), y.size());
689 >        assertEquals(x.toString(), y.toString());
690 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
691 >        assertEquals(x, y);
692 >        assertEquals(y, x);
693 >        while (!x.isEmpty()) {
694 >            assertFalse(y.isEmpty());
695 >            assertEquals(x.remove(0), y.remove(0));
696 >        }
697 >        assertTrue(y.isEmpty());
698      }
699 <    
699 >
700   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines