1 /*
2 * Copyright (c) 2003, 2013, 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 7129185
27 * @summary Unit test for Collections.checkedSet
28 * @author Josh Bloch
29 * @run testng CheckedSetBash
30 * @key randomness
31 */
32
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Random;
44 import java.util.Set;
45 import java.util.TreeSet;
46 import java.util.function.Supplier;
47
48 import static org.testng.Assert.assertTrue;
49 import static org.testng.Assert.fail;
50
51 public class CheckedSetBash {
52 static final int numItr = 100;
160 return iters.iterator();
161 }
162
163 public static Collection<Object[]> makeCheckedSets() {
164 Object[][] params = {
165 {"Collections.checkedSet(HashSet)",
166 (Supplier) () -> Collections.checkedSet(new HashSet(), Integer.class)},
167 {"Collections.checkedSet(TreeSet(reverseOrder))",
168 (Supplier) () -> Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class)},
169 {"Collections.checkedSet(TreeSet.descendingSet())",
170 (Supplier) () -> Collections.checkedSet(new TreeSet().descendingSet(), Integer.class)},
171 {"Collections.checkedNavigableSet(TreeSet)",
172 (Supplier) () -> Collections.checkedNavigableSet(new TreeSet(), Integer.class)},
173 {"Collections.checkedNavigableSet(TreeSet(reverseOrder))",
174 (Supplier) () -> Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class)},
175 {"Collections.checkedNavigableSet(TreeSet.descendingSet())",
176 (Supplier) () -> Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class)},
177 };
178 return Arrays.asList(params);
179 }
180 }
|
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 7129185
27 * @summary Unit test for Collections.checkedSet
28 * @author Josh Bloch
29 * @key randomness
30 * @library /test/lib
31 * @run testng CheckedSetBash
32 */
33
34 import jdk.test.lib.valueclass.VClass;
35 import org.testng.annotations.DataProvider;
36 import org.testng.annotations.Test;
37
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collection;
41 import java.util.Collections;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Random;
46 import java.util.Set;
47 import java.util.TreeSet;
48 import java.util.function.Supplier;
49
50 import static org.testng.Assert.assertTrue;
51 import static org.testng.Assert.fail;
52
53 public class CheckedSetBash {
54 static final int numItr = 100;
162 return iters.iterator();
163 }
164
165 public static Collection<Object[]> makeCheckedSets() {
166 Object[][] params = {
167 {"Collections.checkedSet(HashSet)",
168 (Supplier) () -> Collections.checkedSet(new HashSet(), Integer.class)},
169 {"Collections.checkedSet(TreeSet(reverseOrder))",
170 (Supplier) () -> Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class)},
171 {"Collections.checkedSet(TreeSet.descendingSet())",
172 (Supplier) () -> Collections.checkedSet(new TreeSet().descendingSet(), Integer.class)},
173 {"Collections.checkedNavigableSet(TreeSet)",
174 (Supplier) () -> Collections.checkedNavigableSet(new TreeSet(), Integer.class)},
175 {"Collections.checkedNavigableSet(TreeSet(reverseOrder))",
176 (Supplier) () -> Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class)},
177 {"Collections.checkedNavigableSet(TreeSet.descendingSet())",
178 (Supplier) () -> Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class)},
179 };
180 return Arrays.asList(params);
181 }
182
183 @Test
184 public static void testValueCheckedSet() {
185 Set<VClass> s = Collections.checkedSet(new HashSet<>(), VClass.class);
186 s.add(new VClass(1, new int[] { 1 }));
187 s.add(new VClass(2, new int[] { 2 }));
188 if (!s.contains(new VClass(1, new int[] { 1 })))
189 fail("value checkedSet lookup failed");
190 Set<VClass> copy = Collections.checkedSet(new HashSet<>(), VClass.class);
191 copy.addAll(Arrays.asList(s.toArray(new VClass[0])));
192 if (!s.equals(copy))
193 fail("value checkedSet addAll failed");
194 try {
195 ((Set) s).add("not a Tuple");
196 fail("value checkedSet accepted wrong type");
197 } catch (ClassCastException expected) { }
198 }
199 }
|