1 /*
  2  * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug     4904067 8336669
 27  * @summary Unit test for Collections.checkedList
 28  * @author  Josh Bloch
 29  * @key randomness
 30  */
 31 
 32 import java.util.ArrayList;
 33 import java.util.Arrays;
 34 import java.util.Collections;
 35 import java.util.Iterator;
 36 import java.util.List;
 37 import java.util.ListIterator;
 38 import java.util.Random;
 39 
 40 public class CheckedListBash {
 41     static Random rnd = new Random();
 42 
 43     public static void main(String[] args) {
 44         int numItr = 100;
 45         int listSize = 100;
 46 
 47         for (int i=0; i<numItr; i++) {
 48             List s1 = newList();
 49             AddRandoms(s1, listSize);
 50 
 51             List s2 = newList();
 52             AddRandoms(s2, listSize);
 53 
 54             List intersection = clone(s1); intersection.retainAll(s2);
 55             List diff1 = clone(s1); diff1.removeAll(s2);
 56             List diff2 = clone(s2); diff2.removeAll(s1);
 57             List union = clone(s1); union.addAll(s2);
 58 
 59             if (diff1.removeAll(diff2))
 60                 fail("List algebra identity 2 failed");
 61             if (diff1.removeAll(intersection))
 62                 fail("List algebra identity 3 failed");
 63             if (diff2.removeAll(diff1))
 64                 fail("List algebra identity 4 failed");
 65             if (diff2.removeAll(intersection))
 66                 fail("List algebra identity 5 failed");
 67             if (intersection.removeAll(diff1))
 68                 fail("List algebra identity 6 failed");
 69             if (intersection.removeAll(diff1))
 70                 fail("List algebra identity 7 failed");
 71 
 72             intersection.addAll(diff1); intersection.addAll(diff2);
 73             if (!(intersection.containsAll(union) &&
 74                   union.containsAll(intersection)))
 75                 fail("List algebra identity 1 failed");
 76 
 77             Iterator e = union.iterator();
 78             while (e.hasNext())
 79                 intersection.remove(e.next());
 80             if (!intersection.isEmpty())
 81                 fail("Copy nonempty after deleting all elements.");
 82 
 83             e = union.iterator();
 84             while (e.hasNext()) {
 85                 Object o = e.next();
 86                 if (!union.contains(o))
 87                     fail("List doesn't contain one of its elements.");
 88                 e.remove();
 89             }
 90             if (!union.isEmpty())
 91                 fail("List nonempty after deleting all elements.");
 92 
 93             s1.clear();
 94             if (s1.size() != 0)
 95                 fail("Clear didn't reduce size to zero.");
 96 
 97             s1.addAll(0, s2);
 98             if (!(s1.equals(s2) && s2.equals(s1)))
 99                 fail("addAll(int, Collection) doesn't work.");
100             // Reverse List
101             for (int j=0, n=s1.size(); j<n; j++)
102                 s1.set(j, s1.set(n-j-1, s1.get(j)));
103             // Reverse it again
104             for (int j=0, n=s1.size(); j<n; j++)
105                 s1.set(j, s1.set(n-j-1, s1.get(j)));
106             if (!(s1.equals(s2) && s2.equals(s1)))
107                 fail("set(int, Object) doesn't work");
108         }
109 
110         List s = newList();
111         for (int i=0; i<listSize; i++)
112             s.add(i);
113         if (s.size() != listSize)
114             fail("Size of [0..n-1] != n");
115 
116         List even = clone(s);
117         Iterator it = even.iterator();
118         while (it.hasNext())
119             if (((Integer)it.next()).intValue() % 2 == 1)
120                 it.remove();
121         it = even.iterator();
122         while (it.hasNext())
123             if (((Integer)it.next()).intValue() % 2 == 1)
124                 fail("Failed to remove all odd nubmers.");
125 
126         List odd = clone(s);
127         for (int i=0; i<(listSize/2); i++)
128             odd.remove(i);
129         for (int i=0; i<(listSize/2); i++)
130             if (((Integer)odd.get(i)).intValue() % 2 != 1)
131                 fail("Failed to remove all even nubmers.");
132 
133         List all = clone(odd);
134         for (int i=0; i<(listSize/2); i++)
135             all.add(2*i, even.get(i));
136         if (!all.equals(s))
137             fail("Failed to reconstruct ints from odds and evens.");
138 
139         all = clone(odd);
140         ListIterator itAll = all.listIterator(all.size());
141         ListIterator itEven = even.listIterator(even.size());
142         while (itEven.hasPrevious()) {
143             itAll.previous();
144             itAll.add(itEven.previous());
145             itAll.previous(); // ???
146         }
147         itAll = all.listIterator();
148         while (itAll.hasNext()) {
149             Integer i = (Integer)itAll.next();
150             itAll.set(i);
151         }
152         if (!all.equals(s))
153             fail("Failed to reconstruct ints with ListIterator.");
154 
155         it = all.listIterator();
156         int i=0;
157         while (it.hasNext()) {
158             Object o = it.next();
159             if (all.indexOf(o) != all.lastIndexOf(o))
160                 fail("Apparent duplicate detected.");
161             if (all.subList(i,   all.size()).indexOf(o) != 0 ||
162                 all.subList(i+1, all.size()).indexOf(o) != -1)
163                 fail("subList/indexOf is screwy.");
164             if (all.subList(0,i+1).lastIndexOf(o) != i)
165                 fail("subList/lastIndexOf is screwy.");
166             i++;
167         }
168 
169         List l = newList();
170         AddRandoms(l, listSize);
171         Integer[] ia = (Integer[]) l.toArray(new Integer[0]);
172         if (!l.equals(Arrays.asList(ia)))
173             fail("toArray(Object[]) is hosed (1)");
174         ia = new Integer[listSize];
175         Integer[] ib = (Integer[]) l.toArray(ia);
176         if (ia != ib || !l.equals(Arrays.asList(ia)))
177             fail("toArray(Object[]) is hosed (2)");
178         ia = new Integer[listSize+1];
179         ia[listSize] = 69;
180         ib = (Integer[]) l.toArray(ia);
181         if (ia != ib || ia[listSize] != null
182             || !l.equals(Arrays.asList(ia).subList(0, listSize)))
183             fail("toArray(Object[]) is hosed (3)");
184 
185     }
186 
187     // Done inefficiently so as to exercise toArray
188     static List clone(List s) {
189         List a = Arrays.asList(s.toArray());
190         if (s.hashCode() != a.hashCode())
191             fail("Incorrect hashCode computation.");
192 
193         List clone = newList();
194         clone.addAll(a);
195         if (!s.equals(clone))
196             fail("List not equal to copy.");
197         if (!s.containsAll(clone))
198             fail("List does not contain copy.");
199         if (!clone.containsAll(s))
200             fail("Copy does not contain list.");
201 
202         return clone;
203     }
204 
205     static List newList() {
206         List s =  Collections.checkedList(new ArrayList(), Integer.class);
207         if (!s.isEmpty())
208             fail("New instance non empty.");
209         return s;
210     }
211 
212     static void AddRandoms(List s, int n) {
213         for (int i = 0; i < n; i++) {
214             Integer e = rnd.nextInt(n);
215 
216             int preSize = s.size();
217             if (!s.add(e))
218                 fail("Add failed.");
219             int postSize = s.size();
220             if (postSize - preSize != 1)
221                 fail("Add didn't increase size by 1.");
222         }
223     }
224 
225     static void fail(String s) {
226         throw new RuntimeException(s);
227     }
228 }