/** * Unit test cases for the implementation of a Singly-Linked List. * * @author __your_name___ */ import static org.junit.Assert.*; import java.util.NoSuchElementException; import org.junit.Test; public class SLinkedListTest { /** * the list to use for testing */ private SLinkedList<Integer> list; /** * Returns a linked list with the given items. * (the items in the list are in the same order as in the given array) */ private static <E> SLinkedList<E> load(E... items) { SLinkedList<E> list = new SLinkedList<E>(); // if addLast works correctly, use this version (else use version below) for (E data : items) { list.addLast(data); } // // if only addFirst works, use this version (else use version above) // for (int i = items.length - 1; i >= 0; --i) { // list.addFirst(items[i]); // } return list; } @Test public void test_addFirst() { // testing empty list list = load(); list.addFirst( 4 ); assertEquals( list.toStringNext(), "[4]" ); //for DLL: assertEquals( list.toStringPrev(), "[4]" ); // testing single list list = load( 8 ); list.addFirst( 9 ); assertEquals( list.toStringNext(), "[9 8]" ); //for DLL: assertEquals( list.toStringPrev(), "[9 8]" ); // testing multi list list = load( 6, 1, 7, 5, 3, 4 ); list.addFirst( 2 ); assertEquals( list.toStringNext(), "[2 6 1 7 5 3 4]" ); //for DLL: assertEquals( list.toStringPrev(), "[2 6 1 7 5 3 4]" ); } @Test public void test_addLast() { // testing empty list list = load(); list.addLast( 4 ); assertEquals( list.toStringNext(), "[4]" ); //for DLL: assertEquals( list.toStringPrev(), "[4]" ); // testing single list list = load( 8 ); list.addLast( 7 ); assertEquals( list.toStringNext(), "[8 7]" ); //for DLL: assertEquals( list.toStringPrev(), "[8 7]" ); // testing multi list list = load( 6, 1, 7, 5, 3, 4 ); list.addLast( 9 ); assertEquals( list.toStringNext(), "[6 1 7 5 3 4 9]" ); //for DLL: assertEquals( list.toStringPrev(), "[6 1 7 5 3 4 9]" ); } @Test public void test_isEmpty() { // testing empty list list = load(); assertTrue( list.isEmpty() ); assertEquals( list.toStringNext(), "[]" ); //for DLL: assertEquals( list.toStringPrev(), "[]" ); // testing single list list = load( 8 ); assertFalse( list.isEmpty() ); assertEquals( list.toStringNext(), "[8]" ); //for DLL: assertEquals( list.toStringPrev(), "[8]" ); // testing multi list list = load( 6, 1, 7, 5, 3, 4 ); assertFalse( list.isEmpty() ); assertEquals( list.toStringNext(), "[6 1 7 5 3 4]" ); //for DLL: assertEquals( list.toStringPrev(), "[6 1 7 5 3 4]" ); } @Test public void test_getFirst() { // testing empty list list = load(); assertThrows( NoSuchElementException.class, () -> list.getFirst() ); assertEquals( list.toStringNext(), "[]" ); //for DLL: assertEquals( list.toStringPrev(), "[]" ); // testing single list list = load( 8 ); assertTrue( list.getFirst() == 8 ); assertEquals( list.toStringNext(), "[8]" ); //for DLL: assertEquals( list.toStringPrev(), "[8]" ); // testing multi list list = load( 6, 1, 7, 5, 3, 4 ); assertTrue( list.getFirst() == 6 ); assertEquals( list.toStringNext(), "[6 1 7 5 3 4]" ); //for DLL: assertEquals( list.toStringPrev(), "[6 1 7 5 3 4]" ); } }