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.3 by jsr166, Thu Oct 29 23:09:07 2009 UTC vs.
Revision 1.13 by dl, Sat Oct 22 19:30:48 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;
35 <
36 <        for (int j = 0; j < 3; ++j) {
32 >    static int random(int bound) {
33 >        return (random() & 0x7fffffff) % bound;
34 >    }
35 >    
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 = (Deque<Integer>) cls.newInstance();
42              nextTail =  0;
43              nextHead = -1;
44 <            long start = System.currentTimeMillis();
49 <            mainTest(deque, n);
50 <            long end = System.currentTimeMillis();
51 <            System.out.println("Time: " + (end - start) + "ms");
44 >            mainTest(deque, (random() & ((1 << 20) - 1)));
45              if (deque.isEmpty()) System.out.print(" ");
46 +            if ((j % 10) == 9) System.out.print(".");
47          }
48 <
48 >        long now = System.nanoTime();
49 >        long elapsedTimeMillis = (now - startTime) / (1000L * 1000L);
50 >        System.out.printf("\ntotal time %d ms\n", elapsedTimeMillis);
51 >        
52      }
53  
54      static void mainTest(Deque<Integer> deque, int n) throws Exception {
# Line 71 | Line 68 | public class DequeBash {
68  
69              // Test serialization and copying
70              if ((i & 4095) == 0) {
71 <                testEqual(deque, deepCopy(deque));
72 <                testEqual(deque, (Deque<Integer>) deque.getClass().
76 <                          getConstructor(Collection.class).newInstance(deque));
71 >                testEqual(deque, serialClone(deque));
72 >                testEqual(deque, copyConstructorClone(deque));
73              }
74  
75              // Test fancy removal stuff once in a blue moon
76              if ((i & 8191) == 0)
77                  testRemove(deque);
78 <
83 <         }
78 >        }
79  
80          // Stupid tests for clear, toString
81          deque.clear();
# Line 153 | Line 148 | public class DequeBash {
148                                  deque.getLast() + " expecting " + (nextTail - 1));
149      }
150  
156
151      static void randomOp(Deque<Integer> deque) throws Exception {
152  
153          // Perform a random operation
154 <        switch(random() & 3) {
154 >        switch (random() & 3) {
155          case 0:
156 <            switch(random() & 3) {
156 >            switch (random() & 3) {
157              case 0: deque.addLast(nextTail++);   break;
158              case 1: deque.offerLast(nextTail++); break;
159              case 2: deque.offer(nextTail++);     break;
# Line 172 | Line 166 | public class DequeBash {
166                  int result = 666;
167                  boolean threw = false;
168                  try {
169 <                    switch(random(3)) {
169 >                    switch (random(3)) {
170                      case 0: result = deque.removeFirst(); break;
171                      case 1: result = deque.remove();      break;
172                      case 2: result = deque.pop();         break;
173                      default: throw new Exception("How'd we get here");
174                      }
175 <                } catch(NoSuchElementException e) {
175 >                } catch (NoSuchElementException e) {
176                      threw = true;
177                  }
178                  if (!threw)
179                      throw new Exception("Remove-no exception: " + result);
180              } else { // deque nonempty
181                  int result = -1;
182 <                switch(random(5)) {
182 >                switch (random(5)) {
183                  case 0: result = deque.removeFirst(); break;
184                  case 1: result = deque.remove();      break;
185                  case 2: result = deque.pop();         break;
# Line 199 | Line 193 | public class DequeBash {
193              }
194              break;
195          case 2:
196 <            switch(random(3)) {
196 >            switch (random(3)) {
197              case 0: deque.addFirst(nextHead--);   break;
198              case 1: deque.offerFirst(nextHead--); break;
199              case 2: deque.push(nextHead--);       break;
# Line 212 | Line 206 | public class DequeBash {
206                  boolean threw = false;
207                  try {
208                      result = deque.removeLast();
209 <                } catch(NoSuchElementException e) {
209 >                } catch (NoSuchElementException e) {
210                      threw = true;
211                  }
212                  if (!threw)
213                      throw new Exception("Remove-no exception: " + result);
214              } else { // deque nonempty
215 <                int result = ((random() & 1) == 0?
215 >                int result = ((random() & 1) == 0 ?
216                                deque.removeLast() : deque.pollLast());
217                  if (result != --nextTail)
218                      throw new Exception(
219 <                                        "Removed "+ result + " expecting "+(nextTail + 1));
219 >                        "Removed "+ result + " expecting "+(nextTail + 1));
220              }
221              break;
222          default:
# Line 230 | Line 224 | public class DequeBash {
224          }
225      }
226  
233
227      private static void testEqual(Deque<Integer> d1, Deque<Integer> d2)
228          throws Exception
229      {
230          if (d1.size() != d2.size())
231              throw new Exception("Size " + d1.size() + " != " + d2.size());
232          Iterator<Integer> it = d2.iterator();
233 <        for(int i : d1) {
233 >        for (int i : d1) {
234              int j = it.next();
235              if (j != i)
236                  throw new Exception("Element " + j + " != " + i);
237          }
238  
239 <        for(int i : d1)
239 >        for (int i : d1)
240              if (!d2.contains(i))
241                  throw new Exception("d2 doesn't contain " + i);
242 <        for(int i : d2)
242 >        for (int i : d2)
243              if (!d1.contains(i))
244                  throw new Exception("d2 doesn't contain " + i);
245  
# Line 270 | Line 263 | public class DequeBash {
263              throw new Exception("d2 contains all of {Integer.MIN_VALUE }");
264      }
265  
266 <    private static <T> T deepCopy(T o) {
267 <        try {
268 <            ByteArrayOutputStream bos = new ByteArrayOutputStream();
269 <            ObjectOutputStream oos = new ObjectOutputStream(bos);
270 <            oos.writeObject(o);
271 <            oos.flush();
272 <            ByteArrayInputStream bin = new ByteArrayInputStream(
273 <                bos.toByteArray());
274 <            ObjectInputStream ois = new ObjectInputStream(bin);
266 >    @SuppressWarnings("unchecked")
267 >    private static <T> T copyConstructorClone(T o) throws Exception {
268 >        return (T) o.getClass().getConstructor(Collection.class).newInstance(o);
269 >    }
270 >
271 >    @SuppressWarnings("unchecked")
272 >    private static <T> T serialClone(T o) throws Exception {
273 >        ByteArrayOutputStream bos = new ByteArrayOutputStream();
274 >        ObjectOutputStream oos = new ObjectOutputStream(bos);
275 >        oos.writeObject(o);
276 >        oos.flush();
277 >        oos.close();
278 >        ByteArrayInputStream bin =
279 >            new ByteArrayInputStream(bos.toByteArray());
280 >        ObjectInputStream ois = new ObjectInputStream(bin);
281              return (T) ois.readObject();
283        } catch(Exception e) {
284            throw new IllegalArgumentException(e);
285        }
282      }
283  
284      private static void testRemove(Deque<Integer> deque) throws Exception {
285 <        Deque<Integer> copy = null;
286 <        switch(random() & 1) {
287 <          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 <        }
285 >        Deque<Integer> copy = ((random() & 1) == 0) ?
286 >            copyConstructorClone(deque) :
287 >            serialClone(deque);
288  
289          int numRemoved = 0;
290          for (Iterator<Integer> it = copy.iterator(); it.hasNext(); ) {
# Line 320 | Line 307 | public class DequeBash {
307                  throw new Exception(e + " missing.");
308  
309              boolean removed = false;
310 <            switch(random(3)) {
310 >            switch (random(3)) {
311                  case 0:  removed = copy.remove(e);                break;
312                  case 1:  removed = copy.removeFirstOccurrence(e); break;
313                  case 2:  removed = copy.removeLastOccurrence(e);  break;
# Line 335 | Line 322 | public class DequeBash {
322  
323          testEmpty(copy);
324  
325 <        copy = (Deque<Integer>) deque.getClass().
339 <            getConstructor(Collection.class).newInstance(deque);
325 >        copy = copyConstructorClone(deque);
326          copy.retainAll(deque);
327          testEqual(deque, copy);
328          copy.removeAll(deque);
# Line 368 | Line 354 | public class DequeBash {
354              boolean threw = false;
355              int result = 666;
356              try {
357 <                result = ((random() & 1) == 0?
357 >                result = ((random() & 1) == 0 ?
358                            deque.getFirst() : deque.element());
359 <            } catch(NoSuchElementException e) {
359 >            } catch (NoSuchElementException e) {
360                  threw = true;
361              }
362              if (!threw)
# Line 378 | Line 364 | public class DequeBash {
364              threw = false;
365              try {
366                  result = deque.getLast();
367 <            } catch(NoSuchElementException e) {
367 >            } catch (NoSuchElementException e) {
368                  threw = true;
369              }
370              if (!threw)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines