I need help making a unit test of a linket list

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.

Hi Melis,

I can tell that you are new to it. I’ll try to give you some pointers on this.

Unit tests are for testing a single unit of work, or you can say a single function.

For e.g. In your case you can write multiple tests for each of the methods, Add, Remove, Get.

[Test]
public void Get_WhenIndexExistInList_ShouldReturnCorrectData(){
	// Arrange
	var list = new ListXwithErrors();
	list.Add("foo");
	list.Add("bar");
	list.Add("baz");
	
	// Act
	var obj = list.Get(2);
	
	// Assert
	Assert.AreEqual("baz", (string)obj);	
}

[Test]
[ExpectedException(typeof(MyException), ExpectedMessage="Error (Get): Invalid index: 3")
public void Get_WhenIndexDoesNotExistInList_ShouldThrowMyException(){
	// Arrange
	var list = new ListXwithErrors();
	list.Add("foo");
	list.Add("bar");
	list.Add("baz");
	
	// Act
	var obj = list.Get(3);
}

You can also have the creation of objects in a private method and reuse it, in case you don’t want to create the object again and again.
This way you can write multiple tests for a single method.

Hope I can be of some help.

Note: The test case written above is not tested, I have written it directly here. :slight_smile:

Hey, and thank you for taking your time explaining things to me.
I understand some of what you are saying, especially when it comes to adding objects.
This small piece of code still confuses me though:

        // returns the number of objects in the list    
        public int Length()
        {
            return size;
        }

I am trying to think logical here. I just confirmed that I had created an empty list. So the number of objects in the list should be 0 or null, correct?
So if I am going to test this, I would assume that I need to use a Get method.
Something like;

            // Arrange
            var obj = new ListXwithErrors();

            // Act
            var obj.Get();

            // Assert
            Assert.AreEqual(0, obj);

What do I “Get” in this case? I know that the list is empty, but I can´t leave the parentheses empty, but if I write obj.Get(0), I am just telling it to Get nothing, correct?.

While we are at it. Regarding the “Invalid index” error,
What actually causes that to happen? I have tried looking at the code a few times, but I can´t see what triggers it.

To test the Length, just create the object and then add some items to your list. And then check the length, it should be equal to the number of add statements.

var list = new ListXwithErrors();
list.Add("foo");
list.Add("bar");
list.Add("baz");

Assert.AreEqual(list.Length(), 3);

And you don’t have to call the Get() method at all, while testing the length.

Regarding the “Invalid Index” error, I am actually adding 3 items in list, and trying to fetch the item at 3rd index. Since, lists/arrays starts from index-0, it should throw me that error.