1 |
dl |
1.1 |
import java.util.*; |
2 |
|
|
|
3 |
|
|
public class SetBash { |
4 |
|
|
static Random rnd = new Random(); |
5 |
|
|
|
6 |
|
|
public static void main(String[] args) { |
7 |
|
|
int numItr = Integer.parseInt(args[1]); |
8 |
|
|
int setSize = Integer.parseInt(args[2]); |
9 |
|
|
Class cl = null; |
10 |
|
|
|
11 |
|
|
try { |
12 |
|
|
cl = Class.forName(args[0]); |
13 |
|
|
} catch(ClassNotFoundException e) { |
14 |
|
|
fail("Class " + args[0] + " not found."); |
15 |
|
|
} |
16 |
|
|
|
17 |
|
|
boolean synch = (args.length>3); |
18 |
|
|
|
19 |
|
|
for (int i=0; i<numItr; i++) { |
20 |
|
|
Set s1 = newSet(cl, synch); |
21 |
|
|
AddRandoms(s1, setSize); |
22 |
|
|
|
23 |
|
|
Set s2 = newSet(cl, synch); |
24 |
|
|
AddRandoms(s2, setSize); |
25 |
|
|
|
26 |
|
|
Set intersection = clone(s1, cl, synch); |
27 |
|
|
intersection.retainAll(s2); |
28 |
|
|
Set diff1 = clone(s1, cl, synch); diff1.removeAll(s2); |
29 |
|
|
Set diff2 = clone(s2, cl, synch); diff2.removeAll(s1); |
30 |
|
|
Set union = clone(s1, cl, synch); union.addAll(s2); |
31 |
|
|
|
32 |
|
|
if (diff1.removeAll(diff2)) |
33 |
|
|
fail("Set algebra identity 2 failed"); |
34 |
|
|
if (diff1.removeAll(intersection)) |
35 |
|
|
fail("Set algebra identity 3 failed"); |
36 |
|
|
if (diff2.removeAll(diff1)) |
37 |
|
|
fail("Set algebra identity 4 failed"); |
38 |
|
|
if (diff2.removeAll(intersection)) |
39 |
|
|
fail("Set algebra identity 5 failed"); |
40 |
|
|
if (intersection.removeAll(diff1)) |
41 |
|
|
fail("Set algebra identity 6 failed"); |
42 |
|
|
if (intersection.removeAll(diff1)) |
43 |
|
|
fail("Set algebra identity 7 failed"); |
44 |
|
|
|
45 |
|
|
intersection.addAll(diff1); intersection.addAll(diff2); |
46 |
|
|
if (!intersection.equals(union)) |
47 |
|
|
fail("Set algebra identity 1 failed"); |
48 |
|
|
|
49 |
|
|
Iterator e = union.iterator(); |
50 |
|
|
while (e.hasNext()) |
51 |
|
|
if (!intersection.remove(e.next())) |
52 |
|
|
fail("Couldn't remove element from copy."); |
53 |
|
|
if (!intersection.isEmpty()) |
54 |
|
|
fail("Copy nonempty after deleting all elements."); |
55 |
|
|
|
56 |
|
|
e = union.iterator(); |
57 |
|
|
while (e.hasNext()) { |
58 |
|
|
Object o = e.next(); |
59 |
|
|
if (!union.contains(o)) |
60 |
|
|
fail("Set doesn't contain one of its elements."); |
61 |
|
|
e.remove(); |
62 |
|
|
if (union.contains(o)) |
63 |
|
|
fail("Set contains element after deletion."); |
64 |
|
|
} |
65 |
|
|
if (!union.isEmpty()) |
66 |
|
|
fail("Set nonempty after deleting all elements."); |
67 |
|
|
|
68 |
|
|
s1.clear(); |
69 |
|
|
if (!s1.isEmpty()) |
70 |
|
|
fail("Set nonempty after clear."); |
71 |
|
|
} |
72 |
|
|
System.out.println("Success."); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
// Done inefficiently so as to exercise toArray |
76 |
|
|
static Set clone(Set s, Class cl, boolean synch) { |
77 |
|
|
Set clone = newSet(cl, synch); |
78 |
|
|
clone.addAll(Arrays.asList(s.toArray())); |
79 |
|
|
if (!s.equals(clone)) |
80 |
|
|
fail("Set not equal to copy."); |
81 |
|
|
if (!s.containsAll(clone)) |
82 |
|
|
fail("Set does not contain copy."); |
83 |
|
|
if (!clone.containsAll(s)) |
84 |
|
|
fail("Copy does not contain set."); |
85 |
|
|
return clone; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
static Set newSet(Class cl, boolean synch) { |
89 |
|
|
try { |
90 |
|
|
Set s = (Set)cl.newInstance(); |
91 |
|
|
if (synch) |
92 |
|
|
s = Collections.synchronizedSet(s); |
93 |
|
|
if (!s.isEmpty()) |
94 |
|
|
fail("New instance non empty."); |
95 |
|
|
return s; |
96 |
|
|
} catch(Throwable t) { |
97 |
|
|
fail("Can't instantiate " + cl + ": " + t); |
98 |
|
|
} |
99 |
|
|
return null; //Shut up compiler. |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
static void AddRandoms(Set s, int n) { |
103 |
|
|
for (int i=0; i<n; i++) { |
104 |
|
|
int r = rnd.nextInt() % n; |
105 |
|
|
Integer e = new Integer(r < 0 ? -r : r); |
106 |
|
|
|
107 |
|
|
int preSize = s.size(); |
108 |
|
|
boolean prePresent = s.contains(e); |
109 |
|
|
boolean added = s.add(e); |
110 |
|
|
if (!s.contains(e)) |
111 |
|
|
fail ("Element not present after addition."); |
112 |
|
|
if (added == prePresent) |
113 |
|
|
fail ("added == alreadyPresent"); |
114 |
|
|
int postSize = s.size(); |
115 |
|
|
if (added && preSize == postSize) |
116 |
|
|
fail ("Add returned true, but size didn't change."); |
117 |
|
|
if (!added && preSize != postSize) |
118 |
|
|
fail ("Add returned false, but size changed."); |
119 |
|
|
} |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
static void fail(String s) { |
123 |
|
|
System.out.println(s); |
124 |
|
|
System.exit(1); |
125 |
|
|
} |
126 |
|
|
} |