1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-22 12:23:54 +01:00

Added test

This commit is contained in:
Anthony Scemama 2022-03-31 13:53:57 +02:00
parent 8262b97104
commit 15c8e10184
4 changed files with 129 additions and 2 deletions

View File

@ -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 += \

View File

@ -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) {

View File

@ -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.

124
tests/pre_close.c Normal file
View File

@ -0,0 +1,124 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#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;
}