/* * WP: the test-class is adapted so that it can be run without the * original parent class JSR166TestCase, and can be run from * Junit runner. This makes it convenient for the benchmark tool. * * The class JSR166TestCase is kept in the directory just for reader's * reference. * * Original: * * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. * * Some note copied from JSR166TestCase: * * Usually, there is one testcase method per JSR166 method * covering "normal" operation, and then as many exception-testing * methods as there are exceptions the method can throw. Sometimes * there are multiple tests per JSR166 method when the different * "normal" behaviors differ significantly. And sometimes testcases * cover multiple methods when they cannot be tested in * isolation. */ package Examples ; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; //import java.util.LinkedList; import java.util.NoSuchElementException; import org.junit.* ; import static org.junit.Assert.* ; public class TestJavaLinkedList { /** * The number of elements to place in collections, arrays, etc. */ public static final int SIZE = 20; // Some convenient Integer constants public static final Integer zero = new Integer(0); public static final Integer one = new Integer(1); public static final Integer two = new Integer(2); public static final Integer three = new Integer(3); public static final Integer four = new Integer(4); public static final Integer five = new Integer(5); public static final Integer six = new Integer(6); public static final Integer seven = new Integer(7); public static final Integer eight = new Integer(8); public static final Integer nine = new Integer(9); public static final Integer m1 = new Integer(-1); public static final Integer m2 = new Integer(-2); public static final Integer m3 = new Integer(-3); public static final Integer m4 = new Integer(-4); public static final Integer m5 = new Integer(-5); public static final Integer m6 = new Integer(-6); public static final Integer m10 = new Integer(-10); /** * Returns a new queue of given size containing consecutive * Integers 0 ... n. */ private JavaLinkedList populatedQueue(int n) { JavaLinkedList q = new JavaLinkedList(); assertTrue(q.isEmpty()); for (int i = 0; i < n; ++i) assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(n, q.size()); return q; } /** * new queue is empty */ @Test public void testConstructor1() { assertEquals(0, new JavaLinkedList().size()); } /** * Initializing from null Collection throws NPE */ @Test public void testConstructor3() { try { JavaLinkedList q = new JavaLinkedList((Collection)null); fail("should have thrown an exception") /* shouldThrow() */; } catch (NullPointerException success) {} } /** * Queue contains all elements of collection used to initialize */ @Test public void testConstructor6() { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE; ++i) ints[i] = i; JavaLinkedList q = new JavaLinkedList(Arrays.asList(ints)); for (int i = 0; i < SIZE; ++i) assertEquals(ints[i], q.poll()); } /** * isEmpty is true before add, false after */ @Test public void testEmpty() { JavaLinkedList q = new JavaLinkedList(); assertTrue(q.isEmpty()); q.add(new Integer(1)); assertFalse(q.isEmpty()); q.add(new Integer(2)); q.remove(); q.remove(); assertTrue(q.isEmpty()); } /** * size changes when elements added and removed */ @Test public void testSize() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(SIZE-i, q.size()); q.remove(); } for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.size()); q.add(new Integer(i)); } } /** * offer(null) succeeds */ @Test public void testOfferNull() { JavaLinkedList q = new JavaLinkedList(); q.offer(null); } /** * Offer succeeds */ @Test public void testOffer() { JavaLinkedList q = new JavaLinkedList(); assertTrue(q.offer(new Integer(0))); assertTrue(q.offer(new Integer(1))); } /** * add succeeds */ @Test public void testAdd() { JavaLinkedList q = new JavaLinkedList(); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.size()); assertTrue(q.add(new Integer(i))); } } /** * addAll(null) throws NPE */ @Test public void testAddAll1() { try { JavaLinkedList q = new JavaLinkedList(); q.addAll(null); fail("should have thrown an exception") /* shouldThrow() */; } catch (NullPointerException success) {} } /** * Queue contains all elements, in traversal order, of successful addAll */ @Test public void testAddAll5() { Integer[] empty = new Integer[0]; Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE; ++i) ints[i] = i; JavaLinkedList q = new JavaLinkedList(); assertFalse(q.addAll(Arrays.asList(empty))); assertTrue(q.addAll(Arrays.asList(ints))); for (int i = 0; i < SIZE; ++i) assertEquals(ints[i], q.poll()); } /** * addAll with too large an index throws IOOBE */ @Test public void testAddAll2_IndexOutOfBoundsException() { JavaLinkedList l = new JavaLinkedList(); l.add(new Object()); JavaLinkedList m = new JavaLinkedList(); m.add(new Object()); try { l.addAll(4,m); fail("should have thrown an exception") /* shouldThrow() */; } catch (IndexOutOfBoundsException success) {} } /** * addAll with negative index throws IOOBE */ @Test public void testAddAll4_BadIndex() { JavaLinkedList l = new JavaLinkedList(); l.add(new Object()); JavaLinkedList m = new JavaLinkedList(); m.add(new Object()); try { l.addAll(-1,m); fail("should have thrown an exception") /* shouldThrow() */; } catch (IndexOutOfBoundsException success) {} } /** * poll succeeds unless empty */ @Test public void testPoll() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.poll()); } assertNull(q.poll()); } /** * peek returns next element, or null if empty */ @Test public void testPeek() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.peek()); assertEquals(i, q.poll()); assertTrue(q.peek() == null || !q.peek().equals(i)); } assertNull(q.peek()); } /** * element returns next element, or throws NSEE if empty */ @Test public void testElement() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.element()); assertEquals(i, q.poll()); } try { q.element(); fail("should have thrown an exception") /* shouldThrow() */; } catch (NoSuchElementException success) {} } /** * remove removes next element, or throws NSEE if empty */ @Test public void testRemove() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.remove()); } try { q.remove(); fail("should have thrown an exception") /* shouldThrow() */; } catch (NoSuchElementException success) {} } /** * remove(x) removes x and returns true if present */ @Test public void testRemoveElement() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 1; i < SIZE; i+=2) { assertTrue(q.contains(i)); assertTrue(q.remove((Integer)i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } for (int i = 0; i < SIZE; i+=2) { assertTrue(q.contains(i)); assertTrue(q.remove((Integer)i)); assertFalse(q.contains(i)); assertFalse(q.remove((Integer)(i+1))); assertFalse(q.contains(i+1)); } assertTrue(q.isEmpty()); } /** * contains(x) reports true when elements added but not yet removed */ @Test public void testContains() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertTrue(q.contains(new Integer(i))); q.poll(); assertFalse(q.contains(new Integer(i))); } } /** * clear removes all elements */ @Test public void testClear() { JavaLinkedList q = populatedQueue(SIZE); q.clear(); assertTrue(q.isEmpty()); assertEquals(0, q.size()); assertTrue(q.add(new Integer(1))); assertFalse(q.isEmpty()); q.clear(); assertTrue(q.isEmpty()); } /** * containsAll(c) is true when c contains a subset of elements */ @Test public void testContainsAll() { JavaLinkedList q = populatedQueue(SIZE); JavaLinkedList p = new JavaLinkedList(); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); assertTrue(p.add(new Integer(i))); } assertTrue(p.containsAll(q)); } /** * retainAll(c) retains only those elements of c and reports true if changed */ @Test public void testRetainAll() { JavaLinkedList q = populatedQueue(SIZE); JavaLinkedList p = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { boolean changed = q.retainAll(p); if (i == 0) assertFalse(changed); else assertTrue(changed); assertTrue(q.containsAll(p)); assertEquals(SIZE-i, q.size()); p.remove(); } } /** * removeAll(c) removes only those elements of c and reports true if changed */ @Test public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { JavaLinkedList q = populatedQueue(SIZE); JavaLinkedList p = populatedQueue(i); assertTrue(q.removeAll(p)); assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { Integer I = (Integer)(p.remove()); assertFalse(q.contains(I)); } } } /** * toArray contains all elements in FIFO order */ @Test public void testToArray() { JavaLinkedList q = populatedQueue(SIZE); Object[] o = q.toArray(); for (int i = 0; i < o.length; i++) assertSame(o[i], q.poll()); } /** * toArray(a) contains all elements in FIFO order */ @Test public void testToArray2() { JavaLinkedList q = populatedQueue(SIZE); Integer[] ints = new Integer[SIZE]; Integer[] array = q.toArray(ints); assertSame(ints, array); for (int i = 0; i < ints.length; i++) assertSame(ints[i], q.poll()); } /** * toArray(null) throws NullPointerException */ @Test public void testToArray_NullArg() { JavaLinkedList l = new JavaLinkedList(); l.add(new Object()); try { l.toArray(null); fail("should have thrown an exception") /* shouldThrow() */; } catch (NullPointerException success) {} } /** * toArray(incompatible array type) throws ArrayStoreException */ @Test public void testToArray1_BadArg() { JavaLinkedList l = new JavaLinkedList(); l.add(new Integer(5)); try { l.toArray(new String[10]); fail("should have thrown an exception") /* shouldThrow() */; } catch (ArrayStoreException success) {} } /** * iterator iterates through all elements */ @Test public void testIterator() { JavaLinkedList q = populatedQueue(SIZE); int i = 0; Iterator it = q.iterator(); while (it.hasNext()) { assertTrue(q.contains(it.next())); ++i; } assertEquals(i, SIZE); } /** * iterator ordering is FIFO */ @Test public void testIteratorOrdering() { final JavaLinkedList q = new JavaLinkedList(); q.add(new Integer(1)); q.add(new Integer(2)); q.add(new Integer(3)); int k = 0; for (Iterator it = q.iterator(); it.hasNext();) { assertEquals(++k, it.next()); } assertEquals(3, k); } /** * iterator.remove removes current element */ @Test public void testIteratorRemove() { final JavaLinkedList q = new JavaLinkedList(); q.add(new Integer(1)); q.add(new Integer(2)); q.add(new Integer(3)); Iterator it = q.iterator(); assertEquals(1, it.next()); it.remove(); it = q.iterator(); assertEquals(2, it.next()); assertEquals(3, it.next()); assertFalse(it.hasNext()); } /** * Descending iterator iterates through all elements */ @Test public void testDescendingIterator() { JavaLinkedList q = populatedQueue(SIZE); int i = 0; Iterator it = q.descendingIterator(); while (it.hasNext()) { assertTrue(q.contains(it.next())); ++i; } assertEquals(i, SIZE); assertFalse(it.hasNext()); try { it.next(); fail("should have thrown an exception") /* shouldThrow() */; } catch (NoSuchElementException success) {} } /** * Descending iterator ordering is reverse FIFO */ @Test public void testDescendingIteratorOrdering() { final JavaLinkedList q = new JavaLinkedList(); q.add(new Integer(3)); q.add(new Integer(2)); q.add(new Integer(1)); int k = 0; for (Iterator it = q.descendingIterator(); it.hasNext();) { assertEquals(++k, it.next()); } assertEquals(3, k); } /** * descendingIterator.remove removes current element */ @Test public void testDescendingIteratorRemove() { final JavaLinkedList q = new JavaLinkedList(); q.add(three); q.add(two); q.add(one); Iterator it = q.descendingIterator(); it.next(); it.remove(); it = q.descendingIterator(); assertSame(it.next(), two); assertSame(it.next(), three); assertFalse(it.hasNext()); } /** * toString contains toStrings of elements */ @Test public void testToString() { JavaLinkedList q = populatedQueue(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { assertTrue(s.contains(String.valueOf(i))); } } /** * peek returns element inserted with addFirst */ @Test public void testAddFirst() { JavaLinkedList q = populatedQueue(3); q.addFirst(four); assertSame(four, q.peek()); } /** * peekFirst returns element inserted with push */ @Test public void testPush() { JavaLinkedList q = populatedQueue(3); q.push(four); assertSame(four, q.peekFirst()); } /** * pop removes next element, or throws NSEE if empty */ @Test public void testPop() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.pop()); } try { q.pop(); fail("should have thrown an exception") /* shouldThrow() */; } catch (NoSuchElementException success) {} } /** * OfferFirst succeeds */ @Test public void testOfferFirst() { JavaLinkedList q = new JavaLinkedList(); assertTrue(q.offerFirst(new Integer(0))); assertTrue(q.offerFirst(new Integer(1))); } /** * OfferLast succeeds */ @Test public void testOfferLast() { JavaLinkedList q = new JavaLinkedList(); assertTrue(q.offerLast(new Integer(0))); assertTrue(q.offerLast(new Integer(1))); } /** * pollLast succeeds unless empty */ @Test public void testPollLast() { JavaLinkedList q = populatedQueue(SIZE); for (int i = SIZE-1; i >= 0; --i) { assertEquals(i, q.pollLast()); } assertNull(q.pollLast()); } /** * peekFirst returns next element, or null if empty */ @Test public void testPeekFirst() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.peekFirst()); assertEquals(i, q.pollFirst()); assertTrue(q.peekFirst() == null || !q.peekFirst().equals(i)); } assertNull(q.peekFirst()); } /** * peekLast returns next element, or null if empty */ @Test public void testPeekLast() { JavaLinkedList q = populatedQueue(SIZE); for (int i = SIZE-1; i >= 0; --i) { assertEquals(i, q.peekLast()); assertEquals(i, q.pollLast()); assertTrue(q.peekLast() == null || !q.peekLast().equals(i)); } assertNull(q.peekLast()); } @Test public void testFirstElement() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.getFirst()); assertEquals(i, q.pollFirst()); } try { q.getFirst(); fail("should have thrown an exception") /* shouldThrow() */; } catch (NoSuchElementException success) {} } /** * getLast returns next element, or throws NSEE if empty */ @Test public void testLastElement() { JavaLinkedList q = populatedQueue(SIZE); for (int i = SIZE-1; i >= 0; --i) { assertEquals(i, q.getLast()); assertEquals(i, q.pollLast()); } try { q.getLast(); fail("should have thrown an exception") /* shouldThrow() */; } catch (NoSuchElementException success) {} assertNull(q.peekLast()); } /** * removeFirstOccurrence(x) removes x and returns true if present */ @Test public void testRemoveFirstOccurrence() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 1; i < SIZE; i+=2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); } for (int i = 0; i < SIZE; i+=2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); assertFalse(q.removeFirstOccurrence(new Integer(i+1))); } assertTrue(q.isEmpty()); } /** * removeLastOccurrence(x) removes x and returns true if present */ @Test public void testRemoveLastOccurrence() { JavaLinkedList q = populatedQueue(SIZE); for (int i = 1; i < SIZE; i+=2) { assertTrue(q.removeLastOccurrence(new Integer(i))); } for (int i = 0; i < SIZE; i+=2) { assertTrue(q.removeLastOccurrence(new Integer(i))); assertFalse(q.removeLastOccurrence(new Integer(i+1))); } assertTrue(q.isEmpty()); } }