Importance of Unit tests — My journey with Unit-tests 2

#include <cstddef>

class LinkedList{
public:
int value {0};
LinkedList* nextNode {NULL};
};

LinkedList* addNewNode(int nodeVal){

LinkedList *newNode;
newNode->value = nodeVal;
newNode->nextNode = nullptr;

return newNode;
}
TEST_F(LinkedListTest,addNewNodeReturnsItsNodePointer){
linkedlist = addNewNode(5);

EXPECT_TRUE(linkedlist != nullptr);
ASSERT_EQ(linkedlist->value,5);
EXPECT_TRUE(linkedlist->nextNode != nullptr);
}
Segmentation fault
LinkedList* getANodeWithGivenValue(LinkedList *existingList,int value){   while (existingList != NULL){      if(existingList->value == value){          return existingList;
}
existingList = existingList->nextNode;
}
}
TEST_F(LinkedListTest,getANodeWithGivenValueNoMatchingValueReturnsFalse){    LinkedList *existingList = addNewNode(1);    LinkedList *nodeWithTheGivenValue =     getANodeWithGivenValue(existingList,5);    EXPECT_TRUE(nodeWithTheGivenValue->nextNode == nullptr);    EXPECT_EQ(nodeWithTheGivenValue->value,0);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store