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:
I have run a test on this which looks like this:
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?
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;
}
}