Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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 ( msg)
inlineprivate

A function to print given message on console.

Template Parameters
TType of the given message.
Returns
void
233  {
234  // It's just to avoid writing cout and endl
235  std::cout << "[TESTS] : ---> " << msg << std::endl;
236  }
T endl(T... args)
Here is the call graph for this function:

◆ log() [2/3]

template<typename T >
void TestCases::log ( msg)
inlineprivate

A function to print given message on console.

Template Parameters
TType of the given message.
Returns
void
161  {
162  // It's just to avoid writing cout and endl
163  std::cout << "[TESTS] : ---> " << msg << std::endl;
164  }
Here is the call graph for this function:

◆ log() [3/3]

template<typename T >
void TestCases::log ( msg)
inlineprivate

A function to print64_t given message on console.

Template Parameters
TType of the given message.
Returns
void
169  {
170  // It's just to avoid writing cout and endl
171  std::cout << "[TESTS] : ---> " << msg << std::endl;
172  }
Here is the call graph for this function:

◆ runTests() [1/3]

void TestCases::runTests ( )
inline

Executes test cases.

Returns
void
243  {
244  log("Running Tests...");
245 
246  testCase_1();
247  testCase_2();
248  testCase_3();
249 
250  log("Test Cases over!");
251  std::cout << std::endl;
252  }
void log(T msg)
A function to print given message on console.
Definition: inorder_successor_of_bst.cpp:233
void testCase_2()
A test case which contains main list of 100 elements and sublist of 20.
Definition: inorder_successor_of_bst.cpp:304
void testCase_1()
A test case contains edge case, printing inorder successor of last node.
Definition: inorder_successor_of_bst.cpp:259
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
171  {
172  log("Running Tests...");
173 
174  testCase_1();
175  testCase_2();
176  testCase_3();
177 
178  log("Test Cases over!");
179  std::cout << std::endl;
180  }
Here is the call graph for this function:

◆ runTests() [3/3]

void TestCases::runTests ( )
inline

Executes test cases.

Returns
void
179  {
180  log("Running Tests...");
181 
182  testCase_1();
183  testCase_2();
184  testCase_3();
185 
186  log("Test Cases over!");
187  std::cout << std::endl;
188  }
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!

259  {
261  *expectedOutput = nullptr; ///< Expected output of this test
262 
263  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
264  log("This is test case 1 : ");
265  log("Description:");
266  log(" EDGE CASE : Printing inorder successor for last node in the "
267  "BST, Output will be nullptr.");
268 
270  nullptr;
271  std::vector<int64_t> node_data{
272  20, 3, 5, 6, 2, 23, 45, 78, 21}; ///< Data to make nodes in BST
273 
275  root,
276  node_data); ///< Adding nodes to BST
277 
278  std::cout << "Inorder sequence is : ";
280  root); ///< Printing inorder to cross-verify.
281  std::cout << std::endl;
282 
284  *inorderSuccessor = operations_on_datastructures::
285  inorder_traversal_of_bst::getInorderSuccessor(
286  root, 78); ///< The inorder successor node for given data
287 
288  log("Checking assert expression...");
289  assert(inorderSuccessor == expectedOutput);
290  log("Assertion check passed!");
291 
293  root); /// memory cleanup!
294 
295  log("[PASS] : TEST CASE 1 PASS!");
296  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
297  }
A Node structure representing a single node in BST.
Definition: inorder_successor_of_bst.cpp:56
void printInorder(Node *root)
Prints the BST in inorder traversal using recursion.
Definition: inorder_successor_of_bst.cpp:136
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
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
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

