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 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();
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(new Integer(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(new Integer(i.intValue()));
151 }
152 itAll = all.listIterator();
153 it = s.iterator();
154 while (it.hasNext())
155 if (it.next()==itAll.next())
156 fail("Iterator.set failed to change value.");
157 if (!all.equals(s))
158 fail("Failed to reconstruct ints with ListIterator.");
159
160 it = all.listIterator();
161 int i=0;
162 while (it.hasNext()) {
163 Object o = it.next();
164 if (all.indexOf(o) != all.lastIndexOf(o))
165 fail("Apparent duplicate detected.");
166 if (all.subList(i, all.size()).indexOf(o) != 0 ||
167 all.subList(i+1, all.size()).indexOf(o) != -1)
168 fail("subList/indexOf is screwy.");
169 if (all.subList(0,i+1).lastIndexOf(o) != i)
170 fail("subList/lastIndexOf is screwy.");
171 i++;
172 }
173
174 List l = newList();
175 AddRandoms(l, listSize);
176 Integer[] ia = (Integer[]) l.toArray(new Integer[0]);
177 if (!l.equals(Arrays.asList(ia)))
178 fail("toArray(Object[]) is hosed (1)");
179 ia = new Integer[listSize];
180 Integer[] ib = (Integer[]) l.toArray(ia);
181 if (ia != ib || !l.equals(Arrays.asList(ia)))
182 fail("toArray(Object[]) is hosed (2)");
183 ia = new Integer[listSize+1];
184 ia[listSize] = new Integer(69);
185 ib = (Integer[]) l.toArray(ia);
186 if (ia != ib || ia[listSize] != null
187 || !l.equals(Arrays.asList(ia).subList(0, listSize)))
188 fail("toArray(Object[]) is hosed (3)");
189
190 }
191
192 // Done inefficiently so as to exercise toArray
193 static List clone(List s) {
194 List a = Arrays.asList(s.toArray());
195 if (s.hashCode() != a.hashCode())
196 fail("Incorrect hashCode computation.");
197
198 List clone = newList();
199 clone.addAll(a);
200 if (!s.equals(clone))
201 fail("List not equal to copy.");
202 if (!s.containsAll(clone))
203 fail("List does not contain copy.");
204 if (!clone.containsAll(s))
205 fail("Copy does not contain list.");
206
207 return clone;
208 }
209
213 fail("New instance non empty.");
214 return s;
215 }
216
217 static void AddRandoms(List s, int n) {
218 for (int i = 0; i < n; i++) {
219 Integer e = rnd.nextInt(n);
220
221 int preSize = s.size();
222 if (!s.add(e))
223 fail("Add failed.");
224 int postSize = s.size();
225 if (postSize - preSize != 1)
226 fail("Add didn't increase size by 1.");
227 }
228 }
229
230 static void fail(String s) {
231 throw new RuntimeException(s);
232 }
233 }
|
1 /*
2 * Copyright (c) 2003, 2026, 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 * @library /test/lib
31 */
32
33 import jdk.test.lib.valueclass.VClass;
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
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();
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(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(i);
153 }
154 if (!all.equals(s))
155 fail("Failed to reconstruct ints with ListIterator.");
156
157 it = all.listIterator();
158 int i=0;
159 while (it.hasNext()) {
160 Object o = it.next();
161 if (all.indexOf(o) != all.lastIndexOf(o))
162 fail("Apparent duplicate detected.");
163 if (all.subList(i, all.size()).indexOf(o) != 0 ||
164 all.subList(i+1, all.size()).indexOf(o) != -1)
165 fail("subList/indexOf is screwy.");
166 if (all.subList(0,i+1).lastIndexOf(o) != i)
167 fail("subList/lastIndexOf is screwy.");
168 i++;
169 }
170
171 List l = newList();
172 AddRandoms(l, listSize);
173 Integer[] ia = (Integer[]) l.toArray(new Integer[0]);
174 if (!l.equals(Arrays.asList(ia)))
175 fail("toArray(Object[]) is hosed (1)");
176 ia = new Integer[listSize];
177 Integer[] ib = (Integer[]) l.toArray(ia);
178 if (ia != ib || !l.equals(Arrays.asList(ia)))
179 fail("toArray(Object[]) is hosed (2)");
180 ia = new Integer[listSize+1];
181 ia[listSize] = 69;
182 ib = (Integer[]) l.toArray(ia);
183 if (ia != ib || ia[listSize] != null
184 || !l.equals(Arrays.asList(ia).subList(0, listSize)))
185 fail("toArray(Object[]) is hosed (3)");
186
187 testValueCheckedList();
188 }
189
190 // Done inefficiently so as to exercise toArray
191 static List clone(List s) {
192 List a = Arrays.asList(s.toArray());
193 if (s.hashCode() != a.hashCode())
194 fail("Incorrect hashCode computation.");
195
196 List clone = newList();
197 clone.addAll(a);
198 if (!s.equals(clone))
199 fail("List not equal to copy.");
200 if (!s.containsAll(clone))
201 fail("List does not contain copy.");
202 if (!clone.containsAll(s))
203 fail("Copy does not contain list.");
204
205 return clone;
206 }
207
211 fail("New instance non empty.");
212 return s;
213 }
214
215 static void AddRandoms(List s, int n) {
216 for (int i = 0; i < n; i++) {
217 Integer e = rnd.nextInt(n);
218
219 int preSize = s.size();
220 if (!s.add(e))
221 fail("Add failed.");
222 int postSize = s.size();
223 if (postSize - preSize != 1)
224 fail("Add didn't increase size by 1.");
225 }
226 }
227
228 static void fail(String s) {
229 throw new RuntimeException(s);
230 }
231
232 static void testValueCheckedList() {
233 List<VClass> list = Collections.checkedList(new ArrayList<>(), VClass.class);
234 list.add(new VClass(1, new int[] { 1 }));
235 list.add(new VClass(2, new int[] { 2 }));
236 if (!list.contains(new VClass(1, new int[] { 1 })) || list.indexOf(new VClass(2, new int[] { 2 })) != 1)
237 fail("value checkedList lookup failed");
238 VClass[] a = list.toArray(new VClass[0]);
239 if (!Arrays.asList(a).equals(list))
240 fail("value checkedList toArray failed");
241 try {
242 ((List) list).add("not a Tuple");
243 fail("value checkedList accepted wrong type");
244 } catch (ClassCastException expected) { }
245 }
246 }
|