From 96f1ca95d5fbe8ff5bcf8802bb40d366409ea2fb Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sat, 19 Oct 2024 11:19:32 -0400 Subject: array: Add test for array_sort() Signed-off-by: Andrew Opalach --- include/al/array_sort.h | 8 +++++++- src/array.c | 11 +++++++++-- tests/array.c | 30 ++++++++++++++++++++++++++++++ tests/main.c | 3 +++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/include/al/array_sort.h b/include/al/array_sort.h index 51011f4..8d138d0 100644 --- a/include/al/array_sort.h +++ b/include/al/array_sort.h @@ -1,6 +1,8 @@ #ifndef _AL_ARRAY_SORT_H #define _AL_ARRAY_SORT_H +#include "lib.h" + #define AL_INSERSION_SORT(data, type, count, cmp) \ do { \ s32 i = 1; \ @@ -14,6 +16,10 @@ data[j + 1] = x; \ i++; \ } \ - } while (0); + } while (0) + +#if AL_USE_STDLIB +#define AL_STDLIB_QSORT(data, type, count, cmp) qsort(data, count, sizeof(type), cmp) +#endif #endif // _AL_ARRAY_SORT_H diff --git a/src/array.c b/src/array.c index cbeb256..8803c54 100644 --- a/src/array.c +++ b/src/array.c @@ -1,3 +1,7 @@ +#if !defined AL_ARRAY_MALLOC || !defined AL_ARRAY_REALLOC || !defined AL_ARRAY_FREE +#error "define all of AL_ARRAY_MALLOC, AL_ARRAY_REALLOC, and AL_ARRAY_FREE before including array.c" +#endif + #include "../include/al/lib.h" #include "../include/al/array_sort.h" @@ -70,8 +74,11 @@ } \ } while (0) -#define al_array_sort(arr, type, cmp) \ - AL_INSERSION_SORT((arr).data, type, (arr).size, cmp) +#if AL_USE_STDLIB +#define al_array_sort(arr, type, cmp) AL_STDLIB_QSORT((arr).data, type, (arr).size, cmp) +#else +#define al_array_sort(arr, type, cmp) AL_INSERSION_SORT((arr).data, type, (arr).size, cmp) +#endif #define al_array_free(arr) \ if ((arr).alloc) AL_ARRAY_FREE((arr).data) diff --git a/tests/array.c b/tests/array.c index 79664cc..6ac3845 100644 --- a/tests/array.c +++ b/tests/array.c @@ -1,5 +1,6 @@ #include #include +#include #include "array.h" @@ -315,6 +316,34 @@ static bool array_test_foreach(void) AL_TEST_END(); } +static s32 int32_compare(const void *a, const void *b) +{ + if (*(s32 *)a < *(s32 *)b) return 1; + else if (*(s32 *)b < *(s32 *)a) return -1; + return 0; +} + +static bool array_test_sort(void) +{ + AL_TEST_START("array_sort"); + + array(s32) arr; + al_array_init(arr); + + s32 end = al_rand() % 2000; + for (s32 i = 0; i < end; i++) { + al_array_push(arr, al_rand()); + } + + al_array_sort(arr, s32, int32_compare); + + for (u32 i = 1; i < arr.size; i++) { + AL_TEST_GTE(al_array_at(arr, i - 1), al_array_at(arr, i), s32); + } + + AL_TEST_END(); +} + bool array_tests_run(void) { AL_TEST_START_GROUP("array"); @@ -329,6 +358,7 @@ bool array_tests_run(void) AL_TEST_RUN(array_test_pop_at); AL_TEST_RUN(array_test_insert); AL_TEST_RUN(array_test_foreach); + AL_TEST_RUN(array_test_sort); AL_TEST_END_GROUP(); } diff --git a/tests/main.c b/tests/main.c index e263dd5..2c13ce9 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,4 +1,5 @@ #include +#include #include "lib.h" #include "array.h" @@ -10,6 +11,8 @@ s32 main(void) { + al_rand_init(); + if (!lib_tests_run()) goto fail; if (!array_tests_run()) goto fail; #if defined AL_HAVE_GNU_EXTENSIONS -- cgit v1.2.3-101-g0448