This commit is contained in:
Timo Schmidt 2023-05-03 22:49:57 +02:00
parent ae942840b4
commit 78e4d2d407
3 changed files with 184 additions and 52 deletions

View File

@ -57,29 +57,37 @@ static int expect_counter = 0;
int ft_isalpha(int c);
int main(){
test (printf, "%d\n", ft_isalpha('a'));
expect(printf, "%d\n", isalpha('a'));
test (printf, "%d\n", ft_isalpha('A'));
expect(printf, "%d\n", isalpha('A'));
test (printf, "%d\n", ft_isalpha('m'));
expect(printf, "%d\n", isalpha('m'));
test (printf, "%d\n", ft_isalpha('M'));
expect(printf, "%d\n", isalpha('M'));
test (printf, "%d\n", ft_isalpha('z'));
expect(printf, "%d\n", isalpha('z'));
test (printf, "%d\n", ft_isalpha('Z'));
expect(printf, "%d\n", isalpha('Z'));
test (printf, "%d\n", ft_isalpha(0));
expect(printf, "%d\n", isalpha(0));
test (printf, "%d\n", ft_isalpha(1));
expect(printf, "%d\n", isalpha(1));
test (printf, "%d\n", ft_isalpha('a' - 1));
expect(printf, "%d\n", isalpha('a' - 1));
test (printf, "%d\n", ft_isalpha('A' - 1));
expect(printf, "%d\n", isalpha('A' - 1));
test (printf, "%d\n", ft_isalpha('z' + 1));
expect(printf, "%d\n", isalpha('z' + 1));
test (printf, "%d\n", ft_isalpha('Z' + 1));
expect(printf, "%d\n", isalpha('Z' + 1));
int arg;
arg = 'a';
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
arg = 'A';
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
arg = 'z';
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
arg = 'Z';
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
arg = 'm';
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
arg = 'M';
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
arg = 'a' - 1;
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
arg = 'A' - 1
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
arg = 'z' + 1;
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
arg = 'Z' + 1;
test (printf, "%d\n", ft_isalpha(arg));
expect(printf, "%d\n", isalpha(arg));
return 0;
}
}

View File

@ -57,31 +57,52 @@ static int expect_counter = 0;
int ft_isdigit(int c);
int main(){
test (printf, "%d\n", ft_isdigit('0'));
expect(printf, "%d\n", isdigit('0'));
test (printf, "%d\n", ft_isdigit('1'));
expect(printf, "%d\n", isdigit('1'));
test (printf, "%d\n", ft_isdigit('2'));
expect(printf, "%d\n", isdigit('2'));
test (printf, "%d\n", ft_isdigit('3'));
expect(printf, "%d\n", isdigit('3'));
test (printf, "%d\n", ft_isdigit('4'));
expect(printf, "%d\n", isdigit('4'));
test (printf, "%d\n", ft_isdigit('5'));
expect(printf, "%d\n", isdigit('5'));
test (printf, "%d\n", ft_isdigit('6'));
expect(printf, "%d\n", isdigit('6'));
test (printf, "%d\n", ft_isdigit('7'));
expect(printf, "%d\n", isdigit('7'));
test (printf, "%d\n", ft_isdigit('8'));
expect(printf, "%d\n", isdigit('8'));
test (printf, "%d\n", ft_isdigit('9'));
expect(printf, "%d\n", isdigit('9'));
test (printf, "%d\n", ft_isdigit(0));
expect(printf, "%d\n", isdigit(0));
test (printf, "%d\n", ft_isdigit(1));
expect(printf, "%d\n", isdigit(1));
test (printf, "%d\n", ft_isdigit(127));
expect(printf, "%d\n", isdigit(127));
int arg;
arg = '0';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = '1';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = '2';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = '3';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = '4';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = '5';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = '6';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = '7';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = '8';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = '9';
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = 0;
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = 1;
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = 126;
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = 127;
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
arg = 128;
test (printf, "%d\n", ft_isdigit(arg));
expect(printf, "%d\n", isdigit(arg));
return 0;
}
}

103
libft_test/ft_isprint_test.c Executable file
View File

@ -0,0 +1,103 @@
#!/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
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"; }
rm -f -- "${tmp_out}"
exit -- "${exit_status}"
// START_OF_C_FILE
#include <stdio.h>
#include <unistd.h>
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 <ctype.h>
#include <limits.h>
int ft_isprint(int c);
int main(){
int arg;
arg = -1;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 0;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 1;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 20;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 31;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 32;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 33;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 120;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 126;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 127;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = 128;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = INT_MAX;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
arg = INT_MIN;
test (printf, "%d\n", ft_isprint(arg));
expect(printf, "%d\n", isprint(arg));
return 0;
}