From 5cc1d13c366149208e489cc36e0cc92dc815de4f Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 21 Jun 2021 12:55:16 +0200 Subject: [PATCH] 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; +} + +