ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ListBash.java
Revision: 1.17
Committed: Sun Oct 23 03:03:23 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.16: +1 -1 lines
Log Message:
fix deprecation warnings for Class#newInstance

File Contents

# User Rev Content
1 dl 1.2 /*
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 jsr166 1.12 * explained at http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.2 */
6    
7 dl 1.1 import java.util.*;
8    
9     public class ListBash {
10 dl 1.4 static boolean canRemove = true;
11     static final Random rnd = new Random();
12     static int numItr;
13     static int listSize;
14     static boolean synch;
15 jsr166 1.15 static Class<?> cl;
16 dl 1.1
17     public static void main(String[] args) {
18 dl 1.4 numItr = Integer.parseInt(args[1]);
19     listSize = Integer.parseInt(args[2]);
20     cl = null;
21 dl 1.1
22 jsr166 1.10 try {
23     cl = Class.forName(args[0]);
24     } catch (ClassNotFoundException e) {
25     fail("Class " + args[0] + " not found.");
26     }
27 dl 1.1
28 jsr166 1.14 synch = (args.length > 3);
29 dl 1.4 oneRun();
30     oneRun();
31     oneRun();
32     }
33 dl 1.1
34 dl 1.4 static void oneRun() {
35     long startTime = System.nanoTime();
36 jsr166 1.14 for (int i = 0; i < numItr; i++) {
37 dl 1.4 elementLoop();
38 jsr166 1.10 }
39     List<Integer> s = newList(cl, synch);
40 jsr166 1.14 for (int i = 0; i < listSize; i++)
41 jsr166 1.10 s.add(new Integer(i));
42     if (s.size() != listSize)
43     fail("Size of [0..n-1] != n");
44 dl 1.4 evenOdd(s);
45     sublists(s);
46     arrays();
47     long elapsed = System.nanoTime() - startTime;
48     System.out.println("Time: " + (elapsed/1000000000.0) + "s");
49     }
50 dl 1.1
51 dl 1.4 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 jsr166 1.14 for (int j = 0, n = s1.size(); j < n; j++)
69 dl 1.4 s1.set(j, s1.set(n-j-1, s1.get(j)));
70     // Reverse it again
71 jsr166 1.14 for (int j = 0, n = s1.size(); j < n; j++)
72 dl 1.4 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 jsr166 1.6 }
96 dl 1.4
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     }
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 dl 1.3
149     if (!canRemove)
150     all = clone(s, cl, synch);
151     else {
152     it = even.iterator();
153 jsr166 1.7 while (it.hasNext())
154     if ((it.next()).intValue() % 2 == 1)
155 dl 1.3 it.remove();
156     it = even.iterator();
157 jsr166 1.7 while (it.hasNext())
158     if ((it.next()).intValue() % 2 == 1)
159 dl 1.3 fail("Failed to remove all odd nubmers.");
160 jsr166 1.6
161 dl 1.3 for (int i=0; i<(listSize/2); i++)
162     odd.remove(i);
163     for (int i=0; i<(listSize/2); i++) {
164 dl 1.4 int ii = (odd.get(i)).intValue();
165 jsr166 1.7 if (ii % 2 != 1)
166 dl 1.3 fail("Failed to remove all even nubmers. " + ii);
167     }
168    
169     all = clone(odd, cl, synch);
170     for (int i=0; i<(listSize/2); i++)
171     all.add(2*i, even.get(i));
172     if (!all.equals(s))
173     fail("Failed to reconstruct ints from odds and evens.");
174 jsr166 1.6
175 jsr166 1.13 all = clone(odd, cl, synch);
176 dl 1.4 ListIterator<Integer> itAll = all.listIterator(all.size());
177     ListIterator<Integer> itEven = even.listIterator(even.size());
178 dl 1.3 while (itEven.hasPrevious()) {
179     itAll.previous();
180     itAll.add(itEven.previous());
181     itAll.previous(); // ???
182     }
183     itAll = all.listIterator();
184     while (itAll.hasNext()) {
185 dl 1.4 Integer i = itAll.next();
186 dl 1.3 itAll.set(new Integer(i.intValue()));
187     }
188     itAll = all.listIterator();
189     it = s.iterator();
190 jsr166 1.7 while (it.hasNext())
191     if (it.next()==itAll.next())
192 dl 1.3 fail("Iterator.set failed to change value.");
193 dl 1.1 }
194 dl 1.3 if (!all.equals(s))
195     fail("Failed to reconstruct ints with ListIterator.");
196 dl 1.4 }
197    
198     static void sublists(List<Integer> s) {
199     List<Integer> all = clone(s, cl, synch);
200     Iterator it = all.listIterator();
201 dl 1.3 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 jsr166 1.13 if (all.subList(i, all.size()).indexOf(o) != 0) {
207 dl 1.1 System.out.println("s0: " + all.subList(i, all.size()).indexOf(o));
208     fail("subList/indexOf is screwy.");
209     }
210     if (all.subList(i+1, all.size()).indexOf(o) != -1) {
211     System.out.println("s-1: " + all.subList(i+1, all.size()).indexOf(o));
212 dl 1.3 fail("subList/indexOf is screwy.");
213 dl 1.1 }
214     if (all.subList(0,i+1).lastIndexOf(o) != i) {
215     System.out.println("si" + all.subList(0,i+1).lastIndexOf(o));
216 dl 1.3 fail("subList/lastIndexOf is screwy.");
217 dl 1.1 }
218 dl 1.3 i++;
219     }
220 dl 1.4 }
221 dl 1.1
222 dl 1.4 static void arrays() {
223     List<Integer> l = newList(cl, synch);
224 dl 1.1 AddRandoms(l, listSize);
225 jsr166 1.9 Integer[] ia = l.toArray(new Integer[0]);
226 dl 1.1 if (!l.equals(Arrays.asList(ia)))
227     fail("toArray(Object[]) is hosed (1)");
228     ia = new Integer[listSize];
229 jsr166 1.9 Integer[] ib = l.toArray(ia);
230 dl 1.1 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 jsr166 1.9 ib = l.toArray(ia);
235 dl 1.1 if (ia != ib || ia[listSize] != null
236     || !l.equals(Arrays.asList(ia).subList(0, listSize)))
237     fail("toArray(Object[]) is hosed (3)");
238     }
239    
240     // Done inefficiently so as to exercise toArray
241 jsr166 1.15 static List<Integer> clone(List s, Class<?> cl, boolean synch) {
242 dl 1.1 List a = Arrays.asList(s.toArray());
243     if (s.hashCode() != a.hashCode())
244     fail("Incorrect hashCode computation.");
245    
246 jsr166 1.10 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 dl 1.1
255 jsr166 1.10 return (List<Integer>) clone;
256 dl 1.1 }
257    
258 jsr166 1.15 static List<Integer> newList(Class<?> cl, boolean synch) {
259 jsr166 1.10 try {
260 jsr166 1.17 List<Integer> s = (List<Integer>) cl.getConstructor().newInstance();
261 dl 1.1 if (synch)
262     s = Collections.synchronizedList(s);
263 jsr166 1.10 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 dl 1.1 }
271    
272 dl 1.4 static void AddRandoms(List<Integer> s, int n) {
273 jsr166 1.14 for (int i = 0; i < n; i++) {
274 jsr166 1.10 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 jsr166 1.11 fail("Add failed.");
280 jsr166 1.10 int postSize = s.size();
281     if (postSize-preSize != 1)
282 jsr166 1.11 fail("Add didn't increase size by 1.");
283 jsr166 1.10 }
284 dl 1.1 }
285    
286     static void fail(String s) {
287 jsr166 1.10 System.out.println(s);
288     System.exit(1);
289 dl 1.1 }
290     }