1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-11-03 20:54:07 +01:00
trexio/tests/io_dset_int_hdf5.c

141 lines
3.3 KiB
C
Raw Normal View History

2021-06-21 12:55:16 +02:00
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_dset_i.h5"
2021-06-21 12:55:16 +02:00
#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] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
2021-06-21 12:55:16 +02:00
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
2021-06-21 12:55:16 +02:00
assert (file != NULL);
// write numerical attribute in an empty file
2021-10-21 15:52:12 +02:00
rc = trexio_write_basis_shell_num(file, num);
2021-06-21 12:55:16 +02:00
assert (rc == TREXIO_SUCCESS);
2022-07-04 11:23:47 +02:00
2021-06-21 12:55:16 +02:00
// 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 ==================*/
2022-07-04 11:23:47 +02:00
// open file
file = trexio_open(file_name, 'r', backend, &rc);
2021-06-21 12:55:16 +02:00
assert (file != NULL);
2022-07-04 11:23:47 +02:00
// check that the group exists
rc = trexio_has_basis(file);
assert(rc==TREXIO_SUCCESS);
// check that the group does not exist
rc = trexio_has_mo(file);
assert(rc==TREXIO_HAS_NOT);
2021-06-21 12:55:16 +02:00
// 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, &rc);
2021-06-21 12:55:16 +02:00
assert (file != NULL);
// read numerical attribute from the file
2021-10-21 15:52:12 +02:00
rc = trexio_read_basis_shell_num(file, &num);
2021-06-21 12:55:16 +02:00
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-1);
2021-06-21 12:55:16 +02:00
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);
2021-06-21 12:55:16 +02:00
assert (rc == 0);
return 0;
}