Created
January 18, 2013 18:27
-
-
Save chintanparikh/4566955 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import org.junit.Test; | |
import org.junit.Before; | |
import java.util.ArrayList; | |
/** | |
* Linked List Tests | |
* @author Chintan | |
* | |
*/ | |
public class LinkedListTest { | |
private LinkedList<Integer> list; | |
private Integer i, j, k; | |
@Before | |
public void setUp() | |
{ | |
list = new LinkedList<Integer>(); | |
i = new Integer(5); | |
j = new Integer(6); | |
k = new Integer(7); | |
} | |
@Test | |
public void testAdd() { | |
list.add(i); | |
// Test that a node has been added to the head | |
assertTrue(list.getHead().getData().equals(i)); | |
// Test that the node points to it's self | |
assertTrue(list.getHead().getNext().getData().equals(i)); | |
list.add(j); | |
// Test that the head hasn't been changed | |
assertTrue(list.getHead().getData().equals(i)); | |
// Test that the head points to our new node | |
assertTrue(list.getHead().getNext().getData().equals(j)); | |
// Test that our new node points to the head | |
assertTrue(list.getHead().getNext().getNext().getData().equals(i)); | |
} | |
@Test | |
public void testAddAll() { | |
// Test adding an empty collection | |
// Test adding a collection with two elements | |
ArrayList<Integer> collection = new ArrayList<Integer>(); | |
collection.add(i); | |
collection.add(j); | |
list.addAll(collection); | |
assertTrue(list.getHead().getData().equals(i)); | |
assertTrue(list.getHead().getNext().getData().equals(j)); | |
assertTrue(list.getHead().getNext().getNext().getData().equals(i)); | |
collection = new ArrayList<Integer>(); | |
list.addAll(collection); | |
// Shouldn't change anything | |
assertTrue(list.getHead().getData().equals(i)); | |
assertTrue(list.getHead().getNext().getData().equals(j)); | |
assertTrue(list.getHead().getNext().getNext().getData().equals(i)); | |
} | |
@Test | |
public void testClear() { | |
list.add(new Integer(5)); | |
list.add(new Integer(6)); | |
list.clear(); | |
assertTrue(list.getHead() == null); | |
} | |
@Test | |
public void testContains() { | |
list.add(i); | |
list.add(j); | |
assertTrue(list.contains(i)); | |
assertTrue(list.contains(j)); | |
assertFalse(list.contains(k)); | |
} | |
@Test | |
public void testGet() { | |
list.add(i); | |
list.add(j); | |
assertTrue(list.get(0).equals(i)); | |
assertTrue(list.get(1).equals(j)); | |
boolean thrown = false; | |
try | |
{ | |
list.get(3); | |
} | |
catch (IndexOutOfBoundsException e) | |
{ | |
thrown = true; | |
} | |
assertTrue(thrown); | |
thrown = false; | |
try | |
{ | |
list.get(-1); | |
} | |
catch (IndexOutOfBoundsException e) | |
{ | |
thrown = true; | |
} | |
assertTrue(thrown); | |
} | |
@Test | |
public void testIndexOf() { | |
list.add(i); | |
list.add(j); | |
assertTrue(list.indexOf(i) == 0); | |
assertTrue(list.indexOf(j) == 1); | |
assertTrue(list.indexOf(k) == -1); | |
list = new LinkedList<Integer>(); | |
assertTrue(list.indexOf(k) == -1); | |
} | |
@Test | |
public void testIsEmpty() { | |
assertTrue(list.isEmpty()); | |
list.add(i); | |
assertFalse(list.isEmpty()); | |
} | |
@Test | |
public void testRemoveInt() { | |
list.add(i); | |
list.add(j); | |
assertTrue(list.remove(0).equals(i)); | |
assertFalse(list.getHead().getData().equals(i)); | |
System.out.println(list.getHead().getData()); | |
assertTrue(list.getHead().getData().equals(j)); | |
assertTrue(list.remove(0).equals(j)); | |
assertTrue(list.getHead() == null); | |
list.add(i); | |
list.add(j); | |
assertTrue(list.remove(1).equals(j)); | |
assertTrue(list.getHead().getNext().getData() == i); | |
boolean thrown = false; | |
try | |
{ | |
list.remove(1); | |
} | |
catch (IndexOutOfBoundsException e) | |
{ | |
thrown = true; | |
} | |
assertTrue(thrown); | |
thrown = false; | |
try | |
{ | |
list.remove(-1); | |
} | |
catch (IndexOutOfBoundsException e) | |
{ | |
thrown = true; | |
} | |
assertTrue(thrown); | |
} | |
@Test | |
public void testRemoveObject() { | |
list.add(i); | |
list.add(j); | |
assertTrue(list.remove(j).equals(j)); | |
assertTrue(list.remove(i).equals(i)); | |
assertNull(list.remove(k)); | |
} | |
@Test | |
public void testSet() { | |
list.add(i); | |
list.add(j); | |
assertEquals(list.set(1, k), j); | |
assertEquals(list.get(0), i); | |
assertEquals(list.get(1), k); | |
boolean thrown = false; | |
try | |
{ | |
list.set(2, j); | |
} | |
catch (IndexOutOfBoundsException e) | |
{ | |
thrown = true; | |
} | |
assertTrue(thrown); | |
thrown = false; | |
try | |
{ | |
list.set(-1, j); | |
} | |
catch (IndexOutOfBoundsException e) | |
{ | |
thrown = true; | |
} | |
assertTrue(thrown); | |
} | |
@Test | |
public void testSize() { | |
assertEquals(list.size(), 0); | |
list.add(i); | |
assertEquals(list.size(), 1); | |
list.remove(i); | |
assertEquals(list.size(), 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment