I am having trouble writing my unit test

Hede

New member
Joined
Dec 16, 2019
Messages
2
Programming Experience
Beginner
Hey guys.
Very new beginner here.

I have been struggling to finish a unit test of a linked list.
Some of the more simple parts, such as adding an object to a list and checked if the list is empty or not is easy enough, but I have run into some issues.

I am currently trying to run a test of this code:
C#:
// 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;
        }
I have run a test on this which looks like this:
C#:
[TestMethod]
public void Get_WhenIndexExistInList_ShouldReturnCorrectData(){
    // Arrange
    var list = new ListXwithErrors();
    list.Add("A");
    list.Add("B");
    list.Add("C");
    
    // Act
    var obj = list.Get(2);
    
    // Assert
    Assert.AreEqual("C", (string)obj);   
}
This is giving me an invalid index error. Why?
I thought that my objects would have the values 0, 1 and 2, and that 2 = "C".
So what have I missed here?

Last I have this piece of code, which I am not sure how to test.
As I said, I have tried adding normal objects to a list. But how do test this one? In my test file, Node is not even recognized when I type it down.
So how am I supposed to write this one?

C#:
// node in the list
        private class Node
        {
            public Object data;
            public Node next;
            public Node(Object o)
            {
                data = o;
                next = null;
            }
        }
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
Step through your code. It made show that your size is actually 2, which would then suggest there is a bug in your insert code.

That is what unit tests are supposed to reveal: bugs in code. Sometimes, though, the test reveals bugs in the part of the code you aren't testing.
 

Hede

New member
Joined
Dec 16, 2019
Messages
2
Programming Experience
Beginner
Thank you.
I will look into it =)

Regarding "node in the list". I am not quite sure what I am supposed to be testing here. I am assuming I should just test if it is in the list.
So in the Arrange part, it should look something like this?

C#:
            LinkedListNode<string> next = new LinkedListNode<string>(null);
            LinkedList<string> data = new LinkedList<string>();

            data.AddFirst("o");
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
Your original test in post #1 is correct. You are correctly arranging 3 items. You are correctly acting by trying to get the item at index #2. You are correctly asserting that your result should be "C".

The problem is that your list implementation is not correctly adding that items/and or keeping track of the list size.
 
Last edited:
Top Bottom