diff --git a/Makefile.am b/Makefile.am index b6b385e..fa14e41 100644 --- a/Makefile.am +++ b/Makefile.am @@ -103,6 +103,7 @@ TESTS_C += \ tests/io_num_hdf5 \ tests/io_dset_float_hdf5 \ tests/io_dset_int_hdf5 \ + tests/io_dset_sparse_hdf5 \ tests/io_safe_dset_float_hdf5 \ tests/io_str_hdf5 \ tests/io_dset_str_hdf5 \ @@ -136,6 +137,7 @@ tests_open_hdf5_LDFLAGS = -no-install tests_io_num_hdf5_LDFLAGS = -no-install tests_io_dset_float_hdf5_LDFLAGS = -no-install tests_io_dset_int_hdf5_LDFLAGS = -no-install +tests_io_dset_sparse_hdf5_LDFLAGS = -no-install tests_io_safe_dset_float_hdf5_LDFLAGS = -no-install tests_io_str_hdf5_LDFLAGS = -no-install tests_io_dset_str_hdf5_LDFLAGS = -no-install diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b4269e6..94fe414 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -20,6 +20,7 @@ if(ENABLE_HDF5) open_hdf5 io_dset_float_hdf5 io_dset_str_hdf5 + io_dset_sparse_hdf5 io_safe_dset_float_hdf5 io_dset_int_hdf5 io_num_hdf5 diff --git a/tests/io_dset_sparse_hdf5.c b/tests/io_dset_sparse_hdf5.c new file mode 100644 index 0000000..5758a4d --- /dev/null +++ b/tests/io_dset_sparse_hdf5.c @@ -0,0 +1,211 @@ +#include "trexio.h" +#include +#include +#include +#include + +#define TEST_BACKEND TREXIO_HDF5 +#define TREXIO_FILE "test_dset_sparse.h5" +#define RM_COMMAND "rm -f -- " TREXIO_FILE +#define SIZE 100 +#define N_CHUNKS 5 + +static int test_write_dset_sparse (const char* file_name, const back_end_t backend) { + +/* Try to write an array of sparse data into the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend, &rc); + assert (file != NULL); + assert (rc == TREXIO_SUCCESS); + + // parameters to be written + int32_t* index; + double* value; + + index = calloc(4L*SIZE, sizeof(int32_t)); + value = calloc(SIZE, sizeof(double)); + + for(int i=0; i size_max) + offset_file_read = 97L; + offset_data_read = 1; + + // read one chunk that will reach EOF and return TREXIO_END code + rc = trexio_read_mo_2e_int_eri(file, offset_file_read, chunk_read, &index_read[4*offset_data_read], &value_read[offset_data_read]); + assert(rc == TREXIO_END); + assert(index_read[4*size_r-1] == 0); + assert(index_read[4*offset_data_read] == 4 * (int32_t) offset_file_read); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + + // free the memory + free(index_read); + free(value_read); + +/*================= END OF TEST ==================*/ + + return 0; +} + +static int test_read_dset_sparse_size (const char* file_name, const back_end_t backend, const int64_t size_check) { + +/* Try to read a size of the dataset of sparse data in the TREXIO file */ + + trexio_t* file = NULL; + trexio_exit_code rc; + +/*================= START OF TEST ==================*/ + + // open file + file = trexio_open(file_name, 'r', backend, &rc); + assert (file != NULL); + assert (rc == TREXIO_SUCCESS); + + // define the variable to read into + int64_t size_written; + + // read one chunk using the aforementioned parameters + rc = trexio_read_mo_2e_int_eri_size(file, &size_written); + assert(rc == TREXIO_SUCCESS); + assert(size_written == size_check); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + +int main(){ + +/*============== Test launcher ================*/ + + int rc; + rc = system(RM_COMMAND); + assert (rc == 0); + + // check the first write attempt (SIZE elements written in N_CHUNKS chunks) + test_write_dset_sparse (TREXIO_FILE, TEST_BACKEND); + test_has_dset_sparse (TREXIO_FILE, TEST_BACKEND); + //test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND); + //test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE); + + // check the second write attempt (SIZE elements written in N_CHUNKS chunks) + //test_write_dset_sparse (TREXIO_FILE, TEST_BACKEND); + //test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE*2); + + //rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +}