I have been all over Google and Youtube without getting any wiser, so maybe you guys could help explain this to me.
I am new to Unit Testing. I have been doing it in Visual Studio for some months now, but all my exercise files have been normal classes with normal strings that were easy to test.
But now I am stuck with a file that contains code for a linked list. I have yet to find a tutorial that explains in detail how you do a unit test of a linked list.
I will show you the exercise code and my test code below. My idea was that maybe if someone could explain to me how to do one of the tests, then maybe I could have an “aha!” moment and be able to work on the rest of the code myself.
EXERCISE CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace UnitList.Controllers
{
public class ListXwithErrors : Controller
{
// class with 6 (or 7) errors
// HAU & TOR
private Node list;
private int size;
// create a new empty list
public ListXwithErrors()
{
list = null;
size = 0;
}
// returns the number of objects in the list
public int Length()
{
return size;
}
// insert an object in the end of the list
public void Add(Object obj)
{
Node node = new Node(obj);
Node current;
if (list == null) list = node;
else
{
current = list;
while (current.next != null) current = current.next;
current.next = node;
}
}
// insert an object at position index
public void Add(int index, Object obj)
{
if (index < 0 || size < index) throw new MyException("Error (Add): Invalid index: " + index);
Node node = new Node(obj);
// on first position
if (index == 0)
{
list = node;
node.next = list;
}
else
// on a position after the first
{
Node current = list;
for (int i = 0; i < index - 1; i++)
{
current = current.next;
}
node.next = current.next;
current.next = node;
}
size++;
}
// returns a reference to object number "index" from the list
public Object Get(int index)
{
if (index <= 0 || size <= index) throw new MyException("Error (Get): Invalid index: " + index);
Node current = list.next;
for (int i = 0; i < index; i++)
{
current = current.next;
}
return current.data;
}
// returns a reference to object number "index" from the list and removes it from the list
public Object Remove(int index)
{
if (index < 0 || size <= index) throw new MyException("Error (Remove): Invalid index: " + index);
Node node = null;
Node current = list;
// from first position
if (index == 0)
{
list = node.next;
}
else
// from a position after the first
{
current = list;
for (int i = 0; i < index - 1; i++)
{
current = current.next;
}
node = current.next;
current.next = current.next.next;
}
size--;
return current.data;
}
// node in the list
private class Node
{
public Object data;
public Node next;
public Node(Object o)
{
data = o;
next = null;
}
}
// exception handling index out of bound
public class MyException : Exception
{
public MyException(string message) : base(message)
{
}
}
}
}
Okay, so far the only thing I was able to do, was to test the empty list.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitList;
using UnitList.Controllers;
namespace UnitList.Tests.Controllers
{
[TestClass]
public class HomeControllerTest
{
private HomeController ListXwithErrors;
[TestMethod]
public void Index()
{
// Arrange
List<string> list = new List<string>();
// Act
bool isEmpty = (list.Count == 0);
// Assert
Assert.AreEqual(0, list.Count());
And this is how far I have gotten.
The next one I need to test is this:
// returns the number of objects in the list
public int Length()
{
return size;
}
So far, the only thing I could come up with here was this unfinished code:
public void NumberOfObjects()
{
// Arrange
List<int> lenght = new List<int>();
// Act
lenght.Count();
// Assert
}
I feel like I am doing this blindfolded, and I have been fighting with this for days now.