1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package libcore.java.util;
18
19import java.util.ArrayList;
20import java.util.Arrays;
21import java.util.Collection;
22import java.util.ConcurrentModificationException;
23import java.util.Iterator;
24import java.util.List;
25import java.util.ListIterator;
26
27import static junit.framework.Assert.*;
28
29/**
30 * This class contains general purpose test cases for the {@code forEachRemaining} method inherited
31 * from {@code Iterator}.
32 */
33public class ForEachRemainingTester {
34
35    @SuppressWarnings("unchecked")
36    public static <T> void runTests(Class<?> collectionClazz, T[] initialData)
37            throws Exception {
38        test_forEachRemaining((Collection<T>) collectionClazz.newInstance(), initialData);
39        test_forEachRemaining_NPE((Collection<T>) collectionClazz.newInstance(), initialData);
40        test_forEachRemaining_CME((Collection<T>) collectionClazz.newInstance(), initialData);
41
42        if (List.class.isAssignableFrom(collectionClazz)) {
43            List<T> asList = (List<T>) collectionClazz.newInstance();
44            test_forEachRemaining_list(asList, initialData);
45            test_forEachRemaining_NPE_list(asList, initialData);
46            test_forEachRemaining_CME_list(asList, initialData);
47        }
48    }
49
50    public static <T> void test_forEachRemaining_list(List<T> collection, T[] initialData)
51            throws Exception {
52        test_forEachRemaining(collection, initialData);
53
54        ArrayList<T> recorder = new ArrayList<>();
55        ListIterator<T> lit = collection.listIterator(1);
56
57        lit.forEachRemaining((T i) -> recorder.add(i));
58        if (initialData.length > 0) {
59            assertEquals(initialData.length - 1, recorder.size());
60            for (int i = 1; i < initialData.length; i++){
61                assertEquals(initialData[i], recorder.get(i - 1));
62            }
63        }
64    }
65
66    public static <T> void test_forEachRemaining(Collection<T> collection, T[] initialData)
67            throws Exception {
68        collection.addAll(Arrays.asList(initialData));
69
70        ArrayList<T> recorder = new ArrayList<>();
71        collection.iterator().forEachRemaining((T i) -> recorder.add(i));
72        // Note that the collection may not override equals and hashCode.
73        assertEquals(new ArrayList<T>(collection), recorder);
74
75        recorder.clear();
76        Iterator<T> it = collection.iterator();
77        it.next();
78
79        it.forEachRemaining((T i) -> recorder.add(i));
80        if (initialData.length > 0) {
81            assertEquals(initialData.length - 1, recorder.size());
82            for (int i = 1; i < initialData.length; i++){
83                assertEquals(initialData[i], recorder.get(i - 1));
84            }
85        }
86    }
87
88    public static <T> void test_forEachRemaining_NPE(Collection<T> collection, T[] initialData)
89            throws Exception {
90        try {
91            collection.iterator().forEachRemaining(null);
92            fail();
93        } catch (NullPointerException expected) {
94        }
95    }
96
97    public static <T> void test_forEachRemaining_CME(Collection<T> collection, T[] initialData)
98            throws Exception {
99        collection.add(initialData[0]);
100        try {
101            collection.iterator().forEachRemaining(i -> collection.add(i));
102            fail();
103        } catch (ConcurrentModificationException expected) {
104        }
105    }
106
107    public static <T> void test_forEachRemaining_NPE_list(Collection<T> collection, T[] initialData)
108            throws Exception {
109        try {
110            collection.iterator().forEachRemaining(null);
111            fail();
112        } catch (NullPointerException expected) {
113        }
114    }
115
116    public static <T> void test_forEachRemaining_CME_list(Collection<T> collection, T[] initialData)
117            throws Exception {
118        collection.add(initialData[0]);
119        try {
120            collection.iterator().forEachRemaining(i -> collection.add(i));
121            fail();
122        } catch (ConcurrentModificationException expected) {
123        }
124    }
125}
126