Skip to content

Instantly share code, notes, and snippets.

@chintanparikh
Last active December 11, 2015 06:29
Show Gist options
  • Select an option

  • Save chintanparikh/4559784 to your computer and use it in GitHub Desktop.

Select an option

Save chintanparikh/4559784 to your computer and use it in GitHub Desktop.
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
import org.junit.Ignore;
import java.util.ArrayList;
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);
}
@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));
assertTrue(list.getHead().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);
}
@Ignore
@Test
public void testRemoveObject() {
fail("Not yet implemented");
}
@Ignore
@Test
public void testSet() {
fail("Not yet implemented");
}
@Ignore
@Test
public void testSize() {
fail("Not yet implemented");
}
@Ignore
@Test
public void testSetSize() {
fail("Not yet implemented");
}
@Ignore
@Test
public void testGetHead() {
fail("Not yet implemented");
}
@Ignore
@Test
public void testSetHead() {
fail("Not yet implemented");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment