7 |
|
import java.util.*; |
8 |
|
|
9 |
|
public class ListBash { |
10 |
< |
static Random rnd = new Random(); |
10 |
> |
static boolean canRemove = true; |
11 |
> |
static final Random rnd = new Random(); |
12 |
> |
static int numItr; |
13 |
> |
static int listSize; |
14 |
> |
static boolean synch; |
15 |
> |
static Class cl; |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
int numItr = Integer.parseInt(args[1]); |
19 |
< |
int listSize = Integer.parseInt(args[2]); |
20 |
< |
Class cl = null; |
18 |
> |
numItr = Integer.parseInt(args[1]); |
19 |
> |
listSize = Integer.parseInt(args[2]); |
20 |
> |
cl = null; |
21 |
|
|
22 |
|
try { |
23 |
|
cl = Class.forName(args[0]); |
25 |
|
fail("Class " + args[0] + " not found."); |
26 |
|
} |
27 |
|
|
28 |
< |
boolean synch = (args.length>3); |
28 |
> |
synch = (args.length>3); |
29 |
> |
oneRun(); |
30 |
> |
oneRun(); |
31 |
> |
oneRun(); |
32 |
> |
oneRun(); |
33 |
> |
oneRun(); |
34 |
> |
} |
35 |
|
|
36 |
< |
boolean canRemove = true; |
36 |
> |
static void oneRun() { |
37 |
> |
long startTime = System.nanoTime(); |
38 |
|
for (int i=0; i<numItr; i++) { |
39 |
< |
List s1 = newList(cl, synch); |
28 |
< |
AddRandoms(s1, listSize); |
29 |
< |
|
30 |
< |
List s2 = newList(cl, synch); |
31 |
< |
AddRandoms(s2, listSize); |
32 |
< |
|
33 |
< |
List intersection = clone(s1, cl,synch);intersection.retainAll(s2); |
34 |
< |
List diff1 = clone(s1, cl, synch); diff1.removeAll(s2); |
35 |
< |
List diff2 = clone(s2, cl, synch); diff2.removeAll(s1); |
36 |
< |
List union = clone(s1, cl, synch); union.addAll(s2); |
37 |
< |
|
38 |
< |
if (diff1.removeAll(diff2)) |
39 |
< |
fail("List algebra identity 2 failed"); |
40 |
< |
if (diff1.removeAll(intersection)) |
41 |
< |
fail("List algebra identity 3 failed"); |
42 |
< |
if (diff2.removeAll(diff1)) |
43 |
< |
fail("List algebra identity 4 failed"); |
44 |
< |
if (diff2.removeAll(intersection)) |
45 |
< |
fail("List algebra identity 5 failed"); |
46 |
< |
if (intersection.removeAll(diff1)) |
47 |
< |
fail("List algebra identity 6 failed"); |
48 |
< |
if (intersection.removeAll(diff1)) |
49 |
< |
fail("List algebra identity 7 failed"); |
50 |
< |
|
51 |
< |
intersection.addAll(diff1); intersection.addAll(diff2); |
52 |
< |
if (!(intersection.containsAll(union) && |
53 |
< |
union.containsAll(intersection))) |
54 |
< |
fail("List algebra identity 1 failed"); |
55 |
< |
|
56 |
< |
Iterator e = union.iterator(); |
57 |
< |
while (e.hasNext()) |
58 |
< |
intersection.remove(e.next()); |
59 |
< |
if (!intersection.isEmpty()) |
60 |
< |
fail("Copy nonempty after deleting all elements."); |
61 |
< |
|
62 |
< |
e = union.iterator(); |
63 |
< |
while (e.hasNext()) { |
64 |
< |
Object o = e.next(); |
65 |
< |
if (!union.contains(o)) |
66 |
< |
fail("List doesn't contain one of its elements."); |
67 |
< |
if (canRemove) { |
68 |
< |
try { e.remove(); |
69 |
< |
} catch (UnsupportedOperationException uoe) { |
70 |
< |
canRemove = false; |
71 |
< |
} |
72 |
< |
} |
73 |
< |
} |
74 |
< |
if (canRemove && !union.isEmpty()) |
75 |
< |
fail("List nonempty after deleting all elements."); |
76 |
< |
|
77 |
< |
s1.clear(); |
78 |
< |
if (s1.size() != 0) |
79 |
< |
fail("Clear didn't reduce size to zero."); |
80 |
< |
|
81 |
< |
s1.addAll(0, s2); |
82 |
< |
if (!(s1.equals(s2) && s2.equals(s1))) |
83 |
< |
fail("addAll(int, Collection) doesn't work."); |
84 |
< |
// Reverse List |
85 |
< |
for (int j=0, n=s1.size(); j<n; j++) |
86 |
< |
s1.set(j, s1.set(n-j-1, s1.get(j))); |
87 |
< |
// Reverse it again |
88 |
< |
for (int j=0, n=s1.size(); j<n; j++) |
89 |
< |
s1.set(j, s1.set(n-j-1, s1.get(j))); |
90 |
< |
if (!(s1.equals(s2) && s2.equals(s1))) |
91 |
< |
fail("set(int, Object) doesn't work"); |
39 |
> |
elementLoop(); |
40 |
|
} |
41 |
< |
|
94 |
< |
List s = newList(cl, synch); |
41 |
> |
List<Integer> s = newList(cl, synch); |
42 |
|
for (int i=0; i<listSize; i++) |
43 |
|
s.add(new Integer(i)); |
44 |
|
if (s.size() != listSize) |
45 |
|
fail("Size of [0..n-1] != n"); |
46 |
+ |
evenOdd(s); |
47 |
+ |
sublists(s); |
48 |
+ |
arrays(); |
49 |
+ |
long elapsed = System.nanoTime() - startTime; |
50 |
+ |
System.out.println("Time: " + (elapsed/1000000000.0) + "s"); |
51 |
+ |
} |
52 |
|
|
53 |
< |
List even = clone(s, cl, synch); |
54 |
< |
List odd = clone(s, cl, synch); |
55 |
< |
List all; |
56 |
< |
Iterator it; |
53 |
> |
|
54 |
> |
|
55 |
> |
static void elementLoop() { |
56 |
> |
List<Integer> s1 = newList(cl, synch); |
57 |
> |
AddRandoms(s1, listSize); |
58 |
> |
|
59 |
> |
List<Integer> s2 = newList(cl, synch); |
60 |
> |
AddRandoms(s2, listSize); |
61 |
> |
|
62 |
> |
sets(s1, s2); |
63 |
> |
|
64 |
> |
s1.clear(); |
65 |
> |
if (s1.size() != 0) |
66 |
> |
fail("Clear didn't reduce size to zero."); |
67 |
> |
|
68 |
> |
s1.addAll(0, s2); |
69 |
> |
if (!(s1.equals(s2) && s2.equals(s1))) |
70 |
> |
fail("addAll(int, Collection) doesn't work."); |
71 |
> |
// Reverse List |
72 |
> |
for (int j=0, n=s1.size(); j<n; j++) |
73 |
> |
s1.set(j, s1.set(n-j-1, s1.get(j))); |
74 |
> |
// Reverse it again |
75 |
> |
for (int j=0, n=s1.size(); j<n; j++) |
76 |
> |
s1.set(j, s1.set(n-j-1, s1.get(j))); |
77 |
> |
if (!(s1.equals(s2) && s2.equals(s1))) |
78 |
> |
fail("set(int, Object) doesn't work"); |
79 |
> |
sums(s1, s2); |
80 |
> |
} |
81 |
> |
|
82 |
> |
static void sums(List<Integer> s1, List<Integer> s2) { |
83 |
> |
int sum = 0; |
84 |
> |
for (int k = 0; k < listSize; ++k) { |
85 |
> |
sum += (s1.get(k)).intValue(); |
86 |
> |
sum -= (s2.get(k)).intValue(); |
87 |
> |
} |
88 |
> |
for (int k = 0; k < listSize; ++k) { |
89 |
> |
sum += (s1.get(k)).intValue(); |
90 |
> |
s1.set(k, sum); |
91 |
> |
sum -= (s2.get(k)).intValue(); |
92 |
> |
s1.set(k, -sum); |
93 |
> |
} |
94 |
> |
for (int k = 0; k < listSize; ++k) { |
95 |
> |
sum += (s1.get(k)).intValue(); |
96 |
> |
sum -= (s2.get(k)).intValue(); |
97 |
> |
} |
98 |
> |
if (sum == 0) System.out.print(" "); |
99 |
> |
} |
100 |
> |
|
101 |
> |
static void sets(List<Integer> s1, List<Integer> s2) { |
102 |
> |
List<Integer> intersection = clone(s1, cl,synch);intersection.retainAll(s2); |
103 |
> |
List<Integer> diff1 = clone(s1, cl, synch); diff1.removeAll(s2); |
104 |
> |
List<Integer> diff2 = clone(s2, cl, synch); diff2.removeAll(s1); |
105 |
> |
List<Integer> union = clone(s1, cl, synch); union.addAll(s2); |
106 |
> |
|
107 |
> |
if (diff1.removeAll(diff2)) |
108 |
> |
fail("List algebra identity 2 failed"); |
109 |
> |
if (diff1.removeAll(intersection)) |
110 |
> |
fail("List algebra identity 3 failed"); |
111 |
> |
if (diff2.removeAll(diff1)) |
112 |
> |
fail("List algebra identity 4 failed"); |
113 |
> |
if (diff2.removeAll(intersection)) |
114 |
> |
fail("List algebra identity 5 failed"); |
115 |
> |
if (intersection.removeAll(diff1)) |
116 |
> |
fail("List algebra identity 6 failed"); |
117 |
> |
if (intersection.removeAll(diff1)) |
118 |
> |
fail("List algebra identity 7 failed"); |
119 |
> |
|
120 |
> |
intersection.addAll(diff1); intersection.addAll(diff2); |
121 |
> |
if (!(intersection.containsAll(union) && |
122 |
> |
union.containsAll(intersection))) |
123 |
> |
fail("List algebra identity 1 failed"); |
124 |
> |
|
125 |
> |
Iterator e = union.iterator(); |
126 |
> |
while (e.hasNext()) |
127 |
> |
intersection.remove(e.next()); |
128 |
> |
if (!intersection.isEmpty()) |
129 |
> |
fail("Copy nonempty after deleting all elements."); |
130 |
> |
|
131 |
> |
e = union.iterator(); |
132 |
> |
while (e.hasNext()) { |
133 |
> |
Object o = e.next(); |
134 |
> |
if (!union.contains(o)) |
135 |
> |
fail("List doesn't contain one of its elements."); |
136 |
> |
if (canRemove) { |
137 |
> |
try { e.remove(); |
138 |
> |
} catch (UnsupportedOperationException uoe) { |
139 |
> |
canRemove = false; |
140 |
> |
} |
141 |
> |
} |
142 |
> |
} |
143 |
> |
if (canRemove && !union.isEmpty()) |
144 |
> |
fail("List nonempty after deleting all elements."); |
145 |
> |
} |
146 |
> |
|
147 |
> |
static void evenOdd(List<Integer> s) { |
148 |
> |
List<Integer> even = clone(s, cl, synch); |
149 |
> |
List<Integer> odd = clone(s, cl, synch); |
150 |
> |
List<Integer> all; |
151 |
> |
Iterator<Integer> it; |
152 |
|
|
153 |
|
if (!canRemove) |
154 |
|
all = clone(s, cl, synch); |
155 |
|
else { |
156 |
|
it = even.iterator(); |
157 |
|
while(it.hasNext()) |
158 |
< |
if(((Integer)it.next()).intValue() % 2 == 1) |
158 |
> |
if((it.next()).intValue() % 2 == 1) |
159 |
|
it.remove(); |
160 |
|
it = even.iterator(); |
161 |
|
while(it.hasNext()) |
162 |
< |
if(((Integer)it.next()).intValue() % 2 == 1) |
162 |
> |
if((it.next()).intValue() % 2 == 1) |
163 |
|
fail("Failed to remove all odd nubmers."); |
164 |
|
|
165 |
|
for (int i=0; i<(listSize/2); i++) |
166 |
|
odd.remove(i); |
167 |
|
for (int i=0; i<(listSize/2); i++) { |
168 |
< |
int ii = ((Integer)odd.get(i)).intValue(); |
168 |
> |
int ii = (odd.get(i)).intValue(); |
169 |
|
if(ii % 2 != 1) |
170 |
|
fail("Failed to remove all even nubmers. " + ii); |
171 |
|
} |
177 |
|
fail("Failed to reconstruct ints from odds and evens."); |
178 |
|
|
179 |
|
all = clone(odd, cl, synch); |
180 |
< |
ListIterator itAll = all.listIterator(all.size()); |
181 |
< |
ListIterator itEven = even.listIterator(even.size()); |
180 |
> |
ListIterator<Integer> itAll = all.listIterator(all.size()); |
181 |
> |
ListIterator<Integer> itEven = even.listIterator(even.size()); |
182 |
|
while (itEven.hasPrevious()) { |
183 |
|
itAll.previous(); |
184 |
|
itAll.add(itEven.previous()); |
186 |
|
} |
187 |
|
itAll = all.listIterator(); |
188 |
|
while (itAll.hasNext()) { |
189 |
< |
Integer i = (Integer)itAll.next(); |
189 |
> |
Integer i = itAll.next(); |
190 |
|
itAll.set(new Integer(i.intValue())); |
191 |
|
} |
192 |
|
itAll = all.listIterator(); |
197 |
|
} |
198 |
|
if (!all.equals(s)) |
199 |
|
fail("Failed to reconstruct ints with ListIterator."); |
200 |
< |
|
201 |
< |
it = all.listIterator(); |
200 |
> |
} |
201 |
> |
|
202 |
> |
static void sublists(List<Integer> s) { |
203 |
> |
List<Integer> all = clone(s, cl, synch); |
204 |
> |
Iterator it = all.listIterator(); |
205 |
|
int i=0; |
206 |
|
while (it.hasNext()) { |
207 |
|
Object o = it.next(); |
221 |
|
} |
222 |
|
i++; |
223 |
|
} |
224 |
+ |
} |
225 |
|
|
226 |
< |
List l = newList(cl, synch); |
226 |
> |
static void arrays() { |
227 |
> |
List<Integer> l = newList(cl, synch); |
228 |
|
AddRandoms(l, listSize); |
229 |
|
Integer[] ia = (Integer[]) l.toArray(new Integer[0]); |
230 |
|
if (!l.equals(Arrays.asList(ia))) |
239 |
|
if (ia != ib || ia[listSize] != null |
240 |
|
|| !l.equals(Arrays.asList(ia).subList(0, listSize))) |
241 |
|
fail("toArray(Object[]) is hosed (3)"); |
189 |
– |
|
190 |
– |
System.out.println("Success."); |
242 |
|
} |
243 |
|
|
244 |
|
// Done inefficiently so as to exercise toArray |
245 |
< |
static List clone(List s, Class cl, boolean synch) { |
245 |
> |
static List<Integer> clone(List s, Class cl, boolean synch) { |
246 |
|
List a = Arrays.asList(s.toArray()); |
247 |
|
if (s.hashCode() != a.hashCode()) |
248 |
|
fail("Incorrect hashCode computation."); |
256 |
|
if (!clone.containsAll(s)) |
257 |
|
fail("Copy does not contain list."); |
258 |
|
|
259 |
< |
return clone; |
259 |
> |
return (List<Integer>)clone; |
260 |
|
} |
261 |
|
|
262 |
< |
static List newList(Class cl, boolean synch) { |
262 |
> |
static List<Integer> newList(Class cl, boolean synch) { |
263 |
|
try { |
264 |
< |
List s = (List)cl.newInstance(); |
264 |
> |
List<Integer> s = (List<Integer>)cl.newInstance(); |
265 |
|
if (synch) |
266 |
|
s = Collections.synchronizedList(s); |
267 |
|
if (!s.isEmpty()) |
273 |
|
return null; //Shut up compiler. |
274 |
|
} |
275 |
|
|
276 |
< |
static void AddRandoms(List s, int n) { |
276 |
> |
static void AddRandoms(List<Integer> s, int n) { |
277 |
|
for (int i=0; i<n; i++) { |
278 |
|
int r = rnd.nextInt() % n; |
279 |
|
Integer e = new Integer(r < 0 ? -r : r); |