From 3e656854bc6c68896fb92d17aff3c82dd9086b47 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Thu, 13 Feb 2025 01:14:11 +0200 Subject: [PATCH] userspace/test: implement more tests in libc_test Signed-off-by: EnderIce2 --- Userspace/apps/test/libc_test/assert/assert.c | 8 +++++ .../apps/test/libc_test/dirent/alphasort.c | 30 +++++++++++++++++-- .../apps/test/libc_test/dirent/closedir.c | 17 ++++++++++- Userspace/apps/test/libc_test/dirent/dirfd.c | 16 +++++++++- .../apps/test/libc_test/dirent/fdopendir.c | 25 +++++++++++++++- .../apps/test/libc_test/dirent/opendir.c | 15 +++++++++- .../test/libc_test/dirent/posix_getdents.c | 9 +++++- .../apps/test/libc_test/dirent/readdir_r.c | 7 ++++- 8 files changed, 119 insertions(+), 8 deletions(-) diff --git a/Userspace/apps/test/libc_test/assert/assert.c b/Userspace/apps/test/libc_test/assert/assert.c index eaa5f2db..d3ac3997 100644 --- a/Userspace/apps/test/libc_test/assert/assert.c +++ b/Userspace/apps/test/libc_test/assert/assert.c @@ -17,8 +17,16 @@ #include +/* https://pubs.opengroup.org/onlinepubs/9799919799/functions/assert.html */ + int test_assert(void) { assert(1 == 1); +#define A 1 +#define B 2 + assert(A != B); + assert((A == 1) && (B == 2)); + assert(1); + assert(!0); return 0; } diff --git a/Userspace/apps/test/libc_test/dirent/alphasort.c b/Userspace/apps/test/libc_test/dirent/alphasort.c index 04dd7a20..5c3fc86b 100644 --- a/Userspace/apps/test/libc_test/dirent/alphasort.c +++ b/Userspace/apps/test/libc_test/dirent/alphasort.c @@ -15,6 +15,32 @@ along with Fennix Userspace. If not, see . */ -#include +#ifdef __linux__ +#define _DEFAULT_SOURCE 1 /* for alphasort */ +#endif -int test_alphasort(void) { return 2; } +#include +#include +#include + +/* https://pubs.opengroup.org/onlinepubs/9799919799/functions/alphasort.html */ + +int test_alphasort(void) +{ + struct dirent **namelist; + int n = scandir(".", &namelist, NULL, alphasort); + if (n < 0) + { + perror("scandir"); + return n; + } + + for (int i = 0; i < n; i++) + { + // printf("%s\n", namelist[i]->d_name); + free(namelist[i]); + } + + free(namelist); + return 0; +} diff --git a/Userspace/apps/test/libc_test/dirent/closedir.c b/Userspace/apps/test/libc_test/dirent/closedir.c index 3b1a238c..6da97a01 100644 --- a/Userspace/apps/test/libc_test/dirent/closedir.c +++ b/Userspace/apps/test/libc_test/dirent/closedir.c @@ -16,5 +16,20 @@ */ #include +#include -int test_closedir(void) { return 2; } +/* https://pubs.opengroup.org/onlinepubs/9799919799/functions/closedir.html */ + +int test_closedir(void) +{ + DIR *dir = opendir("."); + if (closedir(dir) != 0) + return 0x101; + + if (closedir(NULL) != -1) + return 0x102; + + // if (closedir(dir) != -1) /* yeah... this will result in a core dump */ + // return 0x103; + return 0; +} diff --git a/Userspace/apps/test/libc_test/dirent/dirfd.c b/Userspace/apps/test/libc_test/dirent/dirfd.c index 5a6b4ad4..35971952 100644 --- a/Userspace/apps/test/libc_test/dirent/dirfd.c +++ b/Userspace/apps/test/libc_test/dirent/dirfd.c @@ -16,5 +16,19 @@ */ #include +#include -int test_dirfd(void) { return 2; } +/* https://pubs.opengroup.org/onlinepubs/9799919799/functions/dirfd.html */ + +int test_dirfd(void) +{ + DIR *dir = opendir("."); + + if (dirfd(dir) == -1) + return 0x101; + + // if (dirfd(NULL) != -1) + // return 0x102; + + return 0; +} diff --git a/Userspace/apps/test/libc_test/dirent/fdopendir.c b/Userspace/apps/test/libc_test/dirent/fdopendir.c index 0ef67c91..655523af 100644 --- a/Userspace/apps/test/libc_test/dirent/fdopendir.c +++ b/Userspace/apps/test/libc_test/dirent/fdopendir.c @@ -16,5 +16,28 @@ */ #include +#include +#include -int test_fdopendir(void) { return 2; } +/* https://pubs.opengroup.org/onlinepubs/9799919799/functions/fdopendir.html */ + +int test_fdopendir(void) +{ + DIR *dir; + struct dirent *dp; + + int fd = open(".", O_RDONLY); + + dir = fdopendir(fd); + if (dir == NULL) + return 0x101; + + dp = readdir(dir); + if (dp == NULL) + return 0x102; + + if (closedir(dir) != 0) + return 0x103; + + return 0; +} diff --git a/Userspace/apps/test/libc_test/dirent/opendir.c b/Userspace/apps/test/libc_test/dirent/opendir.c index dfbd1126..63b1a201 100644 --- a/Userspace/apps/test/libc_test/dirent/opendir.c +++ b/Userspace/apps/test/libc_test/dirent/opendir.c @@ -16,5 +16,18 @@ */ #include +#include -int test_opendir() { return 2; } +/* https://pubs.opengroup.org/onlinepubs/9799919799/functions/opendir.html */ + +int test_opendir() +{ + DIR *dir = opendir("."); + if (dir == NULL) + return 0x101; + + if (closedir(dir) != 0) + return 0x102; + + return 0; +} diff --git a/Userspace/apps/test/libc_test/dirent/posix_getdents.c b/Userspace/apps/test/libc_test/dirent/posix_getdents.c index 4db30bd7..7f57c212 100644 --- a/Userspace/apps/test/libc_test/dirent/posix_getdents.c +++ b/Userspace/apps/test/libc_test/dirent/posix_getdents.c @@ -16,5 +16,12 @@ */ #include +#include +#include -int test_posix_getdents() { return 2; } +/* https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_getdents.html */ + +int test_posix_getdents() +{ + return 2; +} diff --git a/Userspace/apps/test/libc_test/dirent/readdir_r.c b/Userspace/apps/test/libc_test/dirent/readdir_r.c index 3e35ff9e..be87cc69 100644 --- a/Userspace/apps/test/libc_test/dirent/readdir_r.c +++ b/Userspace/apps/test/libc_test/dirent/readdir_r.c @@ -17,4 +17,9 @@ #include -int test_readdir_r() { return 2; } +/* https://pubs.opengroup.org/onlinepubs/9799919799/functions/readdir_r.html */ + +int test_readdir_r() +{ + return 2; +}