2
|
1 |
/*
|
|
2 |
* Copyright 2003 Sun Microsystems, Inc. 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 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.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4904067
|
|
27 |
* @summary Unit test for Collections.checkedSet
|
|
28 |
* @author Josh Bloch
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.util.*;
|
|
32 |
|
|
33 |
public class CheckedSetBash {
|
|
34 |
static Random rnd = new Random();
|
|
35 |
|
|
36 |
public static void main(String[] args) {
|
|
37 |
int numItr = 100;
|
|
38 |
int setSize = 100;
|
|
39 |
|
|
40 |
for (int i=0; i<numItr; i++) {
|
|
41 |
Set s1 = newSet();
|
|
42 |
AddRandoms(s1, setSize);
|
|
43 |
|
|
44 |
Set s2 = newSet();
|
|
45 |
AddRandoms(s2, setSize);
|
|
46 |
|
|
47 |
Set intersection = clone(s1);
|
|
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 |
|
|
53 |
if (diff1.removeAll(diff2))
|
|
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 |
|
|
66 |
intersection.addAll(diff1); intersection.addAll(diff2);
|
|
67 |
if (!intersection.equals(union))
|
|
68 |
fail("Set algebra identity 1 failed");
|
|
69 |
|
|
70 |
if (new HashSet(union).hashCode() != union.hashCode())
|
|
71 |
fail("Incorrect hashCode computation.");
|
|
72 |
|
|
73 |
Iterator e = union.iterator();
|
|
74 |
while (e.hasNext())
|
|
75 |
if (!intersection.remove(e.next()))
|
|
76 |
fail("Couldn't remove element from copy.");
|
|
77 |
if (!intersection.isEmpty())
|
|
78 |
fail("Copy nonempty after deleting all elements.");
|
|
79 |
|
|
80 |
e = union.iterator();
|
|
81 |
while (e.hasNext()) {
|
|
82 |
Object o = e.next();
|
|
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 |
|
|
92 |
s1.clear();
|
|
93 |
if (!s1.isEmpty())
|
|
94 |
fail("Set nonempty after clear.");
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
// Done inefficiently so as to exercise toArray
|
|
99 |
static Set clone(Set s) {
|
|
100 |
Set clone = newSet();
|
|
101 |
List arrayList = Arrays.asList(s.toArray());
|
|
102 |
clone.addAll(arrayList);
|
|
103 |
if (!s.equals(clone))
|
|
104 |
fail("Set not equal to copy.");
|
|
105 |
if (!s.containsAll(clone))
|
|
106 |
fail("Set does not contain copy.");
|
|
107 |
if (!clone.containsAll(s))
|
|
108 |
fail("Copy does not contain set.");
|
|
109 |
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 |
}
|
|
118 |
|
|
119 |
static void AddRandoms(Set s, int n) {
|
|
120 |
for (int i=0; i<n; i++) {
|
|
121 |
int r = rnd.nextInt() % n;
|
|
122 |
Integer e = new Integer(r < 0 ? -r : r);
|
|
123 |
|
|
124 |
int preSize = s.size();
|
|
125 |
boolean prePresent = s.contains(e);
|
|
126 |
boolean added = s.add(e);
|
|
127 |
if (!s.contains(e))
|
|
128 |
fail ("Element not present after addition.");
|
|
129 |
if (added == prePresent)
|
|
130 |
fail ("added == alreadyPresent");
|
|
131 |
int postSize = s.size();
|
|
132 |
if (added && preSize == postSize)
|
|
133 |
fail ("Add returned true, but size didn't change.");
|
|
134 |
if (!added && preSize != postSize)
|
|
135 |
fail ("Add returned false, but size changed.");
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
static void fail(String s) {
|
|
140 |
throw new RuntimeException(s);
|
|
141 |
|
|
142 |
}
|
|
143 |
}
|