TheAlgorithms/C++ 1.0.0
All the 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

Definition at line 225 of file inorder_successor_of_bst.cpp.

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

Definition at line 233 of file inorder_successor_of_bst.cpp.

233 {
234 // It's just to avoid writing cout and endl
235 std::cout << "[TESTS] : ---> " << msg << std::endl;
236 }

◆ 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

Definition at line 176 of file sublist_search.cpp.

176 {
177 // It's just to avoid writing cout and endl
178 std::cout << "[TESTS] : ---> " << msg << std::endl;
179 }

◆ 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

Definition at line 189 of file random_pivot_quick_sort.cpp.

189 {
190 // It's just to avoid writing cout and endl
191 std::cout << "[TESTS] : ---> " << msg << std::endl;
192 }

◆ runTests() [1/3]

void TestCases::runTests ( )
inline

Executes test cases.

Returns
void

Definition at line 243 of file inorder_successor_of_bst.cpp.

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.
void testCase_2()
A test case which contains main list of 100 elements and sublist of 20.
void testCase_1()
A test case contains edge case, printing inorder successor of last node.
void testCase_3()
A test case which contains main list of 50 elements and sublist of 20.

◆ runTests() [2/3]

void TestCases::runTests ( )
inline

Executes test cases.

Returns
void

Definition at line 186 of file sublist_search.cpp.

186 {
187 log("Running Tests...");
188
189 testCase_1();
190 testCase_2();
191 testCase_3();
192
193 log("Test Cases over!");
194 std::cout << std::endl;
195 }

◆ runTests() [3/3]

void TestCases::runTests ( )
inline

Executes test cases.

Returns
void

Definition at line 199 of file random_pivot_quick_sort.cpp.

199 {
200 log("Running Tests...");
201
202 testCase_1();
203 testCase_2();
204 testCase_3();
205
206 log("Test Cases over!");
207 std::cout << std::endl;
208 }

◆ 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!

Definition at line 259 of file inorder_successor_of_bst.cpp.

259 {
261 *expectedOutput = nullptr;
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};
273
275 root,
276 node_data);
277
278 std::cout << "Inorder sequence is : ";
280 root);
281 std::cout << std::endl;
282
284 *inorderSuccessor = operations_on_datastructures::
285 inorder_traversal_of_bst::getInorderSuccessor(
286 root, 78);
287
288 log("Checking assert expression...");
289 assert(inorderSuccessor == expectedOutput);
290 log("Assertion check passed!");
291
293 root);
294
295 log("[PASS] : TEST CASE 1 PASS!");
296 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
297 }
A Node structure representing a single node in BST.
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...
void printInorder(Node *root)
Prints the BST in inorder traversal using recursion.
void deallocate(Node *rootNode)
This function clears the memory allocated to entire tree recursively. Its just for clean up the memor...

◆ 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

Definition at line 201 of file sublist_search.cpp.

201 {
202 const bool expectedOutput = true;
203
204 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
205 "~");
206 log("This is test case 1 for sublist search Algorithm : ");
207 log("Description:");
208 log(" EDGE CASE : Only contains one element");
209
210 std::vector<uint64_t> sublistData = {
211 6};
212 std::vector<uint64_t> mainlistData = {
213 2, 5, 6, 7,
214 8};
215
218 sublistData);
219 search::sublist_search::Node *mainlistLL =
221 mainlistData);
223
225 sublistLL, mainlistLL);
226
227 log("Checking assert expression...");
228 assert(exists == expectedOutput);
229 log("Assertion check passed!");
230
231 log("[PASS] : TEST CASE 1 PASS!");
232 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
233 "~");
234
235 deleteList(mainlistLL);
236 deleteList(sublistLL);
237 }
A Node structure representing a single link Node in a linked list.
bool sublistSearch(Node *sublist, Node *mainList)
Main searching function.
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...
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.

◆ testCase_1() [3/3]

void TestCases::testCase_1 ( )
inline

A test case with single input.

Returns
void

