Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/CheckedSetBash.java
38812 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 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
int r = rnd.nextInt() % n;
127
Integer e = new Integer(r < 0 ? -r : r);
128
129
int preSize = s.size();
130
boolean prePresent = s.contains(e);
131
boolean added = s.add(e);
132
if (!s.contains(e))
133
fail ("Element not present after addition.");
134
if (added == prePresent)
135
fail ("added == alreadyPresent");
136
int postSize = s.size();
137
if (added && preSize == postSize)
138
fail ("Add returned true, but size didn't change.");
139
if (!added && preSize != postSize)
140
fail ("Add returned false, but size changed.");
141
}
142
}
143
144
@DataProvider(name = "Supplier<Set<Integer>>", parallel = true)
145
public static Iterator<Object[]> navigableSetsProvider() {
146
ArrayList<Object[]> iters = new ArrayList<>(makeCheckedSets());
147
iters.ensureCapacity(numItr * iters.size());
148
for(int each=1; each < numItr; each++) {
149
iters.addAll( makeCheckedSets());
150
}
151
return iters.iterator();
152
}
153
154
public static Collection<Object[]> makeCheckedSets() {
155
return Arrays.asList(
156
new Object[]{"Collections.checkedSet(HashSet)",
157
(Supplier) () -> {return Collections.checkedSet(new HashSet(), Integer.class);}},
158
new Object[]{"Collections.checkedSet(TreeSet(reverseOrder)",
159
(Supplier) () -> {return Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},
160
new Object[]{"Collections.checkedSet(TreeSet).descendingSet()",
161
(Supplier) () -> {return Collections.checkedSet(new TreeSet().descendingSet(), Integer.class);}},
162
new Object[]{"Collections.checkedNavigableSet(TreeSet)",
163
(Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(), Integer.class);}},
164
new Object[]{"Collections.checkedNavigableSet(TreeSet(reverseOrder)",
165
(Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},
166
new Object[]{"Collections.checkedNavigableSet().descendingSet()",
167
(Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class);}}
168
);
169
}
170
}
171
172