ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/CheckedSetBash.java
Revision: 1.8
Committed: Sat Oct 21 00:59:54 2017 UTC (6 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +2 -3 lines
Log Message:
fix errorprone [RandomModInteger] warnings by using nextInt(n)

File Contents

# Content
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 java.util.*;
34 import java.util.function.Supplier;
35 import org.testng.annotations.Test;
36 import org.testng.annotations.DataProvider;
37
38 import static org.testng.Assert.fail;
39 import static org.testng.Assert.assertTrue;
40
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 <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.");
117 if (!s.containsAll(clone))
118 fail("Set does not contain copy.");
119 if (!clone.containsAll(s))
120 fail("Copy does not contain set.");
121 return clone;
122 }
123
124 static void AddRandoms(Set s, int n) {
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.");
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 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 }