mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-01 17:52:40 +08:00
improved self-tests
This commit is contained in:
@@ -51,6 +51,7 @@ namespace sorting {
|
||||
/** \brief
|
||||
* Insertion Sort Function
|
||||
*
|
||||
* @tparam T type of array
|
||||
* @param [in,out] arr Array to be sorted
|
||||
* @param n Size of Array
|
||||
*/
|
||||
@@ -69,6 +70,7 @@ void insertionSort(T *arr, int n) {
|
||||
|
||||
/** Insertion Sort Function
|
||||
*
|
||||
* @tparam T type of array
|
||||
* @param [in,out] arr pointer to array to be sorted
|
||||
*/
|
||||
template <typename T>
|
||||
@@ -88,6 +90,21 @@ void insertionSort(std::vector<T> *arr) {
|
||||
|
||||
} // namespace sorting
|
||||
|
||||
/**
|
||||
* @brief Create a random array objecthelper function to create a random array
|
||||
*
|
||||
* @tparam T type of array
|
||||
* @param arr array to fill (must be pre-allocated)
|
||||
* @param N number of array elements
|
||||
*/
|
||||
template <typename T>
|
||||
static void create_random_array(T *arr, int N) {
|
||||
while (N--) {
|
||||
double r = (std::rand() % 10000 - 5000) / 100.f;
|
||||
arr[N] = static_cast<T>(r);
|
||||
}
|
||||
}
|
||||
|
||||
/** Test Cases to test algorithm */
|
||||
void tests() {
|
||||
int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
|
||||
@@ -113,6 +130,20 @@ void tests() {
|
||||
sorting::insertionSort(&arr4);
|
||||
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
|
||||
std::cout << "passed" << std::endl;
|
||||
|
||||
int arr5[50];
|
||||
std::cout << "Test 5... ";
|
||||
create_random_array(arr5, 50);
|
||||
sorting::insertionSort(arr5, 50);
|
||||
assert(std::is_sorted(arr5, arr5 + 50));
|
||||
std::cout << "passed" << std::endl;
|
||||
|
||||
float arr6[50];
|
||||
std::cout << "Test 6... ";
|
||||
create_random_array(arr6, 50);
|
||||
sorting::insertionSort(arr6, 50);
|
||||
assert(std::is_sorted(arr6, arr6 + 50));
|
||||
std::cout << "passed" << std::endl;
|
||||
}
|
||||
|
||||
/** Main Function */
|
||||
|
||||
Reference in New Issue
Block a user