From e919a48b345aa0baacd70a1712c225b83d140908 Mon Sep 17 00:00:00 2001 From: Timo Schmidt Date: Wed, 3 May 2023 19:32:39 +0200 Subject: [PATCH] ft_isdigit_test.c --- libft_test/ft_isdigit_test.c | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 libft_test/ft_isdigit_test.c diff --git a/libft_test/ft_isdigit_test.c b/libft_test/ft_isdigit_test.c new file mode 100755 index 0000000..797f3b2 --- /dev/null +++ b/libft_test/ft_isdigit_test.c @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +tmp_out="$(mktemp)" + +if [ -f "${0%_test.c}.c" ]; then + SRC="${0%_test.c}.c" +else + SRC="$($(type -P basename) -- "${0%_test.c}.c")" +fi +grep -A9999 -- "[S]TART_OF_C_FILE" "${0}" | cc -Wall -Wextra -Werror -o "${tmp_out}" -xc "${SRC}" - + +if [ ! "${?}" = "0" ] || [ ! -f "${tmp_out}" ]; then + printf "\e[101;37m%s\e[m\n" "Could not compile" + exit 1 +fi + +diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <("${tmp_out}" 2>&1 1>/dev/null) <("${tmp_out}" 2>/dev/null) && { printf "\e[102;30m%s\e[m\n" "All tests passed"; exit_status="0"; } || { printf "\e[101;37m%s\e[m\n" "At least one test failed"; exit_status="1"; } +rm -f -- "${tmp_out}" + +exit -- "${exit_status}" +// START_OF_C_FILE +#include +#include + +#define test(out_prefix, expected_string, func, ...) \ + do { \ + fprintf(stderr, "Case:%s:%s", out_prefix, expected_string); \ + fflush(stderr); \ + int old_stderr_fd = dup(2); \ + dup2(1, 2); \ + fprintf(stdout, "Case:%s:", out_prefix); \ + fflush(stdout); \ + func(__VA_ARGS__); \ + fflush(stdout); \ + dup2(old_stderr_fd, 2); \ + } while(0) + +int ft_isdigit(int c); + +int main(){ + test("'0'", "1\n", printf, "%d\n", ft_isdigit('0')); + test("'1'", "1\n", printf, "%d\n", ft_isdigit('1')); + test("'2'", "1\n", printf, "%d\n", ft_isdigit('2')); + test("'3'", "1\n", printf, "%d\n", ft_isdigit('3')); + test("'4'", "1\n", printf, "%d\n", ft_isdigit('4')); + test("'5'", "1\n", printf, "%d\n", ft_isdigit('5')); + test("'6'", "1\n", printf, "%d\n", ft_isdigit('6')); + test("'7'", "1\n", printf, "%d\n", ft_isdigit('7')); + test("'8'", "1\n", printf, "%d\n", ft_isdigit('8')); + test("'9'", "1\n", printf, "%d\n", ft_isdigit('9')); + test( "0", "0\n", printf, "%d\n", ft_isdigit(0)); + test( "1", "0\n", printf, "%d\n", ft_isdigit(1)); + test("127", "0\n", printf, "%d\n", ft_isdigit(127)); + test("255", "0\n", printf, "%d\n", ft_isdigit(255)); + test( "-1", "0\n", printf, "%d\n", ft_isdigit(-1)); + return 0; +j