diff --git a/.gitignore b/.gitignore index 2056d7b..ad39a62 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/ libs/ +tests/ mgos_i2c diff --git a/Makefile b/Makefile index a4a1f8b..6edfd0d 100644 --- a/Makefile +++ b/Makefile @@ -14,10 +14,11 @@ SRCDIR = src INCDIR = include OBJDIR = build LIBDIR = libs +TESTDIR = tests BINDIR = . -SRCS := $(shell find -L $(SRCDIR) $(LIBDIR) -type f -name '*.c' | egrep -v 'deps/|build/' ) -INCS := $(shell find -L $(SRCDIR) $(LIBDIR) $(INCDIR) -type d -name 'include'| egrep -v 'deps/|build/' ) +SRCS := $(shell find -L $(SRCDIR) $(LIBDIR) $(TESTDIR) -type f -name '*.c' | egrep -v 'deps/|build/' ) +INCS := $(shell find -L $(SRCDIR) $(LIBDIR) $(TESTDIR) $(INCDIR) -type d -name 'include'| egrep -v 'deps/|build/' ) INCFLAGS := $(patsubst %,-I %, $(INCS)) OBJS := $(patsubst %.c, build/%.o, $(SRCS)) RM = rm -f diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..bbad0ac --- /dev/null +++ b/autogen.sh @@ -0,0 +1,97 @@ +#!/bin/sh + +OUT_SRC=src/tests_autogen.c +OUT_INC=include/tests_autogen.h +DATE=$(date) + +# --- + +gen_src_head() { + cat > $OUT_SRC << EOF +// Generated on $DATE by $USER +// +#include "mgos.h" +#include "$(basename $OUT_INC)" + +EOF +} + +gen_src_body() { + echo "GEN $OUT_SRC" + ( + for func in create run destroy; do + echo "bool tests_${func}(void) {" + echo " uint32_t total=0, success=0;" + for n in $*; do + cat << EOF + if (!test_${n}_${func}()) { + LOG(LL_ERROR, ("Could not $func test $n")); + } else { + success++; + } + total++; + +EOF + done + cat << EOF + return (total == success); +} + +EOF + done + ) >> $OUT_SRC +} + +gen_src_tail() { + cat >> $OUT_SRC << EOF +EOF +} + +gen_inc_head() { + cat > $OUT_INC << EOF +// Generated on $DATE by $USER +// +#pragma once +#include "mgos.h" + +// Master test prototypes (called from main.c) +bool tests_create(void); +bool tests_run(void); +bool tests_destroy(void); + +EOF +} + +gen_inc_body() { + echo "GEN $OUT_INC" + for n in $*; do + cat >> $OUT_INC << EOF +// Test suite "$n" +bool test_${n}_create(void); +bool test_${n}_run(void); +bool test_${n}_destroy(void); + +EOF + done +} + +gen_inc_tail() { + cat >> $OUT_INC << EOF +EOF +} + +gen_inc_head +gen_src_head + +for f in tests/*.c; do + base=$(basename $f | cut -f1 -d'.') + echo "CHK $base in $f" + base_list="$base_list $base" +done + +gen_inc_body $base_list +gen_src_body $base_list + +gen_inc_tail +gen_src_tail + diff --git a/include/.gitignore b/include/.gitignore new file mode 100644 index 0000000..1feeebe --- /dev/null +++ b/include/.gitignore @@ -0,0 +1 @@ +tests_autogen.h diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..137325d --- /dev/null +++ b/src/.gitignore @@ -0,0 +1 @@ +tests_autogen.c diff --git a/src/main.c b/src/main.c index e72a679..d71422c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include "mgos.h" +#include "tests_autogen.h" #include #include #include @@ -59,6 +60,10 @@ int main(int argc, char **argv, char **environ) { i2c_scanner(i2c, true); + tests_create(); + tests_run(); + tests_destroy(); + return 0; (void)argc; (void)argv;