Blake Birkenfeld

CPSC 211-203

Program 1

 

I decided to use Strings as the objects that I was going to be storing in my linked list.  I chose Strings because they are easy to print to the screen and because they are easy to work with.  My test cases will consist of different ways of inserting and deleting Strings in my linked list.  I will do a few operations and then I will use a loop to traverse the list and print out the Strings.  I will make sure that the output is exactly what I expect it to be.  I will do all this within the main() of a testing class that I call ListTest.

 

The code below is the standard code that I will use to run all my tests:

 

import java.lang.*;

 

public class ListTest {

  public static void main(String[] args) {

 

  DLList myList = new DLList();

 

  String str1 = "str1";

  String str2 = "str2";

  String str3 = "str3";

  String str4 = “str4”;

  String str5 = “str5”;

  String str6 = “str6”;

 

  // here is where I will put the code for the various test cases

 

  // this is the loop to print out the data in the list

  Node curNode = myList.firstNode;

  while(curNode != null)

  {

    System.out.println(curNode.data);

    curNode = curNode.next;

  }

 

  }

}

 

Test Cases

 

1.   Insert a string using insertFront(Object o) into a list that is empty.

I did this test to make sure insertFront() works even when the list is empty.

 

myList.insertFront(str1);

 

Output produced by my code is:

str1

 

2.   Insert a string using insertFront() into a list that contains a few nodes already.

I did this test to make sure that insertFront() correctly puts the object at the front of the list.

 

myList.insertFront(str1);

myList.insertFront(str2);

myList.insertFront(str3);

 

Output produced by my code is:

str3

str2

str1

 

3.   Insert a string using insertBack() into a list that is empty.

I did this test to make sure that insertBack() works even when the list is empty.

 

myList.insertBack(str1);

 

Output produced by my code is:

str1

 

4.   Insert a string using insertBack() into a list that contains a few nodes already.

I did this test to make sure that insertBack() correctly puts the object at the back of the list.

 

myList.insertBack(str1);

myList.insertBack(str2);

myList.insertBack(str3);

 

Output produced by my code is:

str1

str2

str3

 

5.   Insert a string using insertAfter(o1, o2) into a list that is empty.

I did this test to make sure that if the list is empty and o2 is not found, then it inserts the object at the back of the list.  In this case it makes the object the front and back node in the list.

 

myList.insertAfter(str1, str2);

 

Output produced by my code is:

str1

 

6.  Insert a string using insertAfter(o1, o2) into a list that contains a few nodes but does not contain o2.

I did this test to make sure that if o2 is not found in the list, then the object is inserted at the back of the list.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertAfter(str3, str4);

 

Output produced by my code is:

str1

str2

str3

 

7.  Insert a string using insertAfter(o1, o2) into a list that does contain o2.

I did this test to make sure that if o2 is found in the list, then o1 is inserted directly after o2 and before any other nodes that might be following o2.  I also tested this when o2 is the last node, and when o2 is the only node in the list, but I’m not going to paste all of the code.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertAfter(str3, str1);

 

Output produced by my code is:

str1

str3

str2

 

8.   Insert a string using insertBefore(o1, o2) into a list that is empty.

I did this test to make sure that if the list is empty and o2 is not found, then it inserts the object at the back of the list.  In this case it makes the object the front and back node in the list.

 

myList.insertBefore(str1, str2);

 

Output produced by my code is:

str1

 

9.  Insert a string using insertBefore(o1, o2) into a list that contains a few nodes but does not contain o2.

I did this test to make sure that if o2 is not found in the list, then the object is inserted at the back of the list.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertBefore(str3, str4);

 

Output produced by my code is:

str1

str2

str3

 

10.  Insert a string using insertBefore(o1, o2) into a list that does contain o2.

I did this test to make sure that if o2 is found in the list, then o1 is inserted directly before o2 and after any other nodes that might be preceeding o2.  I also tested this when o2 is the first node, and when o2 is the only node in the list.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertBefore(str3, str2);

 

Output produced by my code is:

str1

str3

str2

 

