From 8179fbfdec72dda8ea795ad1198525e41dd12740 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 1 Jul 2020 04:03:13 +0530 Subject: [PATCH] test cases added --- sorting/insertion_sort.cpp | 43 +++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index 180330a60..cc3216c1e 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -52,6 +52,8 @@ */ #include +#include +#include /** \brief * Insertion Sort Function @@ -72,22 +74,39 @@ void insertionSort(int *arr, int n) { } } +/** Test Cases to test algorithm */ +void tests() { + int arr1[10] = { 78, 34, 35, 6, 34, 56, 3, 56, 2, 4}; + insertionSort(arr1, 10); + assert(std::is_sorted(arr1, arr1 + 10)); + std::cout << "Test 1 Passed" << std::endl; + + int arr2[5] = {5, -3, 7, -2, 1}; + insertionSort(arr2, 5); + assert(std::is_sorted(arr2, arr2 + 5)); + std::cout << "Test 2 Passed" << std::endl; +} + /** Main Function */ int main() { - int n; - std::cout << "Enter the length of your array : "; - std::cin >> n; - int *arr = new int[n]; - std::cout << "Enter any " << n << " Numbers for Unsorted Array : "; + /// Running predefined tests to test algorithm + tests(); - for (int i = 0; i < n; i++) std::cin >> arr[i]; + /// For user insteraction + int n; + std::cout << "Enter the length of your array : "; + std::cin >> n; + int *arr = new int[n]; + std::cout << "Enter any " << n << " Numbers for Unsorted Array : "; - insertionSort(arr, n); + for (int i = 0; i < n; i++) std::cin >> arr[i]; - std::cout << "\nSorted Array : "; - for (int i = 0; i < n; i++) std::cout << arr[i] << " "; + insertionSort(arr, n); - std::cout << std::endl; - delete[] arr; - return 0; + std::cout << "\nSorted Array : "; + for (int i = 0; i < n; i++) std::cout << arr[i] << " "; + + std::cout << std::endl; + delete[] arr; + return 0; }