--- jsr166/src/test/loops/DequeBash.java 2005/05/02 19:19:38 1.1 +++ jsr166/src/test/loops/DequeBash.java 2009/11/02 23:42:46 1.4 @@ -22,18 +22,18 @@ public class DequeBash { static int nextHead = -1; static int size() { return nextTail - nextHead - 1; } - - static int random(int bound) { + + static int random(int bound) { int x = seed; int t = (x % 127773) * 16807 - (x / 127773) * 2836; - seed = (t > 0)? t : t + 0x7fffffff; + seed = (t > 0) ? t : t + 0x7fffffff; return (t & 0x7fffffff) % bound; } - static int random() { + static int random() { int x = seed; int t = (x % 127773) * 16807 - (x / 127773) * 2836; - seed = (t > 0)? t : t + 0x7fffffff; + seed = (t > 0) ? t : t + 0x7fffffff; return (t & 0x7fffffff); } @@ -64,8 +64,10 @@ public class DequeBash { randomOp(deque); // Test iterator occasionally - if ((i & 1023) == 0) + if ((i & 1023) == 0) { testIter(deque); + testDescendingIter(deque); + } // Test serialization and copying if ((i & 4095) == 0) { @@ -73,11 +75,11 @@ public class DequeBash { testEqual(deque, (Deque) deque.getClass(). getConstructor(Collection.class).newInstance(deque)); } - + // Test fancy removal stuff once in a blue moon if ((i & 8191) == 0) testRemove(deque); - + } // Stupid tests for clear, toString @@ -89,7 +91,7 @@ public class DequeBash { throw new Exception("Deque.toString(): " + deque.toString()); } - static void testIter(Deque deque) throws Exception { + static void testIter(Deque deque) throws Exception { int next = nextHead + 1; int count = 0; for (int j : deque) { @@ -101,6 +103,19 @@ public class DequeBash { throw new Exception("Count " + count + " != " + size()); } + static void testDescendingIter(Deque deque) throws Exception { + int next = deque.size() + nextHead; + int count = 0; + for (Iterator it = deque.descendingIterator(); it.hasNext();) { + int j = it.next(); + if (j != next--) + throw new Exception("Element "+ j + " != " + (next-1)); + count++; + } + if (count != size()) + throw new Exception("Count " + count + " != " + size()); + } + static void sizeTests(Deque deque) throws Exception { if (deque.size() != size()) throw new Exception("Size: " + deque.size() + @@ -109,16 +124,16 @@ public class DequeBash { throw new Exception( "IsEmpty " + deque.isEmpty() + ", size " + size()); // Check head and tail - if (size() == 0) + if (size() == 0) testEmpty(deque); - else + else nonEmptyTest(deque); } - static void nonEmptyTest(Deque deque) throws Exception { + static void nonEmptyTest(Deque deque) throws Exception { if (deque.getFirst() != nextHead + 1) - throw new Exception("getFirst got: " + + throw new Exception("getFirst got: " + deque.getFirst() + " expecting " + (nextHead + 1)); if (deque.element() != nextHead + 1) throw new Exception("element got: " + deque.element() + @@ -129,18 +144,18 @@ public class DequeBash { if (deque.peek() != nextHead + 1) throw new Exception("peek got: " + deque.peek() + " expecting " + (nextHead + 1)); - + if (deque.peekLast() != nextTail - 1) throw new Exception("peekLast got: " + deque.peekLast() + " expecting " + (nextTail - 1)); if (deque.getLast() != nextTail - 1) - throw new Exception("getLast got: " + + throw new Exception("getLast got: " + deque.getLast() + " expecting " + (nextTail - 1)); - } - + } + static void randomOp(Deque deque) throws Exception { - + // Perform a random operation switch(random() & 3) { case 0: @@ -163,7 +178,7 @@ public class DequeBash { case 2: result = deque.pop(); break; default: throw new Exception("How'd we get here"); } - } catch(NoSuchElementException e) { + } catch (NoSuchElementException e) { threw = true; } if (!threw) @@ -197,17 +212,17 @@ public class DequeBash { boolean threw = false; try { result = deque.removeLast(); - } catch(NoSuchElementException e) { + } catch (NoSuchElementException e) { threw = true; } if (!threw) throw new Exception("Remove-no exception: " + result); } else { // deque nonempty - int result = ((random() & 1) == 0? + int result = ((random() & 1) == 0 ? deque.removeLast() : deque.pollLast()); if (result != --nextTail) throw new Exception( - "Removed "+ result + " expecting "+(nextTail + 1)); + "Removed "+ result + " expecting "+(nextTail + 1)); } break; default: @@ -222,16 +237,16 @@ public class DequeBash { if (d1.size() != d2.size()) throw new Exception("Size " + d1.size() + " != " + d2.size()); Iterator it = d2.iterator(); - for(int i : d1) { + for (int i : d1) { int j = it.next(); if (j != i) throw new Exception("Element " + j + " != " + i); } - for(int i : d1) + for (int i : d1) if (!d2.contains(i)) throw new Exception("d2 doesn't contain " + i); - for(int i : d2) + for (int i : d2) if (!d1.contains(i)) throw new Exception("d2 doesn't contain " + i); @@ -265,7 +280,7 @@ public class DequeBash { bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bin); return (T) ois.readObject(); - } catch(Exception e) { + } catch (Exception e) { throw new IllegalArgumentException(e); } } @@ -309,7 +324,7 @@ public class DequeBash { case 0: removed = copy.remove(e); break; case 1: removed = copy.removeFirstOccurrence(e); break; case 2: removed = copy.removeLastOccurrence(e); break; - default: throw new Exception("How'd we get here"); + default: throw new Exception("How'd we get here"); } if (!removed) throw new Exception(e + " not removed."); @@ -353,9 +368,9 @@ public class DequeBash { boolean threw = false; int result = 666; try { - result = ((random() & 1) == 0? + result = ((random() & 1) == 0 ? deque.getFirst() : deque.element()); - } catch(NoSuchElementException e) { + } catch (NoSuchElementException e) { threw = true; } if (!threw) @@ -363,7 +378,7 @@ public class DequeBash { threw = false; try { result = deque.getLast(); - } catch(NoSuchElementException e) { + } catch (NoSuchElementException e) { threw = true; } if (!threw)