1 /*
2  * Copyright (c) 2003, 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
27  * @summary Unit test for Collections.checkedList
28  * @author  Josh Bloch
29  * @key randomness
30  */
31 
32 package test.java.util.Collections;
33 
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.ListIterator;
40 import java.util.Random;
41 
42 public class CheckedListBash {
43     static Random rnd = new Random();
44 
main(String[] args)45     public static void main(String[] args) {
46         int numItr = 100;
47         int listSize = 100;
48 
49         for (int i=0; i<numItr; i++) {
50             List s1 = newList();
51             AddRandoms(s1, listSize);
52 
53             List s2 = newList();
54             AddRandoms(s2, listSize);
55 
56             List intersection = clone(s1); intersection.retainAll(s2);
57             List diff1 = clone(s1); diff1.removeAll(s2);
58             List diff2 = clone(s2); diff2.removeAll(s1);
59             List union = clone(s1); union.addAll(s2);
60 
61             if (diff1.removeAll(diff2))
62                 fail("List algebra identity 2 failed");
63             if (diff1.removeAll(intersection))
64                 fail("List algebra identity 3 failed");
65             if (diff2.removeAll(diff1))
66                 fail("List algebra identity 4 failed");
67             if (diff2.removeAll(intersection))
68                 fail("List algebra identity 5 failed");
69             if (intersection.removeAll(diff1))
70                 fail("List algebra identity 6 failed");
71             if (intersection.removeAll(diff1))
72                 fail("List algebra identity 7 failed");
73 
74             intersection.addAll(diff1); intersection.addAll(diff2);
75             if (!(intersection.containsAll(union) &&
76                   union.containsAll(intersection)))
77                 fail("List algebra identity 1 failed");
78 
79             Iterator e = union.iterator();
80             while (e.hasNext())
81                 intersection.remove(e.next());
82             if (!intersection.isEmpty())
83                 fail("Copy nonempty after deleting all elements.");
84 
85             e = union.iterator();
86             while (e.hasNext()) {
87                 Object o = e.next();
88                 if (!union.contains(o))
89                     fail("List doesn't contain one of its elements.");
90                 e.remove();
91             }
92             if (!union.isEmpty())
93                 fail("List nonempty after deleting all elements.");
94 
95             s1.clear();
96             if (s1.size() != 0)
97                 fail("Clear didn't reduce size to zero.");
98 
99             s1.addAll(0, s2);
100             if (!(s1.equals(s2) && s2.equals(s1)))
101                 fail("addAll(int, Collection) doesn't work.");
102             // Reverse List
103             for (int j=0, n=s1.size(); j<n; j++)
104                 s1.set(j, s1.set(n-j-1, s1.get(j)));
105             // Reverse it again
106             for (int j=0, n=s1.size(); j<n; j++)
107                 s1.set(j, s1.set(n-j-1, s1.get(j)));
108             if (!(s1.equals(s2) && s2.equals(s1)))
109                 fail("set(int, Object) doesn't work");
110         }
111 
112         List s = newList();
113         for (int i=0; i<listSize; i++)
114             s.add(new Integer(i));
115         if (s.size() != listSize)
116             fail("Size of [0..n-1] != n");
117 
118         List even = clone(s);
119         Iterator it = even.iterator();
120         while (it.hasNext())
121             if (((Integer)it.next()).intValue() % 2 == 1)
122                 it.remove();
123         it = even.iterator();
124         while (it.hasNext())
125             if (((Integer)it.next()).intValue() % 2 == 1)
126                 fail("Failed to remove all odd nubmers.");
127 
128         List odd = clone(s);
129         for (int i=0; i<(listSize/2); i++)
130             odd.remove(i);
131         for (int i=0; i<(listSize/2); i++)
132             if (((Integer)odd.get(i)).intValue() % 2 != 1)
133                 fail("Failed to remove all even nubmers.");
134 
135         List all = clone(odd);
136         for (int i=0; i<(listSize/2); i++)
137             all.add(2*i, even.get(i));
138         if (!all.equals(s))
139             fail("Failed to reconstruct ints from odds and evens.");
140 
141         all = clone(odd);
142         ListIterator itAll = all.listIterator(all.size());
143         ListIterator itEven = even.listIterator(even.size());
144         while (itEven.hasPrevious()) {
145             itAll.previous();
146             itAll.add(itEven.previous());
147             itAll.previous(); // ???
148         }
149         itAll = all.listIterator();
150         while (itAll.hasNext()) {
151             Integer i = (Integer)itAll.next();
152             itAll.set(new Integer(i.intValue()));
153         }
154         itAll = all.listIterator();
155         it = s.iterator();
156         while (it.hasNext())
157             if (it.next()==itAll.next())
158                 fail("Iterator.set failed to change value.");
159         if (!all.equals(s))
160             fail("Failed to reconstruct ints with ListIterator.");
161 
162         it = all.listIterator();
163         int i=0;
164         while (it.hasNext()) {
165             Object o = it.next();
166             if (all.indexOf(o) != all.lastIndexOf(o))
167                 fail("Apparent duplicate detected.");
168             if (all.subList(i,   all.size()).indexOf(o) != 0 ||
169                 all.subList(i+1, all.size()).indexOf(o) != -1)
170                 fail("subList/indexOf is screwy.");
171             if (all.subList(0,i+1).lastIndexOf(o) != i)
172                 fail("subList/lastIndexOf is screwy.");
173             i++;
174         }
175 
176         List l = newList();
177         AddRandoms(l, listSize);
178         Integer[] ia = (Integer[]) l.toArray(new Integer[0]);
179         if (!l.equals(Arrays.asList(ia)))
180             fail("toArray(Object[]) is hosed (1)");
181         ia = new Integer[listSize];
182         Integer[] ib = (Integer[]) l.toArray(ia);
183         if (ia != ib || !l.equals(Arrays.asList(ia)))
184             fail("toArray(Object[]) is hosed (2)");
185         ia = new Integer[listSize+1];
186         ia[listSize] = new Integer(69);
187         ib = (Integer[]) l.toArray(ia);
188         if (ia != ib || ia[listSize] != null
189             || !l.equals(Arrays.asList(ia).subList(0, listSize)))
190             fail("toArray(Object[]) is hosed (3)");
191 
192     }
193 
194     // Done inefficiently so as to exercise toArray
clone(List s)195     static List clone(List s) {
196         List a = Arrays.asList(s.toArray());
197         if (s.hashCode() != a.hashCode())
198             fail("Incorrect hashCode computation.");
199 
200         List clone = newList();
201         clone.addAll(a);
202         if (!s.equals(clone))
203             fail("List not equal to copy.");
204         if (!s.containsAll(clone))
205             fail("List does not contain copy.");
206         if (!clone.containsAll(s))
207             fail("Copy does not contain list.");
208 
209         return clone;
210     }
211 
newList()212     static List newList() {
213         List s =  Collections.checkedList(new ArrayList(), Integer.class);
214         if (!s.isEmpty())
215             fail("New instance non empty.");
216         return s;
217     }
218 
AddRandoms(List s, int n)219     static void AddRandoms(List s, int n) {
220         for (int i = 0; i < n; i++) {
221             Integer e = rnd.nextInt(n);
222 
223             int preSize = s.size();
224             if (!s.add(e))
225                 fail("Add failed.");
226             int postSize = s.size();
227             if (postSize - preSize != 1)
228                 fail("Add didn't increase size by 1.");
229         }
230     }
231 
fail(String s)232     static void fail(String s) {
233         throw new RuntimeException(s);
234     }
235 }
236