aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-02-22 12:31:46 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-02-22 12:42:48 +0100
commit29b8c1556bf456596c6c067d413b65b51e17724e (patch)
treeb225381d762af049e9174cdf1459db1f91c9589a /tests
downloadmalloc-29b8c1556bf456596c6c067d413b65b51e17724e.tar.gz
malloc-29b8c1556bf456596c6c067d413b65b51e17724e.zip
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.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile40
-rw-r--r--tests/src/main.c46
2 files changed, 86 insertions, 0 deletions
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 <stdlib.h>
+#include <unistd.h>
+
+/** @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);
+}