1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.util;
19
20import dalvik.annotation.TestTargetNew;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24
25import java.util.AbstractList;
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.Iterator;
29import java.util.LinkedList;
30import java.util.List;
31import java.util.ListIterator;
32import java.util.RandomAccess;
33
34@TestTargetClass(AbstractList.class)
35public class AbstractListTest extends junit.framework.TestCase {
36
37    static class SimpleList extends AbstractList {
38        ArrayList arrayList;
39
40        SimpleList() {
41            this.arrayList = new ArrayList();
42        }
43
44        public Object get(int index) {
45            return this.arrayList.get(index);
46        }
47
48        public void add(int i, Object o) {
49            this.arrayList.add(i, o);
50        }
51
52        public Object remove(int i) {
53            return this.arrayList.remove(i);
54        }
55
56        public int size() {
57            return this.arrayList.size();
58        }
59    }
60
61    /**
62     * @tests java.util.AbstractList#hashCode()
63     */
64    @TestTargetNew(
65        level = TestLevel.COMPLETE,
66        notes = "",
67        method = "hashCode",
68        args = {}
69    )
70    public void test_hashCode() {
71
72        List list = new ArrayList();
73        list.add(new Integer(3));
74        list.add(new Integer(15));
75        list.add(new Integer(5));
76        list.add(new Integer(1));
77        list.add(new Integer(7));
78        int hashCode = 1;
79        Iterator i = list.iterator();
80        while (i.hasNext()) {
81            Object obj = i.next();
82            hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
83        }
84        assertTrue("Incorrect hashCode returned.  Wanted: " + hashCode
85                + " got: " + list.hashCode(), hashCode == list.hashCode());
86    }
87
88    /**
89     * @tests java.util.AbstractList#iterator()
90     */
91    @TestTargetNew(
92        level = TestLevel.COMPLETE,
93        notes = "",
94        method = "iterator",
95        args = {}
96    )
97    public void test_iterator() {
98        SimpleList list = new SimpleList();
99        list.add(new Object());
100        list.add(new Object());
101        Iterator it = list.iterator();
102        it.next();
103        it.remove();
104        it.next();
105    }
106
107    /**
108     * @tests java.util.AbstractList#listIterator()
109     */
110    @TestTargetNew(
111        level = TestLevel.COMPLETE,
112        notes = "",
113        method = "listIterator",
114        args = {}
115    )
116    public void test_listIterator() {
117        Integer tempValue;
118        List list = new ArrayList();
119        list.add(new Integer(3));
120        list.add(new Integer(15));
121        list.add(new Integer(5));
122        list.add(new Integer(1));
123        list.add(new Integer(7));
124        ListIterator lit = list.listIterator();
125        assertTrue("Should not have previous", !lit.hasPrevious());
126        assertTrue("Should have next", lit.hasNext());
127        tempValue = (Integer) lit.next();
128        assertTrue("next returned wrong value.  Wanted 3, got: " + tempValue,
129                tempValue.intValue() == 3);
130        tempValue = (Integer) lit.previous();
131
132        SimpleList list2 = new SimpleList();
133        list2.add(new Object());
134        ListIterator lit2 = list2.listIterator();
135        lit2.add(new Object());
136        lit2.next();
137    }
138
139    /**
140     * @tests java.util.AbstractList#subList(int, int)
141     */
142    @TestTargetNew(
143        level = TestLevel.PARTIAL_COMPLETE,
144        notes = "Verifies each of the SubList operations to ensure a ConcurrentModificationException does not occur on an AbstractList which does not update modCount.",
145        method = "subList",
146        args = {int.class, int.class}
147    )
148    public void test_subListII() {
149        // Test each of the SubList operations to ensure a
150        // ConcurrentModificationException does not occur on an AbstractList
151        // which does not update modCount
152        SimpleList mList = new SimpleList();
153        mList.add(new Object());
154        mList.add(new Object());
155        List sList = mList.subList(0, 2);
156        sList.add(new Object()); // calls add(int, Object)
157        sList.get(0);
158
159        sList.add(0, new Object());
160        sList.get(0);
161
162        sList.addAll(Arrays.asList(new String[] { "1", "2" }));
163        sList.get(0);
164
165        sList.addAll(0, Arrays.asList(new String[] { "3", "4" }));
166        sList.get(0);
167
168        sList.remove(0);
169        sList.get(0);
170
171        ListIterator lit = sList.listIterator();
172        lit.add(new Object());
173        lit.next();
174        lit.remove();
175        lit.next();
176
177        sList.clear(); // calls removeRange()
178        sList.add(new Object());
179
180        // test the type of sublist that is returned
181        List al = new ArrayList();
182        for (int i = 0; i < 10; i++) {
183            al.add(new Integer(i));
184        }
185        assertTrue(
186                "Sublist returned should have implemented Random Access interface",
187                al.subList(3, 7) instanceof RandomAccess);
188
189        List ll = new LinkedList();
190        for (int i = 0; i < 10; i++) {
191            ll.add(new Integer(i));
192        }
193        assertTrue(
194                "Sublist returned should not have implemented Random Access interface",
195                !(ll.subList(3, 7) instanceof RandomAccess));
196
197        }
198
199    /**
200     * @tests java.util.AbstractList#subList(int, int)
201     */
202    @TestTargetNew(
203        level = TestLevel.PARTIAL_COMPLETE,
204        notes = "Verifies IndexOutOfBoundsException.",
205        method = "subList",
206        args = {int.class, int.class}
207    )
208    public void test_subList_empty() {
209        // Regression for HARMONY-389
210        List al = new ArrayList();
211        al.add("one");
212        List emptySubList = al.subList(0, 0);
213
214        try {
215            emptySubList.get(0);
216            fail("emptySubList.get(0) should throw IndexOutOfBoundsException");
217        } catch (IndexOutOfBoundsException e) {
218            // expected
219        }
220
221        try {
222            emptySubList.set(0, "one");
223            fail("emptySubList.set(0,Object) should throw IndexOutOfBoundsException");
224        } catch (IndexOutOfBoundsException e) {
225            // expected
226        }
227
228        try {
229            emptySubList.remove(0);
230            fail("emptySubList.remove(0) should throw IndexOutOfBoundsException");
231        } catch (IndexOutOfBoundsException e) {
232            // expected
233        }
234    }
235
236    /**
237     * @tests java.util.AbstractList#subList(int, int)
238     */
239    @TestTargetNew(
240        level = TestLevel.PARTIAL_COMPLETE,
241        notes = "Doesn't verify IndexOutOfBoundsException, IllegalArgumentException.",
242        method = "subList",
243        args = {int.class, int.class}
244    )
245    public void test_subList_addAll() {
246        // Regression for HARMONY-390
247        List mainList = new ArrayList();
248        Object[] mainObjects = { "a", "b", "c" };
249        mainList.addAll(Arrays.asList(mainObjects));
250        List subList = mainList.subList(1, 2);
251        assertFalse("subList should not contain \"a\"", subList.contains("a"));
252        assertFalse("subList should not contain \"c\"", subList.contains("c"));
253        assertTrue("subList should contain \"b\"", subList.contains("b"));
254
255        Object[] subObjects = { "one", "two", "three" };
256        subList.addAll(Arrays.asList(subObjects));
257        assertFalse("subList should not contain \"a\"", subList.contains("a"));
258        assertFalse("subList should not contain \"c\"", subList.contains("c"));
259
260        Object[] expected = { "b", "one", "two", "three" };
261        ListIterator iter = subList.listIterator();
262        for (int i = 0; i < expected.length; i++) {
263            assertTrue("subList should contain " + expected[i], subList
264                    .contains(expected[i]));
265            assertTrue("should be more elements", iter.hasNext());
266            assertEquals("element in incorrect position", expected[i], iter
267                    .next());
268        }
269    }
270
271    @TestTargetNew(
272        level = TestLevel.COMPLETE,
273        notes = "",
274        method = "indexOf",
275        args = {java.lang.Object.class}
276    )
277    public void test_indexOfLjava_lang_Object() {
278        AbstractList al = new ArrayList();
279        al.add(0);
280        al.add(1);
281        al.add(2);
282        al.add(3);
283        al.add(4);
284
285        assertEquals(-1, al.indexOf(5));
286        assertEquals(2, al.indexOf(2));
287    }
288
289    @TestTargetNew(
290        level = TestLevel.COMPLETE,
291        notes = "",
292        method = "lastIndexOf",
293        args = {java.lang.Object.class}
294    )
295    public void test_lastIndexOfLjava_lang_Object() {
296        AbstractList al = new ArrayList();
297        al.add(0);
298        al.add(1);
299        al.add(2);
300        al.add(2);
301        al.add(2);
302        al.add(2);
303        al.add(2);
304        al.add(3);
305        al.add(4);
306
307        assertEquals(-1, al.lastIndexOf(5));
308        assertEquals(6, al.lastIndexOf(2));
309    }
310
311    @TestTargetNew(
312        level = TestLevel.COMPLETE,
313        notes = "",
314        method = "listIterator",
315        args = {int.class}
316    )
317    public void test_listIteratorI() {
318        AbstractList al1 = new ArrayList();
319        AbstractList al2 = new ArrayList();
320        al1.add(0);
321        al1.add(1);
322        al1.add(2);
323        al1.add(3);
324        al1.add(4);
325        al2.add(2);
326        al2.add(3);
327        al2.add(4);
328
329        Iterator li1 = al1.listIterator(2);
330        Iterator li2 = al2.listIterator();
331
332        while(li1.hasNext()&&li2.hasNext()) {
333            assertEquals(li1.next(), li2.next());
334        }
335        assertSame(li1.hasNext(),li2.hasNext());
336
337        try {
338            al1.listIterator(-1);
339            fail("IndexOutOfBoundsException expected");
340        } catch (IndexOutOfBoundsException ee) {
341            //expected
342        }
343
344        try {
345            al1.listIterator(al1.size() + 1);
346            fail("IndexOutOfBoundsException expected");
347        } catch (IndexOutOfBoundsException ee) {
348            //expected
349        }
350    }
351
352    protected void doneSuite() {}
353}
354