author | duke |
Wed, 05 Jul 2017 21:44:52 +0200 | |
changeset 38481 | c81b0662f43a |
parent 34347 | 4a17f9e90a0f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
18818
diff
changeset
|
2 |
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
26 |
* @bug 4904067 7129185 |
2 | 27 |
* @summary Unit test for Collections.checkedSet |
28 |
* @author Josh Bloch |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
29 |
* @run testng CheckedSetBash |
30046 | 30 |
* @key randomness |
2 | 31 |
*/ |
32 |
||
33 |
import java.util.*; |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
34 |
import java.util.function.Supplier; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
35 |
import org.testng.annotations.Test; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
36 |
import org.testng.annotations.DataProvider; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
37 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
38 |
import static org.testng.Assert.fail; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
39 |
import static org.testng.Assert.assertTrue; |
2 | 40 |
|
41 |
public class CheckedSetBash { |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
42 |
static final int numItr = 100; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
43 |
static final int setSize = 100; |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
44 |
static final Random rnd = new Random(); |
2 | 45 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
46 |
@Test(dataProvider = "Supplier<Set<Integer>>") |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
47 |
public static void testCheckedSet(String description, Supplier<Set<Integer>> supplier) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
48 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
49 |
Set<Integer> s1 = supplier.get(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
50 |
assertTrue(s1.isEmpty()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
51 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
52 |
AddRandoms(s1, setSize); |
2 | 53 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
54 |
Set<Integer> s2 = supplier.get(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
55 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
56 |
assertTrue(s2.isEmpty()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
57 |
|
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
58 |
AddRandoms(s2, setSize); |
2 | 59 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
60 |
Set<Integer> intersection = clone(s1, supplier); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
61 |
intersection.retainAll(s2); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
62 |
Set<Integer> diff1 = clone(s1, supplier); diff1.removeAll(s2); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
63 |
Set<Integer> diff2 = clone(s2, supplier); diff2.removeAll(s1); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
64 |
Set<Integer> union = clone(s1, supplier); union.addAll(s2); |
2 | 65 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
66 |
if (diff1.removeAll(diff2)) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
67 |
fail("Set algebra identity 2 failed"); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
68 |
if (diff1.removeAll(intersection)) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
69 |
fail("Set algebra identity 3 failed"); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
70 |
if (diff2.removeAll(diff1)) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
71 |
fail("Set algebra identity 4 failed"); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
72 |
if (diff2.removeAll(intersection)) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
73 |
fail("Set algebra identity 5 failed"); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
74 |
if (intersection.removeAll(diff1)) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
75 |
fail("Set algebra identity 6 failed"); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
76 |
if (intersection.removeAll(diff1)) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
77 |
fail("Set algebra identity 7 failed"); |
2 | 78 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
79 |
intersection.addAll(diff1); intersection.addAll(diff2); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
80 |
if (!intersection.equals(union)) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
81 |
fail("Set algebra identity 1 failed"); |
2 | 82 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
83 |
if (new HashSet(union).hashCode() != union.hashCode()) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
84 |
fail("Incorrect hashCode computation."); |
2 | 85 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
86 |
Iterator e = union.iterator(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
87 |
while (e.hasNext()) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
88 |
if (!intersection.remove(e.next())) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
89 |
fail("Couldn't remove element from copy."); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
90 |
if (!intersection.isEmpty()) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
91 |
fail("Copy nonempty after deleting all elements."); |
2 | 92 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
93 |
e = union.iterator(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
94 |
while (e.hasNext()) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
95 |
Object o = e.next(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
96 |
if (!union.contains(o)) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
97 |
fail("Set doesn't contain one of its elements."); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
98 |
e.remove(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
99 |
if (union.contains(o)) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
100 |
fail("Set contains element after deletion."); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
101 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
102 |
if (!union.isEmpty()) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
103 |
fail("Set nonempty after deleting all elements."); |
2 | 104 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
105 |
s1.clear(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
106 |
if (!s1.isEmpty()) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
107 |
fail("Set nonempty after clear."); |
2 | 108 |
} |
109 |
||
110 |
// Done inefficiently so as to exercise toArray |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
111 |
static <T> Set<T> clone(Set<T> s, Supplier<Set<T>> supplier) { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
112 |
Set<T> clone = supplier.get(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
113 |
List<T> arrayList = Arrays.asList((T[]) s.toArray()); |
2 | 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)) |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30046
diff
changeset
|
133 |
fail("Element not present after addition."); |
2 | 134 |
if (added == prePresent) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30046
diff
changeset
|
135 |
fail("added == alreadyPresent"); |
2 | 136 |
int postSize = s.size(); |
137 |
if (added && preSize == postSize) |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30046
diff
changeset
|
138 |
fail("Add returned true, but size didn't change."); |
2 | 139 |
if (!added && preSize != postSize) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30046
diff
changeset
|
140 |
fail("Add returned false, but size changed."); |
2 | 141 |
} |
142 |
} |
|
143 |
||
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
144 |
@DataProvider(name = "Supplier<Set<Integer>>", parallel = true) |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
145 |
public static Iterator<Object[]> navigableSetsProvider() { |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
146 |
ArrayList<Object[]> iters = new ArrayList<>(makeCheckedSets()); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
147 |
iters.ensureCapacity(numItr * iters.size()); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
30046
diff
changeset
|
148 |
for (int each=1; each < numItr; each++) { |
34347 | 149 |
iters.addAll(makeCheckedSets()); |
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
150 |
} |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
151 |
return iters.iterator(); |
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
152 |
} |
2 | 153 |
|
18818
a9ceff754226
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents:
5506
diff
changeset
|
154 |
public static Collection<Object[]> makeCheckedSets() { |
34347 | 155 |
Object[][] params = { |
156 |
{"Collections.checkedSet(HashSet)", |
|
157 |
(Supplier) () -> Collections.checkedSet(new HashSet(), Integer.class)}, |
|
158 |
{"Collections.checkedSet(TreeSet(reverseOrder))", |
|
159 |
(Supplier) () -> Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class)}, |
|
160 |
{"Collections.checkedSet(TreeSet.descendingSet())", |
|
161 |
(Supplier) () -> Collections.checkedSet(new TreeSet().descendingSet(), Integer.class)}, |
|
162 |
{"Collections.checkedNavigableSet(TreeSet)", |
|
163 |
(Supplier) () -> Collections.checkedNavigableSet(new TreeSet(), Integer.class)}, |
|
164 |
{"Collections.checkedNavigableSet(TreeSet(reverseOrder))", |
|
165 |
(Supplier) () -> Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class)}, |
|
166 |
{"Collections.checkedNavigableSet(TreeSet.descendingSet())", |
|
167 |
(Supplier) () -> Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class)}, |
|
168 |
}; |
|
169 |
return Arrays.asList(params); |
|
2 | 170 |
} |
171 |
} |