diff --git a/Makefile.am b/Makefile.am index 00622f2..3bd2623 100644 --- a/Makefile.am +++ b/Makefile.am @@ -117,7 +117,8 @@ TESTS_C = \ tests/io_dset_str_text \ tests/delete_group_text \ tests/overwrite_all_text \ - tests/io_all + tests/io_all \ + tests/pre_close if HAVE_HDF5 TESTS_C += \ diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 0e00be8..f060bfa 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -1422,7 +1422,7 @@ trexio_pre_close (trexio_t* file) rc = trexio_read_electron_dn_num(file, &ndn); if (rc != TREXIO_SUCCESS) return rc; - rc = trexio_read_electron_elec_num(file, &nelec); + rc = trexio_read_electron_num(file, &nelec); if (rc != TREXIO_SUCCESS) return rc; if (nelec != nup + ndn) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 26b3634..52f47a1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,6 +13,7 @@ set(Tests_text io_str_text delete_group_text overwrite_all_text + pre_close.c ) if(ENABLE_HDF5) @@ -28,6 +29,7 @@ if(ENABLE_HDF5) io_str_hdf5 delete_group_hdf5 overwrite_all_hdf5 + pre_close ) # Set ${Tests} variable to the complete list of tests. diff --git a/tests/pre_close.c b/tests/pre_close.c new file mode 100644 index 0000000..ec5edac --- /dev/null +++ b/tests/pre_close.c @@ -0,0 +1,124 @@ +#include "trexio.h" +#include +#include +#include +#include + +#define TEST_BACKEND TREXIO_TEXT +#define TREXIO_FILE "test_dset_sparse.dir" +#define RM_COMMAND "rm -rf " TREXIO_FILE + +static int test_pre_close_1 (const char* file_name, const back_end_t backend) +{ +/* Check if nelec = nup + ndn */ + + 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); + + // write parameters + int32_t nup = 4; + int32_t ndn = 3; + int32_t nelec = 0; + + rc = trexio_write_electron_up_num(file, nup); + assert (rc == TREXIO_SUCCESS); + + rc = trexio_write_electron_dn_num(file, ndn); + assert (rc == TREXIO_SUCCESS); + + // close file + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + + + // re-open file + file = trexio_open(file_name, 'r', backend, &rc); + assert (file != NULL); + assert (rc == TREXIO_SUCCESS); + + rc = trexio_read_electron_num(file, &nelec); + assert (rc == TREXIO_SUCCESS); + printf("nup : %d\n", nup); + printf("ndn : %d\n", ndn); + printf("nelec: %d\n", nelec); + assert (nelec == nup + ndn); + + // close file + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + +/*================= END OF TEST ==================*/ + + return 0; +} + +static int test_pre_close_2 (const char* file_name, const back_end_t backend) +{ +/* Check if nelec = nup */ + + 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); + + // write parameters + int32_t nup = 4; + int32_t nelec = 0; + + rc = trexio_write_electron_up_num(file, nup); + assert (rc == TREXIO_SUCCESS); + + // close file + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + + + // re-open file + file = trexio_open(file_name, 'r', backend, &rc); + assert (file != NULL); + assert (rc == TREXIO_SUCCESS); + + rc = trexio_read_electron_num(file, &nelec); + assert (rc == TREXIO_SUCCESS); + assert (nelec == nup); + + // close file + 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); + + test_pre_close_1 (TREXIO_FILE, TEST_BACKEND); + rc = system(RM_COMMAND); + + test_pre_close_2 (TREXIO_FILE, TEST_BACKEND); + + rc = system(RM_COMMAND); + assert (rc == 0); + + return 0; +}