# Algorithms_in_C++: TestCases Class Reference

Loading...
Searching...
No Matches
TestCases Class Reference

class encapsulating the necessary test cases More...

## Public Member Functions

void | runTests ()
| Executes test cases. More...
void | testCase_1 ()
| A test case contains edge case, printing inorder successor of last node. More...
void | testCase_2 ()
| A test case which contains main list of 100 elements and sublist of 20. More...
void | testCase_3 ()
| A test case which contains main list of 50 elements and sublist of 20. More...
void | runTests ()
| Executes test cases. More...
void | testCase_1 ()
| A test case contains edge case, Only contains one element. More...
void | testCase_2 ()
| A test case which contains main list of 100 elements and sublist of 20. More...
void | testCase_3 ()
| A test case which contains main list of 50 elements and sublist of 20. More...
void | runTests ()
| Executes test cases. More...
void | testCase_1 ()
| A test case with single input. More...
void | testCase_2 ()
| A test case with input array of length 500. More...
void | testCase_3 ()
| A test case with array of length 1000. More...

## Private Member Functions

template<typename T >
void | log (T msg)
| A function to print given message on console. More...
template<typename T >
void | log (T msg)
| A function to print given message on console. More...
template<typename T >
void | log (T msg)
| A function to print64_t given message on console. More...

## Detailed Description

class encapsulating the necessary test cases

a class containing the necessary test cases

## Member Function Documentation

## log() [1/3]

template<typename T >

void TestCases::log | ( | T | msg | ) |

inlineprivate

A function to print given message on console.

Template Parameters

  T | Type of the given message.

Returns
  void

{
// It's just to avoid writing cout and endl
std::cout << "[TESTS] : ---> " << msg << std::endl;
}
std::cout
std::endl
T endl(T... args)
Here is the call graph for this function:

## log() [2/3]

template<typename T >

void TestCases::log | ( | T | msg | ) |

inlineprivate

A function to print given message on console.

Template Parameters

  T | Type of the given message.

Returns
  void

{
// It's just to avoid writing cout and endl
std::cout << "[TESTS] : ---> " << msg << std::endl;
}
Here is the call graph for this function:

## log() [3/3]

template<typename T >

void TestCases::log | ( | T | msg | ) |

inlineprivate

A function to print64_t given message on console.

Template Parameters

  T | Type of the given message.

Returns
  void

{
// It's just to avoid writing cout and endl
std::cout << "[TESTS] : ---> " << msg << std::endl;
}
Here is the call graph for this function:

## runTests() [1/3]

void TestCases::runTests | ( |  | ) |

inline

Executes test cases.

Returns
  void

{
log("Running Tests...");
testCase_1();
testCase_2();
testCase_3();
log("Test Cases over!");
std::cout << std::endl;
}
TestCases::log
void log(T msg)
A function to print given message on console.
Definition: inorder_successor_of_bst.cpp:233
TestCases::testCase_2
void testCase_2()
A test case which contains main list of 100 elements and sublist of 20.
Definition: inorder_successor_of_bst.cpp:304
TestCases::testCase_1
void testCase_1()
A test case contains edge case, printing inorder successor of last node.
Definition: inorder_successor_of_bst.cpp:259
TestCases::testCase_3
void testCase_3()
A test case which contains main list of 50 elements and sublist of 20.
Definition: inorder_successor_of_bst.cpp:345
Here is the call graph for this function:

## runTests() [2/3]

void TestCases::runTests | ( |  | ) |

inline

Executes test cases.

Returns
  void

{
log("Running Tests...");
testCase_1();
testCase_2();
testCase_3();
log("Test Cases over!");
std::cout << std::endl;
}
Here is the call graph for this function:

## runTests() [3/3]

void TestCases::runTests | ( |  | ) |

inline

Executes test cases.

Returns
  void

{
log("Running Tests...");
testCase_1();
testCase_2();
testCase_3();
log("Test Cases over!");
std::cout << std::endl;
}
Here is the call graph for this function:

## testCase_1() [1/3]

void TestCases::testCase_1 | ( |  | ) |

inline

A test case contains edge case, printing inorder successor of last node.

Returns
  void

< Expected output of this test

< Data to make nodes in BST

< Adding nodes to BST

< Printing inorder to cross-verify.

< The inorder successor node for given data

memory cleanup!

