finish tests for strlen and makefile works
This commit is contained in:
7
Makefile
7
Makefile
@ -14,11 +14,12 @@ SRCS = $(addprefix $(SRCS_DIR)/, $(SRCS_NAMES))
|
|||||||
|
|
||||||
OBJS = $(addprefix $(OBJS_DIR)/, $(SRCS_NAMES:.s=.o))
|
OBJS = $(addprefix $(OBJS_DIR)/, $(SRCS_NAMES:.s=.o))
|
||||||
|
|
||||||
|
FLAGS = -felf64
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
test: test.c $(NAME)
|
test: test.c $(NAME)
|
||||||
gcc -o test.o -c test.c
|
clang -z noexecstack test.c $(NAME) -o test
|
||||||
ld tests.c -L. -lasm
|
|
||||||
|
|
||||||
$(NAME): $(OBJS_DIR) $(OBJS)
|
$(NAME): $(OBJS_DIR) $(OBJS)
|
||||||
ar rcs $@ $(OBJS)
|
ar rcs $@ $(OBJS)
|
||||||
@ -27,7 +28,7 @@ $(OBJS_DIR):
|
|||||||
mkdir -p $(OBJS_DIR)
|
mkdir -p $(OBJS_DIR)
|
||||||
|
|
||||||
$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.s
|
$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.s
|
||||||
nasm -o $@ $<
|
nasm $(FLAGS) -o $@ $<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(OBJS_DIR)
|
rm -rf $(OBJS_DIR)
|
||||||
|
24
ft_strlen.s
24
ft_strlen.s
@ -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
45
test.c
Normal 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
27
tests.c
@ -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();
|
|
||||||
}
|
|
Reference in New Issue
Block a user