namespaces added for search algo, docs added for test cases

This commit is contained in:
foo290 2021-07-02 08:08:51 +05:30
parent 4247d4a005
commit 648b3a5431
No known key found for this signature in database
GPG Key ID: 37349CBEF6299747

View File

@ -34,6 +34,17 @@ struct Node {
Node *next;
};
/**
* @namespace search
* @brief Searching algorithms
* */
namespace search {
/**
* @namespace sublist_search
* @brief Functions for the [Sublist Search](https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list) implementation
* */
namespace sublist_search {
/**
* @brief Main searching function
* @param sublist A linked list which is supposed to be searched in mainList.
@ -84,6 +95,9 @@ bool sublistSearch(Node *sublist, Node *mainList) {
return false;
}
} // namespace sublist_search
} // namespace search
/**
* @brief A simple function to print the linked list
* @param start The head of the linked list
@ -122,10 +136,15 @@ Node *makeLinkedList(const std::vector<int> &data) {
}
/**
* A class, encapsulating the necessary test cases.
* @brief A class, encapsulating the necessary test cases.
*/
class TestCases {
private:
/**
* @brief A function to print given message on console.
* @tparam T Type of the given message.
* @returns void
* */
template<typename T>
void log(T msg) {
// It's just to avoid writing cout and endl
@ -133,6 +152,10 @@ class TestCases {
}
public:
/**
* @brief Executes test cases
* @returns void
* */
void runTests() {
log("Running Tests...");
@ -144,11 +167,14 @@ class TestCases {
std::cout << std::endl;
}
/**
* @brief A test case contains edge case, Only contains one element.
* @returns void
* */
void testCase_1() {
const bool expectedOutput = true;
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
log("This is test case 1 for sublist search Algorithm : ");
log("Description:");
log(" EDGE CASE : Only contains one element");
@ -159,20 +185,23 @@ class TestCases {
Node *sublistLL = makeLinkedList(sublistData);
Node *mainlistLL = makeLinkedList(mainlistData);
bool exists = sublistSearch(sublistLL, mainlistLL);
bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
log("Checking assert expression...");
assert(exists == expectedOutput);
log("Assertion check passed!");
log("[PASS] : TEST CASE 1 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
delete (sublistLL);
delete (mainlistLL);
}
/**
* @brief A test case which contains main list of 100 elements and sublist of 20.
* @returns void
* */
void testCase_2() {
const bool expectedOutput = true;
@ -200,7 +229,7 @@ class TestCases {
Node *sublistLL = makeLinkedList(sublistData);
Node *mainlistLL = makeLinkedList(mainlistData);
bool exists = sublistSearch(sublistLL, mainlistLL);
bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
log("Checking assert expression...");
assert(exists == expectedOutput);
@ -211,6 +240,10 @@ class TestCases {
"~");
}
/**
* @brief A test case which contains main list of 50 elements and sublist of 20.
* @returns void
* */
void testCase_3() {
const bool expectedOutput = false;
@ -236,7 +269,7 @@ class TestCases {
Node *sublistLL = makeLinkedList(sublistData);
Node *mainlistLL = makeLinkedList(mainlistData);
bool exists = sublistSearch(sublistLL, mainlistLL);
bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
log("Checking assert expression...");
assert(exists == expectedOutput);
@ -264,14 +297,14 @@ static void test() {
* @returns 0 on exit
*/
int main(int argc, char *argv[]) {
test(); // Executing various test cases first.
test(); // Run self test implementations
std::vector<int> mainlistData = {2, 5, 6, 7, 8};
std::vector<int> sublistData = {6, 8};
Node *mainlistLL = makeLinkedList(mainlistData);
Node *sublistLL = makeLinkedList(sublistData);
bool exists = sublistSearch(sublistLL, mainlistLL);
bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
std::cout << "Sublist :" << std::endl;
printLinkedList(sublistLL);