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.checkedSet
28 * @author Josh Bloch
29 */
30
31 import java.util.*;
32
33 public class CheckedSetBash {
34 static Random rnd = new Random();
35
36 public static void main(String[] args) {
37 int numItr = 100;
38 int setSize = 100;
39
40 for (int i=0; i<numItr; i++) {
41 Set s1 = newSet();
42 AddRandoms(s1, setSize);
43
44 Set s2 = newSet();
45 AddRandoms(s2, setSize);
46
47 Set intersection = clone(s1);
48 intersection.retainAll(s2);
49 Set diff1 = clone(s1); diff1.removeAll(s2);
50 Set diff2 = clone(s2); diff2.removeAll(s1);
51 Set union = clone(s1); union.addAll(s2);
52
53 if (diff1.removeAll(diff2))
54 fail("Set algebra identity 2 failed");
55 if (diff1.removeAll(intersection))
56 fail("Set algebra identity 3 failed");
57 if (diff2.removeAll(diff1))
58 fail("Set algebra identity 4 failed");
59 if (diff2.removeAll(intersection))
60 fail("Set algebra identity 5 failed");
61 if (intersection.removeAll(diff1))
62 fail("Set algebra identity 6 failed");
63 if (intersection.removeAll(diff1))
64 fail("Set algebra identity 7 failed");
65
66 intersection.addAll(diff1); intersection.addAll(diff2);
67 if (!intersection.equals(union))
68 fail("Set algebra identity 1 failed");
69
70 if (new HashSet(union).hashCode() != union.hashCode())
71 fail("Incorrect hashCode computation.");
76 fail("Couldn't remove element from copy.");
77 if (!intersection.isEmpty())
78 fail("Copy nonempty after deleting all elements.");
79
80 e = union.iterator();
81 while (e.hasNext()) {
82 Object o = e.next();
83 if (!union.contains(o))
84 fail("Set doesn't contain one of its elements.");
85 e.remove();
86 if (union.contains(o))
87 fail("Set contains element after deletion.");
88 }
89 if (!union.isEmpty())
90 fail("Set nonempty after deleting all elements.");
91
92 s1.clear();
93 if (!s1.isEmpty())
94 fail("Set nonempty after clear.");
95 }
96 }
97
98 // Done inefficiently so as to exercise toArray
99 static Set clone(Set s) {
100 Set clone = newSet();
101 List arrayList = Arrays.asList(s.toArray());
102 clone.addAll(arrayList);
103 if (!s.equals(clone))
104 fail("Set not equal to copy.");
105 if (!s.containsAll(clone))
106 fail("Set does not contain copy.");
107 if (!clone.containsAll(s))
108 fail("Copy does not contain set.");
109 return clone;
110 }
111
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
119 static void AddRandoms(Set s, int n) {
120 for (int i=0; i<n; i++) {
121 int r = rnd.nextInt() % n;
122 Integer e = new Integer(r < 0 ? -r : r);
123
124 int preSize = s.size();
125 boolean prePresent = s.contains(e);
126 boolean added = s.add(e);
127 if (!s.contains(e))
128 fail ("Element not present after addition.");
129 if (added == prePresent)
130 fail ("added == alreadyPresent");
131 int postSize = s.size();
132 if (added && preSize == postSize)
133 fail ("Add returned true, but size didn't change.");
134 if (!added && preSize != postSize)
135 fail ("Add returned false, but size changed.");
136 }
137 }
138
139 static void fail(String s) {
140 throw new RuntimeException(s);
141
142 }
143 }
|
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 */
31
32 import java.util.*;
33 import java.util.function.Supplier;
34 import org.testng.annotations.Test;
35 import org.testng.annotations.DataProvider;
36
37 import static org.testng.Assert.fail;
38 import static org.testng.Assert.assertTrue;
39
40 public class CheckedSetBash {
41 static final int numItr = 100;
42 static final int setSize = 100;
43 static final Random rnd = new Random();
44
45 @Test(dataProvider = "Supplier<Set<Integer>>")
46 public static void testCheckedSet(String description, Supplier<Set<Integer>> supplier) {
47
48 Set<Integer> s1 = supplier.get();
49 assertTrue(s1.isEmpty());
50
51 AddRandoms(s1, setSize);
52
53 Set<Integer> s2 = supplier.get();
54
55 assertTrue(s2.isEmpty());
56
57 AddRandoms(s2, setSize);
58
59 Set<Integer> intersection = clone(s1, supplier);
60 intersection.retainAll(s2);
61 Set<Integer> diff1 = clone(s1, supplier); diff1.removeAll(s2);
62 Set<Integer> diff2 = clone(s2, supplier); diff2.removeAll(s1);
63 Set<Integer> union = clone(s1, supplier); union.addAll(s2);
64
65 if (diff1.removeAll(diff2))
66 fail("Set algebra identity 2 failed");
67 if (diff1.removeAll(intersection))
68 fail("Set algebra identity 3 failed");
69 if (diff2.removeAll(diff1))
70 fail("Set algebra identity 4 failed");
71 if (diff2.removeAll(intersection))
72 fail("Set algebra identity 5 failed");
73 if (intersection.removeAll(diff1))
74 fail("Set algebra identity 6 failed");
75 if (intersection.removeAll(diff1))
76 fail("Set algebra identity 7 failed");
77
78 intersection.addAll(diff1); intersection.addAll(diff2);
79 if (!intersection.equals(union))
80 fail("Set algebra identity 1 failed");
81
82 if (new HashSet(union).hashCode() != union.hashCode())
83 fail("Incorrect hashCode computation.");
88 fail("Couldn't remove element from copy.");
89 if (!intersection.isEmpty())
90 fail("Copy nonempty after deleting all elements.");
91
92 e = union.iterator();
93 while (e.hasNext()) {
94 Object o = e.next();
95 if (!union.contains(o))
96 fail("Set doesn't contain one of its elements.");
97 e.remove();
98 if (union.contains(o))
99 fail("Set contains element after deletion.");
100 }
101 if (!union.isEmpty())
102 fail("Set nonempty after deleting all elements.");
103
104 s1.clear();
105 if (!s1.isEmpty())
106 fail("Set nonempty after clear.");
107 }
108
109 // Done inefficiently so as to exercise toArray
110 static <T> Set<T> clone(Set<T> s, Supplier<Set<T>> supplier) {
111 Set<T> clone = supplier.get();
112 List<T> arrayList = Arrays.asList((T[]) s.toArray());
113 clone.addAll(arrayList);
114 if (!s.equals(clone))
115 fail("Set not equal to copy.");
116 if (!s.containsAll(clone))
117 fail("Set does not contain copy.");
118 if (!clone.containsAll(s))
119 fail("Copy does not contain set.");
120 return clone;
121 }
122
123 static void AddRandoms(Set s, int n) {
124 for (int i=0; i<n; i++) {
125 int r = rnd.nextInt() % n;
126 Integer e = new Integer(r < 0 ? -r : r);
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.");
133 if (added == prePresent)
134 fail ("added == alreadyPresent");
135 int postSize = s.size();
136 if (added && preSize == postSize)
137 fail ("Add returned true, but size didn't change.");
138 if (!added && preSize != postSize)
139 fail ("Add returned false, but size changed.");
140 }
141 }
142
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 return Arrays.asList(
155 new Object[]{"Collections.checkedSet(HashSet)",
156 (Supplier) () -> {return Collections.checkedSet(new HashSet(), Integer.class);}},
157 new Object[]{"Collections.checkedSet(TreeSet(reverseOrder)",
158 (Supplier) () -> {return Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},
159 new Object[]{"Collections.checkedSet(TreeSet).descendingSet()",
160 (Supplier) () -> {return Collections.checkedSet(new TreeSet().descendingSet(), Integer.class);}},
161 new Object[]{"Collections.checkedNavigableSet(TreeSet)",
162 (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(), Integer.class);}},
163 new Object[]{"Collections.checkedNavigableSet(TreeSet(reverseOrder)",
164 (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},
165 new Object[]{"Collections.checkedNavigableSet().descendingSet()",
166 (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class);}}
167 );
168 }
169 }
|