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