diff options
author | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:00 -0500 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:32 -0500 |
commit | bd5969fc876a10b18613302db7087ef3c40f18e1 (patch) | |
tree | 7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/posix/search.c | |
parent | a95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff) |
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/search.c')
-rw-r--r-- | lib/mlibc/tests/posix/search.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/search.c b/lib/mlibc/tests/posix/search.c new file mode 100644 index 0000000..6b68ef9 --- /dev/null +++ b/lib/mlibc/tests/posix/search.c @@ -0,0 +1,51 @@ +#include <search.h> +#include <assert.h> +#include <stddef.h> +#include <stdlib.h> + +static int compare(const void *pa, const void *pb) { + if (*(int*)pa < *(int*) pb) + return -1; + if (*(int*)pa > *(int*) pb) + return 1; + return 0; +} + +static void check_key(int key, void *root) { + int keyp = key; + void *ret = tfind((void*) &keyp, &root, compare); + assert(ret); + assert(**((int **) ret) == key); +} + +static void free_key(void *key) { + free(key); +} + +int main() { + void *root = NULL; + for (int i = 0; i < 12; i++) { + int *ptr = malloc(sizeof(int)); + assert(ptr); + *ptr = i; + + void *ret = tsearch((void*) ptr, &root, compare); + assert(ret); + assert(**((int **) ret) == i); + } + + // Test a couple of keys + check_key(1, root); + check_key(5, root); + check_key(10, root); + + // Verify NULL on non-existent key + int key = -1; + void *ret = tfind((void*) &key, &root, compare); + assert(ret == NULL); + + // tdelete is not implemented yet (#351) + (void)free_key; + // tdestroy(root, free_key); + return 0; +} |