Definition at line 214 of file random_pivot_quick_sort.cpp.

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 }
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.

◆ 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!

Definition at line 304 of file inorder_successor_of_bst.cpp.

304 {
305 const int expectedOutput = 21;
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};
314
316 root,
317 node_data);
318
319 std::cout << "Inorder sequence is : ";
321 root);
322 std::cout << std::endl;
323
325 *inorderSuccessor = operations_on_datastructures::
326 inorder_traversal_of_bst::getInorderSuccessor(
327 root, 20);
328
329 log("Checking assert expression...");
330 assert(inorderSuccessor->data == expectedOutput);
331 log("Assertion check passed!");
332
334 root);
335
336 log("[PASS] : TEST CASE 2 PASS!");
337 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
338 }

◆ 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

Definition at line 244 of file sublist_search.cpp.

244 {
245 const bool expectedOutput = true;
246
247 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
248 "~");
249 log("This is test case 2 for sublist search Algorithm : ");
250 log("Description:");
251 log(" contains main list of 100 elements and sublist of 20");
252
253 std::vector<uint64_t> sublistData(
254 20);
255 std::vector<uint64_t> mainlistData(
256 100);
257
258 for (int i = 0; i < 100; i++) {
260 mainlistData[i] = i + 1;
261 }
262
263 int temp = 0;
264 for (int i = 45; i < 65; i++) {
266 sublistData[temp] = i + 1;
267 temp++;
268 }
269
272 sublistData);
273 search::sublist_search::Node *mainlistLL =
275 mainlistData);
277
279 sublistLL, mainlistLL);
280
281 log("Checking assert expression...");
282 assert(exists == expectedOutput);
283 log("Assertion check passed!");
284
285 log("[PASS] : TEST CASE 2 PASS!");
286 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
287 "~");
288
289 deleteList(mainlistLL);
290 deleteList(sublistLL);
291 }

◆ testCase_2() [3/3]

void TestCases::testCase_2 ( )
inline

A test case with input array of length 500.

Returns
void

Definition at line 245 of file random_pivot_quick_sort.cpp.

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 }

◆ 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!

Definition at line 345 of file inorder_successor_of_bst.cpp.

345 {
346 const int expectedOutput = 110;
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};
356
358 root,
359 node_data);
360
361 std::cout << "Inorder sequence is : ";
363 root);
364 std::cout << std::endl;
365
367 *inorderSuccessor = operations_on_datastructures::
368 inorder_traversal_of_bst::getInorderSuccessor(
369 root, 90);
370
371 log("Checking assert expression...");
372 assert(inorderSuccessor->data == expectedOutput);
373 log("Assertion check passed!");
374
376 root);
377
378 log("[PASS] : TEST CASE 3 PASS!");
379 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
380 }

◆ 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

Definition at line 298 of file sublist_search.cpp.

298 {
299 const bool expectedOutput = false;
300
301 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
302 "~");
303 log("This is test case 3 for sublist search Algorithm : ");
304 log("Description:");
305 log(" contains main list of 50 elements and sublist of 20");
306
307 std::vector<uint64_t> sublistData(20);
308 std::vector<uint64_t> mainlistData(
309 50);
310
311 for (int i = 0; i < 50; i++) {
313 mainlistData.push_back(i + 1);
314 }
315
316 for (int i = 45; i < 65; i++) {
318 sublistData.push_back(i + 1);
319 }
320
323 sublistData);
324 search::sublist_search::Node *mainlistLL =
326 mainlistData);
328
330 sublistLL, mainlistLL);
331
332 log("Checking assert expression...");
333 assert(exists == expectedOutput);
334 log("Assertion check passed!");
335
336 log("[PASS] : TEST CASE 3 PASS!");
337 log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
338 "~");
339
340 deleteList(mainlistLL);
341 deleteList(sublistLL);
342 }

◆ testCase_3() [3/3]

void TestCases::testCase_3 ( )
inline

A test case with array of length 1000.

Returns
void

Definition at line 278 of file random_pivot_quick_sort.cpp.

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 }

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