{
const operations_on_datastructures::inorder_traversal_of_bst::Node
*expectedOutput = nullptr; ///< Expected output of this test
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
log("This is test case 1 : ");
log("Description:");
log(" EDGE CASE : Printing inorder successor for last node in the "
"BST, Output will be nullptr.");
operations_on_datastructures::inorder_traversal_of_bst::Node *root =
nullptr;
std::vector<int64_t> node_data{
20, 3, 5, 6, 2, 23, 45, 78, 21}; ///< Data to make nodes in BST
root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(
root,
node_data); ///< Adding nodes to BST
std::cout << "Inorder sequence is : ";
operations_on_datastructures::inorder_traversal_of_bst::printInorder(
root); ///< Printing inorder to cross-verify.
std::cout << std::endl;
operations_on_datastructures::inorder_traversal_of_bst::Node
*inorderSuccessor = operations_on_datastructures::
inorder_traversal_of_bst::getInorderSuccessor(
root, 78); ///< The inorder successor node for given data
log("Checking assert expression...");
assert(inorderSuccessor == expectedOutput);
log("Assertion check passed!");
operations_on_datastructures::inorder_traversal_of_bst::deallocate(
root); /// memory cleanup!
log("[PASS] : TEST CASE 1 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
operations_on_datastructures::inorder_traversal_of_bst::Node
A Node structure representing a single node in BST.
Definition: inorder_successor_of_bst.cpp:56
operations_on_datastructures::inorder_traversal_of_bst::makeBST
Node * makeBST(Node *root, const std::vector< int64_t > &data)
This function is used in test cases to quickly create BST containing large data instead of hard codin...
Definition: inorder_successor_of_bst.cpp:155
operations_on_datastructures::inorder_traversal_of_bst::printInorder
void printInorder(Node *root)
Prints the BST in inorder traversal using recursion.
Definition: inorder_successor_of_bst.cpp:136
operations_on_datastructures::inorder_traversal_of_bst::deallocate
void deallocate(Node *rootNode)
This function clears the memory allocated to entire tree recursively. Its just for clean up the memor...
Definition: inorder_successor_of_bst.cpp:210
std::vector
Here is the call graph for this function:

## testCase_1() [2/3]

void TestCases::testCase_1 | ( |  | ) |

inline

A test case contains edge case, Only contains one element.

Returns
  void

< Expected output of this test

< Data to make linked list which will be the sublist

< Data to make linked list which will be the main list

< Sublist to be searched

< Main list in which sublist is to be searched

< boolean, if sublist exist or not

{
const bool expectedOutput = true; ///< Expected output of this test
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
log("This is test case 1 for sublist search Algorithm : ");
log("Description:");
log(" EDGE CASE : Only contains one element");
std::vector<uint64_t> sublistData = {
6}; ///< Data to make linked list which will be the sublist
std::vector<uint64_t> mainlistData = {
2, 5, 6, 7,
8}; ///< Data to make linked list which will be the main list
search::sublist_search::Node *sublistLL =
search::sublist_search::makeLinkedList(
sublistData); ///< Sublist to be searched
search::sublist_search::Node *mainlistLL =
search::sublist_search::makeLinkedList(
mainlistData); ///< Main list in which sublist is to be
///< searched
bool exists = search::sublist_search::sublistSearch(
sublistLL, mainlistLL); ///< boolean, if sublist exist or not
log("Checking assert expression...");
assert(exists == expectedOutput);
log("Assertion check passed!");
log("[PASS] : TEST CASE 1 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
delete (sublistLL);
delete (mainlistLL);
}
search::sublist_search::Node
A Node structure representing a single link Node in a linked list.
Definition: sublist_search.cpp:47
search::sublist_search::sublistSearch
bool sublistSearch(Node *sublist, Node *mainList)
Main searching function.
Definition: sublist_search.cpp:100
search::sublist_search::makeLinkedList
Node * makeLinkedList(const std::vector< uint64_t > &data)
Give a vector of data, it adds each element of vector in the linked list and return the address of he...
Definition: sublist_search.cpp:73
dynamic_programming::word_break::exists
bool exists(const std::string &str, const std::unordered_set< std::string > &strSet)
Function that checks if the string passed in param is present in the the unordered_set passed.
Definition: word_break.cpp:60
Here is the call graph for this function:

## testCase_1() [3/3]

void TestCases::testCase_1 | ( |  | ) |

inline

A test case with single input.

Returns
  void

{
const int64_t inputSize = 1;
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
log("This is test case 1 for Random Pivot Quick Sort Algorithm : ");
log("Description:");
log(" EDGE CASE : Only contains one element");
std::array<int64_t, inputSize> unsorted_arr{2};
int64_t start = 0;
int64_t end = unsorted_arr.size() - 1; // length - 1
log("Running algorithm of data of length 50 ...");
std::array<int64_t, unsorted_arr.size()> sorted_arr =
sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr, start,
end);
log("Algorithm finished!");
log("Checking assert expression...");
assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
log("Assertion check passed!");
log("[PASS] : TEST CASE 1 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
}
std::array
std::end
T end(T... args)
std::is_sorted
T is_sorted(T... args)
sorting::random_pivot_quick_sort::quickSortRP
std::array< int64_t, size > quickSortRP(std::array< int64_t, size > arr, int64_t start, int64_t end)
Random pivot quick sort function. This function is the starting point of the algorithm.
Definition: random_pivot_quick_sort.cpp:130
std::array::size
T size(T... args)
Here is the call graph for this function:

## testCase_2() [1/3]

void TestCases::testCase_2 | ( |  | ) |

inline

A test case which contains main list of 100 elements and sublist of 20.

Returns
  void

< Expected output of this test

< Data to make nodes in BST

< Adding nodes to BST

< Printing inorder to cross-verify.

< The inorder successor node for given data

memory cleanup!

{
const int expectedOutput = 21; ///< Expected output of this test
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
log("This is test case 2 : ");
operations_on_datastructures::inorder_traversal_of_bst::Node *root =
nullptr;
std::vector<int64_t> node_data{
20, 3, 5, 6, 2, 23, 45, 78, 21}; ///< Data to make nodes in BST
root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(
root,
node_data); ///< Adding nodes to BST
std::cout << "Inorder sequence is : ";
operations_on_datastructures::inorder_traversal_of_bst::printInorder(
root); ///< Printing inorder to cross-verify.
std::cout << std::endl;
operations_on_datastructures::inorder_traversal_of_bst::Node
*inorderSuccessor = operations_on_datastructures::
inorder_traversal_of_bst::getInorderSuccessor(
root, 20); ///< The inorder successor node for given data
log("Checking assert expression...");
assert(inorderSuccessor->data == expectedOutput);
log("Assertion check passed!");
operations_on_datastructures::inorder_traversal_of_bst::deallocate(
root); /// memory cleanup!
log("[PASS] : TEST CASE 2 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
operations_on_datastructures::inorder_traversal_of_bst::Node::data
int64_t data
The key/value of the node.
Definition: inorder_successor_of_bst.cpp:58
Here is the call graph for this function:

## testCase_2() [2/3]

void TestCases::testCase_2 | ( |  | ) |

inline

A test case which contains main list of 100 elements and sublist of 20.

Returns
  void

Expected output of this test

< Data to make linked list which will be the sublist

< Main list in which sublist is to be searched

Inserts 100 elements in main list

Inserts 20 elements in sublist

< Sublist to be searched

< Main list in which sublist is to be searched

< boolean, if sublist exist or not

{
const bool expectedOutput = true; /// Expected output of this test
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
log("This is test case 2 for sublist search Algorithm : ");
log("Description:");
log(" contains main list of 100 elements and sublist of 20");
std::vector<uint64_t> sublistData(
20); ///< Data to make linked list which will be the sublist
std::vector<uint64_t> mainlistData(
100); ///< Main list in which sublist is to be searched
for (int i = 0; i < 100; i++) {
/// Inserts 100 elements in main list
mainlistData[i] = i + 1;
}
int temp = 0;
for (int i = 45; i < 65; i++) {
/// Inserts 20 elements in sublist
sublistData[temp] = i + 1;
temp++;
}
search::sublist_search::Node *sublistLL =
search::sublist_search::makeLinkedList(
sublistData); ///< Sublist to be searched
search::sublist_search::Node *mainlistLL =
search::sublist_search::makeLinkedList(
mainlistData); ///< Main list in which sublist is to be
///< searched
bool exists = search::sublist_search::sublistSearch(
sublistLL, mainlistLL); ///< boolean, if sublist exist or not
log("Checking assert expression...");
assert(exists == expectedOutput);
log("Assertion check passed!");
log("[PASS] : TEST CASE 2 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
}
Here is the call graph for this function:

## testCase_2() [3/3]

void TestCases::testCase_2 | ( |  | ) |

inline

A test case with input array of length 500.

Returns
  void

{
const int64_t inputSize = 500;
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
log("Description:");
log(" BIG INPUT : Contains 500 elements and repeated elements");
log("This is test case 2 for Random Pivot Quick Sort Algorithm : ");
std::array<int64_t, inputSize> unsorted_arr =
sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(
1, 10000);
int64_t start = 0;
int64_t end = unsorted_arr.size() - 1; // length - 1
log("Running algorithm of data of length 500 ...");
std::array<int64_t, unsorted_arr.size()> sorted_arr =
sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr, start,
end);
log("Algorithm finished!");
log("Checking assert expression...");
assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
log("Assertion check passed!");
log("[PASS] : TEST CASE 2 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
}
Here is the call graph for this function:

## testCase_3() [1/3]

void TestCases::testCase_3 | ( |  | ) |

inline

A test case which contains main list of 50 elements and sublist of 20.

Returns
  void

< Expected output of this test

< Data to make nodes in BST

< Adding nodes to BST

< Printing inorder to cross-verify.

< The inorder successor node for given data

memory cleanup!

{
const int expectedOutput = 110; ///< Expected output of this test
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
log("This is test case 3 : ");
operations_on_datastructures::inorder_traversal_of_bst::Node *root =
nullptr;
std::vector<int64_t> node_data{
89, 67, 32, 56, 90, 123, 120,
110, 115, 6, 78, 7, 10}; ///< Data to make nodes in BST
root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(
root,
node_data); ///< Adding nodes to BST
std::cout << "Inorder sequence is : ";
operations_on_datastructures::inorder_traversal_of_bst::printInorder(
root); ///< Printing inorder to cross-verify.
std::cout << std::endl;
operations_on_datastructures::inorder_traversal_of_bst::Node
*inorderSuccessor = operations_on_datastructures::
inorder_traversal_of_bst::getInorderSuccessor(
root, 90); ///< The inorder successor node for given data
log("Checking assert expression...");
assert(inorderSuccessor->data == expectedOutput);
log("Assertion check passed!");
operations_on_datastructures::inorder_traversal_of_bst::deallocate(
root); /// memory cleanup!
log("[PASS] : TEST CASE 3 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
Here is the call graph for this function:

## testCase_3() [2/3]

void TestCases::testCase_3 | ( |  | ) |

inline

A test case which contains main list of 50 elements and sublist of 20.

Returns
  void

< Expected output of this test

< Sublist to be searched

< Main list in which sublist is to be searched

Inserts 100 elements in main list

Inserts 20 elements in sublist

< Sublist to be searched

< Main list in which sublist is to be searched

< boolean, if sublist exist or not

{
const bool expectedOutput = false; ///< Expected output of this test
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
log("This is test case 3 for sublist search Algorithm : ");
log("Description:");
log(" contains main list of 50 elements and sublist of 20");
std::vector<uint64_t> sublistData(20); ///< Sublist to be searched
std::vector<uint64_t> mainlistData(
50); ///< Main list in which sublist is to be searched
for (int i = 0; i < 50; i++) {
/// Inserts 100 elements in main list
mainlistData.push_back(i + 1);
}
for (int i = 45; i < 65; i++) {
/// Inserts 20 elements in sublist
sublistData.push_back(i + 1);
}
search::sublist_search::Node *sublistLL =
search::sublist_search::makeLinkedList(
sublistData); ///< Sublist to be searched
search::sublist_search::Node *mainlistLL =
search::sublist_search::makeLinkedList(
mainlistData); ///< Main list in which sublist is to be
///< searched
bool exists = search::sublist_search::sublistSearch(
sublistLL, mainlistLL); ///< boolean, if sublist exist or not
log("Checking assert expression...");
assert(exists == expectedOutput);
log("Assertion check passed!");
log("[PASS] : TEST CASE 3 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
}
Here is the call graph for this function:

## testCase_3() [3/3]

void TestCases::testCase_3 | ( |  | ) |

inline

A test case with array of length 1000.

Returns
  void

{
const int64_t inputSize = 1000;
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
log("This is test case 3 for Random Pivot Quick Sort Algorithm : ");
log("Description:");
log(" LARGE INPUT : Contains 1000 elements and repeated elements");
std::array<int64_t, inputSize> unsorted_arr =
sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(
1, 10000);
int64_t start = 0;
int64_t end = unsorted_arr.size() - 1; // length - 1
log("Running algorithm...");
std::array<int64_t, unsorted_arr.size()> sorted_arr =
sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr, start,
end);
log("Algorithm finished!");
log("Checking assert expression...");
assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
log("Assertion check passed!");
log("[PASS] : TEST CASE 3 PASS!");
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~");
}
Here is the call graph for this function:

The documentation for this class was generated from the following files:

- operations_on_datastructures/inorder_successor_of_bst.cpp
- search/sublist_search.cpp
- sorting/random_pivot_quick_sort.cpp