star-meteo

Time varying meteorological data
git clone git://git.meso-star.fr/star-meteo.git
Log | Files | Refs | README | LICENSE

commit b2282979d6ecb713bc9d40c62ffed5e48fb95bfd
parent 4c47a3f94b833519d51ea4a340b9b6894c57ce60
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 14 May 2025 11:59:03 +0200

Test the library handle API

Add automation of test construction and verification to the Makefile.

Diffstat:
MMakefile | 57++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Asrc/test_smeteo.c | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 126 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -23,7 +23,7 @@ LIBNAME_SHARED = libsmeteo.so LIBNAME = $(LIBNAME_$(LIB_TYPE)) default: library -all: default #tests +all: default tests ################################################################################ # Library @@ -109,3 +109,58 @@ lint: clean: rm -f $(DEP) $(OBJ) $(LIBNAME) rm -f .config libsmeteo.o smeteo.pc smeteo-local.pc + +################################################################################ +# Tests +################################################################################ +TEST_SRC = src/test_smeteo.c +TEST_OBJ = $(TEST_SRC:.c=.o) +TEST_DEP = $(TEST_SRC:.c=.d) +TEST_BIN = $(TEST_SRC:.c=.b) + +PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) +INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys smeteo-local.pc) +LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys smeteo-local.pc) + +CFLAGS_TEST = $(CFLAGS_EXE) $(INCS_TEST) +LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) + +tests: library $(TEST_DEP) $(TEST_BIN) + @$(MAKE) -fMakefile \ + $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) \ + $$(for i in $(TEST_BIN); do echo -f"$${i}"; done) \ + test_bin + +.c.b: + @{ \ + bin="$$(basename "$<" ".c")"; \ + printf '%s: %s\n' "$${bin}" $(<:.c=.o); \ + printf 'test_bin: %s\n' "$${bin}"; \ + } > $@ + +$(TEST_DEP): config.mk smeteo-local.pc + @$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + +$(TEST_OBJ): config.mk smeteo-local.pc + $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@ + +test_smeteo \ +: config.mk smeteo-local.pc $(LIBNAME) + $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) + +clean_test: + rm -f $(TEST_BIN) $(TEST_DEP) $(TEST_OBJ) + for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done + +test: tests + @err=0; \ + for i in $(TEST_SRC); do \ + test="$$(basename "$${i}" ".c")"; \ + if "./$${test}" > /dev/null 2>&1; then \ + printf '%s\n' "$${test}"; \ + else \ + >&2 printf '%s: error %s\n' "$${test}" "$$?"; \ + err=$$((err+1)); \ + fi \ + done; \ + [ "$${err}" -eq 0 ] diff --git a/src/test_smeteo.c b/src/test_smeteo.c @@ -0,0 +1,70 @@ +/* Copyright (C) 2025 |Méso|Star> (contact@meso-star.com) + * + * This program is free software: you can redismeteobute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is dismeteobuted in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "smeteo.h" + +#include <rsys/logger.h> + +static void +log_stream(const char* msg, void* ctx) +{ + ASSERT(msg); + (void)msg, (void)ctx; + printf("%s\n", msg); +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct logger logger; + struct smeteo_create_args args = SMETEO_CREATE_ARGS_DEFAULT; + struct smeteo* smeteo; + (void)argc, (void)argv; + + CHK(smeteo_create(NULL, &smeteo) == RES_BAD_ARG); + CHK(smeteo_create(&args, NULL) == RES_BAD_ARG); + CHK(smeteo_create(&args, &smeteo) == RES_OK); + + CHK(smeteo_ref_get(NULL) == RES_BAD_ARG); + CHK(smeteo_ref_get(smeteo) == RES_OK); + CHK(smeteo_ref_put(NULL) == RES_BAD_ARG); + CHK(smeteo_ref_put(smeteo) == RES_OK); + CHK(smeteo_ref_put(smeteo) == RES_OK); + + CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); + args.allocator = &allocator; + args.verbose = 1; + CHK(smeteo_create(&args, &smeteo) == RES_OK); + CHK(smeteo_ref_put(smeteo) == RES_OK); + + CHK(logger_init(&allocator, &logger) == RES_OK); + logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); + logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); + logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); + + args.logger = &logger; + args.verbose = 0; + CHK(smeteo_create(&args, &smeteo) == RES_OK); + CHK(smeteo_ref_put(smeteo) == RES_OK); + args.allocator = NULL; + CHK(smeteo_create(&args, &smeteo) == RES_OK); + CHK(smeteo_ref_put(smeteo) == RES_OK); + + logger_release(&logger); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +}