ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/CheckedSetBash.java
(Generate patch)

Comparing jsr166/src/test/jtreg/util/Collections/CheckedSetBash.java (file contents):
Revision 1.1 by jsr166, Tue Sep 1 01:24:16 2009 UTC vs.
Revision 1.8 by jsr166, Sat Oct 21 00:59:54 2017 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
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
# Line 16 | Line 16
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 < * CA 95054 USA or visit www.sun.com if you need additional information or
21 < * have any questions.
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
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 java.util.*;
34 + import java.util.function.Supplier;
35 + import org.testng.annotations.Test;
36 + import org.testng.annotations.DataProvider;
37  
38 < public class CheckedSetBash {
39 <    static Random rnd = new Random();
38 > import static org.testng.Assert.fail;
39 > import static org.testng.Assert.assertTrue;
40  
41 <    public static void main(String[] args) {
42 <        int numItr = 100;
43 <        int setSize = 100;
44 <
45 <        for (int i=0; i<numItr; i++) {
46 <            Set s1 = newSet();
47 <            AddRandoms(s1, setSize);
48 <
49 <            Set s2 = newSet();
50 <            AddRandoms(s2, setSize);
51 <
52 <            Set intersection = clone(s1);
53 <            intersection.retainAll(s2);
54 <            Set diff1 = clone(s1); diff1.removeAll(s2);
55 <            Set diff2 = clone(s2); diff2.removeAll(s1);
56 <            Set union = clone(s1); union.addAll(s2);
57 <
58 <            if (diff1.removeAll(diff2))
59 <                fail("Set algebra identity 2 failed");
60 <            if (diff1.removeAll(intersection))
61 <                fail("Set algebra identity 3 failed");
62 <            if (diff2.removeAll(diff1))
63 <                fail("Set algebra identity 4 failed");
64 <            if (diff2.removeAll(intersection))
65 <                fail("Set algebra identity 5 failed");
66 <            if (intersection.removeAll(diff1))
67 <                fail("Set algebra identity 6 failed");
68 <            if (intersection.removeAll(diff1))
69 <                fail("Set algebra identity 7 failed");
70 <
71 <            intersection.addAll(diff1); intersection.addAll(diff2);
72 <            if (!intersection.equals(union))
73 <                fail("Set algebra identity 1 failed");
74 <
75 <            if (new HashSet(union).hashCode() != union.hashCode())
76 <                fail("Incorrect hashCode computation.");
77 <
78 <            Iterator e = union.iterator();
79 <            while (e.hasNext())
80 <                if (!intersection.remove(e.next()))
81 <                    fail("Couldn't remove element from copy.");
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("Set doesn't contain one of its elements.");
90 <                e.remove();
91 <                if (union.contains(o))
92 <                    fail("Set contains element after deletion.");
93 <            }
94 <            if (!union.isEmpty())
95 <                fail("Set nonempty after deleting all elements.");
96 <
97 <            s1.clear();
98 <            if (!s1.isEmpty())
99 <                fail("Set nonempty after clear.");
41 > public class CheckedSetBash {
42 >    static final int numItr = 100;
43 >    static final int setSize = 100;
44 >    static final Random rnd = new Random();
45 >
46 >    @Test(dataProvider = "Supplier<Set<Integer>>")
47 >    public static void testCheckedSet(String description, Supplier<Set<Integer>> supplier) {
48 >
49 >        Set<Integer> s1 = supplier.get();
50 >        assertTrue(s1.isEmpty());
51 >
52 >        AddRandoms(s1, setSize);
53 >
54 >        Set<Integer> s2 = supplier.get();
55 >
56 >        assertTrue(s2.isEmpty());
57 >
58 >        AddRandoms(s2, setSize);
59 >
60 >        Set<Integer> intersection = clone(s1, supplier);
61 >        intersection.retainAll(s2);
62 >        Set<Integer> diff1 = clone(s1, supplier); diff1.removeAll(s2);
63 >        Set<Integer> diff2 = clone(s2, supplier); diff2.removeAll(s1);
64 >        Set<Integer> union = clone(s1, supplier); union.addAll(s2);
65 >
66 >        if (diff1.removeAll(diff2))
67 >            fail("Set algebra identity 2 failed");
68 >        if (diff1.removeAll(intersection))
69 >            fail("Set algebra identity 3 failed");
70 >        if (diff2.removeAll(diff1))
71 >            fail("Set algebra identity 4 failed");
72 >        if (diff2.removeAll(intersection))
73 >            fail("Set algebra identity 5 failed");
74 >        if (intersection.removeAll(diff1))
75 >            fail("Set algebra identity 6 failed");
76 >        if (intersection.removeAll(diff1))
77 >            fail("Set algebra identity 7 failed");
78 >
79 >        intersection.addAll(diff1); intersection.addAll(diff2);
80 >        if (!intersection.equals(union))
81 >            fail("Set algebra identity 1 failed");
82 >
83 >        if (new HashSet(union).hashCode() != union.hashCode())
84 >            fail("Incorrect hashCode computation.");
85 >
86 >        Iterator e = union.iterator();
87 >        while (e.hasNext())
88 >            if (!intersection.remove(e.next()))
89 >                fail("Couldn't remove element from copy.");
90 >        if (!intersection.isEmpty())
91 >            fail("Copy nonempty after deleting all elements.");
92 >
93 >        e = union.iterator();
94 >        while (e.hasNext()) {
95 >            Object o = e.next();
96 >            if (!union.contains(o))
97 >                fail("Set doesn't contain one of its elements.");
98 >            e.remove();
99 >            if (union.contains(o))
100 >                fail("Set contains element after deletion.");
101          }
102 +        if (!union.isEmpty())
103 +            fail("Set nonempty after deleting all elements.");
104 +
105 +        s1.clear();
106 +        if (!s1.isEmpty())
107 +            fail("Set nonempty after clear.");
108      }
109  
110      // Done inefficiently so as to exercise toArray
111 <    static Set clone(Set s) {
112 <        Set clone = newSet();
113 <        List arrayList = Arrays.asList(s.toArray());
111 >    static <T> Set<T> clone(Set<T> s, Supplier<Set<T>> supplier) {
112 >        Set<T> clone = supplier.get();
113 >        List<T> arrayList = Arrays.asList((T[]) s.toArray());
114          clone.addAll(arrayList);
115          if (!s.equals(clone))
116              fail("Set not equal to copy.");
# Line 109 | Line 121 | public class CheckedSetBash {
121          return clone;
122      }
123  
112    static Set newSet() {
113        Set s = Collections.checkedSet(new HashSet(), Integer.class);
114        if (!s.isEmpty())
115            fail("New instance non empty.");
116        return s;
117    }
118
124      static void AddRandoms(Set s, int n) {
125 <        for (int i=0; i<n; i++) {
126 <            int r = rnd.nextInt() % n;
122 <            Integer e = new Integer(r < 0 ? -r : r);
125 >        for (int i = 0; i < n; i++) {
126 >            Integer e = rnd.nextInt(n);
127  
128              int preSize = s.size();
129              boolean prePresent = s.contains(e);
130              boolean added = s.add(e);
131              if (!s.contains(e))
132 <                fail ("Element not present after addition.");
132 >                fail("Element not present after addition.");
133              if (added == prePresent)
134 <                fail ("added == alreadyPresent");
134 >                fail("added == alreadyPresent");
135              int postSize = s.size();
136              if (added && preSize == postSize)
137 <                fail ("Add returned true, but size didn't change.");
137 >                fail("Add returned true, but size didn't change.");
138              if (!added && preSize != postSize)
139 <                fail ("Add returned false, but size changed.");
139 >                fail("Add returned false, but size changed.");
140          }
141      }
142  
143 <    static void fail(String s) {
144 <        throw new RuntimeException(s);
143 >    @DataProvider(name = "Supplier<Set<Integer>>", parallel = true)
144 >    public static Iterator<Object[]> navigableSetsProvider() {
145 >        ArrayList<Object[]> iters = new ArrayList<>(makeCheckedSets());
146 >        iters.ensureCapacity(numItr * iters.size());
147 >        for (int each=1; each < numItr; each++) {
148 >            iters.addAll(makeCheckedSets());
149 >        }
150 >        return iters.iterator();
151 >    }
152  
153 +    public static Collection<Object[]> makeCheckedSets() {
154 +        Object[][] params = {
155 +            {"Collections.checkedSet(HashSet)",
156 +             (Supplier) () -> Collections.checkedSet(new HashSet(), Integer.class)},
157 +            {"Collections.checkedSet(TreeSet(reverseOrder))",
158 +             (Supplier) () -> Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class)},
159 +            {"Collections.checkedSet(TreeSet.descendingSet())",
160 +             (Supplier) () -> Collections.checkedSet(new TreeSet().descendingSet(), Integer.class)},
161 +            {"Collections.checkedNavigableSet(TreeSet)",
162 +             (Supplier) () -> Collections.checkedNavigableSet(new TreeSet(), Integer.class)},
163 +            {"Collections.checkedNavigableSet(TreeSet(reverseOrder))",
164 +             (Supplier) () -> Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class)},
165 +            {"Collections.checkedNavigableSet(TreeSet.descendingSet())",
166 +             (Supplier) () -> Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class)},
167 +        };
168 +        return Arrays.asList(params);
169      }
170   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines