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.1 by dl, Mon May 2 19:19:38 2005 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 <    
26 <    static int random(int bound) {
25 >    static int random(int bound) {
26          int x = seed;
27          int t = (x % 127773) * 16807 - (x / 127773) * 2836;
28 <        seed = (t > 0)? t : t + 0x7fffffff;
28 >        seed = (t > 0) ? t : t + 0x7fffffff;
29          return (t & 0x7fffffff) % bound;
30      }
31  
32 <    static int random() {
32 >    static int random() {
33          int x = seed;
34          int t = (x % 127773) * 16807 - (x / 127773) * 2836;
35 <        seed = (t > 0)? t : t + 0x7fffffff;
35 >        seed = (t > 0) ? t : t + 0x7fffffff;
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 64 | Line 65 | public class DequeBash {
65              randomOp(deque);
66  
67              // Test iterator occasionally
68 <            if ((i & 1023) == 0)
68 >            if ((i & 1023) == 0) {
69                  testIter(deque);
70 +                testDescendingIter(deque);
71 +            }
72  
73              // Test serialization and copying
74              if ((i & 4095) == 0) {
75 <                testEqual(deque, deepCopy(deque));
76 <                testEqual(deque, (Deque<Integer>) deque.getClass().
74 <                          getConstructor(Collection.class).newInstance(deque));
75 >                testEqual(deque, serialClone(deque));
76 >                testEqual(deque, copyConstructorClone(deque));
77              }
78 <            
78 >
79              // Test fancy removal stuff once in a blue moon
80              if ((i & 8191) == 0)
81                  testRemove(deque);
82 <            
83 <         }
82 >
83 >        }
84  
85          // Stupid tests for clear, toString
86          deque.clear();
# Line 89 | Line 91 | public class DequeBash {
91              throw new Exception("Deque.toString():  " + deque.toString());
92      }
93  
94 <    static void testIter(Deque<Integer> deque) throws Exception {                
94 >    static void testIter(Deque<Integer> deque) throws Exception {
95          int next = nextHead + 1;
96          int count = 0;
97          for (int j : deque) {
# Line 101 | Line 103 | public class DequeBash {
103              throw new Exception("Count " + count + " != " + size());
104      }
105  
106 +    static void testDescendingIter(Deque<Integer> deque) throws Exception {
107 +        int next = deque.size() + nextHead;
108 +        int count = 0;
109 +        for (Iterator<Integer> it = deque.descendingIterator(); it.hasNext();) {
110 +            int j = it.next();
111 +            if (j != next--)
112 +                throw new Exception("Element "+ j + " != " + (next-1));
113 +            count++;
114 +        }
115 +        if (count != size())
116 +            throw new Exception("Count " + count + " != " + size());
117 +    }
118 +
119      static void sizeTests(Deque<Integer> deque) throws Exception {
120          if (deque.size() != size())
121              throw new Exception("Size: " + deque.size() +
# Line 109 | Line 124 | public class DequeBash {
124              throw new Exception(
125                                  "IsEmpty " + deque.isEmpty() + ", size " + size());
126          // Check head and tail
127 <        if (size() == 0)
127 >        if (size() == 0)
128              testEmpty(deque);
129 <        else
129 >        else
130              nonEmptyTest(deque);
131  
132      }
133  
134 <    static void nonEmptyTest(Deque<Integer> deque) throws Exception {            
134 >    static void nonEmptyTest(Deque<Integer> deque) throws Exception {
135          if (deque.getFirst() != nextHead + 1)
136 <            throw new Exception("getFirst got: " +
136 >            throw new Exception("getFirst got: " +
137                                  deque.getFirst() + " expecting " + (nextHead + 1));
138          if (deque.element() != nextHead + 1)
139              throw new Exception("element got: " + deque.element() +
# Line 129 | Line 144 | public class DequeBash {
144          if (deque.peek() != nextHead + 1)
145              throw new Exception("peek got: " +  deque.peek() +
146                                  " expecting " + (nextHead + 1));
147 <        
147 >
148          if (deque.peekLast() != nextTail - 1)
149              throw new Exception("peekLast got: " + deque.peekLast() +
150                                  " expecting " + (nextTail - 1));
151          if (deque.getLast() != nextTail - 1)
152 <            throw new Exception("getLast got: " +
152 >            throw new Exception("getLast got: " +
153                                  deque.getLast() + " expecting " + (nextTail - 1));
154 <    }        
140 <    
154 >    }
155  
156      static void randomOp(Deque<Integer> deque) throws Exception {
157 <        
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 157 | 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;
178                      default: throw new Exception("How'd we get here");
179                      }
180 <                } catch(NoSuchElementException e) {
180 >                } catch (NoSuchElementException e) {
181                      threw = true;
182                  }
183                  if (!threw)
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 184 | 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 197 | Line 211 | public class DequeBash {
211                  boolean threw = false;
212                  try {
213                      result = deque.removeLast();
214 <                } catch(NoSuchElementException e) {
214 >                } catch (NoSuchElementException e) {
215                      threw = true;
216                  }
217                  if (!threw)
218                      throw new Exception("Remove-no exception: " + result);
219              } else { // deque nonempty
220 <                int result = ((random() & 1) == 0?
220 >                int result = ((random() & 1) == 0 ?
221                                deque.removeLast() : deque.pollLast());
222                  if (result != --nextTail)
223                      throw new Exception(
224 <                                        "Removed "+ result + " expecting "+(nextTail + 1));
224 >                        "Removed "+ result + " expecting "+(nextTail + 1));
225              }
226              break;
227          default:
# Line 215 | Line 229 | public class DequeBash {
229          }
230      }
231  
218
232      private static void testEqual(Deque<Integer> d1, Deque<Integer> d2)
233          throws Exception
234      {
235          if (d1.size() != d2.size())
236              throw new Exception("Size " + d1.size() + " != " + d2.size());
237          Iterator<Integer> it = d2.iterator();
238 <        for(int i : d1) {
238 >        for (int i : d1) {
239              int j = it.next();
240              if (j != i)
241                  throw new Exception("Element " + j + " != " + i);
242          }
243  
244 <        for(int i : d1)
244 >        for (int i : d1)
245              if (!d2.contains(i))
246                  throw new Exception("d2 doesn't contain " + i);
247 <        for(int i : d2)
247 >        for (int i : d2)
248              if (!d1.contains(i))
249                  throw new Exception("d2 doesn't contain " + i);
250  
# Line 255 | 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();
268        } catch(Exception e) {
269            throw new IllegalArgumentException(e);
270        }
287      }
288  
289      private static void testRemove(Deque<Integer> deque) throws Exception {
290 <        Deque<Integer> copy = null;
291 <        switch(random() & 1) {
292 <          case 0:
277 <            copy = (Deque<Integer>) deque.getClass().
278 <                getConstructor(Collection.class).newInstance(deque);
279 <            break;
280 <          case 1:
281 <            copy = deepCopy(deque);
282 <            break;
283 <          default:
284 <            throw new Exception("How'd we get here");
285 <        }
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 305 | 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;
319 <                default: throw new Exception("How'd we get here");            
319 >                default: throw new Exception("How'd we get here");
320              }
321              if (!removed)
322                  throw new Exception(e + " not removed.");
# Line 320 | Line 327 | public class DequeBash {
327  
328          testEmpty(copy);
329  
330 <        copy = (Deque<Integer>) deque.getClass().
324 <            getConstructor(Collection.class).newInstance(deque);
330 >        copy = copyConstructorClone(deque);
331          copy.retainAll(deque);
332          testEqual(deque, copy);
333          copy.removeAll(deque);
# Line 353 | Line 359 | public class DequeBash {
359              boolean threw = false;
360              int result = 666;
361              try {
362 <                result = ((random() & 1) == 0?
362 >                result = ((random() & 1) == 0 ?
363                            deque.getFirst() : deque.element());
364 <            } catch(NoSuchElementException e) {
364 >            } catch (NoSuchElementException e) {
365                  threw = true;
366              }
367              if (!threw)
# Line 363 | Line 369 | public class DequeBash {
369              threw = false;
370              try {
371                  result = deque.getLast();
372 <            } catch(NoSuchElementException e) {
372 >            } catch (NoSuchElementException e) {
373                  threw = true;
374              }
375              if (!threw)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines