userspace/libc_test: add test functions for various libc components

Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
EnderIce2 2025-01-28 11:42:28 +02:00
parent cbe651d2da
commit 1b55332027
No known key found for this signature in database
GPG Key ID: 2EE20AF089811A5A
22 changed files with 144 additions and 1 deletions

View File

@ -16,3 +16,9 @@
*/
#include <assert.h>
int test_assert(void)
{
assert(1 == 1);
return 0;
}

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_alphasort(void) { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_closedir(void) { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_dirfd(void) { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_fdopendir(void) { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_opendir() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_posix_getdents() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_readdir() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_readdir_r() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_rewinddir() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_scandir() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_seekdir() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <dirent.h>
int test_telldir() { return 2; }

View File

@ -16,3 +16,11 @@
*/
#include <errno.h>
int test___errno_location(void)
{
(*__errno_location()) = ENOENT;
if ((*__errno_location()) != ENOENT)
return 1;
return 0;
}

View File

@ -16,3 +16,11 @@
*/
#include <errno.h>
int test_strerror(void)
{
char *enosys = strerror(ENOSYS);
if (enosys == ((void *)0))
return -1;
return 0;
}

View File

@ -16,3 +16,5 @@
*/
#include <fcntl.h>
int test_creat() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <fcntl.h>
int test_fcntl() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <fcntl.h>
int test_open() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <fcntl.h>
int test_openat() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <fcntl.h>
int test_posix_fadvise() { return 2; }

View File

@ -16,3 +16,5 @@
*/
#include <fcntl.h>
int test_posix_fallocate() { return 2; }

View File

@ -15,7 +15,92 @@
along with Fennix Userspace. If not, see <https://www.gnu.org/licenses/>.
*/
int main(int argc, char *argv[])
#include <stdio.h>
#include <unistd.h>
__attribute__((noreturn)) __attribute__((no_stack_protector)) void __stack_chk_fail(void)
{
_exit(0xbeef);
}
int sample_test_pass()
{
return 0;
}
int sample_test_fail()
{
return 1;
}
#define TEST(func) \
do \
{ \
printf("Testing \033[1;30m%s\033[0m...", #func); \
fflush(stdout); \
int func(void); /* Declare the function */ \
int result = func(); \
if (result == 0) \
printf("\033[0;32m PASS (%d)\033[0m\n", result); \
else \
{ \
printf("\033[0;31m FAIL (%d)\033[0m\n", result); \
failed_tests[failed_count++] = #func; \
} \
fflush(stdout); \
total_tests++; \
} while (0)
int main(int, char *[])
{
printf("--- Fennix C Library Test Suite ---\n");
printf("Required functions: printf, fflush & _exit\n");
printf("-------------------------------------------\n");
char *failed_tests[100];
int failed_count = 0;
int total_tests = 0;
printf("--- assert.h ---\n");
TEST(test_assert);
printf("--- dirent.h ---\n");
TEST(test_alphasort);
TEST(test_closedir);
TEST(test_dirfd);
TEST(test_fdopendir);
TEST(test_opendir);
TEST(test_posix_getdents);
TEST(test_readdir_r);
TEST(test_readdir);
TEST(test_rewinddir);
TEST(test_scandir);
TEST(test_seekdir);
TEST(test_telldir);
printf("--- errno.h ---\n");
TEST(test___errno_location);
TEST(test_strerror);
printf("--- fnctl.h ---\n");
TEST(test_creat);
TEST(test_fcntl);
TEST(test_open);
TEST(test_openat);
TEST(test_posix_fadvise);
TEST(test_posix_fallocate);
// TEST();
printf("-------------------------------------------\n");
printf("Total tests: \033[1;34m%d\033[0m\n", total_tests);
printf("Failed tests: \033[1;31m%d\033[0m\n", failed_count);
if (failed_count > 0)
{
printf("Failed test functions:\n");
for (int i = 0; i < failed_count; i++)
printf(" - \033[1;31m%s\033[0m\n", failed_tests[i]);
}
printf("Failure rate: \033[1;34m%.2f%%\033[0m\n", (failed_count / (float)total_tests) * 100);
return 0;
}