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;
 55     static final int setSize = 100;
 56     static final Random rnd = new Random();
 57 
 58     @Test(dataProvider = "Supplier<Set<Integer>>")
 59     public static void testCheckedSet(String description, Supplier<Set<Integer>> supplier) {
 60 
 61         Set<Integer> s1 = supplier.get();
 62         assertTrue(s1.isEmpty());
 63 
 64         AddRandoms(s1, setSize);
 65 
 66         Set<Integer> s2 = supplier.get();
 67 
 68         assertTrue(s2.isEmpty());
 69 
 70         AddRandoms(s2, setSize);
 71 
 72         Set<Integer> intersection = clone(s1, supplier);
 73         intersection.retainAll(s2);
 74         Set<Integer> diff1 = clone(s1, supplier); diff1.removeAll(s2);
 75         Set<Integer> diff2 = clone(s2, supplier); diff2.removeAll(s1);
 76         Set<Integer> union = clone(s1, supplier); union.addAll(s2);
 77 
 78         if (diff1.removeAll(diff2))
 79             fail("Set algebra identity 2 failed");
 80         if (diff1.removeAll(intersection))
 81             fail("Set algebra identity 3 failed");
 82         if (diff2.removeAll(diff1))
 83             fail("Set algebra identity 4 failed");
 84         if (diff2.removeAll(intersection))
 85             fail("Set algebra identity 5 failed");
 86         if (intersection.removeAll(diff1))
 87             fail("Set algebra identity 6 failed");
 88         if (intersection.removeAll(diff1))
 89             fail("Set algebra identity 7 failed");
 90 
 91         intersection.addAll(diff1); intersection.addAll(diff2);
 92         if (!intersection.equals(union))
 93             fail("Set algebra identity 1 failed");
 94 
 95         if (new HashSet(union).hashCode() != union.hashCode())
 96             fail("Incorrect hashCode computation.");
 97 
 98         Iterator e = union.iterator();
 99         while (e.hasNext())
100             if (!intersection.remove(e.next()))
101                 fail("Couldn't remove element from copy.");
102         if (!intersection.isEmpty())
103             fail("Copy nonempty after deleting all elements.");
104 
105         e = union.iterator();
106         while (e.hasNext()) {
107             Object o = e.next();
108             if (!union.contains(o))
109                 fail("Set doesn't contain one of its elements.");
110             e.remove();
111             if (union.contains(o))
112                 fail("Set contains element after deletion.");
113         }
114         if (!union.isEmpty())
115             fail("Set nonempty after deleting all elements.");
116 
117         s1.clear();
118         if (!s1.isEmpty())
119             fail("Set nonempty after clear.");
120     }
121 
122     // Done inefficiently so as to exercise toArray
123     static <T> Set<T> clone(Set<T> s, Supplier<Set<T>> supplier) {
124         Set<T> clone = supplier.get();
125         List<T> arrayList = Arrays.asList((T[]) s.toArray());
126         clone.addAll(arrayList);
127         if (!s.equals(clone))
128             fail("Set not equal to copy.");
129         if (!s.containsAll(clone))
130             fail("Set does not contain copy.");
131         if (!clone.containsAll(s))
132             fail("Copy does not contain set.");
133         return clone;
134     }
135 
136     static void AddRandoms(Set s, int n) {
137         for (int i = 0; i < n; i++) {
138             Integer e = rnd.nextInt(n);
139 
140             int preSize = s.size();
141             boolean prePresent = s.contains(e);
142             boolean added = s.add(e);
143             if (!s.contains(e))
144                 fail("Element not present after addition.");
145             if (added == prePresent)
146                 fail("added == alreadyPresent");
147             int postSize = s.size();
148             if (added && preSize == postSize)
149                 fail("Add returned true, but size didn't change.");
150             if (!added && preSize != postSize)
151                 fail("Add returned false, but size changed.");
152         }
153     }
154 
155     @DataProvider(name = "Supplier<Set<Integer>>", parallel = true)
156     public static Iterator<Object[]> navigableSetsProvider() {
157         ArrayList<Object[]> iters = new ArrayList<>(makeCheckedSets());
158         iters.ensureCapacity(numItr * iters.size());
159         for (int each=1; each < numItr; each++) {
160             iters.addAll(makeCheckedSets());
161         }
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 }