From 76cf33dd1c8fff225b54bbdac6f4afbe2b5193db Mon Sep 17 00:00:00 2001 From: tomoron Date: Thu, 27 Mar 2025 20:07:18 +0100 Subject: [PATCH] finish tests for strlen and makefile works --- Makefile | 7 ++++--- ft_strlen.s | 24 ------------------------ test.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests.c | 27 --------------------------- 4 files changed, 49 insertions(+), 54 deletions(-) delete mode 100644 ft_strlen.s create mode 100644 test.c delete mode 100644 tests.c diff --git a/Makefile b/Makefile index 5ff9782..c23cb88 100644 --- a/Makefile +++ b/Makefile @@ -14,11 +14,12 @@ SRCS = $(addprefix $(SRCS_DIR)/, $(SRCS_NAMES)) OBJS = $(addprefix $(OBJS_DIR)/, $(SRCS_NAMES:.s=.o)) +FLAGS = -felf64 + all: $(NAME) test: test.c $(NAME) - gcc -o test.o -c test.c - ld tests.c -L. -lasm + clang -z noexecstack test.c $(NAME) -o test $(NAME): $(OBJS_DIR) $(OBJS) ar rcs $@ $(OBJS) @@ -27,7 +28,7 @@ $(OBJS_DIR): mkdir -p $(OBJS_DIR) $(OBJS_DIR)/%.o: $(SRCS_DIR)/%.s - nasm -o $@ $< + nasm $(FLAGS) -o $@ $< clean: rm -rf $(OBJS_DIR) diff --git a/ft_strlen.s b/ft_strlen.s deleted file mode 100644 index df5fb91..0000000 --- a/ft_strlen.s +++ /dev/null @@ -1,24 +0,0 @@ -section .text - global ft_strlen - -ft_strlen: - test rdi, rdi - je err - mov rsi, rdi - jmp loop_start - -increase_pointer: - lea rsi, [rsi + 1] - -loop_start: - mov al, [rsi] - test al, al - jnz increase_pointer -end: - sub rsi, rdi - mov rax, rsi - ret - -err: - xor rax, rax - ret diff --git a/test.c b/test.c new file mode 100644 index 0000000..5994364 --- /dev/null +++ b/test.c @@ -0,0 +1,45 @@ +#include +#include + +#define OK "\033[32mOK\n\033[0m" +#define KO "\033[31mKO\n\033[0m" + +#define FNC_TEST(title, condition)\ + printf(title":\n");\ + if(test_strlen())\ + printf("RESULT: "OK);\ + else\ + printf(" RESULT: "KO); + +#define TEST(title, condition) \ + printf("%s: ", title);\ + if(condition)\ + {\ + printf(OK);\ + passed++;\ + }\ + else \ + printf(KO); + +size_t ft_strlen(char *str); + +int test_strlen(void) +{ + int nb_tests; + int passed; + + nb_tests = 3; + passed = 0; + + TEST("normal string", ft_strlen("hello") == 5); + TEST("empty string", ft_strlen("") == 0); + TEST("null pointer", ft_strlen(NULL) == 0); + + return(nb_tests == passed); +} + +int main(void) +{ + FNC_TEST("ft_strlen", test_strlen()); + printf("\n\n"); +} diff --git a/tests.c b/tests.c deleted file mode 100644 index c130ad0..0000000 --- a/tests.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include - -size_t ft_strlen(char *str); - -void test_strlen(void) -{ - int nb_tests; - int passed; - - nb_tests = 3; - passed = 0; - - if(ft_strlen("hello") == 5) - passed++; - if(ft_strlen("") == 0) - passed++; - if(ft_strlen(NULL) == 0) - passed++; - - -} - -int main(void) -{ - test_strlen(); -}