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