1 |
|
/* |
2 |
|
* Written by Josh Bloch and Doug Lea with assistance from members of |
3 |
|
* JCP JSR-166 Expert Group and released to the public domain, as |
4 |
< |
* explained at http://creativecommons.org/licenses/publicdomain |
4 |
> |
* explained at http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
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; |
21 |
< |
|
22 |
< |
try { |
23 |
< |
cl = Class.forName(args[0]); |
24 |
< |
} catch(ClassNotFoundException e) { |
25 |
< |
fail("Class " + args[0] + " not found."); |
26 |
< |
} |
27 |
< |
|
28 |
< |
boolean synch = (args.length>3); |
29 |
< |
|
30 |
< |
boolean canRemove = true; |
31 |
< |
for (int i=0; i<numItr; i++) { |
32 |
< |
List s1 = newList(cl, synch); |
33 |
< |
AddRandoms(s1, listSize); |
34 |
< |
|
35 |
< |
List s2 = newList(cl, synch); |
36 |
< |
AddRandoms(s2, listSize); |
37 |
< |
|
38 |
< |
List intersection = clone(s1, cl,synch);intersection.retainAll(s2); |
39 |
< |
List diff1 = clone(s1, cl, synch); diff1.removeAll(s2); |
40 |
< |
List diff2 = clone(s2, cl, synch); diff2.removeAll(s1); |
41 |
< |
List union = clone(s1, cl, synch); union.addAll(s2); |
42 |
< |
|
43 |
< |
if (diff1.removeAll(diff2)) |
44 |
< |
fail("List algebra identity 2 failed"); |
45 |
< |
if (diff1.removeAll(intersection)) |
46 |
< |
fail("List algebra identity 3 failed"); |
47 |
< |
if (diff2.removeAll(diff1)) |
48 |
< |
fail("List algebra identity 4 failed"); |
49 |
< |
if (diff2.removeAll(intersection)) |
50 |
< |
fail("List algebra identity 5 failed"); |
51 |
< |
if (intersection.removeAll(diff1)) |
52 |
< |
fail("List algebra identity 6 failed"); |
53 |
< |
if (intersection.removeAll(diff1)) |
54 |
< |
fail("List algebra identity 7 failed"); |
55 |
< |
|
56 |
< |
intersection.addAll(diff1); intersection.addAll(diff2); |
57 |
< |
if (!(intersection.containsAll(union) && |
58 |
< |
union.containsAll(intersection))) |
59 |
< |
fail("List algebra identity 1 failed"); |
60 |
< |
|
61 |
< |
Iterator e = union.iterator(); |
62 |
< |
while (e.hasNext()) |
63 |
< |
intersection.remove(e.next()); |
64 |
< |
if (!intersection.isEmpty()) |
65 |
< |
fail("Copy nonempty after deleting all elements."); |
66 |
< |
|
67 |
< |
e = union.iterator(); |
68 |
< |
while (e.hasNext()) { |
69 |
< |
Object o = e.next(); |
70 |
< |
if (!union.contains(o)) |
71 |
< |
fail("List doesn't contain one of its elements."); |
72 |
< |
if (canRemove) { |
73 |
< |
try { e.remove(); |
74 |
< |
} catch (UnsupportedOperationException uoe) { |
75 |
< |
canRemove = false; |
76 |
< |
} |
18 |
> |
numItr = Integer.parseInt(args[1]); |
19 |
> |
listSize = Integer.parseInt(args[2]); |
20 |
> |
cl = null; |
21 |
> |
|
22 |
> |
try { |
23 |
> |
cl = Class.forName(args[0]); |
24 |
> |
} catch (ClassNotFoundException e) { |
25 |
> |
fail("Class " + args[0] + " not found."); |
26 |
> |
} |
27 |
> |
|
28 |
> |
synch = (args.length > 3); |
29 |
> |
oneRun(); |
30 |
> |
oneRun(); |
31 |
> |
oneRun(); |
32 |
> |
} |
33 |
> |
|
34 |
> |
static void oneRun() { |
35 |
> |
long startTime = System.nanoTime(); |
36 |
> |
for (int i = 0; i < numItr; i++) { |
37 |
> |
elementLoop(); |
38 |
> |
} |
39 |
> |
List<Integer> s = newList(cl, synch); |
40 |
> |
for (int i = 0; i < listSize; i++) |
41 |
> |
s.add(new Integer(i)); |
42 |
> |
if (s.size() != listSize) |
43 |
> |
fail("Size of [0..n-1] != n"); |
44 |
> |
evenOdd(s); |
45 |
> |
sublists(s); |
46 |
> |
arrays(); |
47 |
> |
long elapsed = System.nanoTime() - startTime; |
48 |
> |
System.out.println("Time: " + (elapsed/1000000000.0) + "s"); |
49 |
> |
} |
50 |
> |
|
51 |
> |
static void elementLoop() { |
52 |
> |
List<Integer> s1 = newList(cl, synch); |
53 |
> |
AddRandoms(s1, listSize); |
54 |
> |
|
55 |
> |
List<Integer> s2 = newList(cl, synch); |
56 |
> |
AddRandoms(s2, listSize); |
57 |
> |
|
58 |
> |
sets(s1, s2); |
59 |
> |
|
60 |
> |
s1.clear(); |
61 |
> |
if (s1.size() != 0) |
62 |
> |
fail("Clear didn't reduce size to zero."); |
63 |
> |
|
64 |
> |
s1.addAll(0, s2); |
65 |
> |
if (!(s1.equals(s2) && s2.equals(s1))) |
66 |
> |
fail("addAll(int, Collection) doesn't work."); |
67 |
> |
// Reverse List |
68 |
> |
for (int j = 0, n = s1.size(); j < n; j++) |
69 |
> |
s1.set(j, s1.set(n-j-1, s1.get(j))); |
70 |
> |
// Reverse it again |
71 |
> |
for (int j = 0, n = s1.size(); j < n; j++) |
72 |
> |
s1.set(j, s1.set(n-j-1, s1.get(j))); |
73 |
> |
if (!(s1.equals(s2) && s2.equals(s1))) |
74 |
> |
fail("set(int, Object) doesn't work"); |
75 |
> |
sums(s1, s2); |
76 |
> |
} |
77 |
> |
|
78 |
> |
static void sums(List<Integer> s1, List<Integer> s2) { |
79 |
> |
int sum = 0; |
80 |
> |
for (int k = 0; k < listSize; ++k) { |
81 |
> |
sum += (s1.get(k)).intValue(); |
82 |
> |
sum -= (s2.get(k)).intValue(); |
83 |
> |
} |
84 |
> |
for (int k = 0; k < listSize; ++k) { |
85 |
> |
sum += (s1.get(k)).intValue(); |
86 |
> |
s1.set(k, sum); |
87 |
> |
sum -= (s2.get(k)).intValue(); |
88 |
> |
s1.set(k, -sum); |
89 |
> |
} |
90 |
> |
for (int k = 0; k < listSize; ++k) { |
91 |
> |
sum += (s1.get(k)).intValue(); |
92 |
> |
sum -= (s2.get(k)).intValue(); |
93 |
> |
} |
94 |
> |
if (sum == 0) System.out.print(" "); |
95 |
> |
} |
96 |
> |
|
97 |
> |
static void sets(List<Integer> s1, List<Integer> s2) { |
98 |
> |
List<Integer> intersection = clone(s1, cl,synch);intersection.retainAll(s2); |
99 |
> |
List<Integer> diff1 = clone(s1, cl, synch); diff1.removeAll(s2); |
100 |
> |
List<Integer> diff2 = clone(s2, cl, synch); diff2.removeAll(s1); |
101 |
> |
List<Integer> union = clone(s1, cl, synch); union.addAll(s2); |
102 |
> |
|
103 |
> |
if (diff1.removeAll(diff2)) |
104 |
> |
fail("List algebra identity 2 failed"); |
105 |
> |
if (diff1.removeAll(intersection)) |
106 |
> |
fail("List algebra identity 3 failed"); |
107 |
> |
if (diff2.removeAll(diff1)) |
108 |
> |
fail("List algebra identity 4 failed"); |
109 |
> |
if (diff2.removeAll(intersection)) |
110 |
> |
fail("List algebra identity 5 failed"); |
111 |
> |
if (intersection.removeAll(diff1)) |
112 |
> |
fail("List algebra identity 6 failed"); |
113 |
> |
if (intersection.removeAll(diff1)) |
114 |
> |
fail("List algebra identity 7 failed"); |
115 |
> |
|
116 |
> |
intersection.addAll(diff1); intersection.addAll(diff2); |
117 |
> |
if (!(intersection.containsAll(union) && |
118 |
> |
union.containsAll(intersection))) |
119 |
> |
fail("List algebra identity 1 failed"); |
120 |
> |
|
121 |
> |
Iterator e = union.iterator(); |
122 |
> |
while (e.hasNext()) |
123 |
> |
intersection.remove(e.next()); |
124 |
> |
if (!intersection.isEmpty()) |
125 |
> |
fail("Copy nonempty after deleting all elements."); |
126 |
> |
|
127 |
> |
e = union.iterator(); |
128 |
> |
while (e.hasNext()) { |
129 |
> |
Object o = e.next(); |
130 |
> |
if (!union.contains(o)) |
131 |
> |
fail("List doesn't contain one of its elements."); |
132 |
> |
if (canRemove) { |
133 |
> |
try { e.remove(); |
134 |
> |
} catch (UnsupportedOperationException uoe) { |
135 |
> |
canRemove = false; |
136 |
|
} |
137 |
< |
} |
138 |
< |
if (canRemove && !union.isEmpty()) |
139 |
< |
fail("List nonempty after deleting all elements."); |
140 |
< |
|
141 |
< |
s1.clear(); |
142 |
< |
if (s1.size() != 0) |
143 |
< |
fail("Clear didn't reduce size to zero."); |
144 |
< |
|
145 |
< |
s1.addAll(0, s2); |
146 |
< |
if (!(s1.equals(s2) && s2.equals(s1))) |
147 |
< |
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"); |
92 |
< |
} |
93 |
< |
|
94 |
< |
List s = newList(cl, synch); |
95 |
< |
for (int i=0; i<listSize; i++) |
96 |
< |
s.add(new Integer(i)); |
97 |
< |
if (s.size() != listSize) |
98 |
< |
fail("Size of [0..n-1] != n"); |
99 |
< |
|
100 |
< |
List even = clone(s, cl, synch); |
101 |
< |
List odd = clone(s, cl, synch); |
102 |
< |
List all; |
103 |
< |
Iterator it; |
137 |
> |
} |
138 |
> |
} |
139 |
> |
if (canRemove && !union.isEmpty()) |
140 |
> |
fail("List nonempty after deleting all elements."); |
141 |
> |
} |
142 |
> |
|
143 |
> |
static void evenOdd(List<Integer> s) { |
144 |
> |
List<Integer> even = clone(s, cl, synch); |
145 |
> |
List<Integer> odd = clone(s, cl, synch); |
146 |
> |
List<Integer> all; |
147 |
> |
Iterator<Integer> it; |
148 |
|
|
149 |
|
if (!canRemove) |
150 |
|
all = clone(s, cl, synch); |
151 |
|
else { |
152 |
|
it = even.iterator(); |
153 |
< |
while(it.hasNext()) |
154 |
< |
if(((Integer)it.next()).intValue() % 2 == 1) |
153 |
> |
while (it.hasNext()) |
154 |
> |
if ((it.next()).intValue() % 2 == 1) |
155 |
|
it.remove(); |
156 |
|
it = even.iterator(); |
157 |
< |
while(it.hasNext()) |
158 |
< |
if(((Integer)it.next()).intValue() % 2 == 1) |
157 |
> |
while (it.hasNext()) |
158 |
> |
if ((it.next()).intValue() % 2 == 1) |
159 |
|
fail("Failed to remove all odd nubmers."); |
160 |
< |
|
160 |
> |
|
161 |
|
for (int i=0; i<(listSize/2); i++) |
162 |
|
odd.remove(i); |
163 |
|
for (int i=0; i<(listSize/2); i++) { |
164 |
< |
int ii = ((Integer)odd.get(i)).intValue(); |
165 |
< |
if(ii % 2 != 1) |
164 |
> |
int ii = (odd.get(i)).intValue(); |
165 |
> |
if (ii % 2 != 1) |
166 |
|
fail("Failed to remove all even nubmers. " + ii); |
167 |
|
} |
168 |
|
|
171 |
|
all.add(2*i, even.get(i)); |
172 |
|
if (!all.equals(s)) |
173 |
|
fail("Failed to reconstruct ints from odds and evens."); |
174 |
< |
|
175 |
< |
all = clone(odd, cl, synch); |
176 |
< |
ListIterator itAll = all.listIterator(all.size()); |
177 |
< |
ListIterator itEven = even.listIterator(even.size()); |
174 |
> |
|
175 |
> |
all = clone(odd, cl, synch); |
176 |
> |
ListIterator<Integer> itAll = all.listIterator(all.size()); |
177 |
> |
ListIterator<Integer> itEven = even.listIterator(even.size()); |
178 |
|
while (itEven.hasPrevious()) { |
179 |
|
itAll.previous(); |
180 |
|
itAll.add(itEven.previous()); |
182 |
|
} |
183 |
|
itAll = all.listIterator(); |
184 |
|
while (itAll.hasNext()) { |
185 |
< |
Integer i = (Integer)itAll.next(); |
185 |
> |
Integer i = itAll.next(); |
186 |
|
itAll.set(new Integer(i.intValue())); |
187 |
|
} |
188 |
|
itAll = all.listIterator(); |
189 |
|
it = s.iterator(); |
190 |
< |
while(it.hasNext()) |
191 |
< |
if(it.next()==itAll.next()) |
190 |
> |
while (it.hasNext()) |
191 |
> |
if (it.next()==itAll.next()) |
192 |
|
fail("Iterator.set failed to change value."); |
193 |
|
} |
194 |
|
if (!all.equals(s)) |
195 |
|
fail("Failed to reconstruct ints with ListIterator."); |
196 |
< |
|
197 |
< |
it = all.listIterator(); |
196 |
> |
} |
197 |
> |
|
198 |
> |
static void sublists(List<Integer> s) { |
199 |
> |
List<Integer> all = clone(s, cl, synch); |
200 |
> |
Iterator it = all.listIterator(); |
201 |
|
int i=0; |
202 |
|
while (it.hasNext()) { |
203 |
|
Object o = it.next(); |
204 |
|
if (all.indexOf(o) != all.lastIndexOf(o)) |
205 |
|
fail("Apparent duplicate detected."); |
206 |
< |
if (all.subList(i, all.size()).indexOf(o) != 0) { |
206 |
> |
if (all.subList(i, all.size()).indexOf(o) != 0) { |
207 |
|
System.out.println("s0: " + all.subList(i, all.size()).indexOf(o)); |
208 |
|
fail("subList/indexOf is screwy."); |
209 |
|
} |
217 |
|
} |
218 |
|
i++; |
219 |
|
} |
220 |
+ |
} |
221 |
|
|
222 |
< |
List l = newList(cl, synch); |
222 |
> |
static void arrays() { |
223 |
> |
List<Integer> l = newList(cl, synch); |
224 |
|
AddRandoms(l, listSize); |
225 |
< |
Integer[] ia = (Integer[]) l.toArray(new Integer[0]); |
225 |
> |
Integer[] ia = l.toArray(new Integer[0]); |
226 |
|
if (!l.equals(Arrays.asList(ia))) |
227 |
|
fail("toArray(Object[]) is hosed (1)"); |
228 |
|
ia = new Integer[listSize]; |
229 |
< |
Integer[] ib = (Integer[]) l.toArray(ia); |
229 |
> |
Integer[] ib = l.toArray(ia); |
230 |
|
if (ia != ib || !l.equals(Arrays.asList(ia))) |
231 |
|
fail("toArray(Object[]) is hosed (2)"); |
232 |
|
ia = new Integer[listSize+1]; |
233 |
|
ia[listSize] = new Integer(69); |
234 |
< |
ib = (Integer[]) l.toArray(ia); |
234 |
> |
ib = l.toArray(ia); |
235 |
|
if (ia != ib || ia[listSize] != null |
236 |
|
|| !l.equals(Arrays.asList(ia).subList(0, listSize))) |
237 |
|
fail("toArray(Object[]) is hosed (3)"); |
189 |
– |
|
190 |
– |
System.out.println("Success."); |
238 |
|
} |
239 |
|
|
240 |
|
// Done inefficiently so as to exercise toArray |
241 |
< |
static List clone(List s, Class cl, boolean synch) { |
241 |
> |
static List<Integer> clone(List s, Class<?> cl, boolean synch) { |
242 |
|
List a = Arrays.asList(s.toArray()); |
243 |
|
if (s.hashCode() != a.hashCode()) |
244 |
|
fail("Incorrect hashCode computation."); |
245 |
|
|
246 |
< |
List clone = newList(cl, synch); |
247 |
< |
clone.addAll(a); |
248 |
< |
if (!s.equals(clone)) |
249 |
< |
fail("List not equal to copy."); |
250 |
< |
if (!s.containsAll(clone)) |
251 |
< |
fail("List does not contain copy."); |
252 |
< |
if (!clone.containsAll(s)) |
253 |
< |
fail("Copy does not contain list."); |
254 |
< |
|
255 |
< |
return clone; |
246 |
> |
List clone = newList(cl, synch); |
247 |
> |
clone.addAll(a); |
248 |
> |
if (!s.equals(clone)) |
249 |
> |
fail("List not equal to copy."); |
250 |
> |
if (!s.containsAll(clone)) |
251 |
> |
fail("List does not contain copy."); |
252 |
> |
if (!clone.containsAll(s)) |
253 |
> |
fail("Copy does not contain list."); |
254 |
> |
|
255 |
> |
return (List<Integer>) clone; |
256 |
|
} |
257 |
|
|
258 |
< |
static List newList(Class cl, boolean synch) { |
259 |
< |
try { |
260 |
< |
List s = (List)cl.newInstance(); |
258 |
> |
static List<Integer> newList(Class<?> cl, boolean synch) { |
259 |
> |
try { |
260 |
> |
List<Integer> s = (List<Integer>) cl.getConstructor().newInstance(); |
261 |
|
if (synch) |
262 |
|
s = Collections.synchronizedList(s); |
263 |
< |
if (!s.isEmpty()) |
264 |
< |
fail("New instance non empty."); |
265 |
< |
return s; |
266 |
< |
} catch(Throwable t) { |
267 |
< |
fail("Can't instantiate " + cl + ": " + t); |
268 |
< |
} |
269 |
< |
return null; //Shut up compiler. |
270 |
< |
} |
271 |
< |
|
272 |
< |
static void AddRandoms(List s, int n) { |
273 |
< |
for (int i=0; i<n; i++) { |
274 |
< |
int r = rnd.nextInt() % n; |
275 |
< |
Integer e = new Integer(r < 0 ? -r : r); |
276 |
< |
|
277 |
< |
int preSize = s.size(); |
278 |
< |
if (!s.add(e)) |
279 |
< |
fail ("Add failed."); |
280 |
< |
int postSize = s.size(); |
281 |
< |
if (postSize-preSize != 1) |
282 |
< |
fail ("Add didn't increase size by 1."); |
283 |
< |
} |
263 |
> |
if (!s.isEmpty()) |
264 |
> |
fail("New instance non empty."); |
265 |
> |
return s; |
266 |
> |
} catch (Throwable t) { |
267 |
> |
fail("Can't instantiate " + cl + ": " + t); |
268 |
> |
} |
269 |
> |
return null; //Shut up compiler. |
270 |
> |
} |
271 |
> |
|
272 |
> |
static void AddRandoms(List<Integer> s, int n) { |
273 |
> |
for (int i = 0; i < n; i++) { |
274 |
> |
int r = rnd.nextInt() % n; |
275 |
> |
Integer e = new Integer(r < 0 ? -r : r); |
276 |
> |
|
277 |
> |
int preSize = s.size(); |
278 |
> |
if (!s.add(e)) |
279 |
> |
fail("Add failed."); |
280 |
> |
int postSize = s.size(); |
281 |
> |
if (postSize-preSize != 1) |
282 |
> |
fail("Add didn't increase size by 1."); |
283 |
> |
} |
284 |
|
} |
285 |
|
|
286 |
|
static void fail(String s) { |
287 |
< |
System.out.println(s); |
288 |
< |
System.exit(1); |
287 |
> |
System.out.println(s); |
288 |
> |
System.exit(1); |
289 |
|
} |
290 |
|
} |