jdk/test/java/util/Collections/CheckedSetBash.java
changeset 18818 a9ceff754226
parent 5506 202f599c92aa
child 23010 6dadb192ad81
--- a/jdk/test/java/util/Collections/CheckedSetBash.java	Mon Jul 08 14:05:59 2013 +0200
+++ b/jdk/test/java/util/Collections/CheckedSetBash.java	Fri Jul 12 11:11:30 2013 -0700
@@ -23,82 +23,93 @@
 
 /*
  * @test
- * @bug     4904067
+ * @bug     4904067 7129185
  * @summary Unit test for Collections.checkedSet
  * @author  Josh Bloch
+ * @run testng CheckedSetBash
  */
 
 import java.util.*;
+import java.util.function.Supplier;
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
+
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertTrue;
 
 public class CheckedSetBash {
-    static Random rnd = new Random();
+    static final int numItr = 100;
+    static final int setSize = 100;
+    static final Random rnd = new Random();
 
-    public static void main(String[] args) {
-        int numItr = 100;
-        int setSize = 100;
+    @Test(dataProvider = "Supplier<Set<Integer>>")
+    public static void testCheckedSet(String description, Supplier<Set<Integer>> supplier) {
+
+        Set<Integer> s1 = supplier.get();
+        assertTrue(s1.isEmpty());
+
+        AddRandoms(s1, setSize);
 
-        for (int i=0; i<numItr; i++) {
-            Set s1 = newSet();
-            AddRandoms(s1, setSize);
+        Set<Integer> s2 = supplier.get();
+
+        assertTrue(s2.isEmpty());
+
+        AddRandoms(s2, setSize);
 
-            Set s2 = newSet();
-            AddRandoms(s2, setSize);
-
-            Set intersection = clone(s1);
-            intersection.retainAll(s2);
-            Set diff1 = clone(s1); diff1.removeAll(s2);
-            Set diff2 = clone(s2); diff2.removeAll(s1);
-            Set union = clone(s1); union.addAll(s2);
+        Set<Integer> intersection = clone(s1, supplier);
+        intersection.retainAll(s2);
+        Set<Integer> diff1 = clone(s1, supplier); diff1.removeAll(s2);
+        Set<Integer> diff2 = clone(s2, supplier); diff2.removeAll(s1);
+        Set<Integer> union = clone(s1, supplier); union.addAll(s2);
 
-            if (diff1.removeAll(diff2))
-                fail("Set algebra identity 2 failed");
-            if (diff1.removeAll(intersection))
-                fail("Set algebra identity 3 failed");
-            if (diff2.removeAll(diff1))
-                fail("Set algebra identity 4 failed");
-            if (diff2.removeAll(intersection))
-                fail("Set algebra identity 5 failed");
-            if (intersection.removeAll(diff1))
-                fail("Set algebra identity 6 failed");
-            if (intersection.removeAll(diff1))
-                fail("Set algebra identity 7 failed");
+        if (diff1.removeAll(diff2))
+            fail("Set algebra identity 2 failed");
+        if (diff1.removeAll(intersection))
+            fail("Set algebra identity 3 failed");
+        if (diff2.removeAll(diff1))
+            fail("Set algebra identity 4 failed");
+        if (diff2.removeAll(intersection))
+            fail("Set algebra identity 5 failed");
+        if (intersection.removeAll(diff1))
+            fail("Set algebra identity 6 failed");
+        if (intersection.removeAll(diff1))
+            fail("Set algebra identity 7 failed");
 
-            intersection.addAll(diff1); intersection.addAll(diff2);
-            if (!intersection.equals(union))
-                fail("Set algebra identity 1 failed");
+        intersection.addAll(diff1); intersection.addAll(diff2);
+        if (!intersection.equals(union))
+            fail("Set algebra identity 1 failed");
 
-            if (new HashSet(union).hashCode() != union.hashCode())
-                fail("Incorrect hashCode computation.");
+        if (new HashSet(union).hashCode() != union.hashCode())
+            fail("Incorrect hashCode computation.");
 
-            Iterator e = union.iterator();
-            while (e.hasNext())
-                if (!intersection.remove(e.next()))
-                    fail("Couldn't remove element from copy.");
-            if (!intersection.isEmpty())
-                fail("Copy nonempty after deleting all elements.");
+        Iterator e = union.iterator();
+        while (e.hasNext())
+            if (!intersection.remove(e.next()))
+                fail("Couldn't remove element from copy.");
+        if (!intersection.isEmpty())
+            fail("Copy nonempty after deleting all elements.");
 
-            e = union.iterator();
-            while (e.hasNext()) {
-                Object o = e.next();
-                if (!union.contains(o))
-                    fail("Set doesn't contain one of its elements.");
-                e.remove();
-                if (union.contains(o))
-                    fail("Set contains element after deletion.");
-            }
-            if (!union.isEmpty())
-                fail("Set nonempty after deleting all elements.");
+        e = union.iterator();
+        while (e.hasNext()) {
+            Object o = e.next();
+            if (!union.contains(o))
+                fail("Set doesn't contain one of its elements.");
+            e.remove();
+            if (union.contains(o))
+                fail("Set contains element after deletion.");
+        }
+        if (!union.isEmpty())
+            fail("Set nonempty after deleting all elements.");
 
-            s1.clear();
-            if (!s1.isEmpty())
-                fail("Set nonempty after clear.");
-        }
+        s1.clear();
+        if (!s1.isEmpty())
+            fail("Set nonempty after clear.");
     }
 
     // Done inefficiently so as to exercise toArray
-    static Set clone(Set s) {
-        Set clone = newSet();
-        List arrayList = Arrays.asList(s.toArray());
+    static <T> Set<T> clone(Set<T> s, Supplier<Set<T>> supplier) {
+        Set<T> clone = supplier.get();
+        List<T> arrayList = Arrays.asList((T[]) s.toArray());
         clone.addAll(arrayList);
         if (!s.equals(clone))
             fail("Set not equal to copy.");
@@ -109,13 +120,6 @@
         return clone;
     }
 
-    static Set newSet() {
-        Set s = Collections.checkedSet(new HashSet(), Integer.class);
-        if (!s.isEmpty())
-            fail("New instance non empty.");
-        return s;
-    }
-
     static void AddRandoms(Set s, int n) {
         for (int i=0; i<n; i++) {
             int r = rnd.nextInt() % n;
@@ -136,8 +140,30 @@
         }
     }
 
-    static void fail(String s) {
-        throw new RuntimeException(s);
+    @DataProvider(name = "Supplier<Set<Integer>>", parallel = true)
+    public static Iterator<Object[]> navigableSetsProvider() {
+        ArrayList<Object[]> iters = new ArrayList<>(makeCheckedSets());
+        iters.ensureCapacity(numItr * iters.size());
+        for(int each=1; each < numItr; each++) {
+            iters.addAll( makeCheckedSets());
+        }
+        return iters.iterator();
+    }
 
+    public static Collection<Object[]> makeCheckedSets() {
+        return Arrays.asList(
+            new Object[]{"Collections.checkedSet(HashSet)",
+                (Supplier) () -> {return Collections.checkedSet(new HashSet(), Integer.class);}},
+            new Object[]{"Collections.checkedSet(TreeSet(reverseOrder)",
+                (Supplier) () -> {return Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},
+            new Object[]{"Collections.checkedSet(TreeSet).descendingSet()",
+                (Supplier) () -> {return Collections.checkedSet(new TreeSet().descendingSet(), Integer.class);}},
+            new Object[]{"Collections.checkedNavigableSet(TreeSet)",
+                (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(), Integer.class);}},
+            new Object[]{"Collections.checkedNavigableSet(TreeSet(reverseOrder)",
+                (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},
+            new Object[]{"Collections.checkedNavigableSet().descendingSet()",
+                (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class);}}
+            );
     }
 }