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.4 by jsr166, Mon Nov 2 23:42:46 2009 UTC vs.
Revision 1.11 by jsr166, Thu Jan 15 18:34:19 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          }
55  
# Line 71 | Line 72 | public class DequeBash {
72  
73              // Test serialization and copying
74              if ((i & 4095) == 0) {
75 <                testEqual(deque, deepCopy(deque));
76 <                testEqual(deque, (Deque<Integer>) deque.getClass().
76 <                          getConstructor(Collection.class).newInstance(deque));
75 >                testEqual(deque, serialClone(deque));
76 >                testEqual(deque, copyConstructorClone(deque));
77              }
78  
79              // Test fancy removal stuff once in a blue moon
80              if ((i & 8191) == 0)
81                  testRemove(deque);
82  
83 <         }
83 >        }
84  
85          // Stupid tests for clear, toString
86          deque.clear();
# Line 153 | Line 153 | public class DequeBash {
153                                  deque.getLast() + " expecting " + (nextTail - 1));
154      }
155  
156
156      static void randomOp(Deque<Integer> deque) throws Exception {
157  
158          // Perform a random operation
159 <        switch(random() & 3) {
159 >        switch (random() & 3) {
160          case 0:
161 <            switch(random() & 3) {
161 >            switch (random() & 3) {
162              case 0: deque.addLast(nextTail++);   break;
163              case 1: deque.offerLast(nextTail++); break;
164              case 2: deque.offer(nextTail++);     break;
# Line 172 | Line 171 | public class DequeBash {
171                  int result = 666;
172                  boolean threw = false;
173                  try {
174 <                    switch(random(3)) {
174 >                    switch (random(3)) {
175                      case 0: result = deque.removeFirst(); break;
176                      case 1: result = deque.remove();      break;
177                      case 2: result = deque.pop();         break;
# Line 185 | Line 184 | public class DequeBash {
184                      throw new Exception("Remove-no exception: " + result);
185              } else { // deque nonempty
186                  int result = -1;
187 <                switch(random(5)) {
187 >                switch (random(5)) {
188                  case 0: result = deque.removeFirst(); break;
189                  case 1: result = deque.remove();      break;
190                  case 2: result = deque.pop();         break;
# Line 199 | Line 198 | public class DequeBash {
198              }
199              break;
200          case 2:
201 <            switch(random(3)) {
201 >            switch (random(3)) {
202              case 0: deque.addFirst(nextHead--);   break;
203              case 1: deque.offerFirst(nextHead--); break;
204              case 2: deque.push(nextHead--);       break;
# Line 230 | Line 229 | public class DequeBash {
229          }
230      }
231  
233
232      private static void testEqual(Deque<Integer> d1, Deque<Integer> d2)
233          throws Exception
234      {
# Line 270 | Line 268 | public class DequeBash {
268              throw new Exception("d2 contains all of {Integer.MIN_VALUE }");
269      }
270  
271 <    private static <T> T deepCopy(T o) {
272 <        try {
273 <            ByteArrayOutputStream bos = new ByteArrayOutputStream();
274 <            ObjectOutputStream oos = new ObjectOutputStream(bos);
275 <            oos.writeObject(o);
276 <            oos.flush();
277 <            ByteArrayInputStream bin = new ByteArrayInputStream(
278 <                bos.toByteArray());
279 <            ObjectInputStream ois = new ObjectInputStream(bin);
271 >    @SuppressWarnings("unchecked")
272 >    private static <T> T copyConstructorClone(T o) throws Exception {
273 >        return (T) o.getClass().getConstructor(Collection.class).newInstance(o);
274 >    }
275 >
276 >    @SuppressWarnings("unchecked")
277 >    private static <T> T serialClone(T o) throws Exception {
278 >        ByteArrayOutputStream bos = new ByteArrayOutputStream();
279 >        ObjectOutputStream oos = new ObjectOutputStream(bos);
280 >        oos.writeObject(o);
281 >        oos.flush();
282 >        oos.close();
283 >        ByteArrayInputStream bin =
284 >            new ByteArrayInputStream(bos.toByteArray());
285 >        ObjectInputStream ois = new ObjectInputStream(bin);
286              return (T) ois.readObject();
283        } catch (Exception e) {
284            throw new IllegalArgumentException(e);
285        }
287      }
288  
289      private static void testRemove(Deque<Integer> deque) throws Exception {
290 <        Deque<Integer> copy = null;
291 <        switch(random() & 1) {
292 <          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 <        }
290 >        Deque<Integer> copy = ((random() & 1) == 0) ?
291 >            copyConstructorClone(deque) :
292 >            serialClone(deque);
293  
294          int numRemoved = 0;
295          for (Iterator<Integer> it = copy.iterator(); it.hasNext(); ) {
# Line 320 | Line 312 | public class DequeBash {
312                  throw new Exception(e + " missing.");
313  
314              boolean removed = false;
315 <            switch(random(3)) {
315 >            switch (random(3)) {
316                  case 0:  removed = copy.remove(e);                break;
317                  case 1:  removed = copy.removeFirstOccurrence(e); break;
318                  case 2:  removed = copy.removeLastOccurrence(e);  break;
# Line 335 | Line 327 | public class DequeBash {
327  
328          testEmpty(copy);
329  
330 <        copy = (Deque<Integer>) deque.getClass().
339 <            getConstructor(Collection.class).newInstance(deque);
330 >        copy = copyConstructorClone(deque);
331          copy.retainAll(deque);
332          testEqual(deque, copy);
333          copy.removeAll(deque);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines