diff options
| author | Mistivia <i@mistivia.com> | 2025-06-08 21:40:25 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-06-08 21:40:25 +0800 |
| commit | 2d372ca3d44cb050d0dad577c41205177fcef023 (patch) | |
| tree | 1754aa090633753a2d3e9e56b0ac45ec23e150e4 /tests/test_sort.c | |
| parent | 9b98985d16e92e728d28d6c8ce4294f8a7230d9d (diff) | |
add more tests for qsort
Diffstat (limited to 'tests/test_sort.c')
| -rw-r--r-- | tests/test_sort.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/test_sort.c b/tests/test_sort.c index d996df2..e564c7a 100644 --- a/tests/test_sort.c +++ b/tests/test_sort.c @@ -2,8 +2,35 @@ #include <assert.h> #include <stdio.h> +#include <stdlib.h> +#include <time.h> + + +int compareInts(const void *a, const void *b) { + int arg1 = *(const int *)a; + int arg2 = *(const int *)b; + return (arg1 > arg2) - (arg1 < arg2); +} + +void rand_test(int n) { + int *arr1 = malloc(sizeof(int) * n); + int *arr2 = malloc(sizeof(int) * n); + for (int i = 0; i < n; i++) { + int val = rand(); + arr1[i] = val; + arr2[i] = val; + } + qsort(arr1, n, sizeof(int), compareInts); + Int_qsort(arr2, n); + for (int i = 0; i < n; i++) { + assert(arr1[i] == arr2[i]); + } + free(arr1); + free(arr2); +} int main() { + srand(time(NULL)); printf("[TEST] sort\n"); int arr[] = {2,4,5,7,4,6,7,8,8,5,4}; @@ -16,5 +43,8 @@ int main() { for (int i = 0; i < arrlen - 1; i++) { assert(arr[i] <= arr[i+1]); } + for (int i = 0; i < 10; i++) { + rand_test(rand() % 10000); + } printf("[PASS] sort\n"); } |
