Add template

This commit is contained in:
Timo Schmidt 2023-05-03 19:24:57 +02:00
parent d7829338ca
commit 618920e550
1 changed files with 41 additions and 0 deletions

41
template_test.c Executable file
View File

@ -0,0 +1,41 @@
#!/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 <stdio.h>
#include <unistd.h>
#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 main(){
test("dummy", "true\n", printf, "%s\n", "true");
return 0;
}