Also adding some basic tests...
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Create()
{
int data = 420;
Node firstNode = new(data);
SinglyLinkedList singlyLinkedList = new();
singlyLinkedList.head = firstNode;
Assert.AreEqual(data, singlyLinkedList.head.Data);
}
[TestMethod]
public void InsertFront()
{
int data = 420;
Node firstNode = new(data);
SinglyLinkedList singlyLinkedList = new();
singlyLinkedList.head = firstNode;
int newData = 240;
Operations operations = new();
operations.InsertFront(singlyLinkedList, newData);
Assert.AreEqual(newData, singlyLinkedList.head.Data);
Assert.AreEqual(data, singlyLinkedList.head.Next.Data);
}
[TestMethod]
public void InsertLast()
{
int data = 420;
Node firstNode = new(data);
SinglyLinkedList singlyLinkedList = new();
singlyLinkedList.head = firstNode;
int newData = 240;
Operations operations = new();
operations.InsertLast(singlyLinkedList, newData);
Assert.AreEqual(data, singlyLinkedList.head.Data);
Assert.AreEqual(newData, singlyLinkedList.head.Next.Data);
}
[TestMethod]
public void GetLastNode()
{
int data1 = 420;
Node firstNode = new(data1);
SinglyLinkedList singlyLinkedList = new();
singlyLinkedList.head = firstNode;
int data2 = 240;
Operations operations = new();
operations.InsertLast(singlyLinkedList, data2);
int data3 = 123;
operations.InsertLast(singlyLinkedList, data3);
Node lastNode = operations.GetLastNode(singlyLinkedList);
Assert.AreEqual(data3, lastNode.Data);
Assert.IsNull(lastNode.Next);
}
[TestMethod]
public void InserAfter()
{
int data1 = 420;
Node firstNode = new(data1);
SinglyLinkedList singlyLinkedList = new();
singlyLinkedList.head = firstNode;
int data2 = 240;
Operations operations = new();
operations.InsertLast(singlyLinkedList, data2);
int data3 = 123;
operations.InsertAfter(firstNode, data3);
Assert.AreEqual(data3, firstNode.Next.Data);
Assert.AreEqual(data2, firstNode.Next.Next.Data);
}
[TestMethod]
public void InserAfter_PreviousNodeNull_ThrowsException()
{
Operations operations = new();
try
{
operations.InsertAfter(null, 420);
Assert.Fail();
}
catch (Exception e)
{
Assert.AreEqual("Previous node cannot be null!", e.Message);
}
}
}