21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /* |
24 /* |
25 * @test |
25 * @test |
26 * @bug 4904067 |
26 * @bug 4904067 7129185 |
27 * @summary Unit test for Collections.checkedSet |
27 * @summary Unit test for Collections.checkedSet |
28 * @author Josh Bloch |
28 * @author Josh Bloch |
|
29 * @run testng CheckedSetBash |
29 */ |
30 */ |
30 |
31 |
31 import java.util.*; |
32 import java.util.*; |
|
33 import java.util.function.Supplier; |
|
34 import org.testng.annotations.Test; |
|
35 import org.testng.annotations.DataProvider; |
|
36 |
|
37 import static org.testng.Assert.fail; |
|
38 import static org.testng.Assert.assertTrue; |
32 |
39 |
33 public class CheckedSetBash { |
40 public class CheckedSetBash { |
34 static Random rnd = new Random(); |
41 static final int numItr = 100; |
|
42 static final int setSize = 100; |
|
43 static final Random rnd = new Random(); |
35 |
44 |
36 public static void main(String[] args) { |
45 @Test(dataProvider = "Supplier<Set<Integer>>") |
37 int numItr = 100; |
46 public static void testCheckedSet(String description, Supplier<Set<Integer>> supplier) { |
38 int setSize = 100; |
|
39 |
47 |
40 for (int i=0; i<numItr; i++) { |
48 Set<Integer> s1 = supplier.get(); |
41 Set s1 = newSet(); |
49 assertTrue(s1.isEmpty()); |
42 AddRandoms(s1, setSize); |
|
43 |
50 |
44 Set s2 = newSet(); |
51 AddRandoms(s1, setSize); |
45 AddRandoms(s2, setSize); |
|
46 |
52 |
47 Set intersection = clone(s1); |
53 Set<Integer> s2 = supplier.get(); |
48 intersection.retainAll(s2); |
|
49 Set diff1 = clone(s1); diff1.removeAll(s2); |
|
50 Set diff2 = clone(s2); diff2.removeAll(s1); |
|
51 Set union = clone(s1); union.addAll(s2); |
|
52 |
54 |
53 if (diff1.removeAll(diff2)) |
55 assertTrue(s2.isEmpty()); |
54 fail("Set algebra identity 2 failed"); |
|
55 if (diff1.removeAll(intersection)) |
|
56 fail("Set algebra identity 3 failed"); |
|
57 if (diff2.removeAll(diff1)) |
|
58 fail("Set algebra identity 4 failed"); |
|
59 if (diff2.removeAll(intersection)) |
|
60 fail("Set algebra identity 5 failed"); |
|
61 if (intersection.removeAll(diff1)) |
|
62 fail("Set algebra identity 6 failed"); |
|
63 if (intersection.removeAll(diff1)) |
|
64 fail("Set algebra identity 7 failed"); |
|
65 |
56 |
66 intersection.addAll(diff1); intersection.addAll(diff2); |
57 AddRandoms(s2, setSize); |
67 if (!intersection.equals(union)) |
|
68 fail("Set algebra identity 1 failed"); |
|
69 |
58 |
70 if (new HashSet(union).hashCode() != union.hashCode()) |
59 Set<Integer> intersection = clone(s1, supplier); |
71 fail("Incorrect hashCode computation."); |
60 intersection.retainAll(s2); |
|
61 Set<Integer> diff1 = clone(s1, supplier); diff1.removeAll(s2); |
|
62 Set<Integer> diff2 = clone(s2, supplier); diff2.removeAll(s1); |
|
63 Set<Integer> union = clone(s1, supplier); union.addAll(s2); |
72 |
64 |
73 Iterator e = union.iterator(); |
65 if (diff1.removeAll(diff2)) |
74 while (e.hasNext()) |
66 fail("Set algebra identity 2 failed"); |
75 if (!intersection.remove(e.next())) |
67 if (diff1.removeAll(intersection)) |
76 fail("Couldn't remove element from copy."); |
68 fail("Set algebra identity 3 failed"); |
77 if (!intersection.isEmpty()) |
69 if (diff2.removeAll(diff1)) |
78 fail("Copy nonempty after deleting all elements."); |
70 fail("Set algebra identity 4 failed"); |
|
71 if (diff2.removeAll(intersection)) |
|
72 fail("Set algebra identity 5 failed"); |
|
73 if (intersection.removeAll(diff1)) |
|
74 fail("Set algebra identity 6 failed"); |
|
75 if (intersection.removeAll(diff1)) |
|
76 fail("Set algebra identity 7 failed"); |
79 |
77 |
80 e = union.iterator(); |
78 intersection.addAll(diff1); intersection.addAll(diff2); |
81 while (e.hasNext()) { |
79 if (!intersection.equals(union)) |
82 Object o = e.next(); |
80 fail("Set algebra identity 1 failed"); |
83 if (!union.contains(o)) |
|
84 fail("Set doesn't contain one of its elements."); |
|
85 e.remove(); |
|
86 if (union.contains(o)) |
|
87 fail("Set contains element after deletion."); |
|
88 } |
|
89 if (!union.isEmpty()) |
|
90 fail("Set nonempty after deleting all elements."); |
|
91 |
81 |
92 s1.clear(); |
82 if (new HashSet(union).hashCode() != union.hashCode()) |
93 if (!s1.isEmpty()) |
83 fail("Incorrect hashCode computation."); |
94 fail("Set nonempty after clear."); |
84 |
|
85 Iterator e = union.iterator(); |
|
86 while (e.hasNext()) |
|
87 if (!intersection.remove(e.next())) |
|
88 fail("Couldn't remove element from copy."); |
|
89 if (!intersection.isEmpty()) |
|
90 fail("Copy nonempty after deleting all elements."); |
|
91 |
|
92 e = union.iterator(); |
|
93 while (e.hasNext()) { |
|
94 Object o = e.next(); |
|
95 if (!union.contains(o)) |
|
96 fail("Set doesn't contain one of its elements."); |
|
97 e.remove(); |
|
98 if (union.contains(o)) |
|
99 fail("Set contains element after deletion."); |
95 } |
100 } |
|
101 if (!union.isEmpty()) |
|
102 fail("Set nonempty after deleting all elements."); |
|
103 |
|
104 s1.clear(); |
|
105 if (!s1.isEmpty()) |
|
106 fail("Set nonempty after clear."); |
96 } |
107 } |
97 |
108 |
98 // Done inefficiently so as to exercise toArray |
109 // Done inefficiently so as to exercise toArray |
99 static Set clone(Set s) { |
110 static <T> Set<T> clone(Set<T> s, Supplier<Set<T>> supplier) { |
100 Set clone = newSet(); |
111 Set<T> clone = supplier.get(); |
101 List arrayList = Arrays.asList(s.toArray()); |
112 List<T> arrayList = Arrays.asList((T[]) s.toArray()); |
102 clone.addAll(arrayList); |
113 clone.addAll(arrayList); |
103 if (!s.equals(clone)) |
114 if (!s.equals(clone)) |
104 fail("Set not equal to copy."); |
115 fail("Set not equal to copy."); |
105 if (!s.containsAll(clone)) |
116 if (!s.containsAll(clone)) |
106 fail("Set does not contain copy."); |
117 fail("Set does not contain copy."); |
107 if (!clone.containsAll(s)) |
118 if (!clone.containsAll(s)) |
108 fail("Copy does not contain set."); |
119 fail("Copy does not contain set."); |
109 return clone; |
120 return clone; |
110 } |
|
111 |
|
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 } |
121 } |
118 |
122 |
119 static void AddRandoms(Set s, int n) { |
123 static void AddRandoms(Set s, int n) { |
120 for (int i=0; i<n; i++) { |
124 for (int i=0; i<n; i++) { |
121 int r = rnd.nextInt() % n; |
125 int r = rnd.nextInt() % n; |
134 if (!added && preSize != postSize) |
138 if (!added && preSize != postSize) |
135 fail ("Add returned false, but size changed."); |
139 fail ("Add returned false, but size changed."); |
136 } |
140 } |
137 } |
141 } |
138 |
142 |
139 static void fail(String s) { |
143 @DataProvider(name = "Supplier<Set<Integer>>", parallel = true) |
140 throw new RuntimeException(s); |
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 } |
141 |
152 |
|
153 public static Collection<Object[]> makeCheckedSets() { |
|
154 return Arrays.asList( |
|
155 new Object[]{"Collections.checkedSet(HashSet)", |
|
156 (Supplier) () -> {return Collections.checkedSet(new HashSet(), Integer.class);}}, |
|
157 new Object[]{"Collections.checkedSet(TreeSet(reverseOrder)", |
|
158 (Supplier) () -> {return Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class);}}, |
|
159 new Object[]{"Collections.checkedSet(TreeSet).descendingSet()", |
|
160 (Supplier) () -> {return Collections.checkedSet(new TreeSet().descendingSet(), Integer.class);}}, |
|
161 new Object[]{"Collections.checkedNavigableSet(TreeSet)", |
|
162 (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(), Integer.class);}}, |
|
163 new Object[]{"Collections.checkedNavigableSet(TreeSet(reverseOrder)", |
|
164 (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class);}}, |
|
165 new Object[]{"Collections.checkedNavigableSet().descendingSet()", |
|
166 (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class);}} |
|
167 ); |
142 } |
168 } |
143 } |
169 } |