186  {
187  const bool expectedOutput = true; ///< Expected output of this test
188 
189  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
190  "~");
191  log("This is test case 1 for sublist search Algorithm : ");
192  log("Description:");
193  log(" EDGE CASE : Only contains one element");
194 
195  std::vector<uint64_t> sublistData = {
196  6}; ///< Data to make linked list which will be the sublist
197  std::vector<uint64_t> mainlistData = {
198  2, 5, 6, 7,
199  8}; ///< Data to make linked list which will be the main list
200 
201  search::sublist_search::Node *sublistLL =
203  sublistData); ///< Sublist to be searched
204  search::sublist_search::Node *mainlistLL =
206  mainlistData); ///< Main list in which sublist is to be
207  ///< searched
208 
210  sublistLL, mainlistLL); ///< boolean, if sublist exist or not
211 
212  log("Checking assert expression...");
213  assert(exists == expectedOutput);
214  log("Assertion check passed!");
215 
216  log("[PASS] : TEST CASE 1 PASS!");
217  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
218  "~");
219 
220  delete (sublistLL);
221  delete (mainlistLL);
222  }
A Node structure representing a single link Node in a linked list.
Definition: sublist_search.cpp:47
bool sublistSearch(Node *sublist, Node *mainList)
Main searching function.
Definition: sublist_search.cpp:100
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
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
194  {
195  const int64_t inputSize = 1;
196  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
197  log("This is test case 1 for Random Pivot Quick Sort Algorithm : ");
198  log("Description:");
199  log(" EDGE CASE : Only contains one element");
200  std::array<int64_t , inputSize> unsorted_arr{2};
201 
202  int64_t start = 0;
203  int64_t end = unsorted_arr.size() - 1; // length - 1
204 
205  log("Running algorithm of data of length 50 ...");
206  std::array<int64_t , unsorted_arr.size()> sorted_arr = sorting::random_pivot_quick_sort::quickSortRP(
207  unsorted_arr, start, end
208  );
209  log("Algorithm finished!");
210 
211  log("Checking assert expression...");
212  assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
213  log("Assertion check passed!");
214 
215  log("[PASS] : TEST CASE 1 PASS!");
216  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
217  }
T end(T... args)
T is_sorted(T... args)
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:112
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!

304  {
305  const int expectedOutput = 21; ///< Expected output of this test
306 
307  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
308  log("This is test case 2 : ");
309 
311  nullptr;
312  std::vector<int64_t> node_data{
313  20, 3, 5, 6, 2, 23, 45, 78, 21}; ///< Data to make nodes in BST
314 
316  root,
317  node_data); ///< Adding nodes to BST
318 
319  std::cout << "Inorder sequence is : ";
321  root); ///< Printing inorder to cross-verify.
322  std::cout << std::endl;
323 
325  *inorderSuccessor = operations_on_datastructures::
326  inorder_traversal_of_bst::getInorderSuccessor(
327  root, 20); ///< The inorder successor node for given data
328 
329  log("Checking assert expression...");
330  assert(inorderSuccessor->data == expectedOutput);
331  log("Assertion check passed!");
332 
334  root); /// memory cleanup!
335 
336  log("[PASS] : TEST CASE 2 PASS!");
337  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
338  }
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

229  {
230  const bool expectedOutput = true; /// Expected output of this test
231 
232  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
233  "~");
234  log("This is test case 2 for sublist search Algorithm : ");
235  log("Description:");
236  log(" contains main list of 100 elements and sublist of 20");
237 
238  std::vector<uint64_t> sublistData(
239  20); ///< Data to make linked list which will be the sublist
240  std::vector<uint64_t> mainlistData(
241  100); ///< Main list in which sublist is to be searched
242 
243  for (int i = 0; i < 100; i++) {
244  /// Inserts 100 elements in main list
245  mainlistData[i] = i + 1;
246  }
247 
248  int temp = 0;
249  for (int i = 45; i < 65; i++) {
250  /// Inserts 20 elements in sublist
251  sublistData[temp] = i + 1;
252  temp++;
253  }
254 
255  search::sublist_search::Node *sublistLL =
257  sublistData); ///< Sublist to be searched
258  search::sublist_search::Node *mainlistLL =
260  mainlistData); ///< Main list in which sublist is to be
261  ///< searched
262 
264  sublistLL, mainlistLL); ///< boolean, if sublist exist or not
265 
266  log("Checking assert expression...");
267  assert(exists == expectedOutput);
268  log("Assertion check passed!");
269 
270  log("[PASS] : TEST CASE 2 PASS!");
271  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
272  "~");
273  }
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
223  {
224  const int64_t inputSize = 500;
225  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
226  log("Description:");
227  log(" BIG INPUT : Contains 500 elements and repeated elements");
228  log("This is test case 2 for Random Pivot Quick Sort Algorithm : ");
229  std::array<int64_t , inputSize> unsorted_arr = sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(1, 10000);
230 
231  int64_t start = 0;
232  int64_t end = unsorted_arr.size() - 1; // length - 1
233 
234  log("Running algorithm of data of length 500 ...");
235  std::array<int64_t , unsorted_arr.size()> sorted_arr = sorting::random_pivot_quick_sort::quickSortRP(
236  unsorted_arr, start, end
237  );
238  log("Algorithm finished!");
239 
240  log("Checking assert expression...");
241  assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
242  log("Assertion check passed!");
243 
244  log("[PASS] : TEST CASE 2 PASS!");
245  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
246  }
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!

345  {
346  const int expectedOutput = 110; ///< Expected output of this test
347 
348  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
349  log("This is test case 3 : ");
350 
352  nullptr;
353  std::vector<int64_t> node_data{
354  89, 67, 32, 56, 90, 123, 120,
355  110, 115, 6, 78, 7, 10}; ///< Data to make nodes in BST
356 
358  root,
359  node_data); ///< Adding nodes to BST
360 
361  std::cout << "Inorder sequence is : ";
363  root); ///< Printing inorder to cross-verify.
364  std::cout << std::endl;
365 
367  *inorderSuccessor = operations_on_datastructures::
368  inorder_traversal_of_bst::getInorderSuccessor(
369  root, 90); ///< The inorder successor node for given data
370 
371  log("Checking assert expression...");
372  assert(inorderSuccessor->data == expectedOutput);
373  log("Assertion check passed!");
374 
376  root); /// memory cleanup!
377 
378  log("[PASS] : TEST CASE 3 PASS!");
379  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
380  }
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

280  {
281  const bool expectedOutput = false; ///< Expected output of this test
282 
283  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
284  "~");
285  log("This is test case 3 for sublist search Algorithm : ");
286  log("Description:");
287  log(" contains main list of 50 elements and sublist of 20");
288 
289  std::vector<uint64_t> sublistData(20); ///< Sublist to be searched
290  std::vector<uint64_t> mainlistData(
291  50); ///< Main list in which sublist is to be searched
292 
293  for (int i = 0; i < 50; i++) {
294  /// Inserts 100 elements in main list
295  mainlistData.push_back(i + 1);
296  }
297 
298  for (int i = 45; i < 65; i++) {
299  /// Inserts 20 elements in sublist
300  sublistData.push_back(i + 1);
301  }
302 
303  search::sublist_search::Node *sublistLL =
305  sublistData); ///< Sublist to be searched
306  search::sublist_search::Node *mainlistLL =
308  mainlistData); ///< Main list in which sublist is to be
309  ///< searched
310 
312  sublistLL, mainlistLL); ///< boolean, if sublist exist or not
313 
314  log("Checking assert expression...");
315  assert(exists == expectedOutput);
316  log("Assertion check passed!");
317 
318  log("[PASS] : TEST CASE 3 PASS!");
319  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
320  "~");
321  }
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
252  {
253  const int64_t inputSize = 1000;
254  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
255  log("This is test case 3 for Random Pivot Quick Sort Algorithm : ");
256  log("Description:");
257  log(" LARGE INPUT : Contains 1000 elements and repeated elements");
258  std::array<int64_t , inputSize> unsorted_arr = sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(1, 10000);
259 
260  int64_t start = 0;
261  int64_t end = unsorted_arr.size() - 1; // length - 1
262 
263  log("Running algorithm...");
264  std::array<int64_t , unsorted_arr.size()> sorted_arr = sorting::random_pivot_quick_sort::quickSortRP(
265  unsorted_arr, start, end
266  );
267  log("Algorithm finished!");
268 
269  log("Checking assert expression...");
270  assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
271  log("Assertion check passed!");
272 
273  log("[PASS] : TEST CASE 3 PASS!");
274  log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
275  }
Here is the call graph for this function:

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