Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
TestCases Class Reference

class encapsulating the necessary test cases More...

Public Member Functions

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

Private Member Functions

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

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
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 ( T 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 ( T msg)
inlineprivate

A function to print64_t given message on console.

Template Parameters
TType of the given message.
Returns
void
189 {
190 // It's just to avoid writing cout and endl
191 std::cout << "[TESTS] : ---> " << msg << std::endl;
192 }
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!");
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!");
180 }
Here is the call graph for this function:

◆ runTests() [3/3]

void TestCases::runTests ( )
inline

Executes test cases.

Returns
void
199 {
200 log("Running Tests...");
201
202 testCase_1();
203 testCase_2();
204 testCase_3();
205
206 log("Test Cases over!");
208 }
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.
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
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
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
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
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
214 {
215 const int64_t inputSize = 1;
216 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
217 "~");
218 log("This is test case 1 for Random Pivot Quick Sort Algorithm : ");
219 log("Description:");
220 log(" EDGE CASE : Only contains one element");
221 std::array<int64_t, inputSize> unsorted_arr{2};
222
223 int64_t start = 0;
224 int64_t end = unsorted_arr.size() - 1; // length - 1
225
226 log("Running algorithm of data of length 50 ...");
227 std::array<int64_t, unsorted_arr.size()> sorted_arr =
229 end);
230 log("Algorithm finished!");
231
232 log("Checking assert expression...");
233 assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
234 log("Assertion check passed!");
235
236 log("[PASS] : TEST CASE 1 PASS!");
237 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
238 "~");
239 }
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:130
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.
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
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
245 {
246 const int64_t inputSize = 500;
247 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
248 "~");
249 log("Description:");
250 log(" BIG INPUT : Contains 500 elements and repeated elements");
251 log("This is test case 2 for Random Pivot Quick Sort Algorithm : ");
252 std::array<int64_t, inputSize> unsorted_arr =
253 sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(
254 1, 10000);
255
256 int64_t start = 0;
257 int64_t end = unsorted_arr.size() - 1; // length - 1
258
259 log("Running algorithm of data of length 500 ...");
260 std::array<int64_t, unsorted_arr.size()> sorted_arr =
262 end);
263 log("Algorithm finished!");
264
265 log("Checking assert expression...");
266 assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
267 log("Assertion check passed!");
268
269 log("[PASS] : TEST CASE 2 PASS!");
270 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
271 "~");
272 }
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.
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
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
278 {
279 const int64_t inputSize = 1000;
280 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
281 "~");
282 log("This is test case 3 for Random Pivot Quick Sort Algorithm : ");
283 log("Description:");
284 log(" LARGE INPUT : Contains 1000 elements and repeated elements");
285 std::array<int64_t, inputSize> unsorted_arr =
286 sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(
287 1, 10000);
288
289 int64_t start = 0;
290 int64_t end = unsorted_arr.size() - 1; // length - 1
291
292 log("Running algorithm...");
293 std::array<int64_t, unsorted_arr.size()> sorted_arr =
295 end);
296 log("Algorithm finished!");
297
298 log("Checking assert expression...");
299 assert(std::is_sorted(sorted_arr.begin(), sorted_arr.end()));
300 log("Assertion check passed!");
301
302 log("[PASS] : TEST CASE 3 PASS!");
303 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
304 "~");
305 }
Here is the call graph for this function:

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