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

Comparing jsr166/src/test/loops/DequeBash.java (file contents):
Revision 1.5 by jsr166, Mon Nov 16 04:57:09 2009 UTC vs.
Revision 1.12 by jsr166, Mon Aug 10 03:13:33 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Josh Bloch of Google Inc. and released to the public domain,
3 < * as explained at http://creativecommons.org/licenses/publicdomain.
3 > * as explained at http://creativecommons.org/publicdomain/zero/1.0/.
4   */
5  
6 import java.util.*;
6   import java.io.*;
7 + import java.util.*;
8  
9   /**
10   * Interface-based Deque tester.  This test currently makes three
# Line 22 | Line 22 | public class DequeBash {
22      static int nextHead = -1;
23      static int size() { return nextTail - nextHead - 1; }
24  
25
25      static int random(int bound) {
26          int x = seed;
27          int t = (x % 127773) * 16807 - (x / 127773) * 2836;
# Line 37 | Line 36 | public class DequeBash {
36          return (t & 0x7fffffff);
37      }
38  
39 <    public static void main(String args[]) throws Exception {
40 <        Class cls = Class.forName(args[0]);
39 >    public static void main(String[] args) throws Exception {
40 >        Class<?> cls = Class.forName(args[0]);
41          int n = 1000000;
42  
43          for (int j = 0; j < 3; ++j) {
44 +            @SuppressWarnings("unchecked")
45              Deque<Integer> deque = (Deque<Integer>) cls.newInstance();
46              nextTail =  0;
47              nextHead = -1;
48 <            long start = System.currentTimeMillis();
48 >            long start = System.nanoTime();
49              mainTest(deque, n);
50 <            long end = System.currentTimeMillis();
51 <            System.out.println("Time: " + (end - start) + "ms");
50 >            long end = System.nanoTime();
51 >            long elapsedTimeMillis = (end - start) / (1000L * 1000L);
52 >            System.out.printf("Time: %d ms%n", elapsedTimeMillis);
53              if (deque.isEmpty()) System.out.print(" ");
54          }
54
55      }
56  
57      static void mainTest(Deque<Integer> deque, int n) throws Exception {
# Line 71 | Line 71 | public class DequeBash {
71  
72              // Test serialization and copying
73              if ((i & 4095) == 0) {
74 <                testEqual(deque, deepCopy(deque));
75 <                testEqual(deque, (Deque<Integer>) deque.getClass().
76 <                          getConstructor(Collection.class).newInstance(deque));
74 >                testEqual(deque, serialClone(deque));
75 >                testEqual(deque, copyConstructorClone(deque));
76              }
77  
78              // Test fancy removal stuff once in a blue moon
79              if ((i & 8191) == 0)
80                  testRemove(deque);
81 <
83 <         }
81 >        }
82  
83          // Stupid tests for clear, toString
84          deque.clear();
# Line 153 | Line 151 | public class DequeBash {
151                                  deque.getLast() + " expecting " + (nextTail - 1));
152      }
153  
156
154      static void randomOp(Deque<Integer> deque) throws Exception {
155  
156          // Perform a random operation
# Line 230 | Line 227 | public class DequeBash {
227          }
228      }
229  
233
230      private static void testEqual(Deque<Integer> d1, Deque<Integer> d2)
231          throws Exception
232      {
# Line 270 | Line 266 | public class DequeBash {
266              throw new Exception("d2 contains all of {Integer.MIN_VALUE }");
267      }
268  
269 <    private static <T> T deepCopy(T o) {
270 <        try {
271 <            ByteArrayOutputStream bos = new ByteArrayOutputStream();
272 <            ObjectOutputStream oos = new ObjectOutputStream(bos);
273 <            oos.writeObject(o);
274 <            oos.flush();
275 <            ByteArrayInputStream bin = new ByteArrayInputStream(
276 <                bos.toByteArray());
277 <            ObjectInputStream ois = new ObjectInputStream(bin);
269 >    @SuppressWarnings("unchecked")
270 >    private static <T> T copyConstructorClone(T o) throws Exception {
271 >        return (T) o.getClass().getConstructor(Collection.class).newInstance(o);
272 >    }
273 >
274 >    @SuppressWarnings("unchecked")
275 >    private static <T> T serialClone(T o) throws Exception {
276 >        ByteArrayOutputStream bos = new ByteArrayOutputStream();
277 >        ObjectOutputStream oos = new ObjectOutputStream(bos);
278 >        oos.writeObject(o);
279 >        oos.flush();
280 >        oos.close();
281 >        ByteArrayInputStream bin =
282 >            new ByteArrayInputStream(bos.toByteArray());
283 >        ObjectInputStream ois = new ObjectInputStream(bin);
284              return (T) ois.readObject();
283        } catch (Exception e) {
284            throw new IllegalArgumentException(e);
285        }
285      }
286  
287      private static void testRemove(Deque<Integer> deque) throws Exception {
288 <        Deque<Integer> copy = null;
289 <        switch (random() & 1) {
290 <          case 0:
292 <            copy = (Deque<Integer>) deque.getClass().
293 <                getConstructor(Collection.class).newInstance(deque);
294 <            break;
295 <          case 1:
296 <            copy = deepCopy(deque);
297 <            break;
298 <          default:
299 <            throw new Exception("How'd we get here");
300 <        }
288 >        Deque<Integer> copy = ((random() & 1) == 0) ?
289 >            copyConstructorClone(deque) :
290 >            serialClone(deque);
291  
292          int numRemoved = 0;
293          for (Iterator<Integer> it = copy.iterator(); it.hasNext(); ) {
# Line 335 | Line 325 | public class DequeBash {
325  
326          testEmpty(copy);
327  
328 <        copy = (Deque<Integer>) deque.getClass().
339 <            getConstructor(Collection.class).newInstance(deque);
328 >        copy = copyConstructorClone(deque);
329          copy.retainAll(deque);
330          testEqual(deque, copy);
331          copy.removeAll(deque);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines