From 29b8c1556bf456596c6c067d413b65b51e17724e Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Sun, 22 Feb 2026 12:31:46 +0100 Subject: Initial scaffold: Makefile, stub malloc/free/realloc, test harness Build system produces libft_malloc_$HOSTTYPE.so shared library with Libft (NOMALLOC=1) as dependency. Stub functions print their name and return NULL. Test runner compares system malloc vs LD_PRELOAD output using write(2) to avoid stdio interference. --- tests/Makefile | 40 ++++++++++++++++++++++++++++++++++++++++ tests/src/main.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tests/Makefile create mode 100644 tests/src/main.c (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..04d7c1f --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,40 @@ +CC = cc +CFLAGS = -Wall -Wextra -Werror $(EXTRA) + +SRCDIR = src +OBJDIR = obj +BINDIR = bin + +NAME = $(BINDIR)/test + +SRCS = main.c + +OBJS = $(SRCS:%.c=$(OBJDIR)/%.o) + +MALLOC_LIB = ../lib/libft_malloc.so + +$(NAME): $(OBJS) | $(BINDIR) + $(CC) $(CFLAGS) -o $@ $(OBJS) + +all: $(NAME) + +$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJDIR): + mkdir -p $(OBJDIR) + +$(BINDIR): + mkdir -p $(BINDIR) + +test: $(NAME) + @echo "=== system malloc ===" + @$(NAME) + @echo "" + @echo "=== ft_malloc ===" + @LD_PRELOAD=$(MALLOC_LIB) $(NAME) + +clean: + rm -rf $(OBJDIR) $(BINDIR) + +.PHONY: all test clean diff --git a/tests/src/main.c b/tests/src/main.c new file mode 100644 index 0000000..0ded605 --- /dev/null +++ b/tests/src/main.c @@ -0,0 +1,46 @@ +/** + * @file main.c + * @brief Smoke test — call malloc once and print the returned pointer. + * + * Uses write(2) instead of printf to avoid stdio calling malloc + * internally, which would pollute output under LD_PRELOAD. + */ + +#include +#include + +/** @brief Write the hex representation of @p ptr to stdout. */ +static void +_s_put_ptr (void *ptr) +{ + const char hex[] = "0123456789abcdef"; + char buf[20]; + unsigned long v = (unsigned long)ptr; + int i; + + if (!ptr) + { + write (1, "(nil)", 5); + return; + } + i = (int)sizeof (buf); + while (v) + { + buf[--i] = hex[v % 16]; + v /= 16; + } + buf[--i] = 'x'; + buf[--i] = '0'; + write (1, buf + i, (size_t)(sizeof (buf) - i)); +} + +int +main (void) +{ + void *p = malloc (42); + write (1, "malloc(42) = ", 13); + _s_put_ptr (p); + write (1, "\n", 1); + free (p); + return (0); +} -- cgit v1.2.3