finish tests for strlen and makefile works

This commit is contained in:
2025-03-27 20:07:18 +01:00
parent 589addcb91
commit 76cf33dd1c
4 changed files with 49 additions and 54 deletions

View File

@ -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)

View File

@ -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

45
test.c Normal file
View File

@ -0,0 +1,45 @@
#include <stdlib.h>
#include <stdio.h>
#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");
}

27
tests.c
View File

@ -1,27 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
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();
}