add tests to build system

This commit is contained in:
Jakub Sztandera 2016-07-25 16:18:34 +01:00
parent bad1a6308f
commit 60fa67b590
4 changed files with 74 additions and 13 deletions

View file

@ -7,36 +7,51 @@ CFLAGS = -fPIC -g -O2 -std=c99 \
-Wold-style-definition \ -Wold-style-definition \
-Woverflow \ -Woverflow \
-Wpointer-arith \ -Wpointer-arith \
-Wstrict-prototypes \
-Wunused \ -Wunused \
-Wvla -Wvla
LDFLAGS = -g -shared LDFLAGS = -g
LDLIBS = LDLIBS =
TARGET_LIB = mulithash.so TARGET_LIB = mulithash.so
SRCS = errors.c SRCS = errors.c
OBJS = $(subst .c,.o,$(SRCS)) OBJS = $(SRCS:.c=.o)
all: ${TARGET_LIB} all: ${TARGET_LIB}
$(TARGET_LIB): $(OBJS) $(TARGET_LIB): $(OBJS)
$(CC) ${LDFLAGS} -o $@ $^ $(CC) -g -shared -o $@ $^
depend: .depend # Tests
.depend: $(SRCS) TEST_SRCS = $(wildcard test/test_*.c)
rm -f ./.depend TEST_OBJS = $(TEST_SRCS:.c=.o)
$(CC) $(CFLAGS) -MM $^>>./.depend; 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: clean:
$(RM) $(OBJS) $(RM) $(OBJS)
$(RM) $(TEST_OBJS)
$(RM) $(TEST_BINS)
$(RM) $(TARGET_LIB) $(RM) $(TARGET_LIB)
dist-clean: clean dist-clean: clean
$(RM) *~ .depend
.PHONY: all depend clean dist-clean .PHONY: $(PHONY)
include .depend

View file

@ -6,7 +6,10 @@ typedef enum {
MH_E_UNKNOWN_CODE = -1, MH_E_UNKNOWN_CODE = -1,
MH_E_TOO_SHORT = -2, MH_E_TOO_SHORT = -2,
MH_E_TOO_LONG = -3, 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; } mh_error;
const char *mh_error_string(mh_error code); const char *mh_error_string(mh_error code);

5
test/minunit.h Normal file
View file

@ -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;

38
test/test_error_string.c Normal file
View file

@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#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;
}