userspace/test: implement more tests in libc_test

Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
EnderIce2 2025-02-13 01:14:11 +02:00
parent 6c3eefa85d
commit 3e656854bc
No known key found for this signature in database
GPG Key ID: 2EE20AF089811A5A
8 changed files with 119 additions and 8 deletions

View File

@ -17,8 +17,16 @@
#include <assert.h> #include <assert.h>
/* https://pubs.opengroup.org/onlinepubs/9799919799/functions/assert.html */
int test_assert(void) int test_assert(void)
{ {
assert(1 == 1); assert(1 == 1);
#define A 1
#define B 2
assert(A != B);
assert((A == 1) && (B == 2));
assert(1);
assert(!0);
return 0; return 0;
} }

View File

@ -15,6 +15,32 @@
along with Fennix Userspace. If not, see <https://www.gnu.org/licenses/>. along with Fennix Userspace. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <dirent.h> #ifdef __linux__
#define _DEFAULT_SOURCE 1 /* for alphasort */
#endif
int test_alphasort(void) { return 2; } #include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
/* 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;
}

View File

@ -16,5 +16,20 @@
*/ */
#include <dirent.h> #include <dirent.h>
#include <stddef.h>
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;
}

View File

@ -16,5 +16,19 @@
*/ */
#include <dirent.h> #include <dirent.h>
#include <stddef.h>
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;
}

View File

@ -16,5 +16,28 @@
*/ */
#include <dirent.h> #include <dirent.h>
#include <stddef.h>
#include <fcntl.h>
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;
}

View File

@ -16,5 +16,18 @@
*/ */
#include <dirent.h> #include <dirent.h>
#include <stddef.h>
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;
}

View File

@ -16,5 +16,12 @@
*/ */
#include <dirent.h> #include <dirent.h>
#include <stddef.h>
#include <fcntl.h>
int test_posix_getdents() { return 2; } /* https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_getdents.html */
int test_posix_getdents()
{
return 2;
}

View File

@ -17,4 +17,9 @@
#include <dirent.h> #include <dirent.h>
int test_readdir_r() { return 2; } /* https://pubs.opengroup.org/onlinepubs/9799919799/functions/readdir_r.html */
int test_readdir_r()
{
return 2;
}