#include "libft.h" #include "test_utils.h" #include /* ── helper: capture fd output into a buffer ──────────────────────── */ static int _s_read_pipe (void (*fn) (int), char *buf, size_t bufsz) { int fds[2]; ssize_t n; if (pipe (fds) < 0) return (-1); fn (fds[1]); close (fds[1]); n = read (fds[0], buf, bufsz - 1); close (fds[0]); if (n < 0) return (-1); buf[n] = '\0'; return (n); } /* ====================================== * ft_putchar_fd * ====================================== */ static void _s_putchar_a (int fd) { ft_putchar_fd ('a', fd); } static void _s_putchar_nl (int fd) { ft_putchar_fd ('\n', fd); } static void _s_putchar_zero (int fd) { ft_putchar_fd ('\0', fd); } static void _s_test_putchar_fd (void) { char buf[64]; _s_section ("ft_putchar_fd"); _s_read_pipe (_s_putchar_a, buf, sizeof (buf)); _s_check ("char 'a'", strcmp (buf, "a") == 0); _s_read_pipe (_s_putchar_nl, buf, sizeof (buf)); _s_check ("newline", strcmp (buf, "\n") == 0); { int n = _s_read_pipe (_s_putchar_zero, buf, sizeof (buf)); _s_check ("null byte", n == 1 && buf[0] == '\0'); } } /* ====================================== * ft_putstr_fd * ====================================== */ static void _s_putstr_hello (int fd) { ft_putstr_fd ("hello", fd); } static void _s_putstr_empty (int fd) { ft_putstr_fd ("", fd); } _S_CRASH_V (ft_putstr_null, ft_putstr_fd (NULL, 1)) static void _s_test_putstr_fd (void) { char buf[256]; _s_section ("ft_putstr_fd"); _s_check ("NULL crashes", _s_crashes (_s_crash_ft_putstr_null)); _s_read_pipe (_s_putstr_hello, buf, sizeof (buf)); _s_check ("hello", strcmp (buf, "hello") == 0); { int n = _s_read_pipe (_s_putstr_empty, buf, sizeof (buf)); _s_check ("empty", n == 0); } } /* ====================================== * ft_putendl_fd * ====================================== */ static void _s_putendl_hello (int fd) { ft_putendl_fd ("hello", fd); } static void _s_putendl_empty (int fd) { ft_putendl_fd ("", fd); } _S_CRASH_V (ft_putendl_null, ft_putendl_fd (NULL, 1)) static void _s_test_putendl_fd (void) { char buf[256]; _s_section ("ft_putendl_fd"); _s_check ("NULL crashes", _s_crashes (_s_crash_ft_putendl_null)); _s_read_pipe (_s_putendl_hello, buf, sizeof (buf)); _s_check ("hello\\n", strcmp (buf, "hello\n") == 0); _s_read_pipe (_s_putendl_empty, buf, sizeof (buf)); _s_check ("empty\\n", strcmp (buf, "\n") == 0); } /* ====================================== * ft_putnbr_fd * ====================================== */ static void _s_putnbr_0 (int fd) { ft_putnbr_fd (0, fd); } static void _s_putnbr_42 (int fd) { ft_putnbr_fd (42, fd); } static void _s_putnbr_neg (int fd) { ft_putnbr_fd (-42, fd); } static void _s_putnbr_max (int fd) { ft_putnbr_fd (INT_MAX, fd); } static void _s_putnbr_min (int fd) { ft_putnbr_fd (INT_MIN, fd); } static void _s_test_putnbr_fd (void) { char buf[64]; char expected[64]; _s_section ("ft_putnbr_fd"); _s_read_pipe (_s_putnbr_0, buf, sizeof (buf)); _s_check ("zero", strcmp (buf, "0") == 0); _s_read_pipe (_s_putnbr_42, buf, sizeof (buf)); _s_check ("positive", strcmp (buf, "42") == 0); _s_read_pipe (_s_putnbr_neg, buf, sizeof (buf)); _s_check ("negative", strcmp (buf, "-42") == 0); _s_read_pipe (_s_putnbr_max, buf, sizeof (buf)); snprintf (expected, sizeof (expected), "%d", INT_MAX); _s_check ("INT_MAX", strcmp (buf, expected) == 0); _s_read_pipe (_s_putnbr_min, buf, sizeof (buf)); snprintf (expected, sizeof (expected), "%d", INT_MIN); _s_check ("INT_MIN", strcmp (buf, expected) == 0); } int main (void) { _s_test_putchar_fd (); _s_test_putstr_fd (); _s_test_putendl_fd (); _s_test_putnbr_fd (); _s_print_results (); return (_s_fail != 0); }