ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ListBash.java
(Generate patch)

Comparing jsr166/src/test/loops/ListBash.java (file contents):
Revision 1.4 by dl, Sun Sep 25 13:10:45 2005 UTC vs.
Revision 1.17 by jsr166, Sun Oct 23 03:03:23 2016 UTC

# Line 1 | Line 1
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.*;
# Line 12 | Line 12 | public class ListBash {
12      static int numItr;
13      static int listSize;
14      static boolean synch;
15 <    static Class cl;
15 >    static Class<?> cl;
16  
17      public static void main(String[] args) {
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 <        }
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();
28 >        synch = (args.length > 3);
29          oneRun();
30          oneRun();
31          oneRun();
# Line 35 | Line 33 | public class ListBash {
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();
# Line 50 | Line 48 | public class ListBash {
48          System.out.println("Time: " + (elapsed/1000000000.0) + "s");
49      }
50  
53
54
51      static void elementLoop() {
52          List<Integer> s1 = newList(cl, synch);
53          AddRandoms(s1, listSize);
# Line 69 | Line 65 | public class ListBash {
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++)
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++)
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");
# Line 96 | Line 92 | public class ListBash {
92              sum -= (s2.get(k)).intValue();
93          }
94          if (sum == 0) System.out.print(" ");
95 <    }    
95 >    }
96  
97      static void sets(List<Integer> s1, List<Integer> s2) {
98          List<Integer> intersection = clone(s1, cl,synch);intersection.retainAll(s2);
# Line 154 | Line 150 | public class ListBash {
150              all = clone(s, cl, synch);
151          else {
152              it = even.iterator();
153 <            while(it.hasNext())
154 <                if((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((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 = (odd.get(i)).intValue();
165 <                if(ii % 2 != 1)
165 >                if (ii % 2 != 1)
166                      fail("Failed to remove all even nubmers. " + ii);
167              }
168  
# Line 175 | Line 171 | public class ListBash {
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);
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()) {
# Line 191 | Line 187 | public class ListBash {
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))
# Line 207 | Line 203 | public class ListBash {
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              }
# Line 226 | Line 222 | public class ListBash {
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)");
238      }
239  
240      // Done inefficiently so as to exercise toArray
241 <    static List<Integer> 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 (List<Integer>)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<Integer> newList(Class cl, boolean synch) {
259 <        try {
260 <            List<Integer> s = (List<Integer>)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.
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 <        }
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines