diff --git a/libft_test/ft_bzero_test.c b/libft_test/ft_bzero_test.c new file mode 100755 index 0000000..44f6214 --- /dev/null +++ b/libft_test/ft_bzero_test.c @@ -0,0 +1,104 @@ +#!/usr/bin/env bash + +# SYNTAX: +# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] +# https://stackoverflow.com/a/59592881 +catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) +} + +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 + +catch stout sterr "${tmp_out}" + +printf "%s\n" "Testing ${SRC}" +printf "Expected Output --> Test Output\n" +diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" + +if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then + printf "\e[102;30m%s\e[m\n" "All tests passed" + exit_status="0" +else + printf '%s' "${diff_out}" | head -n -1 + printf "\e[101;37m%s\e[m\n" "At least one test failed" + exit_status="1" +fi +rm -f -- "${tmp_out}" + +exit -- "${exit_status}" +// START_OF_C_FILE +#include +#include + +static int test_counter = 0; +static int expect_counter = 0; + +#define test(func, ...) \ + do { \ + int old_stderr_fd = dup(2); \ + dup2(1, 2); \ + fprintf(stdout, "Case %d:", test_counter++); \ + fflush(stdout); \ + func(__VA_ARGS__); \ + fflush(stderr); \ + fflush(stdout); \ + dup2(old_stderr_fd, 2); \ + } while(0) + +#define expect(func, ...) \ + do { \ + int old_stdout_fd = dup(1); \ + dup2(2, 1); \ + fprintf(stderr, "Case %d:", expect_counter++); \ + fflush(stderr); \ + func(__VA_ARGS__); \ + fflush(stderr); \ + fflush(stdout); \ + dup2(old_stdout_fd, 1); \ + } while(0) +// END_TEMPLATE + +#include +#include + +void *ft_bzero(void *s, size_t n); + +int main(){ + void *mymem1; + void *mymem2; + int total_size; + int size; + + total_size = 5; + size = 4; + mymem1 = malloc(total_size); + mymem2 = malloc(total_size); + ft_bzero(mymem1, size); + bzero(mymem2, size); + test (printf, "%s\n", (char *) mymem1); + expect(printf, "%s\n", (char *) mymem2); + test (printf, "%s\n", (char *) mymem1+1); + expect(printf, "%s\n", (char *) mymem2+1); + test (printf, "%s\n", (char *) mymem1+2); + expect(printf, "%s\n", (char *) mymem2+2); + test (printf, "%s\n", (char *) mymem1+3); + expect(printf, "%s\n", (char *) mymem2+3); + free(mymem1); + free(mymem2); +} \ No newline at end of file diff --git a/libft_test/ft_isalnum_test.c b/libft_test/ft_isalnum_test.c index 68498e4..bdb4c56 100755 --- a/libft_test/ft_isalnum_test.c +++ b/libft_test/ft_isalnum_test.c @@ -1,5 +1,16 @@ #!/usr/bin/env bash +# SYNTAX: +# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] +# https://stackoverflow.com/a/59592881 +catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) +} + tmp_out="$(mktemp)" if [ -f "${0%_test.c}.c" ]; then @@ -14,9 +25,20 @@ if [ ! "${?}" = "0" ] || [ ! -f "${tmp_out}" ]; then exit 1 fi +catch stout sterr "${tmp_out}" + printf "%s\n" "Testing ${SRC}" -printf "Expected --> Success\n" -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"; } +printf "Expected Output --> Test Output\n" +diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" + +if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then + printf "\e[102;30m%s\e[m\n" "All tests passed" + exit_status="0" +else + printf '%s' "${diff_out}" | head -n -1 + printf "\e[101;37m%s\e[m\n" "At least one test failed" + exit_status="1" +fi rm -f -- "${tmp_out}" exit -- "${exit_status}" @@ -120,4 +142,4 @@ int main(){ test (printf, "%d\n", ft_isalnum(arg)); expect(printf, "%d\n", isalnum(arg)); return 0; -} +} \ No newline at end of file diff --git a/libft_test/ft_isalpha_test.c b/libft_test/ft_isalpha_test.c index d794843..003b5a0 100755 --- a/libft_test/ft_isalpha_test.c +++ b/libft_test/ft_isalpha_test.c @@ -1,5 +1,16 @@ #!/usr/bin/env bash +# SYNTAX: +# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] +# https://stackoverflow.com/a/59592881 +catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) +} + tmp_out="$(mktemp)" if [ -f "${0%_test.c}.c" ]; then @@ -14,9 +25,20 @@ if [ ! "${?}" = "0" ] || [ ! -f "${tmp_out}" ]; then exit 1 fi +catch stout sterr "${tmp_out}" + printf "%s\n" "Testing ${SRC}" -printf "Expected --> Success\n" -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"; } +printf "Expected Output --> Test Output\n" +diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" + +if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then + printf "\e[102;30m%s\e[m\n" "All tests passed" + exit_status="0" +else + printf '%s' "${diff_out}" | head -n -1 + printf "\e[101;37m%s\e[m\n" "At least one test failed" + exit_status="1" +fi rm -f -- "${tmp_out}" exit -- "${exit_status}" @@ -90,4 +112,4 @@ int main(){ test (printf, "%d\n", ft_isalpha(arg)); expect(printf, "%d\n", isalpha(arg)); return 0; -} +} \ No newline at end of file diff --git a/libft_test/ft_isascii_test.c b/libft_test/ft_isascii_test.c index ac56889..359b831 100755 --- a/libft_test/ft_isascii_test.c +++ b/libft_test/ft_isascii_test.c @@ -1,5 +1,16 @@ #!/usr/bin/env bash +# SYNTAX: +# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] +# https://stackoverflow.com/a/59592881 +catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) +} + tmp_out="$(mktemp)" if [ -f "${0%_test.c}.c" ]; then @@ -14,9 +25,20 @@ if [ ! "${?}" = "0" ] || [ ! -f "${tmp_out}" ]; then exit 1 fi +catch stout sterr "${tmp_out}" + printf "%s\n" "Testing ${SRC}" -printf "Expected --> Success\n" -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"; } +printf "Expected Output --> Test Output\n" +diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" + +if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then + printf "\e[102;30m%s\e[m\n" "All tests passed" + exit_status="0" +else + printf '%s' "${diff_out}" | head -n -1 + printf "\e[101;37m%s\e[m\n" "At least one test failed" + exit_status="1" +fi rm -f -- "${tmp_out}" exit -- "${exit_status}" @@ -85,4 +107,4 @@ int main(){ test (printf, "%d\n", ft_isascii(arg)); expect(printf, "%d\n", isascii(arg)); return 0; -} +} \ No newline at end of file diff --git a/libft_test/ft_isdigit_test.c b/libft_test/ft_isdigit_test.c index 7776c45..de26690 100755 --- a/libft_test/ft_isdigit_test.c +++ b/libft_test/ft_isdigit_test.c @@ -1,5 +1,16 @@ #!/usr/bin/env bash +# SYNTAX: +# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] +# https://stackoverflow.com/a/59592881 +catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) +} + tmp_out="$(mktemp)" if [ -f "${0%_test.c}.c" ]; then @@ -14,9 +25,20 @@ if [ ! "${?}" = "0" ] || [ ! -f "${tmp_out}" ]; then exit 1 fi +catch stout sterr "${tmp_out}" + printf "%s\n" "Testing ${SRC}" -printf "Expected --> Success\n" -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"; } +printf "Expected Output --> Test Output\n" +diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" + +if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then + printf "\e[102;30m%s\e[m\n" "All tests passed" + exit_status="0" +else + printf '%s' "${diff_out}" | head -n -1 + printf "\e[101;37m%s\e[m\n" "At least one test failed" + exit_status="1" +fi rm -f -- "${tmp_out}" exit -- "${exit_status}" @@ -105,4 +127,4 @@ int main(){ test (printf, "%d\n", ft_isdigit(arg)); expect(printf, "%d\n", isdigit(arg)); return 0; -} +} \ No newline at end of file diff --git a/libft_test/ft_isprint_test.c b/libft_test/ft_isprint_test.c index f51562b..72f89d4 100755 --- a/libft_test/ft_isprint_test.c +++ b/libft_test/ft_isprint_test.c @@ -1,5 +1,16 @@ #!/usr/bin/env bash +# SYNTAX: +# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] +# https://stackoverflow.com/a/59592881 +catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) +} + tmp_out="$(mktemp)" if [ -f "${0%_test.c}.c" ]; then @@ -14,9 +25,20 @@ if [ ! "${?}" = "0" ] || [ ! -f "${tmp_out}" ]; then exit 1 fi +catch stout sterr "${tmp_out}" + printf "%s\n" "Testing ${SRC}" -printf "Expected --> Success\n" -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"; } +printf "Expected Output --> Test Output\n" +diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" + +if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then + printf "\e[102;30m%s\e[m\n" "All tests passed" + exit_status="0" +else + printf '%s' "${diff_out}" | head -n -1 + printf "\e[101;37m%s\e[m\n" "At least one test failed" + exit_status="1" +fi rm -f -- "${tmp_out}" exit -- "${exit_status}" @@ -94,4 +116,4 @@ int main(){ test (printf, "%d\n", ft_isprint(arg)); expect(printf, "%d\n", isprint(arg)); return 0; -} +} \ No newline at end of file diff --git a/libft_test/ft_memset_test.c b/libft_test/ft_memset_test.c new file mode 100755 index 0000000..12ca7da --- /dev/null +++ b/libft_test/ft_memset_test.c @@ -0,0 +1,177 @@ +#!/usr/bin/env bash + +# SYNTAX: +# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] +# https://stackoverflow.com/a/59592881 +catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) +} + +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 + +catch stout sterr "${tmp_out}" + +printf "%s\n" "Testing ${SRC}" +printf "Expected Output --> Test Output\n" +diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" + +if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then + printf "\e[102;30m%s\e[m\n" "All tests passed" + exit_status="0" +else + printf '%s' "${diff_out}" | head -n -1 + printf "\e[101;37m%s\e[m\n" "At least one test failed" + exit_status="1" +fi +rm -f -- "${tmp_out}" + +exit -- "${exit_status}" +// START_OF_C_FILE +#include +#include + +static int test_counter = 0; +static int expect_counter = 0; + +#define test(func, ...) \ + do { \ + int old_stderr_fd = dup(2); \ + dup2(1, 2); \ + fprintf(stdout, "Case %d:", test_counter++); \ + fflush(stdout); \ + func(__VA_ARGS__); \ + fflush(stderr); \ + fflush(stdout); \ + dup2(old_stderr_fd, 2); \ + } while(0) + +#define expect(func, ...) \ + do { \ + int old_stdout_fd = dup(1); \ + dup2(2, 1); \ + fprintf(stderr, "Case %d:", expect_counter++); \ + fflush(stderr); \ + func(__VA_ARGS__); \ + fflush(stderr); \ + fflush(stdout); \ + dup2(old_stdout_fd, 1); \ + } while(0) +// END_TEMPLATE + +#include +#include + +void *ft_memset(void *s, int c, size_t n); + +int main(){ + void *mymem; + int total_size; + int byte; + int size; + + total_size = 5; + byte = 97; + size = 0; + mymem = calloc(1, total_size); + test (printf, "%s\n", (char *) ft_memset(mymem, byte, size)); + free(mymem); + mymem = calloc(1, total_size); + expect(printf, "%s\n", (char *) memset(mymem, byte, size)); + free(mymem); + + total_size = 5; + byte = 97; + size = 1; + mymem = calloc(1, total_size); + test (printf, "%s\n", (char *) ft_memset(mymem, byte, size)); + free(mymem); + mymem = calloc(1, total_size); + expect(printf, "%s\n", (char *) memset(mymem, byte, size)); + free(mymem); + + total_size = 5; + byte = 97; + size = 2; + mymem = calloc(1, total_size); + test (printf, "%s\n", (char *) ft_memset(mymem, byte, size)); + free(mymem); + mymem = calloc(1, total_size); + expect(printf, "%s\n", (char *) memset(mymem, byte, size)); + free(mymem); + + total_size = 5; + byte = 97; + size = 3; + mymem = calloc(1, total_size); + test (printf, "%s\n", (char *) ft_memset(mymem, byte, size)); + free(mymem); + mymem = calloc(1, total_size); + expect(printf, "%s\n", (char *) memset(mymem, byte, size)); + free(mymem); + + total_size = 5; + byte = 97; + size = 4; + mymem = calloc(1, total_size); + test (printf, "%s\n", (char *) ft_memset(mymem, byte, size)); + free(mymem); + mymem = calloc(1, total_size); + expect(printf, "%s\n", (char *) memset(mymem, byte, size)); + free(mymem); + + total_size = 5; + byte = 0; + size = 4; + mymem = calloc(1, total_size); + test (printf, "%s\n", (char *) ft_memset(mymem, byte, size)); + free(mymem); + mymem = calloc(1, total_size); + expect(printf, "%s\n", (char *) memset(mymem, byte, size)); + free(mymem); + + total_size = 5; + byte = 1; + size = 4; + mymem = calloc(1, total_size); + test (printf, "%s\n", (char *) ft_memset(mymem, byte, size)); + free(mymem); + mymem = calloc(1, total_size); + expect(printf, "%s\n", (char *) memset(mymem, byte, size)); + free(mymem); + + total_size = 5; + byte = 127; + size = 4; + mymem = calloc(1, total_size); + test (printf, "%s\n", (char *) ft_memset(mymem, byte, size)); + free(mymem); + mymem = calloc(1, total_size); + expect(printf, "%s\n", (char *) memset(mymem, byte, size)); + free(mymem); + + total_size = 5; + byte = 126; + size = 4; + mymem = calloc(1, total_size); + test (printf, "%s\n", (char *) ft_memset(mymem, byte, size)); + free(mymem); + mymem = calloc(1, total_size); + expect(printf, "%s\n", (char *) memset(mymem, byte, size)); + free(mymem); +} \ No newline at end of file diff --git a/libft_test/ft_strlen_test.c b/libft_test/ft_strlen_test.c index 627bf46..f94c9ad 100755 --- a/libft_test/ft_strlen_test.c +++ b/libft_test/ft_strlen_test.c @@ -1,5 +1,16 @@ #!/usr/bin/env bash +# SYNTAX: +# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] +# https://stackoverflow.com/a/59592881 +catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) +} + tmp_out="$(mktemp)" if [ -f "${0%_test.c}.c" ]; then @@ -14,9 +25,20 @@ if [ ! "${?}" = "0" ] || [ ! -f "${tmp_out}" ]; then exit 1 fi +catch stout sterr "${tmp_out}" + printf "%s\n" "Testing ${SRC}" -printf "Expected --> Success\n" -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"; } +printf "Expected Output --> Test Output\n" +diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" + +if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then + printf "\e[102;30m%s\e[m\n" "All tests passed" + exit_status="0" +else + printf '%s' "${diff_out}" | head -n -1 + printf "\e[101;37m%s\e[m\n" "At least one test failed" + exit_status="1" +fi rm -f -- "${tmp_out}" exit -- "${exit_status}" @@ -87,4 +109,4 @@ int main(){ test (printf, "%lu\n", ft_strlen(arg)); expect(printf, "%lu\n", strlen(arg)); return 0; -} +} \ No newline at end of file diff --git a/template_test.c b/template_test.c index de9df80..078bb5f 100755 --- a/template_test.c +++ b/template_test.c @@ -1,5 +1,16 @@ #!/usr/bin/env bash +# SYNTAX: +# catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] +# https://stackoverflow.com/a/59592881 +catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) +} + tmp_out="$(mktemp)" if [ -f "${0%_test.c}.c" ]; then @@ -14,9 +25,20 @@ if [ ! "${?}" = "0" ] || [ ! -f "${tmp_out}" ]; then exit 1 fi +catch stout sterr "${tmp_out}" + printf "%s\n" "Testing ${SRC}" -printf "Expected --> Success\n" -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"; } +printf "Expected Output --> Test Output\n" +diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" + +if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then + printf "\e[102;30m%s\e[m\n" "All tests passed" + exit_status="0" +else + printf '%s' "${diff_out}" | head -n -1 + printf "\e[101;37m%s\e[m\n" "At least one test failed" + exit_status="1" +fi rm -f -- "${tmp_out}" exit -- "${exit_status}" @@ -53,7 +75,7 @@ static int expect_counter = 0; // END_TEMPLATE int main(){ - // test ("Test output", printf, "\n"); - // expect("Expected output", printf, "\n"); + // test (printf, "\n"); + // expect(printf, "\n"); return 0; }