From fd424c21494e8775ef1a5341392c7b098dee4a60 Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 17 Jun 2021 17:20:01 +0200 Subject: [PATCH 01/12] [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; +} + + From dfa2604820cd12200e1a445ab66ccdc82846870f Mon Sep 17 00:00:00 2001 From: q-posev Date: Fri, 18 Jun 2021 12:53:00 +0200 Subject: [PATCH 02/12] complete num attribute testing --- tests/io_num_hdf5.c | 83 ++++++++++++++++++++++++++++++++++++++++----- tests/io_num_text.c | 83 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 150 insertions(+), 16 deletions(-) diff --git a/tests/io_num_hdf5.c b/tests/io_num_hdf5.c index 312a0e3..4e04f1a 100644 --- a/tests/io_num_hdf5.c +++ b/tests/io_num_hdf5.c @@ -3,9 +3,13 @@ #include #include +#define TEST_BACKEND TREXIO_HDF5 +#define TREXIO_FILE "test_num.h5" +#define RM_COMMAND "rm -rf " TREXIO_FILE + 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 */ +/* Try to write a dimensioning attribute (num variable) into the TREXIO file */ trexio_t* file = NULL; trexio_exit_code rc; @@ -19,12 +23,10 @@ static int test_write_num (const char* file_name, const back_end_t backend) { file = trexio_open(file_name, 'w', backend); assert (file != NULL); - // write numerical attribute in an empty file - rc = trexio_write_nucleus_num(file,num); + rc = trexio_write_nucleus_num(file, num); assert (rc == TREXIO_SUCCESS); - // close current session rc = trexio_close(file); assert (rc == TREXIO_SUCCESS); @@ -32,18 +34,83 @@ static int test_write_num (const char* file_name, const back_end_t backend) { /*================= END OF TEST ==================*/ return 0; - } + +static int test_has_num (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a dimensioning attribute (num variable) in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written num variable exists + rc = trexio_has_nucleus_num(file); + assert (rc == TREXIO_SUCCESS); + + // check that the num variable does not exist + rc = trexio_has_mo_num(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_num (const char* file_name, const back_end_t backend) { + +/* Try to read a dimensioning attribute (num variable) from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + int num; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // read numerical attribute from the file + rc = trexio_read_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"); + rc = system(RM_COMMAND); assert (rc == 0); - test_write_num("test_write_num.h5", TREXIO_HDF5); - rc = system("rm -rf test_write_num.h5"); + + test_write_num (TREXIO_FILE, TEST_BACKEND); + test_has_num (TREXIO_FILE, TEST_BACKEND); + test_read_num (TREXIO_FILE, TEST_BACKEND); + + rc = system(RM_COMMAND); assert (rc == 0); return 0; diff --git a/tests/io_num_text.c b/tests/io_num_text.c index e265eca..24ef9fe 100644 --- a/tests/io_num_text.c +++ b/tests/io_num_text.c @@ -3,9 +3,13 @@ #include #include +#define TEST_BACKEND TREXIO_TEXT +#define TREXIO_FILE "test_num.dir" +#define RM_COMMAND "rm -rf " TREXIO_FILE + 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 */ +/* Try to write a dimensioning attribute (num variable) into the TREXIO file */ trexio_t* file = NULL; trexio_exit_code rc; @@ -19,12 +23,10 @@ static int test_write_num (const char* file_name, const back_end_t backend) { file = trexio_open(file_name, 'w', backend); assert (file != NULL); - // write numerical attribute in an empty file - rc = trexio_write_nucleus_num(file,num); + rc = trexio_write_nucleus_num(file, num); assert (rc == TREXIO_SUCCESS); - // close current session rc = trexio_close(file); assert (rc == TREXIO_SUCCESS); @@ -32,18 +34,83 @@ static int test_write_num (const char* file_name, const back_end_t backend) { /*================= END OF TEST ==================*/ return 0; - } + +static int test_has_num (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a dimensioning attribute (num variable) in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written num variable exists + rc = trexio_has_nucleus_num(file); + assert (rc == TREXIO_SUCCESS); + + // check that the num variable does not exist + rc = trexio_has_mo_num(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_num (const char* file_name, const back_end_t backend) { + +/* Try to read a dimensioning attribute (num variable) from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + int num; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // read numerical attribute from the file + rc = trexio_read_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"); + rc = system(RM_COMMAND); assert (rc == 0); - test_write_num("test_write_num.dir", TREXIO_TEXT); - rc = system("rm -rf test_write_num.dir"); + + test_write_num (TREXIO_FILE, TEST_BACKEND); + test_has_num (TREXIO_FILE, TEST_BACKEND); + test_read_num (TREXIO_FILE, TEST_BACKEND); + + rc = system(RM_COMMAND); assert (rc == 0); return 0; From d4f0b731a5e17a6d2e4a6ec7a6ca50486576ed35 Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 21 Jun 2021 09:28:37 +0200 Subject: [PATCH 03/12] add assert to read --- tests/io_num_hdf5.c | 1 + tests/io_num_text.c | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/io_num_hdf5.c b/tests/io_num_hdf5.c index 4e04f1a..a16acc7 100644 --- a/tests/io_num_hdf5.c +++ b/tests/io_num_hdf5.c @@ -87,6 +87,7 @@ static int test_read_num (const char* file_name, const back_end_t backend) { // read numerical attribute from the file rc = trexio_read_nucleus_num(file, &num); assert (rc == TREXIO_SUCCESS); + assert (num == 12); // close current session rc = trexio_close(file); diff --git a/tests/io_num_text.c b/tests/io_num_text.c index 24ef9fe..a75fda2 100644 --- a/tests/io_num_text.c +++ b/tests/io_num_text.c @@ -87,6 +87,7 @@ static int test_read_num (const char* file_name, const back_end_t backend) { // read numerical attribute from the file rc = trexio_read_nucleus_num(file, &num); assert (rc == TREXIO_SUCCESS); + assert (num == 12); // close current session rc = trexio_close(file); From 07d29453331955c8f64b4a1b117dedfe246074e8 Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 21 Jun 2021 12:47:39 +0200 Subject: [PATCH 04/12] rename TREXIO_NUM_ALREADY_EXISTS return code --- src/templates_front/templator_front.org | 64 ++++++++++++------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 6602c47..c40c92f 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -120,33 +120,33 @@ typedef int32_t trexio_exit_code; ** Error handling #+NAME: table-exit-codes - | Macro | Code | Description | - |------------------------------+------+----------------------------------------| - | ~TREXIO_FAILURE~ | -1 | 'Unknown failure' | - | ~TREXIO_SUCCESS~ | 0 | 'Success' | - | ~TREXIO_INVALID_ARG_1~ | 1 | 'Invalid argument 1' | - | ~TREXIO_INVALID_ARG_2~ | 2 | 'Invalid argument 2' | - | ~TREXIO_INVALID_ARG_3~ | 3 | 'Invalid argument 3' | - | ~TREXIO_INVALID_ARG_4~ | 4 | 'Invalid argument 4' | - | ~TREXIO_INVALID_ARG_5~ | 5 | 'Invalid argument 5' | - | ~TREXIO_END~ | 6 | 'End of file' | - | ~TREXIO_READONLY~ | 7 | 'Read-only file' | - | ~TREXIO_ERRNO~ | 8 | strerror(errno) | - | ~TREXIO_INVALID_ID~ | 9 | 'Invalid ID' | - | ~TREXIO_ALLOCATION_FAILED~ | 10 | 'Allocation failed' | - | ~TREXIO_HAS_NOT~ | 11 | 'Element absent' | - | ~TREXIO_INVALID_NUM~ | 12 | 'Invalid dimensions' | - | ~TREXIO_NUM_ALREADY_EXISTS~ | 13 | 'Dimensioning variable already exists' | - | ~TREXIO_DSET_ALREADY_EXISTS~ | 14 | 'Dataset already exists' | - | ~TREXIO_OPEN_ERROR~ | 15 | 'Error opening file' | - | ~TREXIO_LOCK_ERROR~ | 16 | 'Error locking file' | - | ~TREXIO_UNLOCK_ERROR~ | 17 | 'Error unlocking file' | - | ~TREXIO_FILE_ERROR~ | 18 | 'Invalid file handle' | - | ~TREXIO_GROUP_READ_ERROR~ | 19 | 'Error reading group' | - | ~TREXIO_GROUP_WRITE_ERROR~ | 20 | 'Error writing group' | - | ~TREXIO_ELEM_READ_ERROR~ | 21 | 'Error reading element' | - | ~TREXIO_ELEM_WRITE_ERROR~ | 22 | 'Error writing element' | - | ~TREXIO_INVALID_STR_LEN~ | 30 | 'Invalid max_str_len' | + | Macro | Code | Description | + |------------------------------+------+--------------------------------------| + | ~TREXIO_FAILURE~ | -1 | 'Unknown failure' | + | ~TREXIO_SUCCESS~ | 0 | 'Success' | + | ~TREXIO_INVALID_ARG_1~ | 1 | 'Invalid argument 1' | + | ~TREXIO_INVALID_ARG_2~ | 2 | 'Invalid argument 2' | + | ~TREXIO_INVALID_ARG_3~ | 3 | 'Invalid argument 3' | + | ~TREXIO_INVALID_ARG_4~ | 4 | 'Invalid argument 4' | + | ~TREXIO_INVALID_ARG_5~ | 5 | 'Invalid argument 5' | + | ~TREXIO_END~ | 6 | 'End of file' | + | ~TREXIO_READONLY~ | 7 | 'Read-only file' | + | ~TREXIO_ERRNO~ | 8 | strerror(errno) | + | ~TREXIO_INVALID_ID~ | 9 | 'Invalid ID' | + | ~TREXIO_ALLOCATION_FAILED~ | 10 | 'Allocation failed' | + | ~TREXIO_HAS_NOT~ | 11 | 'Element absent' | + | ~TREXIO_INVALID_NUM~ | 12 | 'Invalid dimensions' | + | ~TREXIO_ATTR_ALREADY_EXISTS~ | 13 | 'Attribute (num/str) already exists' | + | ~TREXIO_DSET_ALREADY_EXISTS~ | 14 | 'Dataset already exists' | + | ~TREXIO_OPEN_ERROR~ | 15 | 'Error opening file' | + | ~TREXIO_LOCK_ERROR~ | 16 | 'Error locking file' | + | ~TREXIO_UNLOCK_ERROR~ | 17 | 'Error unlocking file' | + | ~TREXIO_FILE_ERROR~ | 18 | 'Invalid file handle' | + | ~TREXIO_GROUP_READ_ERROR~ | 19 | 'Error reading group' | + | ~TREXIO_GROUP_WRITE_ERROR~ | 20 | 'Error writing group' | + | ~TREXIO_ELEM_READ_ERROR~ | 21 | 'Error reading element' | + | ~TREXIO_ELEM_WRITE_ERROR~ | 22 | 'Error writing element' | + | ~TREXIO_INVALID_STR_LEN~ | 30 | 'Invalid max_str_len' | # We need to force Emacs not to indent the Python code: # -*- org-src-preserve-indentation: t @@ -192,7 +192,7 @@ return '\n'.join(result) #define TREXIO_ALLOCATION_FAILED ((trexio_exit_code) 10) #define TREXIO_HAS_NOT ((trexio_exit_code) 11) #define TREXIO_INVALID_NUM ((trexio_exit_code) 12) - #define TREXIO_NUM_ALREADY_EXISTS ((trexio_exit_code) 13) + #define TREXIO_ATTR_ALREADY_EXISTS ((trexio_exit_code) 13) #define TREXIO_DSET_ALREADY_EXISTS ((trexio_exit_code) 14) #define TREXIO_OPEN_ERROR ((trexio_exit_code) 15) #define TREXIO_LOCK_ERROR ((trexio_exit_code) 16) @@ -220,7 +220,7 @@ return '\n'.join(result) integer(trexio_exit_code), parameter :: TREXIO_ALLOCATION_FAILED = 10 integer(trexio_exit_code), parameter :: TREXIO_HAS_NOT = 11 integer(trexio_exit_code), parameter :: TREXIO_INVALID_NUM = 12 - integer(trexio_exit_code), parameter :: TREXIO_NUM_ALREADY_EXISTS = 13 + integer(trexio_exit_code), parameter :: TREXIO_ATTR_ALREADY_EXISTS = 13 integer(trexio_exit_code), parameter :: TREXIO_DSET_ALREADY_EXISTS = 14 integer(trexio_exit_code), parameter :: TREXIO_OPEN_ERROR = 15 integer(trexio_exit_code), parameter :: TREXIO_LOCK_ERROR = 16 @@ -860,7 +860,7 @@ trexio_write_$group_num$_64 (trexio_t* const file, const int64_t num) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (num < 0 ) return TREXIO_INVALID_ARG_2; - if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_NUM_ALREADY_EXISTS; + if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS; switch (file->back_end) { @@ -921,7 +921,7 @@ trexio_write_$group_num$_32 (trexio_t* const file, const int32_t num) if (file == NULL) return TREXIO_INVALID_ARG_1; if (num < 0 ) return TREXIO_INVALID_ARG_2; - if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_NUM_ALREADY_EXISTS; + if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS; switch (file->back_end) { @@ -1979,7 +1979,7 @@ trexio_write_$group_str$ (trexio_t* const file, const char* str, const uint32_t if (file == NULL) return TREXIO_INVALID_ARG_1; if (str == NULL) return TREXIO_INVALID_ARG_2; if (max_str_len <= 0) return TREXIO_INVALID_ARG_3; - if (trexio_has_$group_str$(file) == TREXIO_SUCCESS) return TREXIO_NUM_ALREADY_EXISTS; + if (trexio_has_$group_str$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS; size_t len_write = strlen(str); if (max_str_len < len_write) return TREXIO_INVALID_STR_LEN; From c8fd3ba045612ed18b7cdc45e5f3cb75f18530c0 Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 21 Jun 2021 12:48:32 +0200 Subject: [PATCH 05/12] remove redundant check of overwriting --- src/templates_hdf5/templator_hdf5.org | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/templates_hdf5/templator_hdf5.org b/src/templates_hdf5/templator_hdf5.org index 83146e8..0b67c67 100644 --- a/src/templates_hdf5/templator_hdf5.org +++ b/src/templates_hdf5/templator_hdf5.org @@ -233,25 +233,16 @@ trexio_hdf5_write_$group_num$ (trexio_t* const file, const uint64_t num) trexio_exit_code rc = trexio_hdf5_read_$group_num$(file, &(infile_num)); if (rc != TREXIO_SUCCESS) return rc; - if (infile_num != num) { + const hid_t dtype = H5Tcopy(H5T_NATIVE_UINT64); + const hid_t num_id = H5Aopen(f->$group$_group, $GROUP_NUM$_NAME, H5P_DEFAULT); + if (num_id <= 0) return TREXIO_INVALID_ID; + + const herr_t status = H5Awrite(num_id, dtype, &(num)); + if (status < 0) return TREXIO_FAILURE; + + H5Aclose(num_id); + H5Tclose(dtype); - if (infile_num != 0) { - - return TREXIO_NUM_ALREADY_EXISTS; - - } else { - - const hid_t dtype = H5Tcopy(H5T_NATIVE_UINT64); - const hid_t num_id = H5Aopen(f->$group$_group, $GROUP_NUM$_NAME, H5P_DEFAULT); - if (num_id <= 0) return TREXIO_INVALID_ID; - - const herr_t status = H5Awrite(num_id, dtype, &(num)); - if (status < 0) return TREXIO_FAILURE; - - H5Aclose(num_id); - H5Tclose(dtype); - } - } return TREXIO_SUCCESS; } From 91319ec3e8538e96c841ced6b67de5290e1a43b8 Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 21 Jun 2021 12:49:59 +0200 Subject: [PATCH 06/12] ignore produced binary and .o test files --- tests/.gitignore | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/.gitignore b/tests/.gitignore index 9a43619..2de809e 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,14 +1,15 @@ -libtrexio.so -trexio_f.o +# Ignore all +* +# Unignore all with extensions +!*.* + trexio_f.f90 trexio.mod -test_c -test_f - +*.o *.log *.trs +.dirstamp *.h5 */ - From c010331114e843771a6ceacd95b30ba527c31f46 Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 21 Jun 2021 12:51:21 +0200 Subject: [PATCH 07/12] fix names and typos --- tests/io_all.c | 20 ++++++++++---------- tests/io_num_hdf5.c | 8 ++++---- tests/io_num_text.c | 8 ++++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/io_all.c b/tests/io_all.c index 92d90dd..4f4709c 100644 --- a/tests/io_all.c +++ b/tests/io_all.c @@ -12,18 +12,18 @@ int main() { /*============== Main test launcher ================*/ int rc; - rc = system("rm -rf test_write.h5"); + rc = system("rm -rf test_all.h5"); assert (rc == 0); - test_write("test_write.h5", TREXIO_HDF5); - test_read ("test_write.h5", TREXIO_HDF5); - rc = system("rm -rf test_write.h5"); + test_write("test_all.h5", TREXIO_HDF5); + test_read ("test_all.h5", TREXIO_HDF5); + rc = system("rm -rf test_all.h5"); assert (rc == 0); - rc = system("rm -rf test_write.dir"); + rc = system("rm -rf test_all.dir"); assert (rc == 0); - test_write("test_write.dir", TREXIO_TEXT); - test_read ("test_write.dir", TREXIO_TEXT); - rc = system("rm -rf test_write.dir"); + test_write("test_all.dir", TREXIO_TEXT); + test_read ("test_all.dir", TREXIO_TEXT); + rc = system("rm -rf test_all.dir"); assert (rc == 0); return 0; @@ -68,7 +68,7 @@ int test_write(const char* file_name, const back_end_t backend) { "H" , "H" }; - const char* sym = "B3U and some stuff"; + const char* sym = "B3U with some comments"; /*================= START OF TEST ==================*/ // open file in 'write' mode @@ -113,7 +113,7 @@ int test_write(const char* file_name, const back_end_t backend) { // should not work: try to overwrite the num variable rc = trexio_write_nucleus_num(file,25); printf("Test error message: %s\n", trexio_string_of_error(rc)); - assert (rc == TREXIO_NUM_ALREADY_EXISTS); + assert (rc == TREXIO_ATTR_ALREADY_EXISTS); // should not work: try to overwrite the dset coord[0] = 666.666; diff --git a/tests/io_num_hdf5.c b/tests/io_num_hdf5.c index a16acc7..7eaf77b 100644 --- a/tests/io_num_hdf5.c +++ b/tests/io_num_hdf5.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_HDF5 -#define TREXIO_FILE "test_num.h5" +#define TREXIO_FILE "test.h5" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_num (const char* file_name, const back_end_t backend) { @@ -46,7 +46,7 @@ static int test_has_num (const char* file_name, const back_end_t backend) { /*================= START OF TEST ==================*/ - // open file in 'write' mode + // open file file = trexio_open(file_name, 'r', backend); assert (file != NULL); @@ -80,8 +80,8 @@ static int test_read_num (const char* file_name, const back_end_t backend) { /*================= START OF TEST ==================*/ - // open file in 'write' mode - file = trexio_open(file_name, 'w', backend); + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); assert (file != NULL); // read numerical attribute from the file diff --git a/tests/io_num_text.c b/tests/io_num_text.c index a75fda2..74db023 100644 --- a/tests/io_num_text.c +++ b/tests/io_num_text.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_TEXT -#define TREXIO_FILE "test_num.dir" +#define TREXIO_FILE "test.dir" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_num (const char* file_name, const back_end_t backend) { @@ -46,7 +46,7 @@ static int test_has_num (const char* file_name, const back_end_t backend) { /*================= START OF TEST ==================*/ - // open file in 'write' mode + // open file file = trexio_open(file_name, 'r', backend); assert (file != NULL); @@ -80,8 +80,8 @@ static int test_read_num (const char* file_name, const back_end_t backend) { /*================= START OF TEST ==================*/ - // open file in 'write' mode - file = trexio_open(file_name, 'w', backend); + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); assert (file != NULL); // read numerical attribute from the file From 5cc1d13c366149208e489cc36e0cc92dc815de4f Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 21 Jun 2021 12:55:16 +0200 Subject: [PATCH 08/12] add C unit tests --- Makefile.am | 36 +++++++-- tests/io_dset_float_hdf5.c | 148 ++++++++++++++++++++++++++++++++++ tests/io_dset_float_text.c | 148 ++++++++++++++++++++++++++++++++++ tests/io_dset_int_hdf5.c | 134 +++++++++++++++++++++++++++++++ tests/io_dset_int_text.c | 134 +++++++++++++++++++++++++++++++ tests/io_dset_str_hdf5.c | 157 +++++++++++++++++++++++++++++++++++++ tests/io_dset_str_text.c | 157 +++++++++++++++++++++++++++++++++++++ tests/io_str_hdf5.c | 133 +++++++++++++++++++++++++++++++ tests/io_str_text.c | 133 +++++++++++++++++++++++++++++++ tests/overwrite_all_hdf5.c | 141 +++++++++++++++++++++++++++++++++ tests/overwrite_all_text.c | 141 +++++++++++++++++++++++++++++++++ 11 files changed, 1454 insertions(+), 8 deletions(-) create mode 100644 tests/io_dset_float_hdf5.c create mode 100644 tests/io_dset_float_text.c create mode 100644 tests/io_dset_int_hdf5.c create mode 100644 tests/io_dset_int_text.c create mode 100644 tests/io_dset_str_hdf5.c create mode 100644 tests/io_dset_str_text.c create mode 100644 tests/io_str_hdf5.c create mode 100644 tests/io_str_text.c create mode 100644 tests/overwrite_all_hdf5.c create mode 100644 tests/overwrite_all_text.c diff --git a/Makefile.am b/Makefile.am index 0231002..9b575c0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -75,9 +75,19 @@ src_libtrexio_la_SOURCES = $(SOURCES) # section related to tests TESTS_C = \ - tests/io_all \ tests/io_num_hdf5 \ - tests/io_num_text + tests/io_num_text \ + tests/io_dset_float_hdf5 \ + tests/io_dset_float_text \ + tests/io_dset_int_hdf5 \ + tests/io_dset_int_text \ + tests/io_str_hdf5 \ + tests/io_str_text \ + tests/io_dset_str_hdf5 \ + tests/io_dset_str_text \ + tests/overwrite_all_hdf5 \ + tests/overwrite_all_text \ + tests/io_all TESTS_F = \ tests/test_f @@ -89,19 +99,29 @@ check_PROGRAMS = $(TESTS) # 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 +# in principal, specifying -no-install (see example below) is not mandatory +# for the tests to compile and pass, but the compilations itself differs +tests_io_num_hdf5_LDFLAGS = -no-install +tests_io_num_text_LDFLAGS = -no-install +tests_io_dset_float_hdf5_LDFLAGS = -no-install +tests_io_dset_float_text_LDFLAGS = -no-install +tests_io_dset_int_hdf5_LDFLAGS = -no-install +tests_io_dset_int_text_LDFLAGS = -no-install +tests_io_str_hdf5_LDFLAGS = -no-install +tests_io_str_text_LDFLAGS = -no-install +tests_io_dset_str_hdf5_LDFLAGS = -no-install +tests_io_dset_str_text_LDFLAGS = -no-install +tests_overwrite_all_hdf5_LDFLAGS = -no-install +tests_overwrite_all_text_LDFLAGS = -no-install +tests_io_all_LDFLAGS = -no-install test_trexio_f = $(srcdir)/tests/trexio_f.f90 $(test_trexio_f): $(trexio_f) cp $(trexio_f) $(test_trexio_f) - tests_test_f_SOURCES = $(test_trexio_f) tests/test_f.f90 +tests_test_f_LDFLAGS = -no-install if TREXIO_DEVEL diff --git a/tests/io_dset_float_hdf5.c b/tests/io_dset_float_hdf5.c new file mode 100644 index 0000000..2a95e69 --- /dev/null +++ b/tests/io_dset_float_hdf5.c @@ -0,0 +1,148 @@ +#include "trexio.h" +#include +#include +#include + +#define TEST_BACKEND TREXIO_HDF5 +#define TREXIO_FILE "test.h5" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write_dset (const char* file_name, const back_end_t backend) { + +/* Try to write a dataset with numerical (floating point) values into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + double coord[36] = { + 0.00000000 , 1.39250319 , 0.00000000 , + -1.20594314 , 0.69625160 , 0.00000000 , + -1.20594314 , -0.69625160 , 0.00000000 , + 0.00000000 , -1.39250319 , 0.00000000 , + 1.20594314 , -0.69625160 , 0.00000000 , + 1.20594314 , 0.69625160 , 0.00000000 , + -2.14171677 , 1.23652075 , 0.00000000 , + -2.14171677 , -1.23652075 , 0.00000000 , + 0.00000000 , -2.47304151 , 0.00000000 , + 2.14171677 , -1.23652075 , 0.00000000 , + 2.14171677 , 1.23652075 , 0.00000000 , + 0.00000000 , 2.47304151 , 0.00000000 , + }; + +/*================= 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); + + // write numerical dataset in a file + rc = trexio_write_nucleus_coord(file, coord); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_has_dset (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a dataset in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written dataset exists + rc = trexio_has_nucleus_coord(file); + assert (rc == TREXIO_SUCCESS); + + // check that another dataset does not exist + rc = trexio_has_mo_coefficient(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_dset (const char* file_name, const back_end_t backend) { + +/* Try to read a dataset with numerical (floating point) values from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + int num; + double* coord; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // read numerical attribute from the file + rc = trexio_read_nucleus_num(file, &num); + assert (rc == TREXIO_SUCCESS); + assert (num == 12); + + // read numerical (floating point) dataset from the file + coord = (double*) calloc(3*num, sizeof(double)); + rc = trexio_read_nucleus_coord(file, coord); + assert (rc == TREXIO_SUCCESS); + + double x = coord[30] - 2.14171677; + assert( x*x < 1.e-14 ); + free(coord); + + // 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_COMMAND); + assert (rc == 0); + + test_write_dset (TREXIO_FILE, TEST_BACKEND); + test_has_dset (TREXIO_FILE, TEST_BACKEND); + test_read_dset (TREXIO_FILE, TEST_BACKEND); + + rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + diff --git a/tests/io_dset_float_text.c b/tests/io_dset_float_text.c new file mode 100644 index 0000000..aaf83b9 --- /dev/null +++ b/tests/io_dset_float_text.c @@ -0,0 +1,148 @@ +#include "trexio.h" +#include +#include +#include + +#define TEST_BACKEND TREXIO_TEXT +#define TREXIO_FILE "test.dir" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write_dset (const char* file_name, const back_end_t backend) { + +/* Try to write a dataset with numerical (floating point) values into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + double coord[36] = { + 0.00000000 , 1.39250319 , 0.00000000 , + -1.20594314 , 0.69625160 , 0.00000000 , + -1.20594314 , -0.69625160 , 0.00000000 , + 0.00000000 , -1.39250319 , 0.00000000 , + 1.20594314 , -0.69625160 , 0.00000000 , + 1.20594314 , 0.69625160 , 0.00000000 , + -2.14171677 , 1.23652075 , 0.00000000 , + -2.14171677 , -1.23652075 , 0.00000000 , + 0.00000000 , -2.47304151 , 0.00000000 , + 2.14171677 , -1.23652075 , 0.00000000 , + 2.14171677 , 1.23652075 , 0.00000000 , + 0.00000000 , 2.47304151 , 0.00000000 , + }; + +/*================= 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); + + // write numerical dataset in a file + rc = trexio_write_nucleus_coord(file, coord); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_has_dset (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a dataset in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written dataset exists + rc = trexio_has_nucleus_coord(file); + assert (rc == TREXIO_SUCCESS); + + // check that another dataset does not exist + rc = trexio_has_mo_coefficient(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_dset (const char* file_name, const back_end_t backend) { + +/* Try to read a dataset with numerical (floating point) values from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + int num; + double* coord; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // read numerical attribute from the file + rc = trexio_read_nucleus_num(file, &num); + assert (rc == TREXIO_SUCCESS); + assert (num == 12); + + // read numerical (floating point) dataset from the file + coord = (double*) calloc(3*num, sizeof(double)); + rc = trexio_read_nucleus_coord(file, coord); + assert (rc == TREXIO_SUCCESS); + + double x = coord[30] - 2.14171677; + assert( x*x < 1.e-14 ); + free(coord); + + // 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_COMMAND); + assert (rc == 0); + + test_write_dset (TREXIO_FILE, TEST_BACKEND); + test_has_dset (TREXIO_FILE, TEST_BACKEND); + test_read_dset (TREXIO_FILE, TEST_BACKEND); + + rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + diff --git a/tests/io_dset_int_hdf5.c b/tests/io_dset_int_hdf5.c new file mode 100644 index 0000000..51135e1 --- /dev/null +++ b/tests/io_dset_int_hdf5.c @@ -0,0 +1,134 @@ +#include "trexio.h" +#include +#include +#include + +#define TEST_BACKEND TREXIO_HDF5 +#define TREXIO_FILE "test.h5" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write_dset (const char* file_name, const back_end_t backend) { + +/* Try to write a dataset with numerical (int) values into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + int nucl_index[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 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); + + // write numerical (integer) dataset in a file + rc = trexio_write_basis_nucleus_index(file, nucl_index); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_has_dset (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a dataset in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written dataset exists + rc = trexio_has_basis_nucleus_index(file); + assert (rc == TREXIO_SUCCESS); + + // check that another dataset does not exist + rc = trexio_has_mo_coefficient(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_dset (const char* file_name, const back_end_t backend) { + +/* Try to read a dataset with numericali (int) values from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + int num; + int* nucl_index; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // read numerical attribute from the file + rc = trexio_read_nucleus_num(file, &num); + assert (rc == TREXIO_SUCCESS); + assert (num == 12); + + // read numerical dataset from the file + nucl_index = (int*) calloc(num, sizeof(int)); + rc = trexio_read_basis_nucleus_index(file, nucl_index); + assert (rc == TREXIO_SUCCESS); + assert ( nucl_index[num-1] == num ); + + free(nucl_index); + + // 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_COMMAND); + assert (rc == 0); + + test_write_dset (TREXIO_FILE, TEST_BACKEND); + test_has_dset (TREXIO_FILE, TEST_BACKEND); + test_read_dset (TREXIO_FILE, TEST_BACKEND); + + //rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + diff --git a/tests/io_dset_int_text.c b/tests/io_dset_int_text.c new file mode 100644 index 0000000..ad8c9a7 --- /dev/null +++ b/tests/io_dset_int_text.c @@ -0,0 +1,134 @@ +#include "trexio.h" +#include +#include +#include + +#define TEST_BACKEND TREXIO_TEXT +#define TREXIO_FILE "test.dir" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write_dset (const char* file_name, const back_end_t backend) { + +/* Try to write a dataset with numerical (int) values into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + int nucl_index[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 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); + + // write numerical (integer) dataset in a file + rc = trexio_write_basis_nucleus_index(file, nucl_index); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_has_dset (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a dataset in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written dataset exists + rc = trexio_has_basis_nucleus_index(file); + assert (rc == TREXIO_SUCCESS); + + // check that another dataset does not exist + rc = trexio_has_mo_coefficient(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_dset (const char* file_name, const back_end_t backend) { + +/* Try to read a dataset with numericali (int) values from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + int num; + int* nucl_index; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // read numerical attribute from the file + rc = trexio_read_nucleus_num(file, &num); + assert (rc == TREXIO_SUCCESS); + assert (num == 12); + + // read numerical dataset from the file + nucl_index = (int*) calloc(num, sizeof(int)); + rc = trexio_read_basis_nucleus_index(file, nucl_index); + assert (rc == TREXIO_SUCCESS); + assert ( nucl_index[num-1] == num ); + + free(nucl_index); + + // 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_COMMAND); + assert (rc == 0); + + test_write_dset (TREXIO_FILE, TEST_BACKEND); + test_has_dset (TREXIO_FILE, TEST_BACKEND); + test_read_dset (TREXIO_FILE, TEST_BACKEND); + + //rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + diff --git a/tests/io_dset_str_hdf5.c b/tests/io_dset_str_hdf5.c new file mode 100644 index 0000000..7d6f1f6 --- /dev/null +++ b/tests/io_dset_str_hdf5.c @@ -0,0 +1,157 @@ +#include "trexio.h" +#include +#include +#include +#include + +#define TEST_BACKEND TREXIO_HDF5 +#define TREXIO_FILE "test.h5" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write_dset_str (const char* file_name, const back_end_t backend) { + +/* Try to write an array of strings into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + const char* labels[] = {"C" , + "Na FAKE" , + "C" , + "C" , + "C" , + "C" , + "H" , + "H" , + "H" , + "H" , + "H" , + "H FAKE" }; + +/*================= 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); + + // write dataset of string in the file (including FAKE statements) + int max_str_len = 16; + rc = trexio_write_nucleus_label(file, labels, max_str_len); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_has_dset_str (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a dataset of strings in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written dataset of strings exists + rc = trexio_has_nucleus_label(file); + assert (rc == TREXIO_SUCCESS); + + // check that the dataset of strings does not exist + rc = trexio_has_mo_symmetry(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_dset_str (const char* file_name, const back_end_t backend) { + +/* Try to read a dataset with strings from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + int num; + char** labels; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // read numerical attribute from the file + rc = trexio_read_nucleus_num(file, &num); + assert (rc == TREXIO_SUCCESS); + assert (num == 12); + + // read the arrays of strings truncated to max_str_len=2 symbols + int max_str_len = 2; + + labels = (char**) malloc(num*sizeof(char*)); + for (int i=0; i +#include +#include +#include + +#define TEST_BACKEND TREXIO_TEXT +#define TREXIO_FILE "test.dir" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write_dset_str (const char* file_name, const back_end_t backend) { + +/* Try to write an array of strings into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + const char* labels[] = {"C" , + "Na FAKE" , + "C" , + "C" , + "C" , + "C" , + "H" , + "H" , + "H" , + "H" , + "H" , + "H FAKE" }; + +/*================= 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); + + // write dataset of string in the file (including FAKE statements) + int max_str_len = 16; + rc = trexio_write_nucleus_label(file, labels, max_str_len); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_has_dset_str (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a dataset of strings in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written dataset of strings exists + rc = trexio_has_nucleus_label(file); + assert (rc == TREXIO_SUCCESS); + + // check that the dataset of strings does not exist + rc = trexio_has_mo_symmetry(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_dset_str (const char* file_name, const back_end_t backend) { + +/* Try to read a dataset with strings from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + int num; + char **labels; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // read numerical attribute from the file + rc = trexio_read_nucleus_num(file, &num); + assert (rc == TREXIO_SUCCESS); + assert (num == 12); + + // read the arrays of strings truncated to max_str_len=2 symbols + int max_str_len = 2; + + labels = (char**) malloc(num*sizeof(char*)); + for (int i=0; i +#include +#include +#include + +#define TEST_BACKEND TREXIO_HDF5 +#define TREXIO_FILE "test.h5" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write_str (const char* file_name, const back_end_t backend) { + +/* Try to write a string attribute (single variable-length string) into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + const char* sym = "B3U with some comments"; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // write string attribute in an empty file + int max_str_len = 32; + rc = trexio_write_nucleus_point_group(file, sym, max_str_len); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_has_str (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a string attribute in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written string attribute exists + rc = trexio_has_nucleus_point_group(file); + assert (rc == TREXIO_SUCCESS); + + // check that another string attribute does not exist + rc = trexio_has_mo_type(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_str (const char* file_name, const back_end_t backend) { + +/* Try to read a string attribute from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + char* sym; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // read string attribute from the file + int max_str_len = 32; + sym = (char*) malloc(max_str_len*sizeof(char)); + + rc = trexio_read_nucleus_point_group(file, sym, max_str_len); + assert (rc == TREXIO_SUCCESS); + + char * pch; + pch = strtok(sym, " "); + assert (strcmp(pch, "B3U") == 0); + /* alternative test when 3 symbols are read from the file to sym */ + /*rc = trexio_read_nucleus_point_group(file, sym, 3); + assert (rc == TREXIO_SUCCESS); + assert (strcmp(sym, "B3U") == 0 );*/ + free(sym); + + // 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_COMMAND); + assert (rc == 0); + + test_write_str (TREXIO_FILE, TEST_BACKEND); + test_has_str (TREXIO_FILE, TEST_BACKEND); + test_read_str (TREXIO_FILE, TEST_BACKEND); + + rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + diff --git a/tests/io_str_text.c b/tests/io_str_text.c new file mode 100644 index 0000000..b87cfcf --- /dev/null +++ b/tests/io_str_text.c @@ -0,0 +1,133 @@ +#include "trexio.h" +#include +#include +#include +#include + +#define TEST_BACKEND TREXIO_TEXT +#define TREXIO_FILE "test.dir" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write_str (const char* file_name, const back_end_t backend) { + +/* Try to write a string attribute (single variable-length string) into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + const char* sym = "B3U with some comments"; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // write string attribute in an empty file + int max_str_len = 32; + rc = trexio_write_nucleus_point_group(file, sym, max_str_len); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_has_str (const char* file_name, const back_end_t backend) { + +/* Try to check the existence of a string attribute in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // check that the previously written string attribute exists + rc = trexio_has_nucleus_point_group(file); + assert (rc == TREXIO_SUCCESS); + + // check that another string attribute does not exist + rc = trexio_has_mo_type(file); + assert (rc == TREXIO_HAS_NOT); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_read_str (const char* file_name, const back_end_t backend) { + +/* Try to read a string attribute from the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be read + char* sym; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // read string attribute from the file + int max_str_len = 32; + sym = (char*) malloc(max_str_len*sizeof(char)); + + rc = trexio_read_nucleus_point_group(file, sym, max_str_len); + assert (rc == TREXIO_SUCCESS); + + char * pch; + pch = strtok(sym, " "); + assert (strcmp(pch, "B3U") == 0); + /* alternative test when 3 symbols are read from the file to sym */ + /*rc = trexio_read_nucleus_point_group(file, sym, 3); + assert (rc == TREXIO_SUCCESS); + assert (strcmp(sym, "B3U") == 0 );*/ + free(sym); + + // 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_COMMAND); + assert (rc == 0); + + test_write_str (TREXIO_FILE, TEST_BACKEND); + test_has_str (TREXIO_FILE, TEST_BACKEND); + test_read_str (TREXIO_FILE, TEST_BACKEND); + + rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + diff --git a/tests/overwrite_all_hdf5.c b/tests/overwrite_all_hdf5.c new file mode 100644 index 0000000..33aaede --- /dev/null +++ b/tests/overwrite_all_hdf5.c @@ -0,0 +1,141 @@ +#include "trexio.h" +#include +#include +#include + +#define TEST_BACKEND TREXIO_HDF5 +#define TREXIO_FILE "test.h5" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write (const char* file_name, const back_end_t backend) { + +/* Try to write a full set of data (num+dset_num+str+dset_str) related to benzene molecule into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + double coord[36] = { + 0.00000000 , 1.39250319 , 0.00000000 , + -1.20594314 , 0.69625160 , 0.00000000 , + -1.20594314 , -0.69625160 , 0.00000000 , + 0.00000000 , -1.39250319 , 0.00000000 , + 1.20594314 , -0.69625160 , 0.00000000 , + 1.20594314 , 0.69625160 , 0.00000000 , + -2.14171677 , 1.23652075 , 0.00000000 , + -2.14171677 , -1.23652075 , 0.00000000 , + 0.00000000 , -2.47304151 , 0.00000000 , + 2.14171677 , -1.23652075 , 0.00000000 , + 2.14171677 , 1.23652075 , 0.00000000 , + 0.00000000 , 2.47304151 , 0.00000000 , + }; + const char* sym = "D6h"; + const char* labels[] = {"C" , + "C" , + "C" , + "C" , + "C" , + "C" , + "H" , + "H" , + "H" , + "H" , + "H" , + "H" }; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // write the data + rc = trexio_write_nucleus_num(file, num); + assert (rc == TREXIO_SUCCESS); + + rc = trexio_write_nucleus_coord(file, coord); + assert (rc == TREXIO_SUCCESS); + + rc = trexio_write_nucleus_point_group(file, sym, 4); + assert (rc == TREXIO_SUCCESS); + + rc = trexio_write_nucleus_label(file, labels, 2); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_overwrite (const char* file_name, const back_end_t backend) { + +/* Try to overwrite the data that already exists in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 24; + double coord[3] = { + 0.00000000 , 666.666, 0.00000000 , + }; + const char* sym = "Unknown"; + const char* labels[] = {"Ru" , + "U" , + "Cl" , + "Na" , + "H" }; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // check that the previously written data cannot be overwritten + rc = trexio_write_nucleus_num(file, num); + assert (rc == TREXIO_ATTR_ALREADY_EXISTS); + + rc = trexio_write_nucleus_coord(file, coord); + assert (rc == TREXIO_DSET_ALREADY_EXISTS); + + rc = trexio_write_nucleus_point_group(file, sym, 16); + assert (rc == TREXIO_ATTR_ALREADY_EXISTS); + + rc = trexio_write_nucleus_label(file, labels, 4); + assert (rc == TREXIO_DSET_ALREADY_EXISTS); + + // 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_COMMAND); + assert (rc == 0); + + test_write (TREXIO_FILE, TEST_BACKEND); + test_overwrite (TREXIO_FILE, TEST_BACKEND); + + rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + diff --git a/tests/overwrite_all_text.c b/tests/overwrite_all_text.c new file mode 100644 index 0000000..ca26e55 --- /dev/null +++ b/tests/overwrite_all_text.c @@ -0,0 +1,141 @@ +#include "trexio.h" +#include +#include +#include + +#define TEST_BACKEND TREXIO_TEXT +#define TREXIO_FILE "test.dir" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_write (const char* file_name, const back_end_t backend) { + +/* Try to write a full set of data (num+dset_num+str+dset_str) related to benzene molecule into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 12; + double coord[36] = { + 0.00000000 , 1.39250319 , 0.00000000 , + -1.20594314 , 0.69625160 , 0.00000000 , + -1.20594314 , -0.69625160 , 0.00000000 , + 0.00000000 , -1.39250319 , 0.00000000 , + 1.20594314 , -0.69625160 , 0.00000000 , + 1.20594314 , 0.69625160 , 0.00000000 , + -2.14171677 , 1.23652075 , 0.00000000 , + -2.14171677 , -1.23652075 , 0.00000000 , + 0.00000000 , -2.47304151 , 0.00000000 , + 2.14171677 , -1.23652075 , 0.00000000 , + 2.14171677 , 1.23652075 , 0.00000000 , + 0.00000000 , 2.47304151 , 0.00000000 , + }; + const char* sym = "D6h"; + const char* labels[] = {"C" , + "C" , + "C" , + "C" , + "C" , + "C" , + "H" , + "H" , + "H" , + "H" , + "H" , + "H" }; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // write the data + rc = trexio_write_nucleus_num(file, num); + assert (rc == TREXIO_SUCCESS); + + rc = trexio_write_nucleus_coord(file, coord); + assert (rc == TREXIO_SUCCESS); + + rc = trexio_write_nucleus_point_group(file, sym, 4); + assert (rc == TREXIO_SUCCESS); + + rc = trexio_write_nucleus_label(file, labels, 2); + assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_overwrite (const char* file_name, const back_end_t backend) { + +/* Try to overwrite the data that already exists in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + + // parameters to be written + int num = 24; + double coord[3] = { + 0.00000000 , 666.666, 0.00000000 , + }; + const char* sym = "Unknown"; + const char* labels[] = {"Ru" , + "U" , + "Cl" , + "Na" , + "H" }; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // check that the previously written data cannot be overwritten + rc = trexio_write_nucleus_num(file, num); + assert (rc == TREXIO_ATTR_ALREADY_EXISTS); + + rc = trexio_write_nucleus_coord(file, coord); + assert (rc == TREXIO_DSET_ALREADY_EXISTS); + + rc = trexio_write_nucleus_point_group(file, sym, 16); + assert (rc == TREXIO_ATTR_ALREADY_EXISTS); + + rc = trexio_write_nucleus_label(file, labels, 4); + assert (rc == TREXIO_DSET_ALREADY_EXISTS); + + // 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_COMMAND); + assert (rc == 0); + + test_write (TREXIO_FILE, TEST_BACKEND); + test_overwrite (TREXIO_FILE, TEST_BACKEND); + + rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + From 2ba109a404aa7bf56bfeb0a304e914aec46d2f66 Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 22 Jun 2021 15:59:35 +0200 Subject: [PATCH 09/12] add tests for open/close --- tests/open_hdf5.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++ tests/open_text.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 tests/open_hdf5.c create mode 100644 tests/open_text.c diff --git a/tests/open_hdf5.c b/tests/open_hdf5.c new file mode 100644 index 0000000..4049903 --- /dev/null +++ b/tests/open_hdf5.c @@ -0,0 +1,95 @@ +#include "trexio.h" +#include +#include +#include + +#define TEST_BACKEND TREXIO_HDF5 +#define TREXIO_FILE "test.h5" +#define TREXIO_VOID "non_existing_" TREXIO_FILE +#define RM_COMMAND "rm -rf " TREXIO_FILE + + +static int test_open_w (const char* file_name, const back_end_t backend) { + +/* Try to open the TREXIO file in 'write' mode */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_open_r (const char* file_name, const back_end_t backend) { + +/* Try to open the TREXIO file in 'read' mode */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_open_void (const char* file_name, const back_end_t backend) { + +/* Try to open the non-existing TREXIO file in 'read' mode */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file == NULL); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +int main(void) { + +/*============== Test launcher ================*/ + + int rc; + rc = system(RM_COMMAND); + assert (rc == 0); + + test_open_w (TREXIO_FILE, TEST_BACKEND); + test_open_r (TREXIO_FILE, TEST_BACKEND); + test_open_void (TREXIO_VOID, TEST_BACKEND); + + rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + diff --git a/tests/open_text.c b/tests/open_text.c new file mode 100644 index 0000000..5a8e89a --- /dev/null +++ b/tests/open_text.c @@ -0,0 +1,95 @@ +#include "trexio.h" +#include +#include +#include + +#define TEST_BACKEND TREXIO_TEXT +#define TREXIO_FILE "test.dir" +#define TREXIO_VOID "non_existing_" TREXIO_FILE +#define RM_COMMAND "rm -rf " TREXIO_FILE + + +static int test_open_w (const char* file_name, const back_end_t backend) { + +/* Try to open the TREXIO file in 'write' mode */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_open_r (const char* file_name, const back_end_t backend) { + +/* Try to open the TREXIO file in 'read' mode */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'r', backend); + assert (file != NULL); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +static int test_open_void (const char* file_name, const back_end_t backend) { + +/* Try to open the non-existing TREXIO file in 'read' mode */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'read' mode + file = trexio_open(file_name, 'r', backend); + assert (file == NULL); + +/*================= END OF TEST ==================*/ + + return 0; +} + + +int main(void) { + +/*============== Test launcher ================*/ + + int rc; + rc = system(RM_COMMAND); + assert (rc == 0); + + test_open_w (TREXIO_FILE, TEST_BACKEND); + test_open_r (TREXIO_FILE, TEST_BACKEND); + test_open_void (TREXIO_VOID, TEST_BACKEND); + + rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +} + + From d637f9b7389a40bf2652e9f3b7f693cb5827351f Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 22 Jun 2021 16:04:25 +0200 Subject: [PATCH 10/12] add sections --- Makefile.am | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/Makefile.am b/Makefile.am index 40fa98f..be6c09f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -30,6 +30,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# =============== SETTINGS =============== # + ACLOCAL_AMFLAGS = -I m4 CLEANFILES = trexio.mod @@ -42,6 +44,7 @@ SUBDIRS = pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = pkgconfig/trexio.pc +# =============== BUILD =============== # trexio_h = $(srcdir)/include/trexio.h trexio_f = $(srcdir)/include/trexio_f.f90 @@ -54,14 +57,14 @@ lib_LTLIBRARIES = src/libtrexio.la SOURCES = \ - $(trexio_h) \ - src/trexio.c \ - src/trexio_private.h \ - src/trexio_s.h \ - src/trexio_hdf5.c \ - src/trexio_hdf5.h \ - src/trexio_text.c \ - src/trexio_text.h + $(trexio_h) \ + src/trexio.c \ + src/trexio_private.h \ + src/trexio_s.h \ + src/trexio_hdf5.c \ + src/trexio_hdf5.h \ + src/trexio_text.c \ + src/trexio_text.h ORG_FILES = \ src/templates_front/templator_front.org \ @@ -72,9 +75,11 @@ ORG_FILES = \ src_libtrexio_la_SOURCES = $(SOURCES) -# section related to tests +# =============== TESTS =============== # TESTS_C = \ + tests/open_hdf5 \ + tests/open_text \ tests/io_num_hdf5 \ tests/io_num_text \ tests/io_dset_float_hdf5 \ @@ -92,16 +97,16 @@ TESTS_C = \ TESTS_F = \ tests/test_f -TESTS_ALL = $(TESTS_C) $(TESTS_F) - -TESTS = $(TESTS_ALL) +TESTS = $(TESTS_C) $(TESTS_F) check_PROGRAMS = $(TESTS) # specify common options for all tests LDADD = src/libtrexio.la -# in principal, specifying -no-install (see example below) is not mandatory -# for the tests to compile and pass, but the compilations itself differs +# in principal, specifying -no-install (see below) is not mandatory +# for the tests to compile and pass, but the produced test binaries differ +tests_open_hdf5_LDFLAGS = -no-install +tests_open_text_LDFLAGS = -no-install tests_io_num_hdf5_LDFLAGS = -no-install tests_io_num_text_LDFLAGS = -no-install tests_io_dset_float_hdf5_LDFLAGS = -no-install @@ -124,6 +129,10 @@ $(test_trexio_f): $(trexio_f) tests_test_f_SOURCES = $(test_trexio_f) tests/test_f.f90 tests_test_f_LDFLAGS = -no-install +clean-local: + -rm -rf -- *.dir/ *.h5 + +# =============== DOCUMENTATION =============== # HTML_FILES = docs/trexio.css \ docs/index.html \ @@ -139,6 +148,7 @@ dist_html_DATA = $(HTML_FILES) $(HTML_FILES): docs/index.html +# =============== DEVELOPER MODE =============== # if TREXIO_DEVEL @@ -153,7 +163,7 @@ src/trexio.c: $(trexio_h) $(trexio_f): $(ORG_FILES) cd $(srcdir)/tools && ./build_trexio.sh -docs/index.html: $(SOURCES) $(srcdir)/src/README.org +docs/index.html: $(ORG_FILES) $(srcdir)/src/README.org cd $(srcdir)/tools && ./build_doc.sh cppcheck: cppcheck.out @@ -170,6 +180,3 @@ cppcheck.out: $(trexio_h) endif -clean-local: - -rm -rf -- *.dir/ *.h5 - From 7293bae0587d9e3018ef3b56a5c16ea2220ffd2c Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 22 Jun 2021 17:55:56 +0200 Subject: [PATCH 11/12] precompile TREXIO Fortran module to execute tests in parallel --- Makefile.am | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile.am b/Makefile.am index be6c09f..ed7067d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -34,6 +34,7 @@ ACLOCAL_AMFLAGS = -I m4 CLEANFILES = trexio.mod +BUILT_SOURCES = trexio.mod VERSION_MAJOR = @VERSION_MAJOR@ VERSION_MINOR = @VERSION_MINOR@ @@ -100,7 +101,7 @@ TESTS_F = \ TESTS = $(TESTS_C) $(TESTS_F) check_PROGRAMS = $(TESTS) -# specify common options for all tests +# specify common LDADD options for all tests LDADD = src/libtrexio.la # in principal, specifying -no-install (see below) is not mandatory @@ -126,6 +127,8 @@ test_trexio_f = $(srcdir)/tests/trexio_f.f90 $(test_trexio_f): $(trexio_f) cp $(trexio_f) $(test_trexio_f) +trexio.mod: tests/trexio_f.o + tests_test_f_SOURCES = $(test_trexio_f) tests/test_f.f90 tests_test_f_LDFLAGS = -no-install @@ -154,7 +157,7 @@ if TREXIO_DEVEL CLEANFILES += $(SOURCES) $(trexio_f) $(trexio_h) -BUILT_SOURCES = $(SOURCES) $(trexio_f) $(test_trexio_f) +BUILT_SOURCES += $(SOURCES) $(trexio_f) $(test_trexio_f) $(SOURCES): $(trexio_f) From 05eb291ed41ca512bb277668879a0cd7700dfa53 Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 22 Jun 2021 17:57:34 +0200 Subject: [PATCH 12/12] rename TREXIO files to execute tests in parallel --- tests/io_dset_float_hdf5.c | 2 +- tests/io_dset_float_text.c | 2 +- tests/io_dset_int_hdf5.c | 2 +- tests/io_dset_int_text.c | 4 ++-- tests/io_dset_str_hdf5.c | 2 +- tests/io_dset_str_text.c | 2 +- tests/io_num_hdf5.c | 2 +- tests/io_num_text.c | 2 +- tests/io_str_hdf5.c | 2 +- tests/io_str_text.c | 2 +- tests/open_hdf5.c | 2 +- tests/open_text.c | 2 +- tests/overwrite_all_hdf5.c | 2 +- tests/overwrite_all_text.c | 2 +- 14 files changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/io_dset_float_hdf5.c b/tests/io_dset_float_hdf5.c index 2a95e69..d645808 100644 --- a/tests/io_dset_float_hdf5.c +++ b/tests/io_dset_float_hdf5.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_HDF5 -#define TREXIO_FILE "test.h5" +#define TREXIO_FILE "test_dset_f.h5" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_dset (const char* file_name, const back_end_t backend) { diff --git a/tests/io_dset_float_text.c b/tests/io_dset_float_text.c index aaf83b9..4e21d25 100644 --- a/tests/io_dset_float_text.c +++ b/tests/io_dset_float_text.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_TEXT -#define TREXIO_FILE "test.dir" +#define TREXIO_FILE "test_dset_f.dir" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_dset (const char* file_name, const back_end_t backend) { diff --git a/tests/io_dset_int_hdf5.c b/tests/io_dset_int_hdf5.c index 51135e1..63d9db1 100644 --- a/tests/io_dset_int_hdf5.c +++ b/tests/io_dset_int_hdf5.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_HDF5 -#define TREXIO_FILE "test.h5" +#define TREXIO_FILE "test_dset_i.h5" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_dset (const char* file_name, const back_end_t backend) { diff --git a/tests/io_dset_int_text.c b/tests/io_dset_int_text.c index ad8c9a7..60f5558 100644 --- a/tests/io_dset_int_text.c +++ b/tests/io_dset_int_text.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_TEXT -#define TREXIO_FILE "test.dir" +#define TREXIO_FILE "test_dset_i.dir" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_dset (const char* file_name, const back_end_t backend) { @@ -125,7 +125,7 @@ int main(void) { test_has_dset (TREXIO_FILE, TEST_BACKEND); test_read_dset (TREXIO_FILE, TEST_BACKEND); - //rc = system(RM_COMMAND); + rc = system(RM_COMMAND); assert (rc == 0); return 0; diff --git a/tests/io_dset_str_hdf5.c b/tests/io_dset_str_hdf5.c index 7d6f1f6..cb97f42 100644 --- a/tests/io_dset_str_hdf5.c +++ b/tests/io_dset_str_hdf5.c @@ -5,7 +5,7 @@ #include #define TEST_BACKEND TREXIO_HDF5 -#define TREXIO_FILE "test.h5" +#define TREXIO_FILE "test_dset_s.h5" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_dset_str (const char* file_name, const back_end_t backend) { diff --git a/tests/io_dset_str_text.c b/tests/io_dset_str_text.c index 34d0b3d..1d8e7dd 100644 --- a/tests/io_dset_str_text.c +++ b/tests/io_dset_str_text.c @@ -5,7 +5,7 @@ #include #define TEST_BACKEND TREXIO_TEXT -#define TREXIO_FILE "test.dir" +#define TREXIO_FILE "test_dset_s.dir" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_dset_str (const char* file_name, const back_end_t backend) { diff --git a/tests/io_num_hdf5.c b/tests/io_num_hdf5.c index 7eaf77b..2983a45 100644 --- a/tests/io_num_hdf5.c +++ b/tests/io_num_hdf5.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_HDF5 -#define TREXIO_FILE "test.h5" +#define TREXIO_FILE "test_num.h5" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_num (const char* file_name, const back_end_t backend) { diff --git a/tests/io_num_text.c b/tests/io_num_text.c index 74db023..f8907fb 100644 --- a/tests/io_num_text.c +++ b/tests/io_num_text.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_TEXT -#define TREXIO_FILE "test.dir" +#define TREXIO_FILE "test_num.dir" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_num (const char* file_name, const back_end_t backend) { diff --git a/tests/io_str_hdf5.c b/tests/io_str_hdf5.c index 78be976..251b5e3 100644 --- a/tests/io_str_hdf5.c +++ b/tests/io_str_hdf5.c @@ -5,7 +5,7 @@ #include #define TEST_BACKEND TREXIO_HDF5 -#define TREXIO_FILE "test.h5" +#define TREXIO_FILE "test_str.h5" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_str (const char* file_name, const back_end_t backend) { diff --git a/tests/io_str_text.c b/tests/io_str_text.c index b87cfcf..d74e7d2 100644 --- a/tests/io_str_text.c +++ b/tests/io_str_text.c @@ -5,7 +5,7 @@ #include #define TEST_BACKEND TREXIO_TEXT -#define TREXIO_FILE "test.dir" +#define TREXIO_FILE "test_str.dir" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write_str (const char* file_name, const back_end_t backend) { diff --git a/tests/open_hdf5.c b/tests/open_hdf5.c index 4049903..88ef17f 100644 --- a/tests/open_hdf5.c +++ b/tests/open_hdf5.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_HDF5 -#define TREXIO_FILE "test.h5" +#define TREXIO_FILE "test_open.h5" #define TREXIO_VOID "non_existing_" TREXIO_FILE #define RM_COMMAND "rm -rf " TREXIO_FILE diff --git a/tests/open_text.c b/tests/open_text.c index 5a8e89a..4b16311 100644 --- a/tests/open_text.c +++ b/tests/open_text.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_TEXT -#define TREXIO_FILE "test.dir" +#define TREXIO_FILE "test_open.dir" #define TREXIO_VOID "non_existing_" TREXIO_FILE #define RM_COMMAND "rm -rf " TREXIO_FILE diff --git a/tests/overwrite_all_hdf5.c b/tests/overwrite_all_hdf5.c index 33aaede..ae0bad7 100644 --- a/tests/overwrite_all_hdf5.c +++ b/tests/overwrite_all_hdf5.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_HDF5 -#define TREXIO_FILE "test.h5" +#define TREXIO_FILE "test_over.h5" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write (const char* file_name, const back_end_t backend) { diff --git a/tests/overwrite_all_text.c b/tests/overwrite_all_text.c index ca26e55..4e5ef42 100644 --- a/tests/overwrite_all_text.c +++ b/tests/overwrite_all_text.c @@ -4,7 +4,7 @@ #include #define TEST_BACKEND TREXIO_TEXT -#define TREXIO_FILE "test.dir" +#define TREXIO_FILE "test_over.dir" #define RM_COMMAND "rm -rf " TREXIO_FILE static int test_write (const char* file_name, const back_end_t backend) {