diff options
| author | 2024-10-19 11:19:32 -0400 | |
|---|---|---|
| committer | 2024-10-19 11:22:21 -0400 | |
| commit | 96f1ca95d5fbe8ff5bcf8802bb40d366409ea2fb (patch) | |
| tree | 3b263a66b29557179ee225fd0ff591942ad9493f | |
| parent | 6c36b3da63ce0c0a4a2281d182f1b4de2e5bef69 (diff) | |
| download | libalabaster-96f1ca95d5fbe8ff5bcf8802bb40d366409ea2fb.tar.gz libalabaster-96f1ca95d5fbe8ff5bcf8802bb40d366409ea2fb.tar.bz2 libalabaster-96f1ca95d5fbe8ff5bcf8802bb40d366409ea2fb.zip | |
array: Add test for array_sort()
Signed-off-by: Andrew Opalach <andrew@akon.city>
| -rw-r--r-- | include/al/array_sort.h | 8 | ||||
| -rw-r--r-- | src/array.c | 11 | ||||
| -rw-r--r-- | tests/array.c | 30 | ||||
| -rw-r--r-- | 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 <al/test.h> #include <al/array.h> +#include <al/random.h> #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 <al/lib.h> +#include <al/random.h> #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 |