From fd424c21494e8775ef1a5341392c7b098dee4a60 Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 17 Jun 2021 17:20:01 +0200 Subject: [PATCH] [WIP] unit testing for C API --- Makefile.am | 39 +++++++++++++++++++--------- tests/{test.c => io_all.c} | 0 tests/io_num_hdf5.c | 52 ++++++++++++++++++++++++++++++++++++++ tests/io_num_text.c | 52 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 12 deletions(-) rename tests/{test.c => io_all.c} (100%) create mode 100644 tests/io_num_hdf5.c create mode 100644 tests/io_num_text.c diff --git a/Makefile.am b/Makefile.am index 2560b5a..0231002 100644 --- a/Makefile.am +++ b/Makefile.am @@ -72,26 +72,37 @@ ORG_FILES = \ src_libtrexio_la_SOURCES = $(SOURCES) -TESTS = tests/test_c tests/test_f +# section related to tests + +TESTS_C = \ + tests/io_all \ + tests/io_num_hdf5 \ + tests/io_num_text + +TESTS_F = \ + tests/test_f + +TESTS_ALL = $(TESTS_C) $(TESTS_F) + +TESTS = $(TESTS_ALL) check_PROGRAMS = $(TESTS) -tests_test_c_DEPENDENCIES = $(trexio_h) -tests_test_c_SOURCES = tests/test.c $(trexio_h) -tests_test_c_LDADD = src/libtrexio.la -tests_test_c_LDFLAGS = -no-install +# specify common options for all tests +LDADD = src/libtrexio.la + +# in principal, specifying -no-install (see example below) for each test provides better +# and faster (?) `make check` but becomes tedious as the number of tests increases + +#tests_io_all_LDFLAGS = -no-install test_trexio_f = $(srcdir)/tests/trexio_f.f90 -tests_test_f_DEPENDENCIES = $(test_trexio_f) -tests_test_f_SOURCES = $(test_trexio_f) tests/test_f.f90 -tests_test_f_LDADD = src/libtrexio.la -tests_test_f_LDFLAGS = -no-install - $(test_trexio_f): $(trexio_f) cp $(trexio_f) $(test_trexio_f) -clean-local: - -rm -rf -- test_write.dir/ test_write_f.dir/ test_write.h5 test_write_f.h5 + +tests_test_f_SOURCES = $(test_trexio_f) tests/test_f.f90 + if TREXIO_DEVEL @@ -118,3 +129,7 @@ cppcheck.out: $(trexio_h) .PHONY: cppcheck endif + +clean-local: + -rm -rf -- *.dir/ *.h5 + diff --git a/tests/test.c b/tests/io_all.c similarity index 100% rename from tests/test.c rename to tests/io_all.c diff --git a/tests/io_num_hdf5.c b/tests/io_num_hdf5.c new file mode 100644 index 0000000..312a0e3 --- /dev/null +++ b/tests/io_num_hdf5.c @@ -0,0 +1,52 @@ +#include "trexio.h" +#include +#include +#include + +static int test_write_num (const char* file_name, const back_end_t backend) { + +/* Try to write dimensioning attribute (num variable) into the HDF5 file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + + // write numerical attribute in an empty file + rc = trexio_write_nucleus_num(file,num); + assert (rc == TREXIO_SUCCESS); + + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; + +} + +int main(void) { + +/*============== Test launcher ================*/ + + int rc; + rc = system("rm -rf test_write_num.h5"); + assert (rc == 0); + test_write_num("test_write_num.h5", TREXIO_HDF5); + rc = system("rm -rf test_write_num.h5"); + assert (rc == 0); + + return 0; +} + + diff --git a/tests/io_num_text.c b/tests/io_num_text.c new file mode 100644 index 0000000..e265eca --- /dev/null +++ b/tests/io_num_text.c @@ -0,0 +1,52 @@ +#include "trexio.h" +#include +#include +#include + +static int test_write_num (const char* file_name, const back_end_t backend) { + +/* Try to write dimensioning attribute (num variable) into the TEXT file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + + // write numerical attribute in an empty file + rc = trexio_write_nucleus_num(file,num); + assert (rc == TREXIO_SUCCESS); + + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; + +} + +int main(void) { + +/*============== Test launcher ================*/ + + int rc; + rc = system("rm -rf test_write_num.dir"); + assert (rc == 0); + test_write_num("test_write_num.dir", TREXIO_TEXT); + rc = system("rm -rf test_write_num.dir"); + assert (rc == 0); + + return 0; +} + +