Compare commits
No commits in common. "8d087d989f03c0acc2b74c13de9630d267fcc39e" and "a7b7cda49626594feb48c96ecf70573e06b8aa2c" have entirely different histories.
8d087d989f
...
a7b7cda496
19 changed files with 21 additions and 548 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
|
@ -1,3 +0,0 @@
|
||||||
[submodule "benchmarks/libs/musl"]
|
|
||||||
path = benchmarks/libs/musl
|
|
||||||
url = git://git.musl-libc.org/musl
|
|
||||||
50
Makefile
50
Makefile
|
|
@ -1,61 +1,31 @@
|
||||||
CC = clang
|
CC = clang
|
||||||
CFLAGS = -Wall -Wextra -Werror -O2
|
CFLAGS = -Wall -Wextra -Werror -O3
|
||||||
LDFLAGS = -flto
|
LDFLAGS = -flto
|
||||||
SRC_DIR = srcs
|
SRC_DIR = srcs
|
||||||
OBJ_DIR = obj
|
OBJ_DIR = obj
|
||||||
BUILD_DIR = build
|
BUILD_DIR = build
|
||||||
HDRS_DIR = hdrs
|
HDR_DIR = hdrs
|
||||||
|
SRCS = $(wildcard $(SRC_DIR)/*.c)
|
||||||
|
OBJS = $(SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
|
||||||
LIBRARY = $(BUILD_DIR)/libft.a
|
LIBRARY = $(BUILD_DIR)/libft.a
|
||||||
|
|
||||||
# Source and object files for library
|
|
||||||
SRCS = $(shell find $(SRC_DIR) -type f -name "*.c")
|
|
||||||
OBJS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS))
|
|
||||||
|
|
||||||
# Test configuration
|
|
||||||
TESTS_DIR = tests
|
|
||||||
TESTS_SRCS = $(wildcard $(TESTS_DIR)/*.c)
|
|
||||||
TESTS_OBJS = $(patsubst $(TESTS_DIR)/%.c,$(OBJ_DIR)/%.test.o,$(TESTS_SRCS))
|
|
||||||
TESTS_BINS = $(patsubst $(TESTS_DIR)/%.c,$(BUILD_DIR)/%_test,$(TESTS_SRCS))
|
|
||||||
|
|
||||||
all: $(LIBRARY)
|
all: $(LIBRARY)
|
||||||
|
|
||||||
$(OBJ_DIR) $(BUILD_DIR):
|
$(OBJ_DIR) $(BUILD_DIR):
|
||||||
@mkdir -p $@
|
@mkdir -p $@
|
||||||
|
|
||||||
# Build object files for library
|
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(HDR_DIR)/*.h | $(OBJ_DIR)
|
||||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(HDRS_DIR)/*.h | $(OBJ_DIR)
|
$(CC) $(CFLAGS) -I$(HDR_DIR) -c $< -o $@
|
||||||
@mkdir -p $(dir $@)
|
|
||||||
@echo -e " CC\t$<"
|
|
||||||
@$(CC) $(CFLAGS) -I$(HDRS_DIR) -c $< -o $@
|
|
||||||
|
|
||||||
$(LIBRARY): $(OBJS) | $(BUILD_DIR)
|
$(LIBRARY): $(OBJS) | $(BUILD_DIR)
|
||||||
@echo -e " AR\t$@"
|
$(AR) rcs $(LIBRARY) $(OBJS)
|
||||||
@$(AR) rcs $(LIBRARY) $(OBJS)
|
|
||||||
# Build object files for tests
|
|
||||||
$(OBJ_DIR)/%.test.o: $(TESTS_DIR)/%.c $(HDRS_DIR)/*.h | $(OBJ_DIR)
|
|
||||||
@mkdir -p $(dir $@)
|
|
||||||
@echo -e " CC\t$<"
|
|
||||||
@$(CC) $(CFLAGS) -I$(HDRS_DIR) -I$(SRC_DIR) -c $< -o $@
|
|
||||||
|
|
||||||
# Link each test binary
|
|
||||||
$(BUILD_DIR)/%_test: $(OBJ_DIR)/%.test.o $(LIBRARY) | $(BUILD_DIR)
|
|
||||||
@echo -e " LD\t$@"
|
|
||||||
@$(CC) $(CFLAGS) $< -L$(BUILD_DIR) -lft -o $@
|
|
||||||
|
|
||||||
tests: $(LIBRARY) $(TESTS_BINS)
|
|
||||||
@for testbin in $(TESTS_BINS); do \
|
|
||||||
./$$testbin; \
|
|
||||||
done
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@echo " CLEAN"
|
rm -rf $(OBJ_DIR)/*.o
|
||||||
@rm -rf $(OBJ_DIR)
|
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
@echo " FCLEAN"
|
rm -rf $(BUILD_DIR)
|
||||||
@rm -rf $(BUILD_DIR)
|
|
||||||
@rm -f $(TESTS_BIN)
|
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
|
||||||
.PHONY: all clean fclean re tests
|
.PHONY: all clean fclean re
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit ae3a8c93a663b553e65f096498937083dad210d2
|
|
||||||
71
hdrs/libft.h
71
hdrs/libft.h
|
|
@ -3,22 +3,21 @@
|
||||||
|
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
|
|
||||||
void *ft_realloc(void *ptr, size_t old_size, size_t new_size);
|
typedef struct s_string
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
char *data;
|
||||||
|
} t_string;
|
||||||
|
|
||||||
/* String functions */
|
|
||||||
size_t ft_strlen(const char *s);
|
|
||||||
char *ft_strdup(const char *s);
|
|
||||||
int ft_strcmp(const char *s1, const char *s2);
|
int ft_strcmp(const char *s1, const char *s2);
|
||||||
|
size_t ft_strlen(const char *s);
|
||||||
/* Character checks and conversions */
|
int ft_putstr(const char *s);
|
||||||
int ft_isalpha(int c);
|
|
||||||
int ft_isdigit(int c);
|
|
||||||
int ft_islower(int c);
|
int ft_islower(int c);
|
||||||
int ft_isupper(int c);
|
int ft_isupper(int c);
|
||||||
int ft_tolower(int c);
|
int ft_tolower(int c);
|
||||||
int ft_toupper(int c);
|
int ft_toupper(int c);
|
||||||
|
int ft_isdigit(int c);
|
||||||
/* Memory functions */
|
int ft_isalpha(int c);
|
||||||
void ft_bzero(void *s, size_t n);
|
void ft_bzero(void *s, size_t n);
|
||||||
void *ft_memset(void *s, int c, size_t n);
|
void *ft_memset(void *s, int c, size_t n);
|
||||||
void *ft_memset32(void *s, int c, size_t n);
|
void *ft_memset32(void *s, int c, size_t n);
|
||||||
|
|
@ -26,61 +25,13 @@ void *ft_memset64(void *s, int c, size_t n);
|
||||||
void *ft_memcpy(void *dst, const void *src, size_t n);
|
void *ft_memcpy(void *dst, const void *src, size_t n);
|
||||||
void *ft_memcpy32(void *dst, const void *src, size_t n);
|
void *ft_memcpy32(void *dst, const void *src, size_t n);
|
||||||
void *ft_memcpy64(void *dst, const void *src, size_t n);
|
void *ft_memcpy64(void *dst, const void *src, size_t n);
|
||||||
int ft_memcmp(const void *s1, const void *s2, size_t n);
|
|
||||||
void *ft_memmove(void *dst, const void *src, size_t n);
|
|
||||||
void *ft_memchr(const void *s, int c, size_t n);
|
|
||||||
/* Conversion */
|
|
||||||
int ft_atoi(const char *str);
|
int ft_atoi(const char *str);
|
||||||
|
|
||||||
/* Output functions */
|
|
||||||
int ft_putchar(char c);
|
int ft_putchar(char c);
|
||||||
int ft_putstr(const char *s);
|
|
||||||
int ft_putnbr(int n);
|
int ft_putnbr(int n);
|
||||||
int ft_putunbr(unsigned int n);
|
int ft_putunbr(unsigned int n);
|
||||||
int ft_puthex(unsigned int n, int uppercase);
|
int ft_puthex(unsigned int n, int uppercase);
|
||||||
|
void ft_putptr(void *ptr);
|
||||||
|
int ft_printf(const char *format, ...);
|
||||||
int ft_putaddr(unsigned long n);
|
int ft_putaddr(unsigned long n);
|
||||||
int ft_putptr(void *ptr);
|
|
||||||
int ft_printf(const char *format, ...);
|
|
||||||
|
|
||||||
/* String */
|
|
||||||
typedef struct s_string
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
char *data;
|
|
||||||
} t_string;
|
|
||||||
|
|
||||||
/* List */
|
|
||||||
typedef struct s_list
|
|
||||||
{
|
|
||||||
void *data;
|
|
||||||
struct s_list *next;
|
|
||||||
} t_list;
|
|
||||||
|
|
||||||
/* List functions */
|
|
||||||
t_list *ft_lstnew(void *data);
|
|
||||||
void ft_lstpush(t_list **lst, t_list *node);
|
|
||||||
void ft_lstunshift(t_list **lst, t_list *node);
|
|
||||||
t_list *ft_lstpop(t_list **lst);
|
|
||||||
t_list *ft_lstshift(t_list **lst);
|
|
||||||
int ft_lstsize(t_list *lst);
|
|
||||||
void ft_lstclear(t_list **lst);
|
|
||||||
void ft_lstreverse(t_list **lst);
|
|
||||||
int ft_lstempty(t_list *lst);
|
|
||||||
void ft_lstiter(t_list *lst, void (*f)(void *));
|
|
||||||
void ft_lstrm(t_list **lst, void *data, int (*cmp)(void *, void *), void (*del)(void *));
|
|
||||||
t_list *ft_lstfind(t_list *lst, void *data, int (*cmp)(void *, void *));
|
|
||||||
int ft_lstinsert(t_list **lst, t_list *node, int index);
|
|
||||||
t_list *ft_lstget(t_list *lst, int index);
|
|
||||||
|
|
||||||
/* Vector */
|
|
||||||
typedef struct s_vec
|
|
||||||
{
|
|
||||||
void *data;
|
|
||||||
size_t size;
|
|
||||||
size_t capacity;
|
|
||||||
size_t element_size;
|
|
||||||
} t_vec;
|
|
||||||
|
|
||||||
/* Vector functions*/
|
|
||||||
t_vec *ft_vecnew(size_t element_size);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
void *ft_memchr(const void *s, int c, size_t n)
|
|
||||||
{
|
|
||||||
const unsigned char *p = s;
|
|
||||||
unsigned char uc = (unsigned char)c;
|
|
||||||
|
|
||||||
while (n && ((uintptr_t)p % sizeof(size_t))) {
|
|
||||||
if (*p == uc)
|
|
||||||
return ((void *)p);
|
|
||||||
++p;
|
|
||||||
--n;
|
|
||||||
}
|
|
||||||
size_t mask = 0;
|
|
||||||
for (size_t i = 0; i < sizeof(size_t); ++i)
|
|
||||||
mask |= (size_t)uc << (i * 8);
|
|
||||||
|
|
||||||
while (n >= sizeof(size_t)) {
|
|
||||||
size_t word = *(const size_t *)p;
|
|
||||||
size_t cmp = word ^ mask;
|
|
||||||
for (size_t i = 0; i < sizeof(size_t); ++i) {
|
|
||||||
if (((cmp >> (i * 8)) & 0xFF) == 0)
|
|
||||||
return ((void *)(p + i));
|
|
||||||
}
|
|
||||||
p += sizeof(size_t);
|
|
||||||
n -= sizeof(size_t);
|
|
||||||
}
|
|
||||||
while (n--) {
|
|
||||||
if (*p == uc)
|
|
||||||
return ((void *)p);
|
|
||||||
++p;
|
|
||||||
}
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
int ft_memcmp(const void *s1, const void *s2, size_t n)
|
|
||||||
{
|
|
||||||
const unsigned char *p1 = s1, *p2 = s2;
|
|
||||||
|
|
||||||
while (n >= sizeof(size_t)) {
|
|
||||||
size_t w1 = *(const size_t *)p1;
|
|
||||||
size_t w2 = *(const size_t *)p2;
|
|
||||||
if (w1 != w2) {
|
|
||||||
for (size_t i = 0; i < sizeof(size_t); ++i) {
|
|
||||||
if (p1[i] != p2[i])
|
|
||||||
return (p1[i] - p2[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p1 += sizeof(size_t);
|
|
||||||
p2 += sizeof(size_t);
|
|
||||||
n -= sizeof(size_t);
|
|
||||||
}
|
|
||||||
while (n--) {
|
|
||||||
if (*p1 != *p2)
|
|
||||||
return (*p1 - *p2);
|
|
||||||
++p1;
|
|
||||||
++p2;
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
void *ft_memmove(void *dst, const void *src, size_t n)
|
|
||||||
{
|
|
||||||
unsigned char *d = dst;
|
|
||||||
const unsigned char *s = src;
|
|
||||||
|
|
||||||
if (d == s || n == 0)
|
|
||||||
return dst;
|
|
||||||
|
|
||||||
if (d < s) {
|
|
||||||
while (((uintptr_t)d % sizeof(size_t)) && n) {
|
|
||||||
*d++ = *s++;
|
|
||||||
n--;
|
|
||||||
}
|
|
||||||
while (n >= sizeof(size_t)) {
|
|
||||||
*(size_t *)d = *(const size_t *)s;
|
|
||||||
d += sizeof(size_t);
|
|
||||||
s += sizeof(size_t);
|
|
||||||
n -= sizeof(size_t);
|
|
||||||
}
|
|
||||||
while (n--) {
|
|
||||||
*d++ = *s++;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
d += n;
|
|
||||||
s += n;
|
|
||||||
while (n && ((uintptr_t)d % sizeof(size_t))) {
|
|
||||||
*(--d) = *(--s);
|
|
||||||
n--;
|
|
||||||
}
|
|
||||||
while (n >= sizeof(size_t)) {
|
|
||||||
d -= sizeof(size_t);
|
|
||||||
s -= sizeof(size_t);
|
|
||||||
*(size_t *)d = *(const size_t *)s;
|
|
||||||
n -= sizeof(size_t);
|
|
||||||
}
|
|
||||||
while (n--) {
|
|
||||||
*(--d) = *(--s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (dst);
|
|
||||||
}
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
#include "libft.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
void *ft_realloc(void *ptr, size_t old_size, size_t new_size)
|
|
||||||
{
|
|
||||||
if (new_size == 0) {
|
|
||||||
free(ptr);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
if (!ptr)
|
|
||||||
return (malloc(new_size));
|
|
||||||
|
|
||||||
void *new_ptr = malloc(new_size);
|
|
||||||
if (!new_ptr)
|
|
||||||
return (NULL);
|
|
||||||
|
|
||||||
size_t copy_size = old_size < new_size ? old_size : new_size;
|
|
||||||
ft_memcpy(new_ptr, ptr, copy_size);
|
|
||||||
free(ptr);
|
|
||||||
return (new_ptr);
|
|
||||||
}
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
#include "libft.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
char *ft_strdup(const char *s)
|
|
||||||
{
|
|
||||||
size_t len = 0;
|
|
||||||
char *dup;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
while (s[len])
|
|
||||||
len++;
|
|
||||||
dup = (char *)malloc(len + 1);
|
|
||||||
if (!dup)
|
|
||||||
return NULL;
|
|
||||||
for (i = 0; i < len; i++)
|
|
||||||
dup[i] = s[i];
|
|
||||||
dup[len] = '\0';
|
|
||||||
return (dup);
|
|
||||||
}
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
t_list *ft_lstnew(void *data)
|
|
||||||
{
|
|
||||||
t_list *node = (t_list *)malloc(sizeof(t_list));
|
|
||||||
|
|
||||||
if (!node)
|
|
||||||
return (NULL);
|
|
||||||
|
|
||||||
node->data = data;
|
|
||||||
node->next = NULL;
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
t_list *ft_lstpop(t_list **lst)
|
|
||||||
{
|
|
||||||
t_list *current;
|
|
||||||
t_list *prev;
|
|
||||||
|
|
||||||
if (!lst || !*lst)
|
|
||||||
return NULL;
|
|
||||||
current = *lst;
|
|
||||||
if (current->next == NULL)
|
|
||||||
{
|
|
||||||
*lst = NULL;
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
while (current->next)
|
|
||||||
{
|
|
||||||
prev = current;
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
prev->next = NULL;
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
void ft_lstpush(t_list **lst, t_list *node)
|
|
||||||
{
|
|
||||||
t_list *current;
|
|
||||||
|
|
||||||
if (!lst || !node)
|
|
||||||
return;
|
|
||||||
|
|
||||||
node->next = NULL;
|
|
||||||
|
|
||||||
if (*lst == NULL)
|
|
||||||
{
|
|
||||||
*lst = node;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
current = *lst;
|
|
||||||
while (current->next != NULL)
|
|
||||||
current = current->next;
|
|
||||||
current->next = node;
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
t_list *ft_lstshift(t_list **lst)
|
|
||||||
{
|
|
||||||
t_list *first;
|
|
||||||
|
|
||||||
if (!lst || !*lst)
|
|
||||||
return NULL;
|
|
||||||
first = *lst;
|
|
||||||
*lst = first->next;
|
|
||||||
first->next = NULL;
|
|
||||||
return first;
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
int ft_lstsize(t_list *lst)
|
|
||||||
{
|
|
||||||
int size = 0;
|
|
||||||
while (lst)
|
|
||||||
{
|
|
||||||
size++;
|
|
||||||
lst = lst->next;
|
|
||||||
}
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
void ft_lstunshift(t_list **lst, t_list *node)
|
|
||||||
{
|
|
||||||
if (!lst || !node)
|
|
||||||
return;
|
|
||||||
node->next = *lst;
|
|
||||||
*lst = node;
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
int ft_putptr(void *ptr)
|
|
||||||
{
|
|
||||||
int count = 0;
|
|
||||||
|
|
||||||
count += ft_putstr("0x");
|
|
||||||
if (!ptr)
|
|
||||||
count += ft_putchar('0');
|
|
||||||
else
|
|
||||||
count += ft_putaddr((unsigned long)ptr);
|
|
||||||
return (count);
|
|
||||||
}
|
|
||||||
62
tests/char.c
62
tests/char.c
|
|
@ -1,62 +0,0 @@
|
||||||
#include "test.h"
|
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
TEST(test_isalpha) {
|
|
||||||
ASSERT(ft_isalpha('a'));
|
|
||||||
ASSERT(ft_isalpha('Z'));
|
|
||||||
ASSERT(!ft_isalpha('1'));
|
|
||||||
ASSERT(!ft_isalpha('@'));
|
|
||||||
ASSERT(!ft_isalpha(' '));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_isdigit) {
|
|
||||||
ASSERT(ft_isdigit('0'));
|
|
||||||
ASSERT(ft_isdigit('9'));
|
|
||||||
ASSERT(!ft_isdigit('a'));
|
|
||||||
ASSERT(!ft_isdigit(' '));
|
|
||||||
ASSERT(!ft_isdigit('#'));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_islower) {
|
|
||||||
ASSERT(ft_islower('a'));
|
|
||||||
ASSERT(ft_islower('z'));
|
|
||||||
ASSERT(!ft_islower('A'));
|
|
||||||
ASSERT(!ft_islower('0'));
|
|
||||||
ASSERT(!ft_islower(' '));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_isupper) {
|
|
||||||
ASSERT(ft_isupper('A'));
|
|
||||||
ASSERT(ft_isupper('Z'));
|
|
||||||
ASSERT(!ft_isupper('a'));
|
|
||||||
ASSERT(!ft_isupper('0'));
|
|
||||||
ASSERT(!ft_isupper(' '));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_tolower) {
|
|
||||||
ASSERT(ft_tolower('A') == 'a');
|
|
||||||
ASSERT(ft_tolower('Z') == 'z');
|
|
||||||
ASSERT(ft_tolower('a') == 'a');
|
|
||||||
ASSERT(ft_tolower('z') == 'z');
|
|
||||||
ASSERT(ft_tolower('1') == '1');
|
|
||||||
ASSERT(ft_tolower(' ') == ' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_toupper) {
|
|
||||||
ASSERT(ft_toupper('a') == 'A');
|
|
||||||
ASSERT(ft_toupper('z') == 'Z');
|
|
||||||
ASSERT(ft_toupper('A') == 'A');
|
|
||||||
ASSERT(ft_toupper('Z') == 'Z');
|
|
||||||
ASSERT(ft_toupper('1') == '1');
|
|
||||||
ASSERT(ft_toupper(' ') == ' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
RUN_TEST(test_isalpha);
|
|
||||||
RUN_TEST(test_isdigit);
|
|
||||||
RUN_TEST(test_islower);
|
|
||||||
RUN_TEST(test_isupper);
|
|
||||||
RUN_TEST(test_tolower);
|
|
||||||
RUN_TEST(test_toupper);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
#include "test.h"
|
|
||||||
#include "libft.h"
|
|
||||||
|
|
||||||
TEST(test_putchar) {
|
|
||||||
int ret = ft_putchar('A');
|
|
||||||
ASSERT(ret == 1); // Should print 'A'
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_putstr) {
|
|
||||||
int ret = ft_putstr("Hello");
|
|
||||||
ASSERT(ret == 5); // Should print 'Hello'
|
|
||||||
ret = ft_putstr(NULL);
|
|
||||||
ASSERT(ret == 6 || ret == 0); // Depending on your implementation: "(null)" or nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_putnbr) {
|
|
||||||
int ret = ft_putnbr(42);
|
|
||||||
ASSERT(ret == 2); // Should print "42"
|
|
||||||
ret = ft_putnbr(-1234);
|
|
||||||
ASSERT(ret == 5); // Should print "-1234"
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_putunbr) {
|
|
||||||
int ret = ft_putunbr(12345U);
|
|
||||||
ASSERT(ret == 5); // Should print "12345"
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_puthex) {
|
|
||||||
int ret = ft_puthex(0x2A, 0);
|
|
||||||
ASSERT(ret == 2); // Should print "2a"
|
|
||||||
ret = ft_puthex(0x2A, 1);
|
|
||||||
ASSERT(ret == 2); // Should print "2A"
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_putaddr) {
|
|
||||||
int ret = ft_putaddr(0x1234abcd);
|
|
||||||
ASSERT(ret >= 3); // Should print "0x..." (at least 3 chars)
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_putptr) {
|
|
||||||
int x = 42;
|
|
||||||
int ret = ft_putptr(&x);
|
|
||||||
ASSERT(ret >= 3); // Should print "0x..." (at least 3 chars)
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(test_printf) {
|
|
||||||
int ret = ft_printf("Hello %s %d %x\n", "world", 42, 255);
|
|
||||||
ASSERT(ret > 0); // Should print "Hello world 42 ff\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
RUN_TEST(test_putchar);
|
|
||||||
RUN_TEST(test_putstr);
|
|
||||||
RUN_TEST(test_putnbr);
|
|
||||||
RUN_TEST(test_putunbr);
|
|
||||||
RUN_TEST(test_puthex);
|
|
||||||
RUN_TEST(test_putaddr);
|
|
||||||
RUN_TEST(test_putptr);
|
|
||||||
RUN_TEST(test_printf);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
67
tests/test.h
67
tests/test.h
|
|
@ -1,67 +0,0 @@
|
||||||
#ifndef TEST_H
|
|
||||||
#define TEST_H
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <setjmp.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
static int _stdout_fd_backup = -1;
|
|
||||||
static int _devnull_fd = -1;
|
|
||||||
|
|
||||||
static void mute_stdout(void) {
|
|
||||||
fflush(stdout);
|
|
||||||
_stdout_fd_backup = dup(STDOUT_FILENO);
|
|
||||||
_devnull_fd = open("/dev/null", O_WRONLY);
|
|
||||||
dup2(_devnull_fd, STDOUT_FILENO);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void unmute_stdout(void) {
|
|
||||||
fflush(stdout);
|
|
||||||
if (_stdout_fd_backup != -1) {
|
|
||||||
dup2(_stdout_fd_backup, STDOUT_FILENO);
|
|
||||||
close(_stdout_fd_backup);
|
|
||||||
_stdout_fd_backup = -1;
|
|
||||||
}
|
|
||||||
if (_devnull_fd != -1) {
|
|
||||||
close(_devnull_fd);
|
|
||||||
_devnull_fd = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#define GREEN "\033[1;32m"
|
|
||||||
#define RED "\033[1;31m"
|
|
||||||
#define RESET "\033[0m"
|
|
||||||
|
|
||||||
#define TEST(name) void name(void)
|
|
||||||
#define RUN_TEST(test) do { \
|
|
||||||
mute_stdout(); \
|
|
||||||
int result = test_runner(test); \
|
|
||||||
unmute_stdout(); \
|
|
||||||
if (result) \
|
|
||||||
printf(" OK\t%s\n", #test); \
|
|
||||||
else \
|
|
||||||
printf(" FAIL\t%s\n", #test); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
static jmp_buf env;
|
|
||||||
|
|
||||||
static int test_runner(void (*test_func)(void)) {
|
|
||||||
int failed = 0;
|
|
||||||
if (setjmp(env) == 0) {
|
|
||||||
test_func();
|
|
||||||
} else {
|
|
||||||
failed = 1;
|
|
||||||
}
|
|
||||||
return !failed;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ASSERT(expr) do { \
|
|
||||||
if (!(expr)) { \
|
|
||||||
longjmp(env, 1); \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue