From 60fa67b590c5841dfd669dfedd5b057919870dbd Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 25 Jul 2016 16:18:34 +0100 Subject: [PATCH] add tests to build system --- Makefile | 39 +++++++++++++++++++++++++++------------ errors.h | 5 ++++- test/minunit.h | 5 +++++ test/test_error_string.c | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 test/minunit.h create mode 100644 test/test_error_string.c diff --git a/Makefile b/Makefile index 109030f..ea265d6 100644 --- a/Makefile +++ b/Makefile @@ -7,36 +7,51 @@ CFLAGS = -fPIC -g -O2 -std=c99 \ -Wold-style-definition \ -Woverflow \ -Wpointer-arith \ - -Wstrict-prototypes \ -Wunused \ -Wvla -LDFLAGS = -g -shared +LDFLAGS = -g LDLIBS = TARGET_LIB = mulithash.so SRCS = errors.c -OBJS = $(subst .c,.o,$(SRCS)) +OBJS = $(SRCS:.c=.o) all: ${TARGET_LIB} $(TARGET_LIB): $(OBJS) - $(CC) ${LDFLAGS} -o $@ $^ + $(CC) -g -shared -o $@ $^ -depend: .depend +# Tests -.depend: $(SRCS) - rm -f ./.depend - $(CC) $(CFLAGS) -MM $^>>./.depend; +TEST_SRCS = $(wildcard test/test_*.c) +TEST_OBJS = $(TEST_SRCS:.c=.o) +TEST_BINS = $(TEST_OBJS:.o=.out) + +PHONY += tests +tests: $(TEST_BINS) + @for t in $^; do \ + echo; \ + echo '***' "$$t" '***'; \ + LD_LIBRARY_PATH=. "./$$t"; \ + echo; \ + done + + +test/test_%.out: test/test_%.o $(TARGET_LIB) + $(CC) $(LDFLAGS) $(TARGET_LIB) $^ -o $@ + +# Utils + +PHONY += clean dist-clean clean: $(RM) $(OBJS) + $(RM) $(TEST_OBJS) + $(RM) $(TEST_BINS) $(RM) $(TARGET_LIB) dist-clean: clean - $(RM) *~ .depend -.PHONY: all depend clean dist-clean - -include .depend +.PHONY: $(PHONY) diff --git a/errors.h b/errors.h index 8ec5ceb..a6e61e9 100644 --- a/errors.h +++ b/errors.h @@ -6,7 +6,10 @@ typedef enum { MH_E_UNKNOWN_CODE = -1, MH_E_TOO_SHORT = -2, MH_E_TOO_LONG = -3, - MH_E_LEN_NOT_SUPPORTED = -4 + MH_E_LEN_NOT_SUPPORTED = -4, + + // Should be always last, if you are adding error update this code. + MH_E_LAST = -5 } mh_error; const char *mh_error_string(mh_error code); diff --git a/test/minunit.h b/test/minunit.h new file mode 100644 index 0000000..cc3d602 --- /dev/null +++ b/test/minunit.h @@ -0,0 +1,5 @@ +/* vim: set ts=8 sw=8 noexpandtab: */ +#define mu_assert(message, test) do { if (!(test)) return message; } while (0) +#define mu_run_test(test) do { char *message = test(); tests_run++; \ + if (message) return message; } while (0) +extern int tests_run; diff --git a/test/test_error_string.c b/test/test_error_string.c new file mode 100644 index 0000000..95a164a --- /dev/null +++ b/test/test_error_string.c @@ -0,0 +1,38 @@ +#include +#include +#include "minunit.h" + +#include "../errors.h" + +int tests_run = 0; +char error_buf[256]; + + +static char *test_error_messages_exist(void) { + int i = -1; + for (; i > MH_E_LAST; i--) { + sprintf(error_buf, "error: code %d has no message", i); + mu_assert(error_buf, strlen(mh_error_string((mh_error) i))); + } + + return 0; +} + +static char *all_tests(void) { + mu_run_test(test_error_messages_exist); + return 0; +} + +int main(void) { + char *result = all_tests(); + if (result != 0) { + printf("%s\n", result); + } + else { + printf("ALL TESTS PASSED\n"); + } + printf("Tests run: %d\n", tests_run); + + return result != 0; +} +