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 --- tests/array.c | 30 ++++++++++++++++++++++++++++++ tests/main.c | 3 +++ 2 files changed, 33 insertions(+) (limited to 'tests') 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