import java.util.ArrayDeque; import java.util.Iterator; import java.util.LinkedList; import java.util.NoSuchElementException; public class ArrayDequeTest { private static void verify(boolean val) { if (! val) { throw new RuntimeException(); } } public static void main(String[] args) throws InterruptedException { QueueHelper.sizeTest(new ArrayDeque()); QueueHelper.isEmptyTest(new ArrayDeque()); QueueHelper.addTest(new ArrayDeque()); QueueHelper.addAllTest(new ArrayDeque()); QueueHelper.elementTest(new ArrayDeque()); QueueHelper.elementFail(new ArrayDeque()); QueueHelper.removeEmptyFail(new ArrayDeque()); QueueHelper.removeTest(new ArrayDeque()); QueueHelper.containsTest(new ArrayDeque()); QueueHelper.containsAllTest(new ArrayDeque()); QueueHelper.removeObjectTest(new ArrayDeque()); QueueHelper.removeAllTest(new ArrayDeque()); QueueHelper.clearTest(new ArrayDeque()); QueueHelper.toArrayTest(new ArrayDeque()); DequeHelper.addFirstTest(new ArrayDeque()); DequeHelper.addLastTest(new ArrayDeque()); DequeHelper.removeFirstTest(new ArrayDeque()); DequeHelper.removeLastTest(new ArrayDeque()); iterateTest(false); iterateTest(true); iteratorRemoveTest(false); iteratorRemoveTest(true); iteratorNoElementFail(false); iteratorNoElementFail(true); } private static void iterateTest(boolean desc) { int testQty = 10; LinkedList compareList = new LinkedList(); ArrayDeque ad = new ArrayDeque(); for (int i = 0; i < testQty; i++) { Object o = new Object(); compareList.add(o); ad.add(o); } Iterator compIt; Iterator testIt; if (desc) { compIt = compareList.descendingIterator(); testIt = ad.descendingIterator(); } else { compIt = compareList.iterator(); testIt = ad.iterator(); } while (testIt.hasNext()) { verify(testIt.next() == compIt.next()); } // remove from the front compareList.removeFirst(); ad.removeFirst(); if (desc) { compIt = compareList.descendingIterator(); testIt = ad.descendingIterator(); } else { compIt = compareList.iterator(); testIt = ad.iterator(); } while (testIt.hasNext()) { verify(testIt.next() == compIt.next()); } // remove from the end compareList.removeLast(); ad.removeLast(); if (desc) { compIt = compareList.descendingIterator(); testIt = ad.descendingIterator(); } else { compIt = compareList.iterator(); testIt = ad.iterator(); } while (testIt.hasNext()) { verify(testIt.next() == compIt.next()); } } private static void iteratorRemoveTest(boolean desc) { int testQty = 20; LinkedList compareList = new LinkedList(); ArrayDeque ad = new ArrayDeque(); for (int i = 0; i < testQty; i++) { Object o = new Object(); compareList.add(o); ad.add(o); } Iterator compIt; Iterator testIt; if (desc) { compIt = compareList.descendingIterator(); testIt = ad.descendingIterator(); } else { compIt = compareList.iterator(); testIt = ad.iterator(); } boolean flip = true; // start with true to ensure first is removed while (testIt.hasNext()) { // advance iterators testIt.next(); compIt.next(); if (flip || ! testIt.hasNext()) { compIt.remove(); testIt.remove(); flip = false; } else { flip = true; } } if (desc) { compIt = compareList.descendingIterator(); testIt = ad.descendingIterator(); } else { compIt = compareList.iterator(); testIt = ad.iterator(); } while (testIt.hasNext()) { verify(testIt.next() == compIt.next()); } } private static void iteratorNoElementFail(boolean desc) { ArrayDeque ad = new ArrayDeque(); Iterator testIt; if (desc) { testIt = ad.descendingIterator(); } else { testIt = ad.iterator(); } try { testIt.next(); throw new RuntimeException("Exception should have thrown"); } catch (NoSuchElementException e) { // expected } } }