mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
namespaces added for search algo, docs added for test cases
This commit is contained in:
parent
4247d4a005
commit
648b3a5431
@ -34,6 +34,17 @@ struct Node {
|
|||||||
Node *next;
|
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
|
* @brief Main searching function
|
||||||
* @param sublist A linked list which is supposed to be searched in mainList.
|
* @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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace sublist_search
|
||||||
|
} // namespace search
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A simple function to print the linked list
|
* @brief A simple function to print the linked list
|
||||||
* @param start The head of 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 {
|
class TestCases {
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* @brief A function to print given message on console.
|
||||||
|
* @tparam T Type of the given message.
|
||||||
|
* @returns void
|
||||||
|
* */
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void log(T msg) {
|
void log(T msg) {
|
||||||
// It's just to avoid writing cout and endl
|
// It's just to avoid writing cout and endl
|
||||||
@ -133,6 +152,10 @@ class TestCases {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Executes test cases
|
||||||
|
* @returns void
|
||||||
|
* */
|
||||||
void runTests() {
|
void runTests() {
|
||||||
log("Running Tests...");
|
log("Running Tests...");
|
||||||
|
|
||||||
@ -144,11 +167,14 @@ class TestCases {
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A test case contains edge case, Only contains one element.
|
||||||
|
* @returns void
|
||||||
|
* */
|
||||||
void testCase_1() {
|
void testCase_1() {
|
||||||
const bool expectedOutput = true;
|
const bool expectedOutput = true;
|
||||||
|
|
||||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
|
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||||
"~");
|
|
||||||
log("This is test case 1 for sublist search Algorithm : ");
|
log("This is test case 1 for sublist search Algorithm : ");
|
||||||
log("Description:");
|
log("Description:");
|
||||||
log(" EDGE CASE : Only contains one element");
|
log(" EDGE CASE : Only contains one element");
|
||||||
@ -159,20 +185,23 @@ class TestCases {
|
|||||||
Node *sublistLL = makeLinkedList(sublistData);
|
Node *sublistLL = makeLinkedList(sublistData);
|
||||||
Node *mainlistLL = makeLinkedList(mainlistData);
|
Node *mainlistLL = makeLinkedList(mainlistData);
|
||||||
|
|
||||||
bool exists = sublistSearch(sublistLL, mainlistLL);
|
bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
|
||||||
|
|
||||||
log("Checking assert expression...");
|
log("Checking assert expression...");
|
||||||
assert(exists == expectedOutput);
|
assert(exists == expectedOutput);
|
||||||
log("Assertion check passed!");
|
log("Assertion check passed!");
|
||||||
|
|
||||||
log("[PASS] : TEST CASE 1 PASS!");
|
log("[PASS] : TEST CASE 1 PASS!");
|
||||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
|
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||||
"~");
|
|
||||||
|
|
||||||
delete (sublistLL);
|
delete (sublistLL);
|
||||||
delete (mainlistLL);
|
delete (mainlistLL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A test case which contains main list of 100 elements and sublist of 20.
|
||||||
|
* @returns void
|
||||||
|
* */
|
||||||
void testCase_2() {
|
void testCase_2() {
|
||||||
const bool expectedOutput = true;
|
const bool expectedOutput = true;
|
||||||
|
|
||||||
@ -200,7 +229,7 @@ class TestCases {
|
|||||||
Node *sublistLL = makeLinkedList(sublistData);
|
Node *sublistLL = makeLinkedList(sublistData);
|
||||||
Node *mainlistLL = makeLinkedList(mainlistData);
|
Node *mainlistLL = makeLinkedList(mainlistData);
|
||||||
|
|
||||||
bool exists = sublistSearch(sublistLL, mainlistLL);
|
bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
|
||||||
|
|
||||||
log("Checking assert expression...");
|
log("Checking assert expression...");
|
||||||
assert(exists == expectedOutput);
|
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() {
|
void testCase_3() {
|
||||||
const bool expectedOutput = false;
|
const bool expectedOutput = false;
|
||||||
|
|
||||||
@ -236,7 +269,7 @@ class TestCases {
|
|||||||
Node *sublistLL = makeLinkedList(sublistData);
|
Node *sublistLL = makeLinkedList(sublistData);
|
||||||
Node *mainlistLL = makeLinkedList(mainlistData);
|
Node *mainlistLL = makeLinkedList(mainlistData);
|
||||||
|
|
||||||
bool exists = sublistSearch(sublistLL, mainlistLL);
|
bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
|
||||||
|
|
||||||
log("Checking assert expression...");
|
log("Checking assert expression...");
|
||||||
assert(exists == expectedOutput);
|
assert(exists == expectedOutput);
|
||||||
@ -264,14 +297,14 @@ static void test() {
|
|||||||
* @returns 0 on exit
|
* @returns 0 on exit
|
||||||
*/
|
*/
|
||||||
int main(int argc, char *argv[]) {
|
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> mainlistData = {2, 5, 6, 7, 8};
|
||||||
std::vector<int> sublistData = {6, 8};
|
std::vector<int> sublistData = {6, 8};
|
||||||
Node *mainlistLL = makeLinkedList(mainlistData);
|
Node *mainlistLL = makeLinkedList(mainlistData);
|
||||||
Node *sublistLL = makeLinkedList(sublistData);
|
Node *sublistLL = makeLinkedList(sublistData);
|
||||||
|
|
||||||
bool exists = sublistSearch(sublistLL, mainlistLL);
|
bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
|
||||||
|
|
||||||
std::cout << "Sublist :" << std::endl;
|
std::cout << "Sublist :" << std::endl;
|
||||||
printLinkedList(sublistLL);
|
printLinkedList(sublistLL);
|
||||||
|
Loading…
Reference in New Issue
Block a user