diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-21 15:49:06 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-21 15:51:44 +0100 |
| commit | 671a58519ef6207b54947ff70eea497ff7eb58ae (patch) | |
| tree | 783a971119bfed965113b84bc306ba941e884663 /tests | |
| parent | d699849b2360f90c61f645c5d4d4232cd3e1c962 (diff) | |
| download | Libft-671a58519ef6207b54947ff70eea497ff7eb58ae.tar.gz Libft-671a58519ef6207b54947ff70eea497ff7eb58ae.zip | |
Restructure project layout and clean up test suite
Move sources to src/, header to inc/, and tests to tests/src/.
Update Makefiles and .gitignore for the new layout.
Refactor test harness: add crash-wrapper macros (_S_CRASH,
_S_CRASH_I, _S_CRASH_V, _S_CRASH_BUF) replacing 58 hand-written
wrappers, add shared _s_test_int_range helper eliminating duplicate
_s_test_func, add _S_RAND_ITERS constant, move srand() to main()
in all test binaries, and add Doxygen comments to test_utils.h.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/.gitignore | 3 | ||||
| -rw-r--r-- | tests/Makefile | 29 | ||||
| -rw-r--r-- | tests/bin/.gitkeep | 0 | ||||
| -rw-r--r-- | tests/inc/test_utils.h | 246 | ||||
| -rw-r--r-- | tests/src/test_alloc.c (renamed from tests/test_alloc.c) | 28 | ||||
| -rw-r--r-- | tests/src/test_atoi.c (renamed from tests/test_atoi.c) | 25 | ||||
| -rw-r--r-- | tests/src/test_case.c | 14 | ||||
| -rw-r--r-- | tests/src/test_cmp.c (renamed from tests/test_cmp.c) | 97 | ||||
| -rw-r--r-- | tests/src/test_is.c | 20 | ||||
| -rw-r--r-- | tests/src/test_mem.c (renamed from tests/test_mem.c) | 131 | ||||
| -rw-r--r-- | tests/src/test_search.c (renamed from tests/test_search.c) | 80 | ||||
| -rw-r--r-- | tests/src/test_strl.c (renamed from tests/test_strl.c) | 73 | ||||
| -rw-r--r-- | tests/src/test_strlen.c (renamed from tests/test_strlen.c) | 47 | ||||
| -rw-r--r-- | tests/test_case.c | 36 | ||||
| -rw-r--r-- | tests/test_is.c | 43 |
15 files changed, 446 insertions, 426 deletions
diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..0e64d2c --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1,3 @@ +*.o +bin/* +!.gitkeep diff --git a/tests/Makefile b/tests/Makefile index 319fa22..f7f7ed4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,31 +1,42 @@ CC = cc -CFLAGS = -LIB = ../libft.a +CFLAGS = -I ../inc -I inc + +SRCDIR = src +BINDIR = bin + +LIB = ../lib/libft.a TESTS = test_strlen test_is test_mem test_cmp test_case test_strl test_search \ test_atoi test_alloc -all: $(TESTS) - @for t in $(TESTS); do \ - echo "--- Running $$t ---"; \ +BINS = $(TESTS:%=$(BINDIR)/%) + +all: $(BINS) + @for t in $(BINS); do \ + echo "$$(basename $$t):"; \ ./$$t; \ done -test_%: test_%.c $(LIB) +# Order-only prerequisite (|): create bin/ if missing, but don't +# rebuild binaries just because the directory's mtime changed. +$(BINDIR)/test_%: $(SRCDIR)/test_%.c $(LIB) | $(BINDIR) $(CC) $(CFLAGS) -o $@ $< $(LIB) # Tests comparing against BSD functions (strlcpy, strlcat, strnstr) # need -lbsd since these are not part of glibc by default. -test_strl: test_strl.c $(LIB) +$(BINDIR)/test_strl: $(SRCDIR)/test_strl.c $(LIB) | $(BINDIR) $(CC) $(CFLAGS) -o $@ $< $(LIB) -lbsd -test_search: test_search.c $(LIB) +$(BINDIR)/test_search: $(SRCDIR)/test_search.c $(LIB) | $(BINDIR) $(CC) $(CFLAGS) -o $@ $< $(LIB) -lbsd +$(BINDIR): + mkdir -p $(BINDIR) + $(LIB): $(MAKE) -C .. clean: - rm -f $(TESTS) + rm -f $(BINS) .PHONY: all clean diff --git a/tests/bin/.gitkeep b/tests/bin/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/bin/.gitkeep diff --git a/tests/inc/test_utils.h b/tests/inc/test_utils.h new file mode 100644 index 0000000..0cb6655 --- /dev/null +++ b/tests/inc/test_utils.h @@ -0,0 +1,246 @@ +/** + * @file test_utils.h + * @brief Minimal test harness for libft unit tests. + * + * Header-only: each test_*.c translation unit gets its own copy + * of the counters and helpers via static linkage. + * + * Output is compact: only failures are printed individually. + * Each section gets a one-line summary, and a totals line is + * printed at the end. + */ + +#ifndef TEST_UTILS_H +#define TEST_UTILS_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/wait.h> +#include <time.h> +#include <unistd.h> + +/* ── global counters ──────────────────────────────────────────────── */ + +static int _s_pass = 0; +static int _s_fail = 0; + +static const char *_s_section_name = NULL; +static int _s_section_pass = 0; +static int _s_section_fail = 0; +static int _s_had_sections = 0; + +/* volatile: prevent dead-store elimination when the return + value of the tested function is never read. */ +static volatile size_t _s_sink; +static volatile int _s_sink_i; + +/* ── sections and assertions ──────────────────────────────────────── */ + +/** Print the current section's summary line and reset its counters. */ +static void +_s_section_end (void) +{ + if (_s_section_name == NULL) + return; + if (_s_section_fail > 0) + printf (" %-16s %d passed, %d failed\n", _s_section_name, _s_section_pass, + _s_section_fail); + else + printf (" %-16s %d passed\n", _s_section_name, _s_section_pass); + _s_section_name = NULL; + _s_section_pass = 0; + _s_section_fail = 0; +} + +/** Close the previous section (if any) and open a new one. */ +static void +_s_section (const char *name) +{ + _s_section_end (); + _s_section_name = name; + _s_had_sections = 1; +} + +/** Record a pass (@p ok true) or print FAIL and record a failure. */ +static void +_s_check (const char *label, int ok) +{ + if (ok) + { + _s_pass++; + _s_section_pass++; + } + else + { + printf (" FAIL %s\n", label); + _s_fail++; + _s_section_fail++; + } +} + +/** Pass if @p got == @p expected; on failure print both values. */ +static void +_s_check_eq (const char *label, size_t got, size_t expected) +{ + if (got == expected) + { + _s_pass++; + _s_section_pass++; + } + else + { + printf (" FAIL %s: got %zu, expected %zu\n", label, got, expected); + _s_fail++; + _s_section_fail++; + } +} + +/** Pass if @p got == @p expected (int variant); print both on failure. */ +static void +_s_check_eq_int (const char *label, int got, int expected) +{ + if (got == expected) + { + _s_pass++; + _s_section_pass++; + } + else + { + printf (" FAIL %s: got %d, expected %d\n", label, got, expected); + _s_fail++; + _s_section_fail++; + } +} + +/* ── crash detection ──────────────────────────────────────────────── */ + +/** Run @p fn in a child process; return 1 if it was killed by a signal. */ +static int +_s_crashes (void (*fn) (void)) +{ + pid_t pid; + int status; + + pid = fork (); + if (pid == 0) + { + fn (); + _exit (0); + } + waitpid (pid, &status, 0); + return (WIFSIGNALED (status)); +} + +static void +_s_check_both_crash (const char *label, void (*ft_fn) (void), + void (*libc_fn) (void)) +{ + int ft_crashed; + int libc_crashed; + + ft_crashed = _s_crashes (ft_fn); + libc_crashed = _s_crashes (libc_fn); + if (ft_crashed == libc_crashed) + { + _s_pass++; + _s_section_pass++; + } + else + { + printf (" FAIL %s: ft_crashed=%d libc_crashed=%d\n", label, + ft_crashed, libc_crashed); + _s_fail++; + _s_section_fail++; + } +} + +/* ── crash-wrapper macros ─────────────────────────────────────────── */ + +/* pointer-returning expression → _s_sink */ +#define _S_CRASH(tag, expr) \ + static void _s_crash_##tag (void) { _s_sink = (size_t)(expr); } + +/* int-returning expression → _s_sink_i */ +#define _S_CRASH_I(tag, expr) \ + static void _s_crash_##tag (void) { _s_sink_i = (expr); } + +/* void expression (e.g. ft_bzero) */ +#define _S_CRASH_V(tag, expr) \ + static void _s_crash_##tag (void) { (expr); } + +/* needs a local char buf[N] before the expression */ +#define _S_CRASH_BUF(tag, bufsz, expr) \ + static void _s_crash_##tag (void) \ + { \ + char buf[bufsz]; \ + (void)buf; \ + (expr); \ + } + +/* ── int-range comparison helper ─────────────────────────────────── */ + +/* Compare ft(i) vs libc(i) for i in [-1..255]. + If normalize is true, compares !!ft(i) == !!libc(i) (for is* funcs). + Otherwise exact comparison (for toupper/tolower). */ +static void +_s_test_int_range (const char *name, int (*ft) (int), int (*libc) (int), + int normalize) +{ + int i; + int ft_r; + int libc_r; + int ok; + + ok = 1; + for (i = -1; i <= 255; i++) + { + if (normalize) + { + ft_r = !!ft (i); + libc_r = !!libc (i); + } + else + { + ft_r = ft (i); + libc_r = libc (i); + } + if (ft_r != libc_r) + { + char label[64]; + snprintf (label, sizeof (label), "%s(%d): ft=%d libc=%d", name, i, + ft_r, libc_r); + _s_check (label, 0); + ok = 0; + } + } + if (ok) + _s_check (name, 1); +} + +/* ── helpers and output ───────────────────────────────────────────── */ + +#define _S_RAND_ITERS 50 + +/** Normalize to -1/0/1 for sign comparison (memcmp, strncmp). */ +static int +_s_sign (int x) +{ + if (x > 0) + return (1); + if (x < 0) + return (-1); + return (0); +} + +static void +_s_print_results (void) +{ + _s_section_end (); + if (_s_fail > 0) + printf (" %d passed, %d failed\n", _s_pass, _s_fail); + else if (!_s_had_sections) + printf (" %d passed\n", _s_pass); +} + +#endif diff --git a/tests/test_alloc.c b/tests/src/test_alloc.c index 191fbbb..6441cd5 100644 --- a/tests/test_alloc.c +++ b/tests/src/test_alloc.c @@ -1,16 +1,8 @@ -#include "../libft.h" +#include "libft.h" #include "test_utils.h" -static void -_s_ft_strdup_null (void) -{ - _s_sink = (size_t)ft_strdup (NULL); -} -static void -_s_libc_strdup_null (void) -{ - _s_sink = (size_t)strdup (NULL); -} +_S_CRASH (ft_strdup_null, ft_strdup (NULL)) +_S_CRASH (libc_strdup_null, strdup (NULL)) /* ====================================== * calloc @@ -22,7 +14,7 @@ _s_test_calloc (void) int i; char label[64]; - printf ("-- calloc --\n"); + _s_section ("ft_calloc"); /* zeroed memory */ { @@ -61,8 +53,7 @@ _s_test_calloc (void) } /* randomized: check all bytes are zero */ - srand (time (NULL)); - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { size_t nmemb = rand () % 500 + 1; size_t size = rand () % 50 + 1; @@ -103,10 +94,11 @@ _s_test_strdup (void) int i; char label[64]; - printf ("-- strdup --\n"); + _s_section ("ft_strdup"); /* NULL */ - _s_check_both_crash ("strdup NULL", _s_ft_strdup_null, _s_libc_strdup_null); + _s_check_both_crash ("strdup NULL", _s_crash_ft_strdup_null, + _s_crash_libc_strdup_null); /* empty string */ { @@ -126,7 +118,7 @@ _s_test_strdup (void) } /* randomized */ - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { int len = rand () % 500 + 1; char *src = malloc (len + 1); @@ -151,7 +143,7 @@ _s_test_strdup (void) int main (void) { - printf ("=== alloc functions ===\n"); + srand (time (NULL)); _s_test_calloc (); _s_test_strdup (); _s_print_results (); diff --git a/tests/test_atoi.c b/tests/src/test_atoi.c index 196a081..91e4dc4 100644 --- a/tests/test_atoi.c +++ b/tests/src/test_atoi.c @@ -1,17 +1,9 @@ -#include "../libft.h" +#include "libft.h" #include "test_utils.h" #include <limits.h> -static void -_s_ft_atoi_null (void) -{ - _s_sink_i = ft_atoi (NULL); -} -static void -_s_libc_atoi_null (void) -{ - _s_sink_i = atoi (NULL); -} +_S_CRASH_I (ft_atoi_null, ft_atoi (NULL)) +_S_CRASH_I (libc_atoi_null, atoi (NULL)) int main (void) @@ -20,10 +12,10 @@ main (void) char buf[64]; char label[64]; - printf ("=== ft_atoi ===\n"); - - /* NULL */ - _s_check_both_crash ("atoi NULL", _s_ft_atoi_null, _s_libc_atoi_null); + srand (time (NULL)); + _s_section ("ft_atoi"); + _s_check_both_crash ("atoi NULL", _s_crash_ft_atoi_null, + _s_crash_libc_atoi_null); /* edge cases */ _s_check_eq_int ("zero", ft_atoi ("0"), atoi ("0")); @@ -44,8 +36,7 @@ main (void) _s_check_eq_int ("leading zeros", ft_atoi ("00042"), atoi ("00042")); /* randomized */ - srand (time (NULL)); - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { int v = rand () - RAND_MAX / 2; snprintf (buf, sizeof (buf), "%d", v); diff --git a/tests/src/test_case.c b/tests/src/test_case.c new file mode 100644 index 0000000..920dc95 --- /dev/null +++ b/tests/src/test_case.c @@ -0,0 +1,14 @@ +#include "libft.h" +#include "test_utils.h" +#include <ctype.h> + +int +main (void) +{ + _s_section ("ft_toupper"); + _s_test_int_range ("ft_toupper", ft_toupper, toupper, 0); + _s_section ("ft_tolower"); + _s_test_int_range ("ft_tolower", ft_tolower, tolower, 0); + _s_print_results (); + return (_s_fail != 0); +} diff --git a/tests/test_cmp.c b/tests/src/test_cmp.c index eea0aea..d98b584 100644 --- a/tests/test_cmp.c +++ b/tests/src/test_cmp.c @@ -1,56 +1,16 @@ -#include "../libft.h" +#include "libft.h" #include "test_utils.h" -static void -_s_ft_memchr_null (void) -{ - _s_sink = (size_t)ft_memchr (NULL, 'a', 10); -} -static void -_s_libc_memchr_null (void) -{ - _s_sink = (size_t)memchr (NULL, 'a', 10); -} -static void -_s_ft_memcmp_null_s1 (void) -{ - _s_sink_i = ft_memcmp (NULL, "abc", 3); -} -static void -_s_libc_memcmp_null_s1 (void) -{ - _s_sink_i = memcmp (NULL, "abc", 3); -} -static void -_s_ft_memcmp_null_s2 (void) -{ - _s_sink_i = ft_memcmp ("abc", NULL, 3); -} -static void -_s_libc_memcmp_null_s2 (void) -{ - _s_sink_i = memcmp ("abc", NULL, 3); -} -static void -_s_ft_strncmp_null_s1 (void) -{ - _s_sink_i = ft_strncmp (NULL, "abc", 3); -} -static void -_s_libc_strncmp_null_s1 (void) -{ - _s_sink_i = strncmp (NULL, "abc", 3); -} -static void -_s_ft_strncmp_null_s2 (void) -{ - _s_sink_i = ft_strncmp ("abc", NULL, 3); -} -static void -_s_libc_strncmp_null_s2 (void) -{ - _s_sink_i = strncmp ("abc", NULL, 3); -} +_S_CRASH (ft_memchr_null, ft_memchr (NULL, 'a', 10)) +_S_CRASH (libc_memchr_null, memchr (NULL, 'a', 10)) +_S_CRASH_I (ft_memcmp_null_s1, ft_memcmp (NULL, "abc", 3)) +_S_CRASH_I (libc_memcmp_null_s1, memcmp (NULL, "abc", 3)) +_S_CRASH_I (ft_memcmp_null_s2, ft_memcmp ("abc", NULL, 3)) +_S_CRASH_I (libc_memcmp_null_s2, memcmp ("abc", NULL, 3)) +_S_CRASH_I (ft_strncmp_null_s1, ft_strncmp (NULL, "abc", 3)) +_S_CRASH_I (libc_strncmp_null_s1, strncmp (NULL, "abc", 3)) +_S_CRASH_I (ft_strncmp_null_s2, ft_strncmp ("abc", NULL, 3)) +_S_CRASH_I (libc_strncmp_null_s2, strncmp ("abc", NULL, 3)) /* ====================================== * memchr @@ -64,10 +24,11 @@ _s_test_memchr (void) int i; char label[64]; - printf ("-- memchr --\n"); + _s_section ("ft_memchr"); /* NULL */ - _s_check_both_crash ("memchr NULL", _s_ft_memchr_null, _s_libc_memchr_null); + _s_check_both_crash ("memchr NULL", _s_crash_ft_memchr_null, + _s_crash_libc_memchr_null); /* find existing char */ _s_check ("find 'W'", ft_memchr (buf, 'W', 13) == memchr (buf, 'W', 13)); @@ -105,8 +66,7 @@ _s_test_memchr (void) } /* randomized */ - srand (time (NULL)); - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { unsigned char rnd[256]; int len = rand () % 256 + 1; @@ -133,13 +93,13 @@ _s_test_memcmp (void) int pos; char label[64]; - printf ("-- memcmp --\n"); + _s_section ("ft_memcmp"); /* NULL */ - _s_check_both_crash ("memcmp NULL s1", _s_ft_memcmp_null_s1, - _s_libc_memcmp_null_s1); - _s_check_both_crash ("memcmp NULL s2", _s_ft_memcmp_null_s2, - _s_libc_memcmp_null_s2); + _s_check_both_crash ("memcmp NULL s1", _s_crash_ft_memcmp_null_s1, + _s_crash_libc_memcmp_null_s1); + _s_check_both_crash ("memcmp NULL s2", _s_crash_ft_memcmp_null_s2, + _s_crash_libc_memcmp_null_s2); /* equal */ _s_check ("equal", _s_sign (ft_memcmp ("abc", "abc", 3)) @@ -170,8 +130,7 @@ _s_test_memcmp (void) } /* randomized */ - srand (time (NULL)); - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { len = rand () % 255 + 1; for (pos = 0; pos < (int)len; pos++) @@ -201,13 +160,13 @@ _s_test_strncmp (void) int len; char label[64]; - printf ("-- strncmp --\n"); + _s_section ("ft_strncmp"); /* NULL */ - _s_check_both_crash ("strncmp NULL s1", _s_ft_strncmp_null_s1, - _s_libc_strncmp_null_s1); - _s_check_both_crash ("strncmp NULL s2", _s_ft_strncmp_null_s2, - _s_libc_strncmp_null_s2); + _s_check_both_crash ("strncmp NULL s1", _s_crash_ft_strncmp_null_s1, + _s_crash_libc_strncmp_null_s1); + _s_check_both_crash ("strncmp NULL s2", _s_crash_ft_strncmp_null_s2, + _s_crash_libc_strncmp_null_s2); /* equal strings */ _s_check ("equal", _s_sign (ft_strncmp ("abc", "abc", 3)) @@ -252,7 +211,7 @@ _s_test_strncmp (void) == _s_sign (strncmp ("\xff", "\x01", 1))); /* randomized */ - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { len = rand () % 64 + 1; for (int j = 0; j < len; j++) @@ -270,7 +229,7 @@ _s_test_strncmp (void) int main (void) { - printf ("=== comparison functions ===\n"); + srand (time (NULL)); _s_test_memchr (); _s_test_memcmp (); _s_test_strncmp (); diff --git a/tests/src/test_is.c b/tests/src/test_is.c new file mode 100644 index 0000000..c0edfa8 --- /dev/null +++ b/tests/src/test_is.c @@ -0,0 +1,20 @@ +#include "libft.h" +#include "test_utils.h" +#include <ctype.h> + +int +main (void) +{ + _s_section ("ft_isalpha"); + _s_test_int_range ("ft_isalpha", ft_isalpha, isalpha, 1); + _s_section ("ft_isdigit"); + _s_test_int_range ("ft_isdigit", ft_isdigit, isdigit, 1); + _s_section ("ft_isalnum"); + _s_test_int_range ("ft_isalnum", ft_isalnum, isalnum, 1); + _s_section ("ft_isascii"); + _s_test_int_range ("ft_isascii", ft_isascii, isascii, 1); + _s_section ("ft_isprint"); + _s_test_int_range ("ft_isprint", ft_isprint, isprint, 1); + _s_print_results (); + return (_s_fail != 0); +} diff --git a/tests/test_mem.c b/tests/src/test_mem.c index adbe12d..3653004 100644 --- a/tests/test_mem.c +++ b/tests/src/test_mem.c @@ -1,75 +1,27 @@ -#include "../libft.h" +#include "libft.h" #include "test_utils.h" #include <strings.h> -static void -_s_ft_memset_null (void) -{ - _s_sink = (size_t)ft_memset (NULL, 'A', 10); -} -static void -_s_libc_memset_null (void) -{ - _s_sink = (size_t)memset (NULL, 'A', 10); -} -static void -_s_ft_bzero_null (void) -{ - ft_bzero (NULL, 10); -} -static void -_s_libc_bzero_null (void) -{ - bzero (NULL, 10); -} -static void -_s_ft_memcpy_null_dst (void) -{ - char buf[10]; - _s_sink = (size_t)ft_memcpy (NULL, buf, 10); -} -static void -_s_libc_memcpy_null_dst (void) -{ - char buf[10]; - _s_sink = (size_t)memcpy (NULL, buf, 10); -} -static void -_s_ft_memcpy_null_src (void) -{ - char buf[10]; - _s_sink = (size_t)ft_memcpy (buf, NULL, 10); -} -static void -_s_libc_memcpy_null_src (void) -{ - char buf[10]; - _s_sink = (size_t)memcpy (buf, NULL, 10); -} -static void -_s_ft_memmove_null_dst (void) -{ - char buf[10]; - _s_sink = (size_t)ft_memmove (NULL, buf, 10); -} -static void -_s_libc_memmove_null_dst (void) -{ - char buf[10]; - _s_sink = (size_t)memmove (NULL, buf, 10); -} -static void -_s_ft_memmove_null_src (void) -{ - char buf[10]; - _s_sink = (size_t)ft_memmove (buf, NULL, 10); -} -static void -_s_libc_memmove_null_src (void) -{ - char buf[10]; - _s_sink = (size_t)memmove (buf, NULL, 10); -} +_S_CRASH (ft_memset_null, ft_memset (NULL, 'A', 10)) +_S_CRASH (libc_memset_null, memset (NULL, 'A', 10)) +_S_CRASH_V (ft_bzero_null, ft_bzero (NULL, 10)) +_S_CRASH_V (libc_bzero_null, bzero (NULL, 10)) +_S_CRASH_BUF (ft_memcpy_null_dst, 10, + _s_sink = (size_t)ft_memcpy (NULL, buf, 10)) +_S_CRASH_BUF (libc_memcpy_null_dst, 10, + _s_sink = (size_t)memcpy (NULL, buf, 10)) +_S_CRASH_BUF (ft_memcpy_null_src, 10, + _s_sink = (size_t)ft_memcpy (buf, NULL, 10)) +_S_CRASH_BUF (libc_memcpy_null_src, 10, + _s_sink = (size_t)memcpy (buf, NULL, 10)) +_S_CRASH_BUF (ft_memmove_null_dst, 10, + _s_sink = (size_t)ft_memmove (NULL, buf, 10)) +_S_CRASH_BUF (libc_memmove_null_dst, 10, + _s_sink = (size_t)memmove (NULL, buf, 10)) +_S_CRASH_BUF (ft_memmove_null_src, 10, + _s_sink = (size_t)ft_memmove (buf, NULL, 10)) +_S_CRASH_BUF (libc_memmove_null_src, 10, + _s_sink = (size_t)memmove (buf, NULL, 10)) /* ====================================== * memset @@ -86,10 +38,11 @@ _s_test_memset (void) void *ret; char label[64]; - printf ("-- memset --\n"); + _s_section ("ft_memset"); /* NULL */ - _s_check_both_crash ("memset NULL", _s_ft_memset_null, _s_libc_memset_null); + _s_check_both_crash ("memset NULL", _s_crash_ft_memset_null, + _s_crash_libc_memset_null); /* zero length */ memset (ft_buf, 'X', 256); @@ -103,8 +56,7 @@ _s_test_memset (void) _s_check ("return value", ret == ft_buf); /* randomized */ - srand (time (NULL)); - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { c = rand () % 256; len = rand () % 256; @@ -128,10 +80,11 @@ _s_test_bzero (void) size_t len; char label[64]; - printf ("-- bzero --\n"); + _s_section ("ft_bzero"); /* NULL */ - _s_check_both_crash ("bzero NULL", _s_ft_bzero_null, _s_libc_bzero_null); + _s_check_both_crash ("bzero NULL", _s_crash_ft_bzero_null, + _s_crash_libc_bzero_null); /* zero length */ memset (ft_buf, 'X', 256); @@ -148,7 +101,7 @@ _s_test_bzero (void) _s_check ("n=256", memcmp (ft_buf, libc_buf, 256) == 0); /* randomized */ - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { len = rand () % 256; memset (ft_buf, 'Z', 256); @@ -175,13 +128,13 @@ _s_test_memcpy (void) void *ret; char label[64]; - printf ("-- memcpy --\n"); + _s_section ("ft_memcpy"); /* NULL */ - _s_check_both_crash ("memcpy NULL dst", _s_ft_memcpy_null_dst, - _s_libc_memcpy_null_dst); - _s_check_both_crash ("memcpy NULL src", _s_ft_memcpy_null_src, - _s_libc_memcpy_null_src); + _s_check_both_crash ("memcpy NULL dst", _s_crash_ft_memcpy_null_dst, + _s_crash_libc_memcpy_null_dst); + _s_check_both_crash ("memcpy NULL src", _s_crash_ft_memcpy_null_src, + _s_crash_libc_memcpy_null_src); /* fill source with pattern */ for (i = 0; i < 256; i++) @@ -203,7 +156,7 @@ _s_test_memcpy (void) _s_check ("NULL,NULL,0", ret == NULL); /* randomized */ - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { len = rand () % 256; memset (ft_dst, 0, 256); @@ -230,13 +183,13 @@ _s_test_memmove (void) void *ret; char label[64]; - printf ("-- memmove --\n"); + _s_section ("ft_memmove"); /* NULL */ - _s_check_both_crash ("memmove NULL dst", _s_ft_memmove_null_dst, - _s_libc_memmove_null_dst); - _s_check_both_crash ("memmove NULL src", _s_ft_memmove_null_src, - _s_libc_memmove_null_src); + _s_check_both_crash ("memmove NULL dst", _s_crash_ft_memmove_null_dst, + _s_crash_libc_memmove_null_dst); + _s_check_both_crash ("memmove NULL src", _s_crash_ft_memmove_null_src, + _s_crash_libc_memmove_null_src); /* return value */ memset (ft_buf, 'A', 256); @@ -269,7 +222,7 @@ _s_test_memmove (void) _s_check ("non-overlapping", memcmp (ft_buf, libc_buf, 256) == 0); /* randomized overlapping copies */ - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { for (int j = 0; j < 256; j++) ft_buf[j] = libc_buf[j] = rand () % 256; @@ -286,7 +239,7 @@ _s_test_memmove (void) int main (void) { - printf ("=== mem functions ===\n"); + srand (time (NULL)); _s_test_memset (); _s_test_bzero (); _s_test_memcpy (); diff --git a/tests/test_search.c b/tests/src/test_search.c index faaaccd..be885b2 100644 --- a/tests/test_search.c +++ b/tests/src/test_search.c @@ -1,47 +1,15 @@ -#include "../libft.h" +#include "libft.h" #include "test_utils.h" #include <bsd/string.h> -static void -_s_ft_strchr_null (void) -{ - _s_sink = (size_t)ft_strchr (NULL, 'a'); -} -static void -_s_libc_strchr_null (void) -{ - _s_sink = (size_t)strchr (NULL, 'a'); -} -static void -_s_ft_strrchr_null (void) -{ - _s_sink = (size_t)ft_strrchr (NULL, 'a'); -} -static void -_s_libc_strrchr_null (void) -{ - _s_sink = (size_t)strrchr (NULL, 'a'); -} -static void -_s_ft_strnstr_null_big (void) -{ - _s_sink = (size_t)ft_strnstr (NULL, "abc", 10); -} -static void -_s_libc_strnstr_null_big (void) -{ - _s_sink = (size_t)strnstr (NULL, "abc", 10); -} -static void -_s_ft_strnstr_null_little (void) -{ - _s_sink = (size_t)ft_strnstr ("abc", NULL, 10); -} -static void -_s_libc_strnstr_null_little (void) -{ - _s_sink = (size_t)strnstr ("abc", NULL, 10); -} +_S_CRASH (ft_strchr_null, ft_strchr (NULL, 'a')) +_S_CRASH (libc_strchr_null, strchr (NULL, 'a')) +_S_CRASH (ft_strrchr_null, ft_strrchr (NULL, 'a')) +_S_CRASH (libc_strrchr_null, strrchr (NULL, 'a')) +_S_CRASH (ft_strnstr_null_big, ft_strnstr (NULL, "abc", 10)) +_S_CRASH (libc_strnstr_null_big, strnstr (NULL, "abc", 10)) +_S_CRASH (ft_strnstr_null_little, ft_strnstr ("abc", NULL, 10)) +_S_CRASH (libc_strnstr_null_little, strnstr ("abc", NULL, 10)) /* ====================================== * strchr @@ -54,10 +22,11 @@ _s_test_strchr (void) int i; char label[64]; - printf ("-- strchr --\n"); + _s_section ("ft_strchr"); /* NULL */ - _s_check_both_crash ("strchr NULL", _s_ft_strchr_null, _s_libc_strchr_null); + _s_check_both_crash ("strchr NULL", _s_crash_ft_strchr_null, + _s_crash_libc_strchr_null); /* find first occurrence */ _s_check ("find 'l'", ft_strchr (buf, 'l') == strchr (buf, 'l')); @@ -90,8 +59,7 @@ _s_test_strchr (void) } /* randomized */ - srand (time (NULL)); - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { char rnd[128]; int len = rand () % 127 + 1; @@ -117,11 +85,11 @@ _s_test_strrchr (void) int i; char label[64]; - printf ("-- strrchr --\n"); + _s_section ("ft_strrchr"); /* NULL */ - _s_check_both_crash ("strrchr NULL", _s_ft_strrchr_null, - _s_libc_strrchr_null); + _s_check_both_crash ("strrchr NULL", _s_crash_ft_strrchr_null, + _s_crash_libc_strrchr_null); /* find last occurrence */ _s_check ("find 'l'", ft_strrchr (buf, 'l') == strrchr (buf, 'l')); @@ -161,7 +129,7 @@ _s_test_strrchr (void) } /* randomized */ - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { char rnd[128]; int len = rand () % 127 + 1; @@ -185,13 +153,13 @@ _s_test_strnstr (void) { char hay[] = "hello world, hello earth"; - printf ("-- strnstr --\n"); + _s_section ("ft_strnstr"); /* NULL */ - _s_check_both_crash ("strnstr NULL big", _s_ft_strnstr_null_big, - _s_libc_strnstr_null_big); - _s_check_both_crash ("strnstr NULL little", _s_ft_strnstr_null_little, - _s_libc_strnstr_null_little); + _s_check_both_crash ("strnstr NULL big", _s_crash_ft_strnstr_null_big, + _s_crash_libc_strnstr_null_big); + _s_check_both_crash ("strnstr NULL little", _s_crash_ft_strnstr_null_little, + _s_crash_libc_strnstr_null_little); /* empty needle */ _s_check ("empty needle", ft_strnstr (hay, "", 24) == strnstr (hay, "", 24)); @@ -250,7 +218,7 @@ _s_test_strnstr (void) ft_strnstr ("aab", "ab", 3) == strnstr ("aab", "ab", 3)); /* randomized */ - for (int i = 0; i < 50; i++) + for (int i = 0; i < _S_RAND_ITERS; i++) { char rbig[128]; char rlittle[16]; @@ -276,7 +244,7 @@ _s_test_strnstr (void) int main (void) { - printf ("=== search functions ===\n"); + srand (time (NULL)); _s_test_strchr (); _s_test_strrchr (); _s_test_strnstr (); diff --git a/tests/test_strl.c b/tests/src/test_strl.c index 1ef29af..a2caa22 100644 --- a/tests/test_strl.c +++ b/tests/src/test_strl.c @@ -1,47 +1,23 @@ -#include "../libft.h" +#include "libft.h" #include "test_utils.h" #include <bsd/string.h> +_S_CRASH (ft_strlcpy_null_dst, ft_strlcpy (NULL, "abc", 10)) +_S_CRASH (libc_strlcpy_null_dst, strlcpy (NULL, "abc", 10)) +_S_CRASH_BUF (ft_strlcpy_null_src, 10, _s_sink = ft_strlcpy (buf, NULL, 10)) +_S_CRASH_BUF (libc_strlcpy_null_src, 10, _s_sink = strlcpy (buf, NULL, 10)) +_S_CRASH (ft_strlcat_null_dst, ft_strlcat (NULL, "abc", 10)) +_S_CRASH (libc_strlcat_null_dst, strlcat (NULL, "abc", 10)) + +/* strlcat NULL src needs an initialized buffer — keep manual */ static void -_s_ft_strlcpy_null_dst (void) -{ - _s_sink = ft_strlcpy (NULL, "abc", 10); -} -static void -_s_libc_strlcpy_null_dst (void) -{ - _s_sink = strlcpy (NULL, "abc", 10); -} -static void -_s_ft_strlcpy_null_src (void) -{ - char buf[10]; - _s_sink = ft_strlcpy (buf, NULL, 10); -} -static void -_s_libc_strlcpy_null_src (void) -{ - char buf[10]; - _s_sink = strlcpy (buf, NULL, 10); -} -static void -_s_ft_strlcat_null_dst (void) -{ - _s_sink = ft_strlcat (NULL, "abc", 10); -} -static void -_s_libc_strlcat_null_dst (void) -{ - _s_sink = strlcat (NULL, "abc", 10); -} -static void -_s_ft_strlcat_null_src (void) +_s_crash_ft_strlcat_null_src (void) { char buf[10] = "hi"; _s_sink = ft_strlcat (buf, NULL, 10); } static void -_s_libc_strlcat_null_src (void) +_s_crash_libc_strlcat_null_src (void) { char buf[10] = "hi"; _s_sink = strlcat (buf, NULL, 10); @@ -63,13 +39,13 @@ _s_test_strlcpy (void) char src[64]; char label[64]; - printf ("-- strlcpy --\n"); + _s_section ("ft_strlcpy"); /* NULL */ - _s_check_both_crash ("strlcpy NULL dst", _s_ft_strlcpy_null_dst, - _s_libc_strlcpy_null_dst); - _s_check_both_crash ("strlcpy NULL src", _s_ft_strlcpy_null_src, - _s_libc_strlcpy_null_src); + _s_check_both_crash ("strlcpy NULL dst", _s_crash_ft_strlcpy_null_dst, + _s_crash_libc_strlcpy_null_dst); + _s_check_both_crash ("strlcpy NULL src", _s_crash_ft_strlcpy_null_src, + _s_crash_libc_strlcpy_null_src); /* basic copy */ ft_ret = ft_strlcpy (ft_dst, "hello", 64); @@ -111,8 +87,7 @@ _s_test_strlcpy (void) _s_check ("size=1 buf", ft_dst[0] == '\0'); /* randomized */ - srand (time (NULL)); - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { int len = rand () % 60 + 1; int j; @@ -145,13 +120,13 @@ _s_test_strlcat (void) size_t size; char label[64]; - printf ("-- strlcat --\n"); + _s_section ("ft_strlcat"); /* NULL */ - _s_check_both_crash ("strlcat NULL dst", _s_ft_strlcat_null_dst, - _s_libc_strlcat_null_dst); - _s_check_both_crash ("strlcat NULL src", _s_ft_strlcat_null_src, - _s_libc_strlcat_null_src); + _s_check_both_crash ("strlcat NULL dst", _s_crash_ft_strlcat_null_dst, + _s_crash_libc_strlcat_null_dst); + _s_check_both_crash ("strlcat NULL src", _s_crash_ft_strlcat_null_src, + _s_crash_libc_strlcat_null_src); /* basic concat */ strcpy (ft_dst, "hello"); @@ -217,7 +192,7 @@ _s_test_strlcat (void) _s_check ("empty dst buf", strcmp (ft_dst, libc_dst) == 0); /* randomized */ - for (i = 0; i < 50; i++) + for (i = 0; i < _S_RAND_ITERS; i++) { int dlen = rand () % 30; int slen = rand () % 30 + 1; @@ -244,7 +219,7 @@ _s_test_strlcat (void) int main (void) { - printf ("=== strlcpy/strlcat ===\n"); + srand (time (NULL)); _s_test_strlcpy (); _s_test_strlcat (); _s_print_results (); diff --git a/tests/test_strlen.c b/tests/src/test_strlen.c index ef569ff..15f317b 100644 --- a/tests/test_strlen.c +++ b/tests/src/test_strlen.c @@ -1,22 +1,12 @@ -#include "../libft.h" +#include "libft.h" #include "test_utils.h" -static void -_s_call_ft_strlen_null (void) -{ - _s_sink = ft_strlen (NULL); -} - -static void -_s_call_strlen_null (void) -{ - _s_sink = strlen (NULL); -} +_S_CRASH (ft_strlen_null, ft_strlen (NULL)) +_S_CRASH (strlen_null, strlen (NULL)) static void _s_test_empty (void) { - printf ("-- empty string --\n"); _s_check_eq ("ft_strlen", ft_strlen (""), 0); _s_check_eq ("libc strlen", strlen (""), 0); } @@ -24,28 +14,8 @@ _s_test_empty (void) static void _s_test_null (void) { - int ft_crashed; - int libc_crashed; - - printf ("-- NULL --\n"); - ft_crashed = _s_crashes (_s_call_ft_strlen_null); - libc_crashed = _s_crashes (_s_call_strlen_null); - if (ft_crashed && libc_crashed) - { - printf (" PASS both crash on NULL\n"); - _s_pass++; - } - else if (ft_crashed == libc_crashed) - { - printf (" PASS both behave the same on NULL (no crash)\n"); - _s_pass++; - } - else - { - printf (" FAIL ft_crashed=%d libc_crashed=%d\n", ft_crashed, - libc_crashed); - _s_fail++; - } + _s_check_both_crash ("strlen NULL", _s_crash_ft_strlen_null, + _s_crash_strlen_null); } static void @@ -56,15 +26,13 @@ _s_test_random (int n) char *buf; char label[64]; - printf ("-- %d randomized tests --\n", n); for (i = 0; i < n; i++) { len = rand () % 10000; buf = malloc (len + 1); if (!buf) { - printf (" FAIL malloc\n"); - _s_fail++; + _s_check ("malloc", 0); return; } memset (buf, 'A' + (i % 26), len); @@ -81,7 +49,6 @@ _s_test_large (void) size_t len; char *buf; - printf ("-- large string (100 MB) --\n"); len = 100 * 1024 * 1024; buf = malloc (len + 1); if (!buf) @@ -99,7 +66,7 @@ int main (void) { srand (time (NULL)); - printf ("=== ft_strlen ===\n"); + _s_section ("ft_strlen"); _s_test_empty (); _s_test_null (); _s_test_random (100); diff --git a/tests/test_case.c b/tests/test_case.c deleted file mode 100644 index 44b567a..0000000 --- a/tests/test_case.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "../libft.h" -#include "test_utils.h" -#include <ctype.h> - -static void -_s_test_func (const char *name, int (*ft) (int), int (*libc) (int)) -{ - int i; - int ok; - - ok = 1; - for (i = -1; i <= 255; i++) - { - if (ft (i) != libc (i)) - { - printf (" FAIL %s(%d): ft=%d libc=%d\n", name, i, ft (i), libc (i)); - _s_fail++; - ok = 0; - } - } - if (ok) - { - printf (" PASS %s (all -1..255)\n", name); - _s_pass++; - } -} - -int -main (void) -{ - printf ("=== case functions ===\n"); - _s_test_func ("ft_toupper", ft_toupper, toupper); - _s_test_func ("ft_tolower", ft_tolower, tolower); - _s_print_results (); - return (_s_fail != 0); -} diff --git a/tests/test_is.c b/tests/test_is.c deleted file mode 100644 index 1af5d40..0000000 --- a/tests/test_is.c +++ /dev/null @@ -1,43 +0,0 @@ -#include "../libft.h" -#include "test_utils.h" -#include <ctype.h> - -static void -_s_test_func (const char *name, int (*ft) (int), int (*libc) (int)) -{ - int i; - int ft_r; - int libc_r; - int ok; - - ok = 1; - for (i = -1; i <= 255; i++) - { - ft_r = !!ft (i); - libc_r = !!libc (i); - if (ft_r != libc_r) - { - printf (" FAIL %s(%d): ft=%d libc=%d\n", name, i, ft_r, libc_r); - _s_fail++; - ok = 0; - } - } - if (ok) - { - printf (" PASS %s (all -1..255)\n", name); - _s_pass++; - } -} - -int -main (void) -{ - printf ("=== is* functions ===\n"); - _s_test_func ("ft_isalpha", ft_isalpha, isalpha); - _s_test_func ("ft_isdigit", ft_isdigit, isdigit); - _s_test_func ("ft_isalnum", ft_isalnum, isalnum); - _s_test_func ("ft_isascii", ft_isascii, isascii); - _s_test_func ("ft_isprint", ft_isprint, isprint); - _s_print_results (); - return (_s_fail != 0); -} |
