import java.util.Iterator; import java.util.NoSuchElementException; import org.junit.AfterClass; import org.junit.Test; import static org.junit.Assert.*; public class Testing { private static int points = 0; @Test public void testAddAndIterator(){ MyLinkedList l = new MyLinkedList<>(); assertTrue(l.add(1)); for (int i = 2; i < 100; i++) assertTrue(l.add(i)); Iterator it = l.iterator(); for (int i = 1; i < 100; i++) { assertTrue(it.hasNext()); assertEquals(new Integer(i), it.next()); } l = new MyLinkedList<>(); for (int i = 0; i < 1000000; i++) l.add(i); points += 10; } @Test public void testAddLast(){ MyLinkedList l = new MyLinkedList<>(); l.addLast(1); for (int i = 2; i < 100; i++) l.addLast(i); Iterator it = l.iterator(); for (int i = 1; i < 100; i++) { assertTrue(it.hasNext()); assertEquals(new Integer(i), it.next()); } l = new MyLinkedList<>(); for (int i = 0; i < 1000000; i++) l.addLast(i); points += 1; } @Test public void testOffer(){ MyLinkedList l = new MyLinkedList<>(); assertTrue(l.offer(1)); for (int i = 2; i < 100; i++) assertTrue(l.offer(i)); Iterator it = l.iterator(); for (int i = 1; i < 100; i++) { assertTrue(it.hasNext()); assertEquals(new Integer(i), it.next()); } l = new MyLinkedList<>(); for (int i = 0; i < 1000000; i++) l.offer(i); points += 1; } @Test public void testOfferLast(){ MyLinkedList l = new MyLinkedList<>(); assertTrue(l.offerLast(1)); for (int i = 2; i < 100; i++) assertTrue(l.offerLast(i)); Iterator it = l.iterator(); for (int i = 1; i < 100; i++) { assertTrue(it.hasNext()); assertEquals(new Integer(i), it.next()); } l = new MyLinkedList<>(); for (int i = 0; i < 1000000; i++) l.addLast(i); points += 1; } @Test public void testAddFirst(){ MyLinkedList l = new MyLinkedList<>(); l.addFirst(1); for (int i = 2; i < 100; i++) l.addFirst(i); Iterator it = l.iterator(); for (int i = 1; i < 100; i++) { assertTrue(it.hasNext()); assertEquals(new Integer(100-i), it.next()); } points += 1; } @Test public void testPush(){ MyLinkedList l = new MyLinkedList<>(); l.push(1); for (int i = 2; i < 100; i++) l.push(i); Iterator it = l.iterator(); for (int i = 1; i < 100; i++) { assertTrue(it.hasNext()); assertEquals(new Integer(100-i), it.next()); } points += 1; } @Test public void testOfferFirst(){ MyLinkedList l = new MyLinkedList<>(); assertTrue(l.offerFirst(1)); for (int i = 2; i < 100; i++) assertTrue(l.offerFirst(i)); Iterator it = l.iterator(); for (int i = 1; i < 100; i++) { assertTrue(it.hasNext()); assertEquals(new Integer(100-i), it.next()); } points += 1; } @Test public void testSize(){ MyLinkedList l = new MyLinkedList<>(); assertEquals(0, l.size()); for (int i = 1; i < 100; i++) { l.add(i); assertEquals(i, l.size()); } points += 5; } @Test public void testClear(){ MyLinkedList l = new MyLinkedList<>(); l.add(1); assertEquals(1, l.size()); l.clear(); assertEquals(0, l.size()); for (int i = 1; i < 50; i++) { l.add(i); assertEquals(i, l.size()); } l.clear(); assertEquals(0, l.size()); for (int i = 50; i < 100; i++) { l.add(i); assertEquals(i-49, l.size()); } l.clear(); assertEquals(0, l.size()); points += 2; } @Test public void testElement(){ MyLinkedList l = new MyLinkedList<>(); try { l.element(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } for (int i = 1; i < 100; i++) { l.addFirst(i); assertEquals(new Integer(i), l.element()); } assertEquals(99, l.size()); l.clear(); try { l.element(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } points += 2; } @Test public void testGetFirst(){ MyLinkedList l = new MyLinkedList<>(); try { l.getFirst(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } for (int i = 1; i < 100; i++) { l.addFirst(i); assertEquals(new Integer(i), l.getFirst()); } assertEquals(99, l.size()); l.clear(); try { l.getFirst(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } points += 1; } @Test public void testPeek(){ MyLinkedList l = new MyLinkedList<>(); assertEquals(null, l.peek()); for (int i = 1; i < 100; i++) { l.addFirst(i); assertEquals(new Integer(i), l.peek()); } assertEquals(99, l.size()); l.clear(); assertEquals(null, l.peek()); points += 2; } @Test public void testPeekFirst(){ MyLinkedList l = new MyLinkedList<>(); assertEquals(null, l.peekFirst()); for (int i = 1; i < 100; i++) { l.addFirst(i); assertEquals(new Integer(i), l.peekFirst()); } assertEquals(99, l.size()); l.clear(); assertEquals(null, l.peekFirst()); points += 1; } @Test public void testGetLast(){ MyLinkedList l = new MyLinkedList<>(); try { l.getLast(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } for (int i = 1; i < 100; i++) { l.add(i); assertEquals(new Integer(i), l.getLast()); } assertEquals(99, l.size()); l.clear(); try { l.getLast(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } points += 2; } @Test public void testPeekLast(){ MyLinkedList l = new MyLinkedList<>(); assertEquals(null, l.peekLast()); for (int i = 1; i < 100; i++) { l.add(i); assertEquals(new Integer(i), l.peekLast()); } assertEquals(99, l.size()); l.clear(); assertEquals(null, l.peekLast()); points += 2; } @Test public void testPoll(){ MyLinkedList l = new MyLinkedList<>(); assertEquals(null, l.poll()); l.add(42); assertEquals(new Integer(42), l.poll()); assertEquals(0, l.size()); assertEquals(null, l.poll()); for (int i = 1; i < 100; i++) { l.add(i); } assertEquals(99, l.size()); for (int i = 1; i < 100; i++) { assertEquals(new Integer(i), l.poll()); } assertEquals(0, l.size()); points += 5; } @Test public void testPollFirst(){ MyLinkedList l = new MyLinkedList<>(); assertEquals(null, l.pollFirst()); l.add(42); assertEquals(new Integer(42), l.pollFirst()); assertEquals(0, l.size()); assertEquals(null, l.pollFirst()); for (int i = 1; i < 100; i++) { l.add(i); } assertEquals(99, l.size()); for (int i = 1; i < 100; i++) { assertEquals(new Integer(i), l.pollFirst()); } assertEquals(0, l.size()); points += 1; } @Test public void testRemove(){ MyLinkedList l = new MyLinkedList<>(); try { l.remove(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } l.add(42); assertEquals(new Integer(42), l.remove()); assertEquals(0, l.size()); try { l.remove(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } for (int i = 1; i < 100; i++) { l.add(i); } assertEquals(99, l.size()); for (int i = 1; i < 100; i++) { assertEquals(new Integer(i), l.remove()); } assertEquals(0, l.size()); try { l.remove(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } points += 2; } @Test public void testPop(){ MyLinkedList l = new MyLinkedList<>(); try { l.pop(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } l.add(42); assertEquals(new Integer(42), l.pop()); assertEquals(0, l.size()); try { l.pop(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } for (int i = 1; i < 100; i++) { l.add(i); } assertEquals(99, l.size()); for (int i = 1; i < 100; i++) { assertEquals(new Integer(i), l.pop()); } assertEquals(0, l.size()); try { l.pop(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } points += 1; } @Test public void testRemoveFirst(){ MyLinkedList l = new MyLinkedList<>(); try { l.removeFirst(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } l.add(42); assertEquals(new Integer(42), l.removeFirst()); assertEquals(0, l.size()); try { l.removeFirst(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } for (int i = 1; i < 100; i++) { l.add(i); } assertEquals(99, l.size()); for (int i = 1; i < 100; i++) { assertEquals(new Integer(i), l.removeFirst()); } assertEquals(0, l.size()); try { l.removeFirst(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } points += 1; } @Test public void testPollLast(){ MyLinkedList l = new MyLinkedList<>(); assertEquals(null, l.pollLast()); l.add(42); assertEquals(new Integer(42), l.pollLast()); assertEquals(0, l.size()); assertEquals(null, l.pollLast()); for (int i = 1; i < 100; i++) { l.add(i); } assertEquals(99, l.size()); for (int i = 99; i >= 1; i--) { assertEquals(new Integer(i), l.pollLast()); } assertEquals(0, l.size()); assertEquals(null, l.pollLast()); points += 5; } @Test public void testRemoveLast(){ MyLinkedList l = new MyLinkedList<>(); try { l.removeLast(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } l.add(42); assertEquals(new Integer(42), l.removeLast()); assertEquals(0, l.size()); try { l.removeLast(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } for (int i = 1; i < 100; i++) { l.add(i); } assertEquals(99, l.size()); for (int i = 99; i >= 1; i--) { assertEquals(new Integer(i), l.removeLast()); } assertEquals(0, l.size()); try { l.removeLast(); fail("Did not throw NoSuchElementException"); } catch (Exception e){ if (!(e instanceof NoSuchElementException)) { fail("Did not throw NoSuchElementException"); } } points += 2; } // >>>>>>>>>>>>>>> @Test public void testDescendingIterator(){ MyLinkedList l = new MyLinkedList<>(); assertTrue(l.add(1)); for (int i = 2; i < 100; i++) assertTrue(l.add(i)); Iterator it = l.descendingIterator(); for (int i = 99; i >= 1; i--) { assertTrue(it.hasNext()); assertEquals(new Integer(i), it.next()); } points += 4; } @Test public void testContains(){ MyLinkedList l = new MyLinkedList<>(); assertFalse(l.contains(42)); l.add(1); assertFalse(l.contains(42)); for (int i = 2; i < 100; i++) l.add(i); assertTrue(l.contains(42)); assertFalse(l.contains(101)); points += 2; } @Test public void testIndexOf(){ MyLinkedList l = new MyLinkedList<>(); assertEquals(-1, l.indexOf(42)); l.add(1); assertEquals(-1, l.indexOf(42)); for (int i = 2; i < 100; i++) l.add(i); for (int i = 2; i < 100; i++) l.add(i); assertEquals(2, l.indexOf(3)); assertEquals(-1, l.indexOf(101)); points += 3; } @Test public void testLastIndexOf(){ MyLinkedList l = new MyLinkedList<>(); assertEquals(-1, l.lastIndexOf(42)); l.add(1); assertEquals(-1, l.lastIndexOf(42)); for (int i = 2; i < 100; i++) l.add(i); assertEquals(2, l.lastIndexOf(3)); for (int i = 2; i < 100; i++) l.add(i); assertEquals(100, l.lastIndexOf(3)); assertEquals(-1, l.lastIndexOf(101)); points += 3; } @Test public void testGet(){ MyLinkedList l = new MyLinkedList<>(); try { l.get(0); fail("Did not throw IndexOutOfBoundsException"); } catch (Exception e){ if (!(e instanceof IndexOutOfBoundsException)) { fail("Did not throw IndexOutOfBoundsException"); } } l.add(1); try { l.get(-1); fail("Did not throw IndexOutOfBoundsException"); } catch (Exception e){ if (!(e instanceof IndexOutOfBoundsException)) { fail("Did not throw IndexOutOfBoundsException"); } } try { l.get(1); fail("Did not throw IndexOutOfBoundsException"); } catch (Exception e){ if (!(e instanceof IndexOutOfBoundsException)) { fail("Did not throw IndexOutOfBoundsException"); } } for (int i = 2; i < 100; i++) l.add(i); assertEquals(new Integer(3), l.get(2)); assertEquals(new Integer(99), l.get(98)); points += 5; } @Test public void testToArray(){ MyLinkedList l = new MyLinkedList<>(); for (int i = 2; i < 100; i++) l.add(i); Object a[] = l.toArray(); assertEquals(98, a.length); for (int i = 0; i < 98; i++) { assertEquals(i+2, a[i]); } points += 3; } @Test public void testToString(){ MyLinkedList l = new MyLinkedList<>(); assertEquals("[]", l.toString()); String s = "["; for (int i = 2; i < 100; i++) { l.add(i); s+= i + ", "; } l.add(100); s+= 100 + "]"; assertEquals(s, l.toString()); points += 3; } @Test public void testRemoveObject(){ MyLinkedList l = new MyLinkedList<>(); assertFalse(l.remove(new Integer(42))); l.add(1); assertFalse(l.remove(new Integer(42))); assertTrue(l.remove(new Integer(1))); assertFalse(l.remove(new Integer(1))); assertEquals(0, l.size()); for (int i = 1; i < 50; i++) l.add(i); for (int i = 51; i < 100; i++) l.add(i); assertFalse(l.remove(new Integer(50))); assertTrue(l.remove(new Integer(1))); assertFalse(l.remove(new Integer(1))); assertTrue(l.remove(new Integer(2))); assertTrue(l.remove(new Integer(99))); assertFalse(l.remove(new Integer(99))); assertTrue(l.remove(new Integer(98))); points += 5; } @Test public void testRemoveFirstOccurence(){ MyLinkedList l = new MyLinkedList<>(); assertFalse(l.removeFirstOccurrence(new Integer(42))); l.add(1); assertFalse(l.removeFirstOccurrence(new Integer(42))); assertTrue(l.removeFirstOccurrence(new Integer(1))); assertFalse(l.removeFirstOccurrence(new Integer(1))); assertEquals(0, l.size()); for (int i = 1; i < 50; i++) l.add(i); for (int i = 51; i < 100; i++) l.add(i); assertFalse(l.removeFirstOccurrence(new Integer(50))); assertTrue(l.removeFirstOccurrence(new Integer(1))); assertFalse(l.removeFirstOccurrence(new Integer(1))); assertTrue(l.removeFirstOccurrence(new Integer(2))); assertTrue(l.removeFirstOccurrence(new Integer(99))); assertFalse(l.removeFirstOccurrence(new Integer(99))); assertTrue(l.removeFirstOccurrence(new Integer(98))); points += 1; } @Test public void testRemoveLastOccurence(){ MyLinkedList l = new MyLinkedList<>(); assertFalse(l.removeLastOccurrence(new Integer(42))); l.add(1); assertFalse(l.removeLastOccurrence(new Integer(42))); assertTrue(l.removeLastOccurrence(new Integer(1))); assertFalse(l.removeLastOccurrence(new Integer(1))); assertEquals(0, l.size()); for (int i = 1; i < 50; i++) l.add(i); for (int i = 1; i < 50; i++) l.add(i); assertEquals(new Integer(12), l.get(60)); assertTrue(l.removeLastOccurrence(new Integer(12))); assertEquals(new Integer(13), l.get(60)); assertTrue(l.removeLastOccurrence(new Integer(12))); assertFalse(l.removeLastOccurrence(new Integer(12))); assertTrue(l.removeLastOccurrence(new Integer(1))); assertTrue(l.removeLastOccurrence(new Integer(1))); assertFalse(l.removeLastOccurrence(new Integer(1))); assertTrue(l.removeLastOccurrence(new Integer(2))); assertTrue(l.removeLastOccurrence(new Integer(49))); assertTrue(l.removeLastOccurrence(new Integer(49))); assertFalse(l.removeLastOccurrence(new Integer(49))); assertTrue(l.removeLastOccurrence(new Integer(48))); points += 1; } @Test public void testRemoveIndex(){ MyLinkedList l = new MyLinkedList<>(); try { l.remove(0); fail("Did not throw IndexOutOfBoundsException"); } catch (Exception e){ if (!(e instanceof IndexOutOfBoundsException)) { fail("Did not throw IndexOutOfBoundsException"); } } l.add(1); try { l.remove(-1); fail("Did not throw IndexOutOfBoundsException"); } catch (Exception e){ if (!(e instanceof IndexOutOfBoundsException)) { fail("Did not throw IndexOutOfBoundsException"); } } try { l.remove(1); fail("Did not throw IndexOutOfBoundsException"); } catch (Exception e){ if (!(e instanceof IndexOutOfBoundsException)) { fail("Did not throw IndexOutOfBoundsException"); } } assertEquals(new Integer(1), l.remove(0)); assertEquals(0, l.size()); try { l.remove(0); fail("Did not throw IndexOutOfBoundsException"); } catch (Exception e){ if (!(e instanceof IndexOutOfBoundsException)) { fail("Did not throw IndexOutOfBoundsException"); } } for (int i = 1; i < 100; i++) l.add(i); assertEquals(new Integer(51), l.remove(50)); for (int i = 1; i < 50; i++) assertEquals(new Integer(i), l.remove(0)); assertEquals(49, l.size()); for (int i = 48; i >= 2; i--) assertEquals(new Integer(i+51), l.remove(i)); assertEquals(2, l.size()); points += 5; } @Test public void testAddIndex(){ MyLinkedList l = new MyLinkedList<>(); try { l.add(-1, new Integer(42)); fail("Did not throw IndexOutOfBoundsException"); } catch (Exception e){ if (!(e instanceof IndexOutOfBoundsException)) { fail("Did not throw IndexOutOfBoundsException"); } } try { l.add(1, new Integer(42)); fail("Did not throw IndexOutOfBoundsException"); } catch (Exception e){ if (!(e instanceof IndexOutOfBoundsException)) { fail("Did not throw IndexOutOfBoundsException"); } } l.add(0, new Integer(42)); assertEquals("[42]", l.toString()); assertEquals(1, l.size()); for (int i = 1; i < 5; i++) l.add(0, new Integer(i)); assertEquals("[4, 3, 2, 1, 42]", l.toString()); assertEquals(5, l.size()); for (int i = 1; i < 5; i++) l.add(i+4, new Integer(i)); assertEquals("[4, 3, 2, 1, 42, 1, 2, 3, 4]", l.toString()); assertEquals(9, l.size()); l.add(1, new Integer(99)); assertEquals("[4, 99, 3, 2, 1, 42, 1, 2, 3, 4]", l.toString()); assertEquals(10, l.size()); l.add(9, new Integer(99)); assertEquals("[4, 99, 3, 2, 1, 42, 1, 2, 3, 99, 4]", l.toString()); assertEquals(11, l.size()); points += 5; } @AfterClass public static void testDoNothing(){ System.out.println("Points: " + points); } }