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.*; |
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 |
< |
} |
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(); |
33 |
|
|
34 |
|
static void oneRun() { |
35 |
|
long startTime = System.nanoTime(); |
36 |
< |
for (int i=0; i<numItr; i++) { |
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"); |
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(); |
174 |
|
if (!all.equals(s)) |
175 |
|
fail("Failed to reconstruct ints from odds and evens."); |
176 |
|
|
177 |
< |
all = clone(odd, cl, synch); |
177 |
> |
all = clone(odd, cl, synch); |
178 |
|
ListIterator<Integer> itAll = all.listIterator(all.size()); |
179 |
|
ListIterator<Integer> itEven = even.listIterator(even.size()); |
180 |
|
while (itEven.hasPrevious()) { |
205 |
|
Object o = it.next(); |
206 |
|
if (all.indexOf(o) != all.lastIndexOf(o)) |
207 |
|
fail("Apparent duplicate detected."); |
208 |
< |
if (all.subList(i, all.size()).indexOf(o) != 0) { |
208 |
> |
if (all.subList(i, all.size()).indexOf(o) != 0) { |
209 |
|
System.out.println("s0: " + all.subList(i, all.size()).indexOf(o)); |
210 |
|
fail("subList/indexOf is screwy."); |
211 |
|
} |
224 |
|
static void arrays() { |
225 |
|
List<Integer> l = newList(cl, synch); |
226 |
|
AddRandoms(l, listSize); |
227 |
< |
Integer[] ia = (Integer[]) l.toArray(new Integer[0]); |
227 |
> |
Integer[] ia = l.toArray(new Integer[0]); |
228 |
|
if (!l.equals(Arrays.asList(ia))) |
229 |
|
fail("toArray(Object[]) is hosed (1)"); |
230 |
|
ia = new Integer[listSize]; |
231 |
< |
Integer[] ib = (Integer[]) l.toArray(ia); |
231 |
> |
Integer[] ib = l.toArray(ia); |
232 |
|
if (ia != ib || !l.equals(Arrays.asList(ia))) |
233 |
|
fail("toArray(Object[]) is hosed (2)"); |
234 |
|
ia = new Integer[listSize+1]; |
235 |
|
ia[listSize] = new Integer(69); |
236 |
< |
ib = (Integer[]) l.toArray(ia); |
236 |
> |
ib = l.toArray(ia); |
237 |
|
if (ia != ib || ia[listSize] != null |
238 |
|
|| !l.equals(Arrays.asList(ia).subList(0, listSize))) |
239 |
|
fail("toArray(Object[]) is hosed (3)"); |
245 |
|
if (s.hashCode() != a.hashCode()) |
246 |
|
fail("Incorrect hashCode computation."); |
247 |
|
|
248 |
< |
List clone = newList(cl, synch); |
249 |
< |
clone.addAll(a); |
250 |
< |
if (!s.equals(clone)) |
251 |
< |
fail("List not equal to copy."); |
252 |
< |
if (!s.containsAll(clone)) |
253 |
< |
fail("List does not contain copy."); |
254 |
< |
if (!clone.containsAll(s)) |
255 |
< |
fail("Copy does not contain list."); |
248 |
> |
List clone = newList(cl, synch); |
249 |
> |
clone.addAll(a); |
250 |
> |
if (!s.equals(clone)) |
251 |
> |
fail("List not equal to copy."); |
252 |
> |
if (!s.containsAll(clone)) |
253 |
> |
fail("List does not contain copy."); |
254 |
> |
if (!clone.containsAll(s)) |
255 |
> |
fail("Copy does not contain list."); |
256 |
|
|
257 |
< |
return (List<Integer>)clone; |
257 |
> |
return (List<Integer>) clone; |
258 |
|
} |
259 |
|
|
260 |
|
static List<Integer> newList(Class cl, boolean synch) { |
261 |
< |
try { |
262 |
< |
List<Integer> s = (List<Integer>)cl.newInstance(); |
261 |
> |
try { |
262 |
> |
List<Integer> s = (List<Integer>) cl.newInstance(); |
263 |
|
if (synch) |
264 |
|
s = Collections.synchronizedList(s); |
265 |
< |
if (!s.isEmpty()) |
266 |
< |
fail("New instance non empty."); |
267 |
< |
return s; |
268 |
< |
} catch (Throwable t) { |
269 |
< |
fail("Can't instantiate " + cl + ": " + t); |
270 |
< |
} |
271 |
< |
return null; //Shut up compiler. |
265 |
> |
if (!s.isEmpty()) |
266 |
> |
fail("New instance non empty."); |
267 |
> |
return s; |
268 |
> |
} catch (Throwable t) { |
269 |
> |
fail("Can't instantiate " + cl + ": " + t); |
270 |
> |
} |
271 |
> |
return null; //Shut up compiler. |
272 |
|
} |
273 |
|
|
274 |
|
static void AddRandoms(List<Integer> s, int n) { |
275 |
< |
for (int i=0; i<n; i++) { |
276 |
< |
int r = rnd.nextInt() % n; |
277 |
< |
Integer e = new Integer(r < 0 ? -r : r); |
278 |
< |
|
279 |
< |
int preSize = s.size(); |
280 |
< |
if (!s.add(e)) |
281 |
< |
fail ("Add failed."); |
282 |
< |
int postSize = s.size(); |
283 |
< |
if (postSize-preSize != 1) |
284 |
< |
fail ("Add didn't increase size by 1."); |
285 |
< |
} |
275 |
> |
for (int i=0; i<n; i++) { |
276 |
> |
int r = rnd.nextInt() % n; |
277 |
> |
Integer e = new Integer(r < 0 ? -r : r); |
278 |
> |
|
279 |
> |
int preSize = s.size(); |
280 |
> |
if (!s.add(e)) |
281 |
> |
fail("Add failed."); |
282 |
> |
int postSize = s.size(); |
283 |
> |
if (postSize-preSize != 1) |
284 |
> |
fail("Add didn't increase size by 1."); |
285 |
> |
} |
286 |
|
} |
287 |
|
|
288 |
|
static void fail(String s) { |
289 |
< |
System.out.println(s); |
290 |
< |
System.exit(1); |
289 |
> |
System.out.println(s); |
290 |
> |
System.exit(1); |
291 |
|
} |
292 |
|
} |