Browse Source

Created tests for linked list and node classes.

master
jrtechs 6 years ago
parent
commit
dc72934594
4 changed files with 179 additions and 9 deletions
  1. +25
    -9
      src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java
  2. +20
    -0
      src/test/java/net/jrtechs/www/AppTest.java
  3. +96
    -0
      src/test/java/net/jrtechs/www/DataStructures/LinkedList/LinkedListTest.java
  4. +38
    -0
      src/test/java/net/jrtechs/www/DataStructures/NodeTest.java

+ 25
- 9
src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java View File

@ -11,6 +11,8 @@ public class LinkedList implements IList
{ {
private Node<E> head; private Node<E> head;
private Node<E> tail;
private int size; private int size;
@ -18,6 +20,7 @@ public class LinkedList implements IList
public LinkedList(E o) public LinkedList(E o)
{ {
this.head = new Node<>(o); this.head = new Node<>(o);
this.tail = head;
this.size = 1; this.size = 1;
} }
@ -25,20 +28,26 @@ public class LinkedList implements IList
public LinkedList() public LinkedList()
{ {
this.head = null; this.head = null;
this.tail = null;
this.size = 0; this.size = 0;
} }
public boolean add(E o) public boolean add(E o)
{ {
Node current = this.head;
while(current != null)
{
current = current.getNext();
}
this.size++; this.size++;
return false;
if(this.head == null)
{
this.head = new Node<E>(o);
this.tail = this.head;
}
else
{
this.tail.setNext(new Node<E>(o));
this.tail = this.tail.getNext();
}
return true;
} }
@ -47,8 +56,9 @@ public class LinkedList implements IList
Node current = this.head; Node current = this.head;
while(current != null) while(current != null)
{ {
if(current.equals(o))
if(current.getData().equals(o))
{ {
return true; return true;
} }
current = current.getNext(); current = current.getNext();
@ -74,13 +84,19 @@ public class LinkedList implements IList
if(count == index) if(count == index)
{ {
if(previous == null) if(previous == null)
this.head = current;
previous.setNext(current.getNext());
{
this.head = current.getNext();
}
else
{
previous.setNext(current.getNext());
}
this.size--; this.size--;
return current.getData(); return current.getData();
} }
previous = current; previous = current;
current = current.getNext(); current = current.getNext();
count++;
} }
return null; return null;
} }

+ 20
- 0
src/test/java/net/jrtechs/www/AppTest.java View File

@ -0,0 +1,20 @@
package net.jrtechs.www;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}

+ 96
- 0
src/test/java/net/jrtechs/www/DataStructures/LinkedList/LinkedListTest.java View File

@ -0,0 +1,96 @@
package net.jrtechs.www.DataStructures.LinkedList;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Jeffery Russell 8-24-18
*/
public class LinkedListTest
{
/**
* Makes sure that instantiations work
*/
@Test
public void testCreation()
{
assertNotNull(new LinkedList<Double>());
assertNotNull(new LinkedList<Double>(12.2));
}
/**
* Test Size of linked list
*/
@Test
public void testSize()
{
LinkedList<Double> list = new LinkedList<>();
list.add(12.0);
assertTrue(list.size() == 1);
list.add(13.0);
assertTrue(list.size() == 2);
}
/**
* Testing addition of data elements
*/
@Test
public void testAddition()
{
LinkedList<Double> list = new LinkedList<>();
list.add(12.0);
list.add(13.0);
list.add(44.9);
assertTrue(list.contains(44.9));
assertTrue(list.contains(12.0));
assertTrue(list.contains(13.0));
assertTrue(list.size() == 3);
LinkedList<Double> list2 = new LinkedList<>(16.0);
list2.add(18.0);
assertTrue(list2.contains(16.0));
assertTrue(list2.contains(18.0));
assertTrue(list2.size() == 2);
}
/**
* Testing addition of data elements
*/
@Test
public void testDeletion()
{
LinkedList<Double> list = new LinkedList<>();
list.add(12.0);
list.add(13.0);
list.add(44.9);
assertTrue(list.contains(44.9));
assertTrue(list.contains(12.0));
assertTrue(list.contains(13.0));
assertTrue(list.size() == 3);
list.remove(0);
list.remove(1);
assertFalse(list.contains(12.0));
assertFalse(list.contains(44.9));
assertTrue(list.contains(13.0));
}
}

+ 38
- 0
src/test/java/net/jrtechs/www/DataStructures/NodeTest.java View File

@ -0,0 +1,38 @@
package net.jrtechs.www.DataStructures;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import net.jrtechs.www.DataStructures.LinkedList.LinkedList;
import org.junit.Test;
public class NodeTest
{
/**
* Rigorous Test :-)
*/
@Test
public void testCreation()
{
assertNotNull(new Node<Double>(12.0));
assertNotNull(new Node<Double>(12.2, new Node<Double>(13.0)));
}
@Test
public void testSetNext()
{
Node<Double> node = new Node<>(13.0);
Node<Double> node2 = new Node<>(14.5);
node.setNext(node2);
assertTrue(node.getNext().getData().equals(14.5));
}
}

Loading…
Cancel
Save