11.   Delete a string using delete(Object o) in a list that contains a few nodes and when o is found in the list.

I did this test to make sure that if o is found in the list, then o is properly deleted and the links are repaired.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertBack(str3);

myList.delete(str2);

 

Output produced by my code is:

str1

str3

 

12.   Delete a string using delete(Object o) in a list that contains a few nodes and when o is the front node in the list.

I did this test to make sure that if o is the first node in the list, then o is properly deleted and the links all repaired, and the first node reference is properly changed to the second node.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertBack(str3);

myList.delete(str1);

 

Output produced by my code is:

str2

str3

 

13.   Delete a string using delete(Object o) in a list that contains a few nodes and when o is the back node in the list.

I did this test to make sure that if o is the back node in the list, then o is properly deleted and the links all repaired, and the back node reference is properly changed to the second-to-last node.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertBack(str3);

myList.delete(str3);

 

Output produced by my code is:

str1

str2

 

 

14.   Delete a string using delete(Object o) in a list in which o is the only node in the list.

I did this test to make sure that if o is found in the list, then o is properly deleted and the references to first and back node are made null.

 

myList.insertFront(str1);

myList.delete(str1);

 

Output produced by my code is:

No output is produced

 

15.   Delete a string using delete(Object o) when o is not found in the list.

I did this test to make sure that if o is not found in the list, then no action is taken by the method.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertBack(str3);

myList.delete(str4);

 

Output produced by my code is:

str1

str2

str3

 

16.   Call deleteFront() on a list that is empty.

I did this test to make sure that it returns a null object when the list is empty.

 

System.out.println(myList.deleteFront());

 

Output produced by my code is:

null

 

17.   Call deleteFront() on a list with one node.

I did this test to make sure that the object is correctly returned and that the first and back node references are set to null.

 

myList.insertFront(str1);

System.out.println(myList.deleteFront());

System.out.println(myList.firstNode == null && myList.backNode == null);

 

Output produced by my code is:

str1

true

 

 

18.  Call deleteFront() on a list with multiple nodes.

I did this test to make sure that the object is correctly returned and the first node reference is properly changed to the second node.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertBack(str3);

System.out.println(myList.deleteFront());

 

Output produced by my code is: (note that the str2 and str3 are printed because of the loop at the end of the code)

str1

str2

str3

 

19.  Call deleteBack() on a list that is empty.

I did this test to make sure that it returns a null object when the list is empty.

 

System.out.println(myList.deleteBack());

 

Output produced by my code is:

null

 

20.  Call deleteBack() on a list with one node.

I did this test to make sure that the object is correctly returned and that the first and back node references are set to null.

 

myList.insertFront(str1);

System.out.println(myList.deleteBack());

System.out.println(myList.firstNode == null && myList.backNode == null);

 

Output produced by my code is:

str1

true

 

 

21.  Call deleteBack() on a list with multiple nodes.

I did this test to make sure that the object is correctly returned and the back node reference is properly changed to the second-to-last node.

 

myList.insertFront(str1);

myList.insertBack(str2);

myList.insertBack(str3);

System.out.println(myList.deleteBack());

 

Output produced by my code is: (note that the str1 and str2 are printed because of the loop at the end of the code)

str3

str1

str2

 

22.   The last test is just a random test with multiple calls to different methods.

 

myList.delete(str1);

myList.insertBack(str2);

myList.insertAfter(str3, str2);

System.out.println(myList.deleteBack());

myList.insertBack(str1);

myList.insertFront(str3);

myList.insertBefore(str4, str1);

myList.delete(str1);

myList.delete(str6);

myList.insertAfter(str1, str6);

System.out.println(myList.deleteFront());

myList.insertBack(str5);

myList.insertFront(str6);

myList.delete(str3);

myList.insertBefore(str3, str6);

 

Output produced by my code is:

str3

str3

str3

str6

str2

str4

str1

str5

 

This is exactly what the output should be.

 

Summary:

All of my test cases produced the exact output that I calculated.  It appears that the linked list class is working correctly.