Template applier

This commit is contained in:
Timo Schmidt 2023-05-03 22:30:45 +02:00
parent 274114ffad
commit 3f12302461
5 changed files with 118 additions and 39 deletions

9
apply_template.sh Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
template_file="./template_test.c"
template="$(grep -B9999 -- "END_TEMPLATE" "${template_file}")"
for file in $(find . -type f -name "*_test.c" -not -name "template_test.c"); do
tests="$(grep -A9999 -- "END_TEMPLATE" "${file}" | sed -e '1d')"
printf '%s\n%s' "${template}" "${tests}" > "${file}"
done

63
libft_test/ft_isalnum_test.c Executable file
View File

@ -0,0 +1,63 @@
#!/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(inp, 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>
int ft_isalnum(int c);
int main(){
test (printf, "%d\n", ft_isalnum('0'));
expect(printf, "%d\n", isalnum('0'));
return 0;
}

View File

@ -24,11 +24,14 @@ exit -- "${exit_status}"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#define test(inp, func, ...) \ static int test_counter = 0;
static int expect_counter = 0;
#define test(func, ...) \
do { \ do { \
int old_stderr_fd = dup(2); \ int old_stderr_fd = dup(2); \
dup2(1, 2); \ dup2(1, 2); \
fprintf(stdout, "Case:%s:", inp); \ fprintf(stdout, "Case %d:", test_counter++); \
fflush(stdout); \ fflush(stdout); \
func(__VA_ARGS__); \ func(__VA_ARGS__); \
fflush(stderr); \ fflush(stderr); \
@ -40,43 +43,43 @@ exit -- "${exit_status}"
do { \ do { \
int old_stdout_fd = dup(1); \ int old_stdout_fd = dup(1); \
dup2(2, 1); \ dup2(2, 1); \
fprintf(stderr, "Case:%s:", inp); \ fprintf(stderr, "Case %d:", expect_counter++); \
fflush(stderr); \ fflush(stderr); \
func(__VA_ARGS__); \ func(__VA_ARGS__); \
fflush(stderr); \ fflush(stderr); \
fflush(stdout); \ fflush(stdout); \
dup2(old_stdout_fd, 1); \ dup2(old_stdout_fd, 1); \
} while(0) } while(0)
// Don't delete above this comment // END_TEMPLATE
#include <ctype.h> #include <ctype.h>
int ft_isalpha(int c); int ft_isalpha(int c);
int main(){ int main(){
test( "'a'", printf, "%d\n", ft_isalpha('a')); test ( "'a'", printf, "%d\n", ft_isalpha('a'));
expect( "'a'", printf, "%d\n", isalpha('a')); expect( "'a'", printf, "%d\n", isalpha('a'));
test( "'A'", printf, "%d\n", ft_isalpha('A')); test ( "'A'", printf, "%d\n", ft_isalpha('A'));
expect( "'A'", printf, "%d\n", isalpha('A')); expect( "'A'", printf, "%d\n", isalpha('A'));
test( "'m'", printf, "%d\n", ft_isalpha('m')); test ( "'m'", printf, "%d\n", ft_isalpha('m'));
expect( "'m'", printf, "%d\n", isalpha('m')); expect( "'m'", printf, "%d\n", isalpha('m'));
test( "'M'", printf, "%d\n", ft_isalpha('M')); test ( "'M'", printf, "%d\n", ft_isalpha('M'));
expect( "'M'", printf, "%d\n", isalpha('M')); expect( "'M'", printf, "%d\n", isalpha('M'));
test( "'z'", printf, "%d\n", ft_isalpha('z')); test ( "'z'", printf, "%d\n", ft_isalpha('z'));
expect( "'z'", printf, "%d\n", isalpha('z')); expect( "'z'", printf, "%d\n", isalpha('z'));
test( "'Z'", printf, "%d\n", ft_isalpha('Z')); test ( "'Z'", printf, "%d\n", ft_isalpha('Z'));
expect( "'Z'", printf, "%d\n", isalpha('Z')); expect( "'Z'", printf, "%d\n", isalpha('Z'));
test( "0", printf, "%d\n", ft_isalpha(0)); test ( "0", printf, "%d\n", ft_isalpha(0));
expect( "0", printf, "%d\n", isalpha(0)); expect( "0", printf, "%d\n", isalpha(0));
test( "1", printf, "%d\n", ft_isalpha(1)); test ( "1", printf, "%d\n", ft_isalpha(1));
expect( "1", printf, "%d\n", isalpha(1)); expect( "1", printf, "%d\n", isalpha(1));
test( "'a' - 1", printf, "%d\n", ft_isalpha('a' - 1)); test ("'a' - 1", printf, "%d\n", ft_isalpha('a' - 1));
expect("'a' - 1", printf, "%d\n", isalpha('a' - 1)); expect("'a' - 1", printf, "%d\n", isalpha('a' - 1));
test( "'A' - 1", printf, "%d\n", ft_isalpha('A' - 1)); test ("'A' - 1", printf, "%d\n", ft_isalpha('A' - 1));
expect("'A' - 1", printf, "%d\n", isalpha('A' - 1)); expect("'A' - 1", printf, "%d\n", isalpha('A' - 1));
test( "'z' + 1", printf, "%d\n", ft_isalpha('z' + 1)); test ("'z' + 1", printf, "%d\n", ft_isalpha('z' + 1));
expect("'z' + 1", printf, "%d\n", isalpha('z' + 1)); expect("'z' + 1", printf, "%d\n", isalpha('z' + 1));
test( "'Z' + 1", printf, "%d\n", ft_isalpha('Z' + 1)); test ("'Z' + 1", printf, "%d\n", ft_isalpha('Z' + 1));
expect("'Z' + 1", printf, "%d\n", isalpha('Z' + 1)); expect("'Z' + 1", printf, "%d\n", isalpha('Z' + 1));
return 0; return 0;
} }

View File

@ -24,11 +24,14 @@ exit -- "${exit_status}"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#define test(inp, func, ...) \ static int test_counter = 0;
static int expect_counter = 0;
#define test(func, ...) \
do { \ do { \
int old_stderr_fd = dup(2); \ int old_stderr_fd = dup(2); \
dup2(1, 2); \ dup2(1, 2); \
fprintf(stdout, "Case:%s:", inp); \ fprintf(stdout, "Case %d:", test_counter++); \
fflush(stdout); \ fflush(stdout); \
func(__VA_ARGS__); \ func(__VA_ARGS__); \
fflush(stderr); \ fflush(stderr); \
@ -40,45 +43,45 @@ exit -- "${exit_status}"
do { \ do { \
int old_stdout_fd = dup(1); \ int old_stdout_fd = dup(1); \
dup2(2, 1); \ dup2(2, 1); \
fprintf(stderr, "Case:%s:", inp); \ fprintf(stderr, "Case %d:", expect_counter++); \
fflush(stderr); \ fflush(stderr); \
func(__VA_ARGS__); \ func(__VA_ARGS__); \
fflush(stderr); \ fflush(stderr); \
fflush(stdout); \ fflush(stdout); \
dup2(old_stdout_fd, 1); \ dup2(old_stdout_fd, 1); \
} while(0) } while(0)
// Don't delete above this comment // END_TEMPLATE
#include <ctype.h> #include <ctype.h>
int ft_isdigit(int c); int ft_isdigit(int c);
int main(){ int main(){
test( "'0'", printf, "%d\n", ft_isdigit('0')); test ("'0'", printf, "%d\n", ft_isdigit('0'));
expect("'0'", printf, "%d\n", isdigit('0')); expect("'0'", printf, "%d\n", isdigit('0'));
test( "'1'", printf, "%d\n", ft_isdigit('1')); test ("'1'", printf, "%d\n", ft_isdigit('1'));
expect("'1'", printf, "%d\n", isdigit('1')); expect("'1'", printf, "%d\n", isdigit('1'));
test( "'2'", printf, "%d\n", ft_isdigit('2')); test ("'2'", printf, "%d\n", ft_isdigit('2'));
expect("'2'", printf, "%d\n", isdigit('2')); expect("'2'", printf, "%d\n", isdigit('2'));
test( "'3'", printf, "%d\n", ft_isdigit('3')); test ("'3'", printf, "%d\n", ft_isdigit('3'));
expect("'3'", printf, "%d\n", isdigit('3')); expect("'3'", printf, "%d\n", isdigit('3'));
test( "'4'", printf, "%d\n", ft_isdigit('4')); test ("'4'", printf, "%d\n", ft_isdigit('4'));
expect("'4'", printf, "%d\n", isdigit('4')); expect("'4'", printf, "%d\n", isdigit('4'));
test( "'5'", printf, "%d\n", ft_isdigit('5')); test ("'5'", printf, "%d\n", ft_isdigit('5'));
expect("'5'", printf, "%d\n", isdigit('5')); expect("'5'", printf, "%d\n", isdigit('5'));
test( "'6'", printf, "%d\n", ft_isdigit('6')); test ("'6'", printf, "%d\n", ft_isdigit('6'));
expect("'6'", printf, "%d\n", isdigit('6')); expect("'6'", printf, "%d\n", isdigit('6'));
test( "'7'", printf, "%d\n", ft_isdigit('7')); test ("'7'", printf, "%d\n", ft_isdigit('7'));
expect("'7'", printf, "%d\n", isdigit('7')); expect("'7'", printf, "%d\n", isdigit('7'));
test( "'8'", printf, "%d\n", ft_isdigit('8')); test ("'8'", printf, "%d\n", ft_isdigit('8'));
expect("'8'", printf, "%d\n", isdigit('8')); expect("'8'", printf, "%d\n", isdigit('8'));
test( "'9'", printf, "%d\n", ft_isdigit('9')); test ("'9'", printf, "%d\n", ft_isdigit('9'));
expect("'9'", printf, "%d\n", isdigit('9')); expect("'9'", printf, "%d\n", isdigit('9'));
test( "0", printf, "%d\n", ft_isdigit(0)); test ("0", printf, "%d\n", ft_isdigit(0));
expect("0", printf, "%d\n", isdigit(0)); expect("0", printf, "%d\n", isdigit(0));
test( "1", printf, "%d\n", ft_isdigit(1)); test ("1", printf, "%d\n", ft_isdigit(1));
expect("1", printf, "%d\n", isdigit(1)); expect("1", printf, "%d\n", isdigit(1));
test( "127", printf, "%d\n", ft_isdigit(127)); test ("127", printf, "%d\n", ft_isdigit(127));
expect("127", printf, "%d\n", isdigit(127)); expect("127", printf, "%d\n", isdigit(127));
return 0; return 0;
} }

13
template_test.c Executable file → Normal file
View File

@ -24,11 +24,14 @@ exit -- "${exit_status}"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#define test(inp, func, ...) \ static int test_counter = 0;
static int expect_counter = 0;
#define test(func, ...) \
do { \ do { \
int old_stderr_fd = dup(2); \ int old_stderr_fd = dup(2); \
dup2(1, 2); \ dup2(1, 2); \
fprintf(stdout, "Case:%s:", inp); \ fprintf(stdout, "Case %d:", test_counter++); \
fflush(stdout); \ fflush(stdout); \
func(__VA_ARGS__); \ func(__VA_ARGS__); \
fflush(stderr); \ fflush(stderr); \
@ -40,16 +43,14 @@ exit -- "${exit_status}"
do { \ do { \
int old_stdout_fd = dup(1); \ int old_stdout_fd = dup(1); \
dup2(2, 1); \ dup2(2, 1); \
fprintf(stderr, "Case:%s:", inp); \ fprintf(stderr, "Case %d:", expect_counter++); \
fflush(stderr); \ fflush(stderr); \
func(__VA_ARGS__); \ func(__VA_ARGS__); \
fflush(stderr); \ fflush(stderr); \
fflush(stdout); \ fflush(stdout); \
dup2(old_stdout_fd, 1); \ dup2(old_stdout_fd, 1); \
} while(0) } while(0)
// Don't delete above this comment // END_TEMPLATE
#include <ctype.h>
int main(){ int main(){
// test( "Test output", printf, "\n"); // test( "Test output", printf, "\n");