1 |
|
/* |
2 |
< |
* Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
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 |
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
20 |
< |
* CA 95054 USA or visit www.sun.com if you need additional information or |
21 |
< |
* have any questions. |
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 |
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 |
< |
public class CheckedSetBash { |
39 |
< |
static Random rnd = new Random(); |
38 |
> |
import static org.testng.Assert.fail; |
39 |
> |
import static org.testng.Assert.assertTrue; |
40 |
|
|
41 |
< |
public static void main(String[] args) { |
42 |
< |
int numItr = 100; |
43 |
< |
int setSize = 100; |
44 |
< |
|
45 |
< |
for (int i=0; i<numItr; i++) { |
46 |
< |
Set s1 = newSet(); |
47 |
< |
AddRandoms(s1, setSize); |
48 |
< |
|
49 |
< |
Set s2 = newSet(); |
50 |
< |
AddRandoms(s2, setSize); |
51 |
< |
|
52 |
< |
Set intersection = clone(s1); |
53 |
< |
intersection.retainAll(s2); |
54 |
< |
Set diff1 = clone(s1); diff1.removeAll(s2); |
55 |
< |
Set diff2 = clone(s2); diff2.removeAll(s1); |
56 |
< |
Set union = clone(s1); union.addAll(s2); |
57 |
< |
|
58 |
< |
if (diff1.removeAll(diff2)) |
59 |
< |
fail("Set algebra identity 2 failed"); |
60 |
< |
if (diff1.removeAll(intersection)) |
61 |
< |
fail("Set algebra identity 3 failed"); |
62 |
< |
if (diff2.removeAll(diff1)) |
63 |
< |
fail("Set algebra identity 4 failed"); |
64 |
< |
if (diff2.removeAll(intersection)) |
65 |
< |
fail("Set algebra identity 5 failed"); |
66 |
< |
if (intersection.removeAll(diff1)) |
67 |
< |
fail("Set algebra identity 6 failed"); |
68 |
< |
if (intersection.removeAll(diff1)) |
69 |
< |
fail("Set algebra identity 7 failed"); |
70 |
< |
|
71 |
< |
intersection.addAll(diff1); intersection.addAll(diff2); |
72 |
< |
if (!intersection.equals(union)) |
73 |
< |
fail("Set algebra identity 1 failed"); |
74 |
< |
|
75 |
< |
if (new HashSet(union).hashCode() != union.hashCode()) |
76 |
< |
fail("Incorrect hashCode computation."); |
77 |
< |
|
78 |
< |
Iterator e = union.iterator(); |
79 |
< |
while (e.hasNext()) |
80 |
< |
if (!intersection.remove(e.next())) |
81 |
< |
fail("Couldn't remove element from copy."); |
82 |
< |
if (!intersection.isEmpty()) |
83 |
< |
fail("Copy nonempty after deleting all elements."); |
84 |
< |
|
85 |
< |
e = union.iterator(); |
86 |
< |
while (e.hasNext()) { |
87 |
< |
Object o = e.next(); |
88 |
< |
if (!union.contains(o)) |
89 |
< |
fail("Set doesn't contain one of its elements."); |
90 |
< |
e.remove(); |
91 |
< |
if (union.contains(o)) |
92 |
< |
fail("Set contains element after deletion."); |
93 |
< |
} |
94 |
< |
if (!union.isEmpty()) |
95 |
< |
fail("Set nonempty after deleting all elements."); |
96 |
< |
|
97 |
< |
s1.clear(); |
98 |
< |
if (!s1.isEmpty()) |
99 |
< |
fail("Set nonempty after clear."); |
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 Set clone(Set s) { |
112 |
< |
Set clone = newSet(); |
113 |
< |
List arrayList = Arrays.asList(s.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."); |
121 |
|
return clone; |
122 |
|
} |
123 |
|
|
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 |
– |
|
124 |
|
static void AddRandoms(Set s, int n) { |
125 |
|
for (int i=0; i<n; i++) { |
126 |
|
int r = rnd.nextInt() % n; |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
< |
static void fail(String s) { |
145 |
< |
throw new RuntimeException(s); |
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 |
|
} |