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.15 by jsr166, Sun Oct 23 02:21:56 2016 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
26    static int random(int bound) {
27        int x = seed;
28        int t = (x % 127773) * 16807 - (x / 127773) * 2836;
29        seed = (t > 0) ? t : t + 0x7fffffff;
30        return (t & 0x7fffffff) % bound;
31    }
32
25      static int random() {
26 <        int x = seed;
27 <        int t = (x % 127773) * 16807 - (x / 127773) * 2836;
28 <        seed = (t > 0) ? t : t + 0x7fffffff;
29 <        return (t & 0x7fffffff);
26 >        int r = seed;
27 >        r ^= r << 13;   // xorshift
28 >        r ^= r >>> 17;
29 >        return seed = r ^ (r << 5);
30      }
31  
32 <    public static void main(String args[]) throws Exception {
33 <        Class cls = Class.forName(args[0]);
34 <        int n = 1000000;
32 >    static int random(int bound) {
33 >        return (random() & 0x7fffffff) % bound;
34 >    }
35  
36 <        for (int j = 0; j < 3; ++j) {
37 <            Deque<Integer> deque = (Deque<Integer>) cls.newInstance();
36 >    public static void main(String[] args) throws Exception {
37 >        Class<?> cls = Class.forName(args[0]);
38 >        long startTime = System.nanoTime();
39 >        for (int j = 0; j < 200; ++j) {
40 >            @SuppressWarnings("unchecked")
41 >            Deque<Integer> deque =
42 >                (Deque<Integer>) cls.getConstructor().newInstance();
43              nextTail =  0;
44              nextHead = -1;
45 <            long start = System.currentTimeMillis();
49 <            mainTest(deque, n);
50 <            long end = System.currentTimeMillis();
51 <            System.out.println("Time: " + (end - start) + "ms");
45 >            mainTest(deque, (random() & ((1 << 20) - 1)));
46              if (deque.isEmpty()) System.out.print(" ");
47 +            if ((j % 10) == 9) System.out.print(".");
48          }
49 +        long now = System.nanoTime();
50 +        long elapsedTimeMillis = (now - startTime) / (1000L * 1000L);
51 +        System.out.printf("\ntotal time %d ms\n", elapsedTimeMillis);
52  
53      }
54  
# Line 71 | Line 69 | public class DequeBash {
69  
70              // Test serialization and copying
71              if ((i & 4095) == 0) {
72 <                testEqual(deque, deepCopy(deque));
73 <                testEqual(deque, (Deque<Integer>) deque.getClass().
76 <                          getConstructor(Collection.class).newInstance(deque));
72 >                testEqual(deque, serialClone(deque));
73 >                testEqual(deque, copyConstructorClone(deque));
74              }
75  
76              // Test fancy removal stuff once in a blue moon
77              if ((i & 8191) == 0)
78                  testRemove(deque);
79 <
83 <         }
79 >        }
80  
81          // Stupid tests for clear, toString
82          deque.clear();
# Line 153 | Line 149 | public class DequeBash {
149                                  deque.getLast() + " expecting " + (nextTail - 1));
150      }
151  
156
152      static void randomOp(Deque<Integer> deque) throws Exception {
153  
154          // Perform a random operation
# Line 230 | Line 225 | public class DequeBash {
225          }
226      }
227  
233
228      private static void testEqual(Deque<Integer> d1, Deque<Integer> d2)
229          throws Exception
230      {
# Line 270 | Line 264 | public class DequeBash {
264              throw new Exception("d2 contains all of {Integer.MIN_VALUE }");
265      }
266  
267 <    private static <T> T deepCopy(T o) {
268 <        try {
269 <            ByteArrayOutputStream bos = new ByteArrayOutputStream();
270 <            ObjectOutputStream oos = new ObjectOutputStream(bos);
271 <            oos.writeObject(o);
272 <            oos.flush();
273 <            ByteArrayInputStream bin = new ByteArrayInputStream(
274 <                bos.toByteArray());
275 <            ObjectInputStream ois = new ObjectInputStream(bin);
267 >    @SuppressWarnings("unchecked")
268 >    private static <T> T copyConstructorClone(T o) throws Exception {
269 >        return (T) o.getClass().getConstructor(Collection.class).newInstance(o);
270 >    }
271 >
272 >    @SuppressWarnings("unchecked")
273 >    private static <T> T serialClone(T o) throws Exception {
274 >        ByteArrayOutputStream bos = new ByteArrayOutputStream();
275 >        ObjectOutputStream oos = new ObjectOutputStream(bos);
276 >        oos.writeObject(o);
277 >        oos.flush();
278 >        oos.close();
279 >        ByteArrayInputStream bin =
280 >            new ByteArrayInputStream(bos.toByteArray());
281 >        ObjectInputStream ois = new ObjectInputStream(bin);
282              return (T) ois.readObject();
283        } catch (Exception e) {
284            throw new IllegalArgumentException(e);
285        }
283      }
284  
285      private static void testRemove(Deque<Integer> deque) throws Exception {
286 <        Deque<Integer> copy = null;
287 <        switch (random() & 1) {
288 <          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 <        }
286 >        Deque<Integer> copy = ((random() & 1) == 0) ?
287 >            copyConstructorClone(deque) :
288 >            serialClone(deque);
289  
290          int numRemoved = 0;
291          for (Iterator<Integer> it = copy.iterator(); it.hasNext(); ) {
# Line 335 | Line 323 | public class DequeBash {
323  
324          testEmpty(copy);
325  
326 <        copy = (Deque<Integer>) deque.getClass().
339 <            getConstructor(Collection.class).newInstance(deque);
326 >        copy = copyConstructorClone(deque);
327          copy.retainAll(deque);
328          testEqual(deque, copy);
329          copy.removeAll(deque);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines