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

Refactored tests

This commit is contained in:
Anthony Scemama 2023-05-02 14:45:51 +02:00
parent 5663487b8e
commit ffb87acc4a
39 changed files with 2255 additions and 4325 deletions

85
tests/delete_group.c Normal file
View File

@ -0,0 +1,85 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static int test_write_delete_group (const char* file_name, const back_end_t backend) {
/* Try to write a dimensioning attribute (num variable) 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, &rc);
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);
// write numerical attribute ao_cartesian as 0
rc = trexio_write_ao_cartesian(file, 0);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// open file in 'unsafe' mode
file = trexio_open(file_name, 'u', backend, &rc);
assert (file != NULL);
// delete a previously written group
rc = trexio_delete_nucleus(file);
assert (rc == TREXIO_SUCCESS);
// 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_delete_group (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,89 +1,4 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "delete_group"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "delete_group.c"
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_del.h5"
#define RM_COMMAND "rm -f -- " TREXIO_FILE
static int test_write_delete_group (const char* file_name, const back_end_t backend) {
/* Try to write a dimensioning attribute (num variable) 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, &rc);
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);
// write numerical attribute ao_cartesian as 0
rc = trexio_write_ao_cartesian(file, 0);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// open file in 'unsafe' mode
file = trexio_open(file_name, 'u', backend, &rc);
assert (file != NULL);
// delete a previously written group
rc = trexio_delete_nucleus(file);
assert (rc == TREXIO_SUCCESS);
// 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_delete_group (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,89 +1,4 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "delete_group"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "delete_group.c"
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_del.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " TREXIO_FILE
static int test_write_delete_group (const char* file_name, const back_end_t backend) {
/* Try to write a dimensioning attribute (num variable) 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, &rc);
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);
// write numerical attribute ao_cartesian as 0
rc = trexio_write_ao_cartesian(file, 0);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// open file in 'unsafe' mode
file = trexio_open(file_name, 'u', backend, &rc);
assert (file != NULL);
// delete a previously written group
rc = trexio_delete_nucleus(file);
assert (rc == TREXIO_SUCCESS);
// 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_delete_group (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

322
tests/io_determinant.c Normal file
View File

@ -0,0 +1,322 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define SIZE 100
#define N_CHUNKS 5
#define STATE_TEST 2
static int test_write_determinant (const char* file_name, const back_end_t backend, const int64_t offset, const int mo_num) {
/* 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
int64_t* det_list;
double* det_coef;
// write mo_num which will be used to determine the optimal size of int indices
if (trexio_has_mo_num(file) == TREXIO_HAS_NOT) {
rc = trexio_write_mo_num(file, mo_num);
assert(rc == TREXIO_SUCCESS);
}
// get the number of int64 bit fields per determinant
int int_num;
rc = trexio_get_int64_num(file, &int_num);
assert(rc == TREXIO_SUCCESS);
assert(int_num == (mo_num-1)/64 + 1);
// allocate memory and fill with values to be written
det_list = (int64_t*) calloc(2 * int_num * SIZE, sizeof(int64_t));
det_coef = (double*) calloc(SIZE, sizeof(double));
int64_t size_list = TREXIO_NORB_PER_INT * int_num;
const int32_t orb_list_up[4] = {0,1,2,3};
const int32_t orb_list_dn[3] = {0,1,2};
for(int i=0; i<SIZE; i++){
rc = trexio_to_bitfield_list (orb_list_up, 4, &(det_list[2*int_num*i]), int_num);
rc = trexio_to_bitfield_list (orb_list_dn, 3, &(det_list[2*int_num*i+int_num]), int_num);
det_coef[i] = 3.14 + (double) i;
}
// write dataset chunks of sparse data in the file (including FAKE statements)
uint64_t chunk_size = (uint64_t) SIZE/N_CHUNKS;
uint64_t offset_f = 0UL;
uint64_t offset_d = 0UL;
if (offset != 0L) offset_f += offset;
// write the state_id of a given file: 0 is ground state
if (trexio_has_state_id(file) == TREXIO_HAS_NOT) {
rc = trexio_write_state_id(file, STATE_TEST);
assert(rc == TREXIO_SUCCESS);
}
// write n_chunks times using write_sparse
for(int i=0; i<N_CHUNKS; ++i){
rc = trexio_write_determinant_list(file, offset_f, chunk_size, &det_list[2*int_num*offset_d]);
assert(rc == TREXIO_SUCCESS);
rc = trexio_write_determinant_coefficient(file, offset_f, chunk_size, &det_coef[offset_d]);
assert(rc == TREXIO_SUCCESS);
offset_d += chunk_size;
offset_f += chunk_size;
}
// manually check the consistency of the determinant_num and coefficient_size after writing
int64_t coeff_size = 0L;
int64_t determinant_num = 0L;
rc = trexio_read_determinant_num_64(file, &determinant_num);
assert(rc == TREXIO_SUCCESS);
rc = trexio_read_determinant_coefficient_size(file, &coeff_size);
assert(rc == TREXIO_SUCCESS);
assert(determinant_num == coeff_size);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the allocated memeory
free(det_list);
free(det_coef);
printf("write determinants OK\n");
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_determinant(const char* file_name, const back_end_t backend) {
/* Try to check the existence of a 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);
// now check that previously written determinant_list exists
rc = trexio_has_determinant_list(file);
assert(rc==TREXIO_SUCCESS);
rc = trexio_has_state_id(file);
assert(rc==TREXIO_SUCCESS);
// now check that previously written determinant_coefficient exists
rc = trexio_has_determinant_coefficient(file);
assert(rc==TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
printf("has_determinant OK\n");
return 0;
}
static int test_read_determinant (const char* file_name, const back_end_t backend, const int64_t offset) {
/* Try to read one chunk of 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);
// compute how many integer bit fields is needed per determinant (for a given spin)
int64_t mo_num;
rc = trexio_read_mo_num_64(file, &mo_num);
assert (rc == TREXIO_SUCCESS);
int int_num;
rc = trexio_get_int64_num(file, &int_num);
assert (rc == TREXIO_SUCCESS);
assert (int_num == (mo_num - 1)/64 + 1);
// define arrays to read into
int64_t* det_list_read;
double* det_coef_read;
double check_diff;
uint64_t size_r = 40L;
det_list_read = (int64_t*) calloc(2*int_num*size_r,sizeof(int64_t));
det_coef_read = (double*) calloc(size_r,sizeof(double));
// specify the read parameters, here:
// 1 chunk of 10 elements using offset of 40 (i.e. lines No. 40--59) into elements of the array starting from 5
int64_t chunk_read = 10L;
int64_t offset_file_read = 40L;
int offset_data_read = 5;
int64_t read_size_check;
read_size_check = chunk_read;
if (offset != 0L) offset_file_read += offset;
// read one chunk using the aforementioned parameters
printf("int_num: %d\n", int_num);
rc = trexio_read_determinant_list(file, offset_file_read, &chunk_read, &det_list_read[2*int_num*offset_data_read]);
assert(rc == TREXIO_SUCCESS);
assert(chunk_read == read_size_check);
assert(det_list_read[2*int_num*offset_data_read] == 15);
assert(det_list_read[2*int_num*offset_data_read+int_num] == 7);
rc = trexio_read_determinant_coefficient(file, offset_file_read, &chunk_read, &det_coef_read[offset_data_read]);
assert(rc == TREXIO_SUCCESS);
assert(chunk_read == read_size_check);
check_diff = det_coef_read[0] - 0.;
assert(check_diff*check_diff < 1e-14);
check_diff = det_coef_read[offset_data_read] - (3.14 + (double) (offset_file_read-offset));
//printf("%lf %lf\n", check_diff, det_coef_read[offset_data_read]);
assert(check_diff*check_diff < 1e-14);
int32_t state_id = 666;
rc = trexio_read_state_id(file, &state_id);
assert(rc == TREXIO_SUCCESS);
assert(state_id == STATE_TEST);
// now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max)
offset_file_read = 97L;
offset_data_read = 1;
int64_t eof_read_size_check = SIZE - offset_file_read; // if offset_file_read=97 => only 3 integrals will be read out of total of 100
if (offset != 0L) offset_file_read += offset;
chunk_read = read_size_check;
// read one chunk that will reach EOF and return TREXIO_END code
rc = trexio_read_determinant_list(file, offset_file_read, &chunk_read, &det_list_read[2*int_num*offset_data_read]);
/*
printf("%s\n", trexio_string_of_error(rc));
for (int i=0; i<size_r; i++) {
printf("%lld %lld\n", det_list_read[6*i], det_list_read[6*i+5]);
}
*/
assert(rc == TREXIO_END);
assert(chunk_read == eof_read_size_check);
chunk_read = read_size_check;
rc = trexio_read_determinant_coefficient(file, offset_file_read, &chunk_read, &det_coef_read[offset_data_read]);
/*
printf("%s\n", trexio_string_of_error(rc));
for (int i=0; i<size_r; i++) {
printf("%lf\n", det_coef_read[i]);
}
*/
assert(rc == TREXIO_END);
assert(chunk_read == eof_read_size_check);
check_diff= det_coef_read[size_r-1] - 0.;
//printf("%lf %lf\n", check_diff, det_coef_read[size_r-1]);
assert(check_diff*check_diff < 1e-14);
// check the value of determinant_num
int32_t det_num = 0;
int32_t size_check = SIZE;
if (offset != 0L) size_check += offset;
rc = trexio_read_determinant_num(file, &det_num);
assert(rc == TREXIO_SUCCESS);
assert(det_num == size_check);
// check conversion of determinants into orbital lists
int64_t size_list = TREXIO_NORB_PER_INT * int_num;
int32_t* orb_list_up = (int32_t*) calloc(size_list, sizeof(int32_t));
int32_t* orb_list_dn = (int32_t*) calloc(size_list, sizeof(int32_t));
int32_t occ_num_up, occ_num_dn;
rc = trexio_read_determinant_list(file, 0L, &chunk_read, &det_list_read[0L]);
assert (rc == TREXIO_SUCCESS);
rc = trexio_to_orbital_list_up_dn (int_num, &det_list_read[0], orb_list_up, orb_list_dn, &occ_num_up, &occ_num_dn);
assert (rc == TREXIO_SUCCESS);
assert (occ_num_up == 4);
assert (occ_num_dn == 3);
// DEBUG printing
for (int i=0; i<occ_num_up; i++) {
assert(orb_list_up[i] == i);
}
for (int i=0; i<occ_num_dn; i++) {
assert(orb_list_dn[i] == i);
}
// check conversion of one orbital list into the bitfield determinant representation
int64_t* det_list_check = (int64_t*) calloc(int_num, sizeof(int64_t));
rc = trexio_to_bitfield_list (orb_list_up, occ_num_up, det_list_check, int_num);
assert (rc == TREXIO_SUCCESS);
for (int i=0; i<int_num; i++) {
assert (det_list_check[i] == det_list_read[2*int_num*5+i]);
}
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the memory
free(det_list_read);
free(det_coef_read);
free(det_list_check);
free(orb_list_up);
free(orb_list_dn);
/*================= END OF TEST ==================*/
return 0;
}
int main(){
/*============== Test launcher ================*/
int mo_nums[5] = {10, 20, 40, 100, 300};
for (int i=0 ; i<5 ; ++i) {
int rc = system(RM_COMMAND);
assert (rc == 0);
// check the first write attempt (SIZE elements written in N_CHUNKS chunks)
printf("mo_num = %d\n", mo_nums[i]);
test_write_determinant (TREXIO_FILE, TEST_BACKEND, 0L, mo_nums[i]);
test_has_determinant (TREXIO_FILE, TEST_BACKEND);
test_read_determinant (TREXIO_FILE, TEST_BACKEND, 0L);
// check the second write attempt (SIZE elements written in N_CHUNKS chunks)
test_write_determinant (TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE, mo_nums[i]);
test_read_determinant (TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE);
rc = system(RM_COMMAND);
assert (rc == 0);
}
return 0;
}

View File

@ -1,325 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "io_determinant"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_determinant.c"
#include <stdint.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_determinant.h5"
#define RM_COMMAND "rm -f -- " TREXIO_FILE
#define SIZE 100
#define N_CHUNKS 5
#define STATE_TEST 2
static int test_write_determinant (const char* file_name, const back_end_t backend, const int64_t offset, const int mo_num) {
/* 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
int64_t* det_list;
double* det_coef;
// write mo_num which will be used to determine the optimal size of int indices
if (trexio_has_mo_num(file) == TREXIO_HAS_NOT) {
rc = trexio_write_mo_num(file, mo_num);
assert(rc == TREXIO_SUCCESS);
}
// get the number of int64 bit fields per determinant
int int_num;
rc = trexio_get_int64_num(file, &int_num);
assert(rc == TREXIO_SUCCESS);
assert(int_num == (mo_num-1)/64 + 1);
// allocate memory and fill with values to be written
det_list = (int64_t*) calloc(2 * int_num * SIZE, sizeof(int64_t));
det_coef = (double*) calloc(SIZE, sizeof(double));
int64_t size_list = TREXIO_NORB_PER_INT * int_num;
const int32_t orb_list_up[4] = {0,1,2,3};
const int32_t orb_list_dn[3] = {0,1,2};
for(int i=0; i<SIZE; i++){
rc = trexio_to_bitfield_list (orb_list_up, 4, &(det_list[2*int_num*i]), int_num);
rc = trexio_to_bitfield_list (orb_list_dn, 3, &(det_list[2*int_num*i+int_num]), int_num);
det_coef[i] = 3.14 + (double) i;
}
// write dataset chunks of sparse data in the file (including FAKE statements)
uint64_t chunk_size = (uint64_t) SIZE/N_CHUNKS;
uint64_t offset_f = 0UL;
uint64_t offset_d = 0UL;
if (offset != 0L) offset_f += offset;
// write the state_id of a given file: 0 is ground state
if (trexio_has_state_id(file) == TREXIO_HAS_NOT) {
rc = trexio_write_state_id(file, STATE_TEST);
assert(rc == TREXIO_SUCCESS);
}
// write n_chunks times using write_sparse
for(int i=0; i<N_CHUNKS; ++i){
rc = trexio_write_determinant_list(file, offset_f, chunk_size, &det_list[2*int_num*offset_d]);
assert(rc == TREXIO_SUCCESS);
rc = trexio_write_determinant_coefficient(file, offset_f, chunk_size, &det_coef[offset_d]);
assert(rc == TREXIO_SUCCESS);
offset_d += chunk_size;
offset_f += chunk_size;
}
// manually check the consistency of the determinant_num and coefficient_size after writing
int64_t coeff_size = 0L;
int64_t determinant_num = 0L;
rc = trexio_read_determinant_num_64(file, &determinant_num);
assert(rc == TREXIO_SUCCESS);
rc = trexio_read_determinant_coefficient_size(file, &coeff_size);
assert(rc == TREXIO_SUCCESS);
assert(determinant_num == coeff_size);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the allocated memeory
free(det_list);
free(det_coef);
printf("write determinants OK\n");
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_determinant(const char* file_name, const back_end_t backend) {
/* Try to check the existence of a 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);
// now check that previously written determinant_list exists
rc = trexio_has_determinant_list(file);
assert(rc==TREXIO_SUCCESS);
rc = trexio_has_state_id(file);
assert(rc==TREXIO_SUCCESS);
// now check that previously written determinant_coefficient exists
rc = trexio_has_determinant_coefficient(file);
assert(rc==TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
printf("has_determinant OK\n");
return 0;
}
static int test_read_determinant (const char* file_name, const back_end_t backend, const int64_t offset) {
/* Try to read one chunk of 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);
// compute how many integer bit fields is needed per determinant (for a given spin)
int64_t mo_num;
rc = trexio_read_mo_num_64(file, &mo_num);
assert (rc == TREXIO_SUCCESS);
int int_num;
rc = trexio_get_int64_num(file, &int_num);
assert (rc == TREXIO_SUCCESS);
assert (int_num == (mo_num - 1)/64 + 1);
// define arrays to read into
int64_t* det_list_read;
double* det_coef_read;
double check_diff;
uint64_t size_r = 40L;
det_list_read = (int64_t*) calloc(2*int_num*size_r,sizeof(int64_t));
det_coef_read = (double*) calloc(size_r,sizeof(double));
// specify the read parameters, here:
// 1 chunk of 10 elements using offset of 40 (i.e. lines No. 40--59) into elements of the array starting from 5
int64_t chunk_read = 10L;
int64_t offset_file_read = 40L;
int offset_data_read = 5;
int64_t read_size_check;
read_size_check = chunk_read;
if (offset != 0L) offset_file_read += offset;
// read one chunk using the aforementioned parameters
printf("int_num: %d\n", int_num);
rc = trexio_read_determinant_list(file, offset_file_read, &chunk_read, &det_list_read[2*int_num*offset_data_read]);
assert(rc == TREXIO_SUCCESS);
assert(chunk_read == read_size_check);
assert(det_list_read[2*int_num*offset_data_read] == 15);
assert(det_list_read[2*int_num*offset_data_read+int_num] == 7);
rc = trexio_read_determinant_coefficient(file, offset_file_read, &chunk_read, &det_coef_read[offset_data_read]);
assert(rc == TREXIO_SUCCESS);
assert(chunk_read == read_size_check);
check_diff = det_coef_read[0] - 0.;
assert(check_diff*check_diff < 1e-14);
check_diff = det_coef_read[offset_data_read] - (3.14 + (double) (offset_file_read-offset));
//printf("%lf %lf\n", check_diff, det_coef_read[offset_data_read]);
assert(check_diff*check_diff < 1e-14);
int32_t state_id = 666;
rc = trexio_read_state_id(file, &state_id);
assert(rc == TREXIO_SUCCESS);
assert(state_id == STATE_TEST);
// now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max)
offset_file_read = 97L;
offset_data_read = 1;
int64_t eof_read_size_check = SIZE - offset_file_read; // if offset_file_read=97 => only 3 integrals will be read out of total of 100
if (offset != 0L) offset_file_read += offset;
chunk_read = read_size_check;
// read one chunk that will reach EOF and return TREXIO_END code
rc = trexio_read_determinant_list(file, offset_file_read, &chunk_read, &det_list_read[2*int_num*offset_data_read]);
/*
printf("%s\n", trexio_string_of_error(rc));
for (int i=0; i<size_r; i++) {
printf("%lld %lld\n", det_list_read[6*i], det_list_read[6*i+5]);
}
*/
assert(rc == TREXIO_END);
assert(chunk_read == eof_read_size_check);
chunk_read = read_size_check;
rc = trexio_read_determinant_coefficient(file, offset_file_read, &chunk_read, &det_coef_read[offset_data_read]);
/*
printf("%s\n", trexio_string_of_error(rc));
for (int i=0; i<size_r; i++) {
printf("%lf\n", det_coef_read[i]);
}
*/
assert(rc == TREXIO_END);
assert(chunk_read == eof_read_size_check);
check_diff= det_coef_read[size_r-1] - 0.;
//printf("%lf %lf\n", check_diff, det_coef_read[size_r-1]);
assert(check_diff*check_diff < 1e-14);
// check the value of determinant_num
int32_t det_num = 0;
int32_t size_check = SIZE;
if (offset != 0L) size_check += offset;
rc = trexio_read_determinant_num(file, &det_num);
assert(rc == TREXIO_SUCCESS);
assert(det_num == size_check);
// check conversion of determinants into orbital lists
int64_t size_list = TREXIO_NORB_PER_INT * int_num;
int32_t* orb_list_up = (int32_t*) calloc(size_list, sizeof(int32_t));
int32_t* orb_list_dn = (int32_t*) calloc(size_list, sizeof(int32_t));
int32_t occ_num_up, occ_num_dn;
rc = trexio_read_determinant_list(file, 0L, &chunk_read, &det_list_read[0L]);
assert (rc == TREXIO_SUCCESS);
rc = trexio_to_orbital_list_up_dn (int_num, &det_list_read[0], orb_list_up, orb_list_dn, &occ_num_up, &occ_num_dn);
assert (rc == TREXIO_SUCCESS);
assert (occ_num_up == 4);
assert (occ_num_dn == 3);
// DEBUG printing
for (int i=0; i<occ_num_up; i++) {
assert(orb_list_up[i] == i);
}
for (int i=0; i<occ_num_dn; i++) {
assert(orb_list_dn[i] == i);
}
// check conversion of one orbital list into the bitfield determinant representation
int64_t* det_list_check = (int64_t*) calloc(int_num, sizeof(int64_t));
rc = trexio_to_bitfield_list (orb_list_up, occ_num_up, det_list_check, int_num);
assert (rc == TREXIO_SUCCESS);
for (int i=0; i<int_num; i++) {
assert (det_list_check[i] == det_list_read[2*int_num*5+i]);
}
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the memory
free(det_list_read);
free(det_coef_read);
free(det_list_check);
free(orb_list_up);
free(orb_list_dn);
/*================= END OF TEST ==================*/
return 0;
}
int main(){
/*============== Test launcher ================*/
int mo_nums[5] = {10, 20, 40, 100, 300};
for (int i=0 ; i<5 ; ++i) {
int rc = system(RM_COMMAND);
assert (rc == 0);
// check the first write attempt (SIZE elements written in N_CHUNKS chunks)
printf("mo_num = %d\n", mo_nums[i]);
test_write_determinant (TREXIO_FILE, TEST_BACKEND, 0L, mo_nums[i]);
test_has_determinant (TREXIO_FILE, TEST_BACKEND);
test_read_determinant (TREXIO_FILE, TEST_BACKEND, 0L);
// check the second write attempt (SIZE elements written in N_CHUNKS chunks)
test_write_determinant (TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE, mo_nums[i]);
test_read_determinant (TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE);
rc = system(RM_COMMAND);
assert (rc == 0);
}
return 0;
}

View File

@ -1,325 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "io_determinant"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_determinant.c"
#include <stdint.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_determinant.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " TREXIO_FILE
#define SIZE 100
#define N_CHUNKS 5
#define STATE_TEST 2
static int test_write_determinant (const char* file_name, const back_end_t backend, const int64_t offset, const int mo_num) {
/* 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
int64_t* det_list;
double* det_coef;
// write mo_num which will be used to determine the optimal size of int indices
if (trexio_has_mo_num(file) == TREXIO_HAS_NOT) {
rc = trexio_write_mo_num(file, mo_num);
assert(rc == TREXIO_SUCCESS);
}
// get the number of int64 bit fields per determinant
int int_num;
rc = trexio_get_int64_num(file, &int_num);
assert(rc == TREXIO_SUCCESS);
assert(int_num == (mo_num-1)/64 + 1);
// allocate memory and fill with values to be written
det_list = (int64_t*) calloc(2 * int_num * SIZE, sizeof(int64_t));
det_coef = (double*) calloc(SIZE, sizeof(double));
int64_t size_list = TREXIO_NORB_PER_INT * int_num;
const int32_t orb_list_up[4] = {0,1,2,3};
const int32_t orb_list_dn[3] = {0,1,2};
for(int i=0; i<SIZE; i++){
rc = trexio_to_bitfield_list (orb_list_up, 4, &(det_list[2*int_num*i]), int_num);
rc = trexio_to_bitfield_list (orb_list_dn, 3, &(det_list[2*int_num*i+int_num]), int_num);
det_coef[i] = 3.14 + (double) i;
}
// write dataset chunks of sparse data in the file (including FAKE statements)
uint64_t chunk_size = (uint64_t) SIZE/N_CHUNKS;
uint64_t offset_f = 0UL;
uint64_t offset_d = 0UL;
if (offset != 0L) offset_f += offset;
// write the state_id of a given file: 0 is ground state
if (trexio_has_state_id(file) == TREXIO_HAS_NOT) {
rc = trexio_write_state_id(file, STATE_TEST);
assert(rc == TREXIO_SUCCESS);
}
// write n_chunks times using write_sparse
for(int i=0; i<N_CHUNKS; ++i){
rc = trexio_write_determinant_list(file, offset_f, chunk_size, &det_list[2*int_num*offset_d]);
assert(rc == TREXIO_SUCCESS);
rc = trexio_write_determinant_coefficient(file, offset_f, chunk_size, &det_coef[offset_d]);
assert(rc == TREXIO_SUCCESS);
offset_d += chunk_size;
offset_f += chunk_size;
}
// manually check the consistency of the determinant_num and coefficient_size after writing
int64_t coeff_size = 0L;
int64_t determinant_num = 0L;
rc = trexio_read_determinant_num_64(file, &determinant_num);
assert(rc == TREXIO_SUCCESS);
rc = trexio_read_determinant_coefficient_size(file, &coeff_size);
assert(rc == TREXIO_SUCCESS);
assert(determinant_num == coeff_size);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the allocated memeory
free(det_list);
free(det_coef);
printf("write determinants OK\n");
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_determinant(const char* file_name, const back_end_t backend) {
/* Try to check the existence of a 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);
// now check that previously written determinant_list exists
rc = trexio_has_determinant_list(file);
assert(rc==TREXIO_SUCCESS);
rc = trexio_has_state_id(file);
assert(rc==TREXIO_SUCCESS);
// now check that previously written determinant_coefficient exists
rc = trexio_has_determinant_coefficient(file);
assert(rc==TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
printf("has_determinant OK\n");
return 0;
}
static int test_read_determinant (const char* file_name, const back_end_t backend, const int64_t offset) {
/* Try to read one chunk of 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);
// compute how many integer bit fields is needed per determinant (for a given spin)
int64_t mo_num;
rc = trexio_read_mo_num_64(file, &mo_num);
assert (rc == TREXIO_SUCCESS);
int int_num;
rc = trexio_get_int64_num(file, &int_num);
assert (rc == TREXIO_SUCCESS);
assert (int_num == (mo_num - 1)/64 + 1);
// define arrays to read into
int64_t* det_list_read;
double* det_coef_read;
double check_diff;
uint64_t size_r = 40L;
det_list_read = (int64_t*) calloc(2*int_num*size_r,sizeof(int64_t));
det_coef_read = (double*) calloc(size_r,sizeof(double));
// specify the read parameters, here:
// 1 chunk of 10 elements using offset of 40 (i.e. lines No. 40--59) into elements of the array starting from 5
int64_t chunk_read = 10L;
int64_t offset_file_read = 40L;
int offset_data_read = 5;
int64_t read_size_check;
read_size_check = chunk_read;
if (offset != 0L) offset_file_read += offset;
// read one chunk using the aforementioned parameters
printf("int_num: %d\n", int_num);
rc = trexio_read_determinant_list(file, offset_file_read, &chunk_read, &det_list_read[2*int_num*offset_data_read]);
assert(rc == TREXIO_SUCCESS);
assert(chunk_read == read_size_check);
assert(det_list_read[2*int_num*offset_data_read] == 15);
assert(det_list_read[2*int_num*offset_data_read+int_num] == 7);
rc = trexio_read_determinant_coefficient(file, offset_file_read, &chunk_read, &det_coef_read[offset_data_read]);
assert(rc == TREXIO_SUCCESS);
assert(chunk_read == read_size_check);
check_diff = det_coef_read[0] - 0.;
assert(check_diff*check_diff < 1e-14);
check_diff = det_coef_read[offset_data_read] - (3.14 + (double) (offset_file_read-offset));
//printf("%lf %lf\n", check_diff, det_coef_read[offset_data_read]);
assert(check_diff*check_diff < 1e-14);
int32_t state_id = 666;
rc = trexio_read_state_id(file, &state_id);
assert(rc == TREXIO_SUCCESS);
assert(state_id == STATE_TEST);
// now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max)
offset_file_read = 97L;
offset_data_read = 1;
int64_t eof_read_size_check = SIZE - offset_file_read; // if offset_file_read=97 => only 3 integrals will be read out of total of 100
if (offset != 0L) offset_file_read += offset;
chunk_read = read_size_check;
// read one chunk that will reach EOF and return TREXIO_END code
rc = trexio_read_determinant_list(file, offset_file_read, &chunk_read, &det_list_read[2*int_num*offset_data_read]);
/*
printf("%s\n", trexio_string_of_error(rc));
for (int i=0; i<size_r; i++) {
printf("%lld %lld\n", det_list_read[6*i], det_list_read[6*i+5]);
}
*/
assert(rc == TREXIO_END);
assert(chunk_read == eof_read_size_check);
chunk_read = read_size_check;
rc = trexio_read_determinant_coefficient(file, offset_file_read, &chunk_read, &det_coef_read[offset_data_read]);
/*
printf("%s\n", trexio_string_of_error(rc));
for (int i=0; i<size_r; i++) {
printf("%lf\n", det_coef_read[i]);
}
*/
assert(rc == TREXIO_END);
assert(chunk_read == eof_read_size_check);
check_diff= det_coef_read[size_r-1] - 0.;
//printf("%lf %lf\n", check_diff, det_coef_read[size_r-1]);
assert(check_diff*check_diff < 1e-14);
// check the value of determinant_num
int32_t det_num = 0;
int32_t size_check = SIZE;
if (offset != 0L) size_check += offset;
rc = trexio_read_determinant_num(file, &det_num);
assert(rc == TREXIO_SUCCESS);
assert(det_num == size_check);
// check conversion of determinants into orbital lists
int64_t size_list = TREXIO_NORB_PER_INT * int_num;
int32_t* orb_list_up = (int32_t*) calloc(size_list, sizeof(int32_t));
int32_t* orb_list_dn = (int32_t*) calloc(size_list, sizeof(int32_t));
int32_t occ_num_up, occ_num_dn;
rc = trexio_read_determinant_list(file, 0L, &chunk_read, &det_list_read[0L]);
assert (rc == TREXIO_SUCCESS);
rc = trexio_to_orbital_list_up_dn (int_num, &det_list_read[0], orb_list_up, orb_list_dn, &occ_num_up, &occ_num_dn);
assert (rc == TREXIO_SUCCESS);
assert (occ_num_up == 4);
assert (occ_num_dn == 3);
// DEBUG printing
for (int i=0; i<occ_num_up; i++) {
assert(orb_list_up[i] == i);
}
for (int i=0; i<occ_num_dn; i++) {
assert(orb_list_dn[i] == i);
}
// check conversion of one orbital list into the bitfield determinant representation
int64_t* det_list_check = (int64_t*) calloc(int_num, sizeof(int64_t));
rc = trexio_to_bitfield_list (orb_list_up, occ_num_up, det_list_check, int_num);
assert (rc == TREXIO_SUCCESS);
for (int i=0; i<int_num; i++) {
assert (det_list_check[i] == det_list_read[2*int_num*5+i]);
}
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the memory
free(det_list_read);
free(det_coef_read);
free(det_list_check);
free(orb_list_up);
free(orb_list_dn);
/*================= END OF TEST ==================*/
return 0;
}
int main(){
/*============== Test launcher ================*/
int mo_nums[5] = {10, 20, 40, 100, 300};
for (int i=0 ; i<5 ; ++i) {
int rc = system(RM_COMMAND);
assert (rc == 0);
// check the first write attempt (SIZE elements written in N_CHUNKS chunks)
printf("mo_num = %d\n", mo_nums[i]);
test_write_determinant (TREXIO_FILE, TEST_BACKEND, 0L, mo_nums[i]);
test_has_determinant (TREXIO_FILE, TEST_BACKEND);
test_read_determinant (TREXIO_FILE, TEST_BACKEND, 0L);
// check the second write attempt (SIZE elements written in N_CHUNKS chunks)
test_write_determinant (TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE, mo_nums[i]);
test_read_determinant (TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE);
rc = system(RM_COMMAND);
assert (rc == 0);
}
return 0;
}

142
tests/io_dset_float.c Normal file
View File

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

View File

@ -1,148 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "io_dset_float"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_dset_float.c"
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_dset_f.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, &rc);
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, &rc);
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, &rc);
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;
}

View File

@ -1,146 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "io_dset_float"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_dset_float.c"
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_dset_f.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " 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, &rc);
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, &rc);
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, &rc);
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;
}

136
tests/io_dset_int.c Normal file
View File

@ -0,0 +1,136 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
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};
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_basis_shell_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, &rc);
assert (file != NULL);
// 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);
// 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);
assert (file != NULL);
// read numerical attribute from the file
rc = trexio_read_basis_shell_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-1);
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;
}

View File

@ -1,140 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "io_dset_int"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_dset_int.c"
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_dset_i.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] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_basis_shell_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, &rc);
assert (file != NULL);
// 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);
// 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);
assert (file != NULL);
// read numerical attribute from the file
rc = trexio_read_basis_shell_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-1);
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;
}

View File

@ -1,140 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "io_dset_int"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_dset_int.c"
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_dset_i.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " 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};
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_basis_shell_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, &rc);
assert (file != NULL);
// 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);
// 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);
assert (file != NULL);
// read numerical attribute from the file
rc = trexio_read_basis_shell_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-1);
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;
}

235
tests/io_dset_sparse.c Normal file
View File

@ -0,0 +1,235 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define SIZE 100
#define N_CHUNKS 5
static int test_write_dset_sparse (const char* file_name, const back_end_t backend, const int64_t offset) {
/* 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; i++){
index[4*i] = 4*i;
index[4*i+1] = 4*i+1;
index[4*i+2] = 4*i+2;
index[4*i+3] = 4*i+3;
value[i] = 3.14 + (double) i;
}
// write mo_num which will be used to determine the optimal size of int indices
if (trexio_has_mo_num(file) == TREXIO_HAS_NOT) {
rc = trexio_write_mo_num(file, 1000);
assert(rc == TREXIO_SUCCESS);
}
// write dataset chunks of sparse data in the file (including FAKE statements)
uint64_t chunk_size = (uint64_t) SIZE/N_CHUNKS;
uint64_t offset_f = 0UL;
uint64_t offset_d = 0UL;
if (offset != 0L) offset_f += offset;
// write n_chunks times using write_sparse
for(int i=0; i<N_CHUNKS; ++i){
rc = trexio_write_mo_2e_int_eri(file, offset_f, chunk_size, &index[4*offset_d], &value[offset_d]);
assert(rc == TREXIO_SUCCESS);
offset_d += chunk_size;
offset_f += chunk_size;
}
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the allocated memeory
free(index);
free(value);
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_dset_sparse (const char* file_name, const back_end_t backend) {
/* Try to check the existence of a 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);
// check that the group exists
rc = trexio_has_mo_2e_int(file);
assert(rc==TREXIO_SUCCESS);
// check that the group does not exist
rc = trexio_has_rdm(file);
assert(rc==TREXIO_HAS_NOT);
// first check that mo_2e_int_eri_lr (we only write non-lr component in this unit test)
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// check that previous call to has_sparse did not create a file/dset
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// now check that previously written mo_2e_int_eri exists
rc = trexio_has_mo_2e_int_eri(file);
assert(rc==TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_read_dset_sparse (const char* file_name, const back_end_t backend, const int64_t offset) {
/* Try to read one chunk of 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 arrays to read into
int32_t* index_read;
double* value_read;
uint64_t size_r = 40L;
index_read = (int32_t*) calloc(4L*size_r,sizeof(int32_t));
value_read = (double*) calloc(size_r,sizeof(double));
// specify the read parameters, here:
// 1 chunk of 10 elements using offset of 40 (i.e. lines No. 40--59) into elements of the array starting from 5
int64_t chunk_read = 10L;
int64_t offset_file_read = 40L;
int offset_data_read = 5;
int64_t read_size_check;
read_size_check = chunk_read;
if (offset != 0L) offset_file_read += offset;
// read one chunk using the aforementioned parameters
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_SUCCESS);
assert(chunk_read == read_size_check);
assert(index_read[0] == 0);
assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset));
// now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max)
offset_file_read = 97L;
offset_data_read = 1;
int64_t eof_read_size_check = SIZE - offset_file_read; // if offset_file_read=97 => only 3 integrals will be read out of total of 100
if (offset != 0L) offset_file_read += offset;
// 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(chunk_read == eof_read_size_check);
assert(index_read[4*size_r-1] == 0);
assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset));
// 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, 0);
test_has_dset_sparse (TREXIO_FILE, TEST_BACKEND);
test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND, 0);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, SIZE);
// check the second write attempt (SIZE elements written in N_CHUNKS chunks)
test_write_dset_sparse (TREXIO_FILE, TEST_BACKEND, SIZE);
test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND, SIZE);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, SIZE*2);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,238 +1,4 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "io_dset_sparse"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_dset_sparse.c"
#include <stdint.h>
#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, const int64_t offset) {
/* 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; i++){
index[4*i] = 4*i;
index[4*i+1] = 4*i+1;
index[4*i+2] = 4*i+2;
index[4*i+3] = 4*i+3;
value[i] = 3.14 + (double) i;
}
// write mo_num which will be used to determine the optimal size of int indices
if (trexio_has_mo_num(file) == TREXIO_HAS_NOT) {
rc = trexio_write_mo_num(file, 1000);
assert(rc == TREXIO_SUCCESS);
}
// write dataset chunks of sparse data in the file (including FAKE statements)
uint64_t chunk_size = (uint64_t) SIZE/N_CHUNKS;
uint64_t offset_f = 0UL;
uint64_t offset_d = 0UL;
if (offset != 0L) offset_f += offset;
// write n_chunks times using write_sparse
for(int i=0; i<N_CHUNKS; ++i){
rc = trexio_write_mo_2e_int_eri(file, offset_f, chunk_size, &index[4*offset_d], &value[offset_d]);
assert(rc == TREXIO_SUCCESS);
offset_d += chunk_size;
offset_f += chunk_size;
}
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the allocated memeory
free(index);
free(value);
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_dset_sparse (const char* file_name, const back_end_t backend) {
/* Try to check the existence of a 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);
// check that the group exists
rc = trexio_has_mo_2e_int(file);
assert(rc==TREXIO_SUCCESS);
// check that the group does not exist
rc = trexio_has_rdm(file);
assert(rc==TREXIO_HAS_NOT);
// first check that mo_2e_int_eri_lr (we only write non-lr component in this unit test)
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// check that previous call to has_sparse did not create a file/dset
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// now check that previously written mo_2e_int_eri exists
rc = trexio_has_mo_2e_int_eri(file);
assert(rc==TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_read_dset_sparse (const char* file_name, const back_end_t backend, const int64_t offset) {
/* Try to read one chunk of 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 arrays to read into
int32_t* index_read;
double* value_read;
uint64_t size_r = 40L;
index_read = (int32_t*) calloc(4L*size_r,sizeof(int32_t));
value_read = (double*) calloc(size_r,sizeof(double));
// specify the read parameters, here:
// 1 chunk of 10 elements using offset of 40 (i.e. lines No. 40--59) into elements of the array starting from 5
int64_t chunk_read = 10L;
int64_t offset_file_read = 40L;
int offset_data_read = 5;
int64_t read_size_check;
read_size_check = chunk_read;
if (offset != 0L) offset_file_read += offset;
// read one chunk using the aforementioned parameters
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_SUCCESS);
assert(chunk_read == read_size_check);
assert(index_read[0] == 0);
assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset));
// now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max)
offset_file_read = 97L;
offset_data_read = 1;
int64_t eof_read_size_check = SIZE - offset_file_read; // if offset_file_read=97 => only 3 integrals will be read out of total of 100
if (offset != 0L) offset_file_read += offset;
// 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(chunk_read == eof_read_size_check);
assert(index_read[4*size_r-1] == 0);
assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset));
// 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, 0);
test_has_dset_sparse (TREXIO_FILE, TEST_BACKEND);
test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND, 0);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, SIZE);
// check the second write attempt (SIZE elements written in N_CHUNKS chunks)
test_write_dset_sparse (TREXIO_FILE, TEST_BACKEND, SIZE);
test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND, SIZE);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, SIZE*2);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,238 +1,4 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "io_dset_sparse"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_dset_sparse.c"
#include <stdint.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_dset_sparse.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " TREXIO_FILE
#define SIZE 100
#define N_CHUNKS 5
static int test_write_dset_sparse (const char* file_name, const back_end_t backend, const int64_t offset) {
/* 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; i++){
index[4*i] = 4*i;
index[4*i+1] = 4*i+1;
index[4*i+2] = 4*i+2;
index[4*i+3] = 4*i+3;
value[i] = 3.14 + (double) i;
}
// write mo_num which will be used to determine the optimal size of int indices
if (trexio_has_mo_num(file) == TREXIO_HAS_NOT) {
rc = trexio_write_mo_num(file, 1000);
assert(rc == TREXIO_SUCCESS);
}
// write dataset chunks of sparse data in the file (including FAKE statements)
uint64_t chunk_size = (uint64_t) SIZE/N_CHUNKS;
uint64_t offset_f = 0UL;
uint64_t offset_d = 0UL;
if (offset != 0L) offset_f += offset;
// write n_chunks times using write_sparse
for(int i=0; i<N_CHUNKS; ++i){
rc = trexio_write_mo_2e_int_eri(file, offset_f, chunk_size, &index[4*offset_d], &value[offset_d]);
assert(rc == TREXIO_SUCCESS);
offset_d += chunk_size;
offset_f += chunk_size;
}
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the allocated memeory
free(index);
free(value);
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_dset_sparse (const char* file_name, const back_end_t backend) {
/* Try to check the existence of a 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);
// check that the group exists
rc = trexio_has_mo_2e_int(file);
assert(rc==TREXIO_SUCCESS);
// check that the group does not exist
rc = trexio_has_rdm(file);
assert(rc==TREXIO_HAS_NOT);
// first check that mo_2e_int_eri_lr (we only write non-lr component in this unit test)
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// check that previous call to has_sparse did not create a file/dset
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// now check that previously written mo_2e_int_eri exists
rc = trexio_has_mo_2e_int_eri(file);
assert(rc==TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_read_dset_sparse (const char* file_name, const back_end_t backend, const int64_t offset) {
/* Try to read one chunk of 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 arrays to read into
int32_t* index_read;
double* value_read;
uint64_t size_r = 40L;
index_read = (int32_t*) calloc(4L*size_r,sizeof(int32_t));
value_read = (double*) calloc(size_r,sizeof(double));
// specify the read parameters, here:
// 1 chunk of 10 elements using offset of 40 (i.e. lines No. 40--59) into elements of the array starting from 5
int64_t chunk_read = 10L;
int64_t offset_file_read = 40L;
int offset_data_read = 5;
int64_t read_size_check;
read_size_check = chunk_read;
if (offset != 0L) offset_file_read += offset;
// read one chunk using the aforementioned parameters
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_SUCCESS);
assert(chunk_read == read_size_check);
assert(index_read[0] == 0);
assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset));
// now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max)
offset_file_read = 97L;
offset_data_read = 1;
int64_t eof_read_size_check = SIZE - offset_file_read; // if offset_file_read=97 => only 3 integrals will be read out of total of 100
if (offset != 0L) offset_file_read += offset;
// 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(chunk_read == eof_read_size_check);
assert(index_read[4*size_r-1] == 0);
assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset));
// 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, 0);
test_has_dset_sparse (TREXIO_FILE, TEST_BACKEND);
test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND, 0);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, SIZE);
// check the second write attempt (SIZE elements written in N_CHUNKS chunks)
test_write_dset_sparse (TREXIO_FILE, TEST_BACKEND, SIZE);
test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND, SIZE);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, SIZE*2);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

153
tests/io_dset_str.c Normal file
View File

@ -0,0 +1,153 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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, &rc);
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, &rc);
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, &rc);
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<num; i++){
labels[i] = (char*) malloc((max_str_len+1)*sizeof(char));
}
rc = trexio_read_nucleus_label(file, labels, max_str_len);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(labels[0], "C") == 0);
assert (strcmp(labels[1], "Na") == 0);
for (int i=0; i<num; i++){
free(labels[i]);
}
free(labels);
// 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_str (TREXIO_FILE, TEST_BACKEND);
test_has_dset_str (TREXIO_FILE, TEST_BACKEND);
test_read_dset_str (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,157 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "io_dset_str"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_dset_str.c"
#include <string.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_dset_s.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, &rc);
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, &rc);
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, &rc);
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<num; i++){
labels[i] = (char*) malloc((max_str_len+1)*sizeof(char));
}
rc = trexio_read_nucleus_label(file, labels, max_str_len);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(labels[0], "C") == 0);
assert (strcmp(labels[1], "Na") == 0);
for (int i=0; i<num; i++){
free(labels[i]);
}
free(labels);
// 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_str (TREXIO_FILE, TEST_BACKEND);
test_has_dset_str (TREXIO_FILE, TEST_BACKEND);
test_read_dset_str (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,155 +1,4 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "io_dset_str"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_dset_str.c"
#include <string.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_dset_s.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " 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, &rc);
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, &rc);
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, &rc);
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<num; i++){
labels[i] = (char*) malloc((max_str_len+1)*sizeof(char));
}
rc = trexio_read_nucleus_label(file, labels, max_str_len);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(labels[0], "C") == 0);
assert (strcmp(labels[1], "Na") == 0);
for (int i=0; i<num; i++){
free(labels[i]);
}
free(labels);
// 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_str (TREXIO_FILE, TEST_BACKEND);
test_has_dset_str (TREXIO_FILE, TEST_BACKEND);
test_read_dset_str (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

194
tests/io_jastrow.c Normal file
View File

@ -0,0 +1,194 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
static int test_write_jastrow (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);
#define nucleus_num 3
#define ee_num 2
#define en_num 3
#define een_num 6
rc = trexio_write_nucleus_num(file, nucleus_num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_type(file, "CHAMP", 6);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_ee_num(file, ee_num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_en_num(file, en_num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_een_num(file, een_num);
assert (rc == TREXIO_SUCCESS);
double ee [2] = { 0.5, 2. };
rc = trexio_write_jastrow_ee(file, ee);
assert (rc == TREXIO_SUCCESS);
double en [3] = { 1., 2., 3. };
rc = trexio_write_jastrow_en(file, en);
assert (rc == TREXIO_SUCCESS);
double een [6] = { 11., 12., 13., 14., 15., 16. };
rc = trexio_write_jastrow_een(file, een);
assert (rc == TREXIO_SUCCESS);
int en_nucleus [3] = { 0, 1, 2 };
rc = trexio_write_jastrow_en_nucleus(file, en_nucleus);
assert (rc == TREXIO_SUCCESS);
int een_nucleus [6] = { 0, 0, 1, 1, 2, 2 };
rc = trexio_write_jastrow_een_nucleus(file, een_nucleus);
assert (rc == TREXIO_SUCCESS);
double ee_scaling = 1.0;
rc = trexio_write_jastrow_ee_scaling(file, ee_scaling);
assert (rc == TREXIO_SUCCESS);
double en_scaling[3] = { 0.5, 1.0, 0.5 };
rc = trexio_write_jastrow_en_scaling(file, en_scaling);
assert (rc == TREXIO_SUCCESS);
#undef nucleus_num
#undef ee_num
#undef en_num
#undef een_num
rc = trexio_close(file);
/*================= END OF TEST ==================*/
return 0;
}
static int test_read_jastrow (const char* file_name, const back_end_t backend) {
/* Try to read one chunk of dataset of sparse data in the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
int nucleus_num = 0;
rc = trexio_read_nucleus_num(file, &nucleus_num);
assert (rc == TREXIO_SUCCESS);
assert (nucleus_num == 3);
char type[16] = "";
rc = trexio_read_jastrow_type(file, type, 16);
assert (rc == TREXIO_SUCCESS);
assert (strcmp("CHAMP",type) == 0);
int ee_num = 0;
rc = trexio_read_jastrow_ee_num(file, &ee_num);
assert (rc == TREXIO_SUCCESS);
assert (ee_num == 2);
int en_num = 0;
rc = trexio_read_jastrow_en_num(file, &en_num);
assert (rc == TREXIO_SUCCESS);
assert (en_num == nucleus_num);
int een_num = 0;
rc = trexio_read_jastrow_een_num(file, &een_num);
assert (rc == TREXIO_SUCCESS);
assert (een_num == 2*nucleus_num);
double ee [2] = { 0., 0. };
rc = trexio_read_jastrow_ee(file, ee);
assert (rc == TREXIO_SUCCESS);
assert (ee[0] == 0.5);
assert (ee[1] == 2.0);
double en [3] = { 0., 0., 0. };
rc = trexio_read_jastrow_en(file, en);
assert (rc == TREXIO_SUCCESS);
assert (en[0] == 1.0);
assert (en[1] == 2.0);
assert (en[2] == 3.0);
double een [6];
rc = trexio_read_jastrow_een(file, een);
assert (rc == TREXIO_SUCCESS);
assert (een[0] == 11.0);
assert (een[1] == 12.0);
assert (een[2] == 13.0);
assert (een[3] == 14.0);
assert (een[4] == 15.0);
assert (een[5] == 16.0);
int en_nucleus [3] = { 0, 0, 0 };
rc = trexio_read_jastrow_en_nucleus(file, en_nucleus);
assert (rc == TREXIO_SUCCESS);
assert (en_nucleus[0] == 0);
assert (en_nucleus[1] == 1);
assert (en_nucleus[2] == 2);
int een_nucleus [6] = { 0, 0, 0, 0, 0, 0 };
rc = trexio_read_jastrow_een_nucleus(file, een_nucleus);
assert (rc == TREXIO_SUCCESS);
assert (een_nucleus[0] == 0);
assert (een_nucleus[1] == 0);
assert (een_nucleus[2] == 1);
assert (een_nucleus[3] == 1);
assert (een_nucleus[4] == 2);
assert (een_nucleus[5] == 2);
double ee_scaling = 0.0;
rc = trexio_read_jastrow_ee_scaling(file, &ee_scaling);
assert (rc == TREXIO_SUCCESS);
assert (ee_scaling == 1.0);
double en_scaling[3] = { 0.5, 1.0, 0.5 };
rc = trexio_read_jastrow_en_scaling(file, en_scaling);
assert (rc == TREXIO_SUCCESS);
assert (en_scaling[0] == 0.5);
assert (en_scaling[1] == 1.0);
assert (en_scaling[2] == 0.5);
rc = trexio_close(file);
/*================= END OF TEST ==================*/
return 0;
}
int main(){
/*============== Test launcher ================*/
int rc;
rc = system(RM_COMMAND);
assert (rc == 0);
test_write_jastrow (TREXIO_FILE, TEST_BACKEND);
test_read_jastrow (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,198 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "io_jastrow"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_jastrow.c"
#include <string.h>
#include <stdint.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_jastrow.h5"
#define RM_COMMAND "rm -f -- " TREXIO_FILE
static int test_write_jastrow (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);
#define nucleus_num 3
#define ee_num 2
#define en_num 3
#define een_num 6
rc = trexio_write_nucleus_num(file, nucleus_num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_type(file, "CHAMP", 6);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_ee_num(file, ee_num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_en_num(file, en_num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_een_num(file, een_num);
assert (rc == TREXIO_SUCCESS);
double ee [2] = { 0.5, 2. };
rc = trexio_write_jastrow_ee(file, ee);
assert (rc == TREXIO_SUCCESS);
double en [3] = { 1., 2., 3. };
rc = trexio_write_jastrow_en(file, en);
assert (rc == TREXIO_SUCCESS);
double een [6] = { 11., 12., 13., 14., 15., 16. };
rc = trexio_write_jastrow_een(file, een);
assert (rc == TREXIO_SUCCESS);
int en_nucleus [3] = { 0, 1, 2 };
rc = trexio_write_jastrow_en_nucleus(file, en_nucleus);
assert (rc == TREXIO_SUCCESS);
int een_nucleus [6] = { 0, 0, 1, 1, 2, 2 };
rc = trexio_write_jastrow_een_nucleus(file, een_nucleus);
assert (rc == TREXIO_SUCCESS);
double ee_scaling = 1.0;
rc = trexio_write_jastrow_ee_scaling(file, ee_scaling);
assert (rc == TREXIO_SUCCESS);
double en_scaling[3] = { 0.5, 1.0, 0.5 };
rc = trexio_write_jastrow_en_scaling(file, en_scaling);
assert (rc == TREXIO_SUCCESS);
#undef nucleus_num
#undef ee_num
#undef en_num
#undef een_num
rc = trexio_close(file);
/*================= END OF TEST ==================*/
return 0;
}
static int test_read_jastrow (const char* file_name, const back_end_t backend) {
/* Try to read one chunk of dataset of sparse data in the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
int nucleus_num = 0;
rc = trexio_read_nucleus_num(file, &nucleus_num);
assert (rc == TREXIO_SUCCESS);
assert (nucleus_num == 3);
char type[16] = "";
rc = trexio_read_jastrow_type(file, type, 16);
assert (rc == TREXIO_SUCCESS);
assert (strcmp("CHAMP",type) == 0);
int ee_num = 0;
rc = trexio_read_jastrow_ee_num(file, &ee_num);
assert (rc == TREXIO_SUCCESS);
assert (ee_num == 2);
int en_num = 0;
rc = trexio_read_jastrow_en_num(file, &en_num);
assert (rc == TREXIO_SUCCESS);
assert (en_num == nucleus_num);
int een_num = 0;
rc = trexio_read_jastrow_een_num(file, &een_num);
assert (rc == TREXIO_SUCCESS);
assert (een_num == 2*nucleus_num);
double ee [2] = { 0., 0. };
rc = trexio_read_jastrow_ee(file, ee);
assert (rc == TREXIO_SUCCESS);
assert (ee[0] == 0.5);
assert (ee[1] == 2.0);
double en [3] = { 0., 0., 0. };
rc = trexio_read_jastrow_en(file, en);
assert (rc == TREXIO_SUCCESS);
assert (en[0] == 1.0);
assert (en[1] == 2.0);
assert (en[2] == 3.0);
double een [6];
rc = trexio_read_jastrow_een(file, een);
assert (rc == TREXIO_SUCCESS);
assert (een[0] == 11.0);
assert (een[1] == 12.0);
assert (een[2] == 13.0);
assert (een[3] == 14.0);
assert (een[4] == 15.0);
assert (een[5] == 16.0);
int en_nucleus [3] = { 0, 0, 0 };
rc = trexio_read_jastrow_en_nucleus(file, en_nucleus);
assert (rc == TREXIO_SUCCESS);
assert (en_nucleus[0] == 0);
assert (en_nucleus[1] == 1);
assert (en_nucleus[2] == 2);
int een_nucleus [6] = { 0, 0, 0, 0, 0, 0 };
rc = trexio_read_jastrow_een_nucleus(file, een_nucleus);
assert (rc == TREXIO_SUCCESS);
assert (een_nucleus[0] == 0);
assert (een_nucleus[1] == 0);
assert (een_nucleus[2] == 1);
assert (een_nucleus[3] == 1);
assert (een_nucleus[4] == 2);
assert (een_nucleus[5] == 2);
double ee_scaling = 0.0;
rc = trexio_read_jastrow_ee_scaling(file, &ee_scaling);
assert (rc == TREXIO_SUCCESS);
assert (ee_scaling == 1.0);
double en_scaling[3] = { 0.5, 1.0, 0.5 };
rc = trexio_read_jastrow_en_scaling(file, en_scaling);
assert (rc == TREXIO_SUCCESS);
assert (en_scaling[0] == 0.5);
assert (en_scaling[1] == 1.0);
assert (en_scaling[2] == 0.5);
rc = trexio_close(file);
/*================= END OF TEST ==================*/
return 0;
}
int main(){
/*============== Test launcher ================*/
int rc;
rc = system(RM_COMMAND);
assert (rc == 0);
test_write_jastrow (TREXIO_FILE, TEST_BACKEND);
test_read_jastrow (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,198 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "io_jastrow"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_jastrow.c"
#include <string.h>
#include <stdint.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_jastrow.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " TREXIO_FILE
static int test_write_jastrow (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);
#define nucleus_num 3
#define ee_num 2
#define en_num 3
#define een_num 6
rc = trexio_write_nucleus_num(file, nucleus_num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_type(file, "CHAMP", 6);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_ee_num(file, ee_num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_en_num(file, en_num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_jastrow_een_num(file, een_num);
assert (rc == TREXIO_SUCCESS);
double ee [2] = { 0.5, 2. };
rc = trexio_write_jastrow_ee(file, ee);
assert (rc == TREXIO_SUCCESS);
double en [3] = { 1., 2., 3. };
rc = trexio_write_jastrow_en(file, en);
assert (rc == TREXIO_SUCCESS);
double een [6] = { 11., 12., 13., 14., 15., 16. };
rc = trexio_write_jastrow_een(file, een);
assert (rc == TREXIO_SUCCESS);
int en_nucleus [3] = { 0, 1, 2 };
rc = trexio_write_jastrow_en_nucleus(file, en_nucleus);
assert (rc == TREXIO_SUCCESS);
int een_nucleus [6] = { 0, 0, 1, 1, 2, 2 };
rc = trexio_write_jastrow_een_nucleus(file, een_nucleus);
assert (rc == TREXIO_SUCCESS);
double ee_scaling = 1.0;
rc = trexio_write_jastrow_ee_scaling(file, ee_scaling);
assert (rc == TREXIO_SUCCESS);
double en_scaling[3] = { 0.5, 1.0, 0.5 };
rc = trexio_write_jastrow_en_scaling(file, en_scaling);
assert (rc == TREXIO_SUCCESS);
#undef nucleus_num
#undef ee_num
#undef en_num
#undef een_num
rc = trexio_close(file);
/*================= END OF TEST ==================*/
return 0;
}
static int test_read_jastrow (const char* file_name, const back_end_t backend) {
/* Try to read one chunk of dataset of sparse data in the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
int nucleus_num = 0;
rc = trexio_read_nucleus_num(file, &nucleus_num);
assert (rc == TREXIO_SUCCESS);
assert (nucleus_num == 3);
char type[16] = "";
rc = trexio_read_jastrow_type(file, type, 16);
assert (rc == TREXIO_SUCCESS);
assert (strcmp("CHAMP",type) == 0);
int ee_num = 0;
rc = trexio_read_jastrow_ee_num(file, &ee_num);
assert (rc == TREXIO_SUCCESS);
assert (ee_num == 2);
int en_num = 0;
rc = trexio_read_jastrow_en_num(file, &en_num);
assert (rc == TREXIO_SUCCESS);
assert (en_num == nucleus_num);
int een_num = 0;
rc = trexio_read_jastrow_een_num(file, &een_num);
assert (rc == TREXIO_SUCCESS);
assert (een_num == 2*nucleus_num);
double ee [2] = { 0., 0. };
rc = trexio_read_jastrow_ee(file, ee);
assert (rc == TREXIO_SUCCESS);
assert (ee[0] == 0.5);
assert (ee[1] == 2.0);
double en [3] = { 0., 0., 0. };
rc = trexio_read_jastrow_en(file, en);
assert (rc == TREXIO_SUCCESS);
assert (en[0] == 1.0);
assert (en[1] == 2.0);
assert (en[2] == 3.0);
double een [6];
rc = trexio_read_jastrow_een(file, een);
assert (rc == TREXIO_SUCCESS);
assert (een[0] == 11.0);
assert (een[1] == 12.0);
assert (een[2] == 13.0);
assert (een[3] == 14.0);
assert (een[4] == 15.0);
assert (een[5] == 16.0);
int en_nucleus [3] = { 0, 0, 0 };
rc = trexio_read_jastrow_en_nucleus(file, en_nucleus);
assert (rc == TREXIO_SUCCESS);
assert (en_nucleus[0] == 0);
assert (en_nucleus[1] == 1);
assert (en_nucleus[2] == 2);
int een_nucleus [6] = { 0, 0, 0, 0, 0, 0 };
rc = trexio_read_jastrow_een_nucleus(file, een_nucleus);
assert (rc == TREXIO_SUCCESS);
assert (een_nucleus[0] == 0);
assert (een_nucleus[1] == 0);
assert (een_nucleus[2] == 1);
assert (een_nucleus[3] == 1);
assert (een_nucleus[4] == 2);
assert (een_nucleus[5] == 2);
double ee_scaling = 0.0;
rc = trexio_read_jastrow_ee_scaling(file, &ee_scaling);
assert (rc == TREXIO_SUCCESS);
assert (ee_scaling == 1.0);
double en_scaling[3] = { 0.5, 1.0, 0.5 };
rc = trexio_read_jastrow_en_scaling(file, en_scaling);
assert (rc == TREXIO_SUCCESS);
assert (en_scaling[0] == 0.5);
assert (en_scaling[1] == 1.0);
assert (en_scaling[2] == 0.5);
rc = trexio_close(file);
/*================= END OF TEST ==================*/
return 0;
}
int main(){
/*============== Test launcher ================*/
int rc;
rc = system(RM_COMMAND);
assert (rc == 0);
test_write_jastrow (TREXIO_FILE, TEST_BACKEND);
test_read_jastrow (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

150
tests/io_num.c Normal file
View File

@ -0,0 +1,150 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static int test_write_num (const char* file_name, const back_end_t backend) {
/* Try to write a dimensioning attribute (num variable) into the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be written
int num = 12;
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_num(file, num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_repulsion(file, 2.14171677);
assert (rc == TREXIO_SUCCESS);
// attempt to write 0 as dimensioning variable in an empty file; should FAIL and return TREXIO_INVALID_ARG_2
rc = trexio_write_mo_num(file, 0);
assert (rc == TREXIO_INVALID_NUM);
// write numerical attribute ao_cartesian as 0
rc = trexio_write_ao_cartesian(file, 0);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_num (const char* file_name, const back_end_t backend) {
/* Try to check the existence of a dimensioning attribute (num variable) 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);
// check that the previously written num variable exists
rc = trexio_has_nucleus_num(file);
assert (rc == TREXIO_SUCCESS);
rc = trexio_has_nucleus_repulsion(file);
assert (rc == TREXIO_SUCCESS);
// check that the num variable does not exist
rc = trexio_has_mo_num(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_num (const char* file_name, const back_end_t backend) {
/* Try to read a dimensioning attribute (num variable) from the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be read
int num;
int cartesian;
float repulsion_32;
double repulsion_64, d;
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file
rc = trexio_read_nucleus_num(file, &num);
assert (rc == TREXIO_SUCCESS);
assert (num == 12);
rc = trexio_read_nucleus_repulsion_32(file, &repulsion_32);
assert (rc == TREXIO_SUCCESS);
d = repulsion_32 - 2.14171677;
assert( d*d < 1.e-8 );
rc = trexio_read_nucleus_repulsion_64(file, &repulsion_64);
assert (rc == TREXIO_SUCCESS);
d = repulsion_64 - 2.14171677;
assert( d*d < 1.e-14 );
// read non-existing numerical attribute from the file
rc = trexio_read_mo_num(file, &num);
assert (rc == TREXIO_ATTR_MISSING);
// read ao_cartesian (zero) value from the file
rc = trexio_read_ao_cartesian(file, &cartesian);
assert (rc == TREXIO_SUCCESS);
assert (cartesian == 0);
// 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_num (TREXIO_FILE, TEST_BACKEND);
test_has_num (TREXIO_FILE, TEST_BACKEND);
test_read_num (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,154 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "io_num"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_num.c"
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_num.h5"
#define RM_COMMAND "rm -rf " TREXIO_FILE
static int test_write_num (const char* file_name, const back_end_t backend) {
/* Try to write a dimensioning attribute (num variable) into the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be written
int num = 12;
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_num(file, num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_repulsion(file, 2.14171677);
assert (rc == TREXIO_SUCCESS);
// attempt to write 0 as dimensioning variable in an empty file; should FAIL and return TREXIO_INVALID_ARG_2
rc = trexio_write_mo_num(file, 0);
assert (rc == TREXIO_INVALID_NUM);
// write numerical attribute ao_cartesian as 0
rc = trexio_write_ao_cartesian(file, 0);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_num (const char* file_name, const back_end_t backend) {
/* Try to check the existence of a dimensioning attribute (num variable) 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);
// check that the previously written num variable exists
rc = trexio_has_nucleus_num(file);
assert (rc == TREXIO_SUCCESS);
rc = trexio_has_nucleus_repulsion(file);
assert (rc == TREXIO_SUCCESS);
// check that the num variable does not exist
rc = trexio_has_mo_num(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_num (const char* file_name, const back_end_t backend) {
/* Try to read a dimensioning attribute (num variable) from the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be read
int num;
int cartesian;
float repulsion_32;
double repulsion_64, d;
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file
rc = trexio_read_nucleus_num(file, &num);
assert (rc == TREXIO_SUCCESS);
assert (num == 12);
rc = trexio_read_nucleus_repulsion_32(file, &repulsion_32);
assert (rc == TREXIO_SUCCESS);
d = repulsion_32 - 2.14171677;
assert( d*d < 1.e-8 );
rc = trexio_read_nucleus_repulsion_64(file, &repulsion_64);
assert (rc == TREXIO_SUCCESS);
d = repulsion_64 - 2.14171677;
assert( d*d < 1.e-14 );
// read non-existing numerical attribute from the file
rc = trexio_read_mo_num(file, &num);
assert (rc == TREXIO_ATTR_MISSING);
// read ao_cartesian (zero) value from the file
rc = trexio_read_ao_cartesian(file, &cartesian);
assert (rc == TREXIO_SUCCESS);
assert (cartesian == 0);
// 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_num (TREXIO_FILE, TEST_BACKEND);
test_has_num (TREXIO_FILE, TEST_BACKEND);
test_read_num (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,154 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "io_num"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_num.c"
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_num.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " TREXIO_FILE
static int test_write_num (const char* file_name, const back_end_t backend) {
/* Try to write a dimensioning attribute (num variable) into the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be written
int num = 12;
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_num(file, num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_repulsion(file, 2.14171677);
assert (rc == TREXIO_SUCCESS);
// attempt to write 0 as dimensioning variable in an empty file; should FAIL and return TREXIO_INVALID_ARG_2
rc = trexio_write_mo_num(file, 0);
assert (rc == TREXIO_INVALID_NUM);
// write numerical attribute ao_cartesian as 0
rc = trexio_write_ao_cartesian(file, 0);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_num (const char* file_name, const back_end_t backend) {
/* Try to check the existence of a dimensioning attribute (num variable) 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);
// check that the previously written num variable exists
rc = trexio_has_nucleus_num(file);
assert (rc == TREXIO_SUCCESS);
rc = trexio_has_nucleus_repulsion(file);
assert (rc == TREXIO_SUCCESS);
// check that the num variable does not exist
rc = trexio_has_mo_num(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_num (const char* file_name, const back_end_t backend) {
/* Try to read a dimensioning attribute (num variable) from the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be read
int num;
int cartesian;
float repulsion_32;
double repulsion_64, d;
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file
rc = trexio_read_nucleus_num(file, &num);
assert (rc == TREXIO_SUCCESS);
assert (num == 12);
rc = trexio_read_nucleus_repulsion_32(file, &repulsion_32);
assert (rc == TREXIO_SUCCESS);
d = repulsion_32 - 2.14171677;
assert( d*d < 1.e-8 );
rc = trexio_read_nucleus_repulsion_64(file, &repulsion_64);
assert (rc == TREXIO_SUCCESS);
d = repulsion_64 - 2.14171677;
assert( d*d < 1.e-14 );
// read non-existing numerical attribute from the file
rc = trexio_read_mo_num(file, &num);
assert (rc == TREXIO_ATTR_MISSING);
// read ao_cartesian (zero) value from the file
rc = trexio_read_ao_cartesian(file, &cartesian);
assert (rc == TREXIO_SUCCESS);
assert (cartesian == 0);
// 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_num (TREXIO_FILE, TEST_BACKEND);
test_has_num (TREXIO_FILE, TEST_BACKEND);
test_read_num (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

161
tests/io_safe_dset_float.c Normal file
View File

@ -0,0 +1,161 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
static int test_write_dset (const char* file_name, const back_end_t backend) {
/* Try to write a dataset with floating point values into the TREXIO file using safe API */
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, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_num(file, num);
assert (rc == TREXIO_SUCCESS);
/* write numerical dataset with an unsafe dimension
* this should return TREXIO_UNSAFE_ARRAY_DIM indicating
* that access beyong allocated memory is likely to occur */
uint64_t dim_unsafe = num * 12;
rc = trexio_write_safe_nucleus_coord(file, coord, dim_unsafe);
assert (rc == TREXIO_UNSAFE_ARRAY_DIM);
/* write numerical dataset with a safe dimension
* this should return TREXIO_SUCCESS */
uint64_t dim_safe = num * 3;
rc = trexio_write_safe_nucleus_coord(file, coord, dim_safe);
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, &rc);
assert (file != NULL);
// check that the previously written dataset exists
rc = trexio_has_nucleus_coord(file);
assert (rc == TREXIO_SUCCESS);
// 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 floating point values from the TREXIO file using safe API */
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, &rc);
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));
/* write numerical dataset with an unsafe dimension
* this should return TREXIO_UNSAFE_ARRAY_DIM indicating
* that access beyong allocated memory is likely to occur */
uint64_t dim_unsafe = num * 12;
rc = trexio_read_safe_nucleus_coord(file, coord, dim_unsafe);
assert (rc == TREXIO_UNSAFE_ARRAY_DIM);
/* write numerical dataset with a safe dimension
* this should return TREXIO_SUCCESS */
uint64_t dim_safe = num * 3;
rc = trexio_read_safe_nucleus_coord(file, coord, dim_safe);
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;
}

View File

@ -1,165 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "io_safe_dset_float"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_safe_dset_float.c"
#include <stdint.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_safe_dset_f.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 floating point values into the TREXIO file using safe API */
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, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_num(file, num);
assert (rc == TREXIO_SUCCESS);
/* write numerical dataset with an unsafe dimension
* this should return TREXIO_UNSAFE_ARRAY_DIM indicating
* that access beyong allocated memory is likely to occur */
uint64_t dim_unsafe = num * 12;
rc = trexio_write_safe_nucleus_coord(file, coord, dim_unsafe);
assert (rc == TREXIO_UNSAFE_ARRAY_DIM);
/* write numerical dataset with a safe dimension
* this should return TREXIO_SUCCESS */
uint64_t dim_safe = num * 3;
rc = trexio_write_safe_nucleus_coord(file, coord, dim_safe);
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, &rc);
assert (file != NULL);
// check that the previously written dataset exists
rc = trexio_has_nucleus_coord(file);
assert (rc == TREXIO_SUCCESS);
// 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 floating point values from the TREXIO file using safe API */
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, &rc);
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));
/* write numerical dataset with an unsafe dimension
* this should return TREXIO_UNSAFE_ARRAY_DIM indicating
* that access beyong allocated memory is likely to occur */
uint64_t dim_unsafe = num * 12;
rc = trexio_read_safe_nucleus_coord(file, coord, dim_unsafe);
assert (rc == TREXIO_UNSAFE_ARRAY_DIM);
/* write numerical dataset with a safe dimension
* this should return TREXIO_SUCCESS */
uint64_t dim_safe = num * 3;
rc = trexio_read_safe_nucleus_coord(file, coord, dim_safe);
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;
}

View File

@ -1,163 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "io_safe_dset_float"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_safe_dset_float.c"
#include <stdint.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_safe_dset_f.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " TREXIO_FILE
static int test_write_dset (const char* file_name, const back_end_t backend) {
/* Try to write a dataset with floating point values into the TREXIO file using safe API */
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, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_num(file, num);
assert (rc == TREXIO_SUCCESS);
/* write numerical dataset with an unsafe dimension
* this should return TREXIO_UNSAFE_ARRAY_DIM indicating
* that access beyong allocated memory is likely to occur */
uint64_t dim_unsafe = num * 12;
rc = trexio_write_safe_nucleus_coord(file, coord, dim_unsafe);
assert (rc == TREXIO_UNSAFE_ARRAY_DIM);
/* write numerical dataset with a safe dimension
* this should return TREXIO_SUCCESS */
uint64_t dim_safe = num * 3;
rc = trexio_write_safe_nucleus_coord(file, coord, dim_safe);
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, &rc);
assert (file != NULL);
// check that the previously written dataset exists
rc = trexio_has_nucleus_coord(file);
assert (rc == TREXIO_SUCCESS);
// 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 floating point values from the TREXIO file using safe API */
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, &rc);
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));
/* write numerical dataset with an unsafe dimension
* this should return TREXIO_UNSAFE_ARRAY_DIM indicating
* that access beyong allocated memory is likely to occur */
uint64_t dim_unsafe = num * 12;
rc = trexio_read_safe_nucleus_coord(file, coord, dim_unsafe);
assert (rc == TREXIO_UNSAFE_ARRAY_DIM);
/* write numerical dataset with a safe dimension
* this should return TREXIO_SUCCESS */
uint64_t dim_safe = num * 3;
rc = trexio_read_safe_nucleus_coord(file, coord, dim_safe);
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;
}

129
tests/io_str.c Normal file
View File

@ -0,0 +1,129 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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, &rc);
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, &rc);
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, &rc);
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;
}

View File

@ -1,133 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "io_str"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_str.c"
#include <string.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_str.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, &rc);
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, &rc);
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, &rc);
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;
}

View File

@ -1,131 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "io_str"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "io_str.c"
#include <string.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_str.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " 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, &rc);
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, &rc);
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, &rc);
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;
}

160
tests/open.c Normal file
View File

@ -0,0 +1,160 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TREXIO_VOID "non_existing_" TREXIO_FILE
static int test_open_w (const char* file_name, const back_end_t backend) {
/* Try to open the TREXIO file in 'write' mode */
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);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_r (const char* file_name, const back_end_t backend) {
/* Try to open the TREXIO file in 'read' mode */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_auto (const char* file_name) {
/* Try to open the TREXIO file in 'read' mode */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'r', TREXIO_AUTO, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_errors (const back_end_t backend) {
/* Try to call trexio_open with bad arguments */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open non-existing file in 'r' (read) mode, should return TREXIO_OPEN_ERROR
file = trexio_open(TREXIO_VOID, 'r', backend, &rc);
assert (file == NULL);
assert (rc == TREXIO_OPEN_ERROR);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
// open file with empty file name, should return TREXIO_INVALID_ARG_1
file = trexio_open("", 'w', backend, &rc);
assert (file == NULL);
assert (rc == TREXIO_INVALID_ARG_1);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
// open existing file in non-supported I/O mode, should return TREXIO_INVALID_ARG_2
file = trexio_open(TREXIO_FILE, 'k', backend, &rc);
assert (file == NULL);
assert (rc == TREXIO_INVALID_ARG_2);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
// open existing file with non-supported back end, should return TREXIO_INVALID_ARG_3
file = trexio_open(TREXIO_FILE, 'w', 666, &rc);
assert (file == NULL);
assert (rc == TREXIO_INVALID_ARG_3);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
/*================= END OF TEST ==================*/
return 0;
}
static int test_inquire (const back_end_t backend) {
/* Try to call trexio_inquire function */
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// inquire non-existing file
rc = trexio_inquire(TREXIO_VOID);
assert (rc == TREXIO_FAILURE);
// inquire existing file
rc = trexio_inquire(TREXIO_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_open_w (TREXIO_FILE, TEST_BACKEND);
test_open_r (TREXIO_FILE, TEST_BACKEND);
test_open_auto (TREXIO_FILE);
test_open_errors(TEST_BACKEND);
test_inquire (TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,163 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "open"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "open.c"
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_open.h5"
#define TREXIO_VOID "non_existing_" TREXIO_FILE
#define RM_COMMAND "rm -rf " TREXIO_FILE
static int test_open_w (const char* file_name, const back_end_t backend) {
/* Try to open the TREXIO file in 'write' mode */
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);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_r (const char* file_name, const back_end_t backend) {
/* Try to open the TREXIO file in 'read' mode */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_auto (const char* file_name) {
/* Try to open the TREXIO file in 'read' mode */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'r', TREXIO_AUTO, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_errors (const back_end_t backend) {
/* Try to call trexio_open with bad arguments */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open non-existing file in 'r' (read) mode, should return TREXIO_OPEN_ERROR
file = trexio_open(TREXIO_VOID, 'r', backend, &rc);
assert (file == NULL);
assert (rc == TREXIO_OPEN_ERROR);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
// open file with empty file name, should return TREXIO_INVALID_ARG_1
file = trexio_open("", 'w', backend, &rc);
assert (file == NULL);
assert (rc == TREXIO_INVALID_ARG_1);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
// open existing file in non-supported I/O mode, should return TREXIO_INVALID_ARG_2
file = trexio_open(TREXIO_FILE, 'k', backend, &rc);
assert (file == NULL);
assert (rc == TREXIO_INVALID_ARG_2);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
// open existing file with non-supported back end, should return TREXIO_INVALID_ARG_3
file = trexio_open(TREXIO_FILE, 'w', 666, &rc);
assert (file == NULL);
assert (rc == TREXIO_INVALID_ARG_3);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
/*================= END OF TEST ==================*/
return 0;
}
static int test_inquire (const back_end_t backend) {
/* Try to call trexio_inquire function */
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// inquire non-existing file
rc = trexio_inquire(TREXIO_VOID);
assert (rc == TREXIO_FAILURE);
// inquire existing file
rc = trexio_inquire(TREXIO_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_open_w (TREXIO_FILE, TEST_BACKEND);
test_open_r (TREXIO_FILE, TEST_BACKEND);
test_open_auto (TREXIO_FILE);
test_open_errors(TEST_BACKEND);
test_inquire (TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,163 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "open"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "open.c"
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_open.dir"
#define TREXIO_VOID "non_existing_" TREXIO_FILE
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " TREXIO_FILE
static int test_open_w (const char* file_name, const back_end_t backend) {
/* Try to open the TREXIO file in 'write' mode */
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);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_r (const char* file_name, const back_end_t backend) {
/* Try to open the TREXIO file in 'read' mode */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_auto (const char* file_name) {
/* Try to open the TREXIO file in 'read' mode */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'r', TREXIO_AUTO, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_errors (const back_end_t backend) {
/* Try to call trexio_open with bad arguments */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open non-existing file in 'r' (read) mode, should return TREXIO_OPEN_ERROR
file = trexio_open(TREXIO_VOID, 'r', backend, &rc);
assert (file == NULL);
assert (rc == TREXIO_OPEN_ERROR);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
// open file with empty file name, should return TREXIO_INVALID_ARG_1
file = trexio_open("", 'w', backend, &rc);
assert (file == NULL);
assert (rc == TREXIO_INVALID_ARG_1);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
// open existing file in non-supported I/O mode, should return TREXIO_INVALID_ARG_2
file = trexio_open(TREXIO_FILE, 'k', backend, &rc);
assert (file == NULL);
assert (rc == TREXIO_INVALID_ARG_2);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
// open existing file with non-supported back end, should return TREXIO_INVALID_ARG_3
file = trexio_open(TREXIO_FILE, 'w', 666, &rc);
assert (file == NULL);
assert (rc == TREXIO_INVALID_ARG_3);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
/*================= END OF TEST ==================*/
return 0;
}
static int test_inquire (const back_end_t backend) {
/* Try to call trexio_inquire function */
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// inquire non-existing file
rc = trexio_inquire(TREXIO_VOID);
assert (rc == TREXIO_FAILURE);
// inquire existing file
rc = trexio_inquire(TREXIO_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_open_w (TREXIO_FILE, TEST_BACKEND);
test_open_r (TREXIO_FILE, TEST_BACKEND);
test_open_auto (TREXIO_FILE);
test_open_errors(TEST_BACKEND);
test_inquire (TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

262
tests/overwrite_all.c Normal file
View File

@ -0,0 +1,262 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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, &rc);
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_unsafe (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file which is open in UNSAFE mode*/
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be written
int num = 5;
double coord[15] = {
0.00000000 , 666.666 , 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
};
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, 'u', backend, &rc);
assert (file != NULL);
// check that the previously written data cannot be overwritten
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, 16);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_label(file, labels, 4);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_overwrite_safe (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file which is open in SAFE mode*/
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, &rc);
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 test_read(const char* file_name, const back_end_t backend) {
/*========= Test read ===========*/
trexio_t* file = NULL;
trexio_exit_code rc;
int num;
double* coord;
char** label;
char* point_group;
/*================= START OF TEST ==================*/
// open existing file on 'read' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read nucleus_num
rc = trexio_read_nucleus_num(file,&num);
assert (rc == TREXIO_SUCCESS);
assert (num == 5);
// read nucleus_coord
coord = (double*) calloc(3*num, sizeof(double));
rc = trexio_read_nucleus_coord(file,coord);
assert (rc == TREXIO_SUCCESS);
double x = coord[1] - 666.666;
assert( x*x < 1.e-14);
free(coord);
// read nucleus_label
label = (char**) malloc(num*sizeof(char*));
for (int i=0; i<num; i++){
label[i] = (char*) malloc(32*sizeof(char));
}
rc = trexio_read_nucleus_label(file, label, 2);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(label[0], "Ru") == 0);
assert (strcmp(label[3], "Na") == 0);
for (int i=0; i<num; i++){
free(label[i]);
}
free(label);
point_group = (char*) malloc(32*sizeof(char));
rc = trexio_read_nucleus_point_group(file, point_group, 10);
assert (rc == TREXIO_SUCCESS);
char * pch;
pch = strtok(point_group, " ");
assert (strcmp(pch, "Unknown") == 0);
/* alternative test when 3 symbols are read from the file to point_group */
/*rc = trexio_read_nucleus_point_group(file, point_group, 3);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(point_group, "B3U") == 0 );*/
free(point_group);
// 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_safe (TREXIO_FILE, TEST_BACKEND);
test_overwrite_unsafe (TREXIO_FILE, TEST_BACKEND);
test_read (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,266 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_HDF5
#include <assert.h> #define TREXIO_FILE_PREFIX "overwrite_all"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "overwrite_all.c"
#include <string.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test_over.h5"
#define RM_COMMAND "rm -f -- " 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, &rc);
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_unsafe (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file which is open in UNSAFE mode*/
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be written
int num = 5;
double coord[15] = {
0.00000000 , 666.666 , 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
};
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, 'u', backend, &rc);
assert (file != NULL);
// check that the previously written data cannot be overwritten
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, 16);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_label(file, labels, 4);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_overwrite_safe (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file which is open in SAFE mode*/
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, &rc);
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 test_read(const char* file_name, const back_end_t backend) {
/*========= Test read ===========*/
trexio_t* file = NULL;
trexio_exit_code rc;
int num;
double* coord;
char** label;
char* point_group;
/*================= START OF TEST ==================*/
// open existing file on 'read' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read nucleus_num
rc = trexio_read_nucleus_num(file,&num);
assert (rc == TREXIO_SUCCESS);
assert (num == 5);
// read nucleus_coord
coord = (double*) calloc(3*num, sizeof(double));
rc = trexio_read_nucleus_coord(file,coord);
assert (rc == TREXIO_SUCCESS);
double x = coord[1] - 666.666;
assert( x*x < 1.e-14);
free(coord);
// read nucleus_label
label = (char**) malloc(num*sizeof(char*));
for (int i=0; i<num; i++){
label[i] = (char*) malloc(32*sizeof(char));
}
rc = trexio_read_nucleus_label(file, label, 2);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(label[0], "Ru") == 0);
assert (strcmp(label[3], "Na") == 0);
for (int i=0; i<num; i++){
free(label[i]);
}
free(label);
point_group = (char*) malloc(32*sizeof(char));
rc = trexio_read_nucleus_point_group(file, point_group, 10);
assert (rc == TREXIO_SUCCESS);
char * pch;
pch = strtok(point_group, " ");
assert (strcmp(pch, "Unknown") == 0);
/* alternative test when 3 symbols are read from the file to point_group */
/*rc = trexio_read_nucleus_point_group(file, point_group, 3);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(point_group, "B3U") == 0 );*/
free(point_group);
// 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_safe (TREXIO_FILE, TEST_BACKEND);
test_overwrite_unsafe (TREXIO_FILE, TEST_BACKEND);
test_read (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

View File

@ -1,266 +1,5 @@
#include "trexio.h" #define TEST_BACKEND_TEXT
#include <assert.h> #define TREXIO_FILE_PREFIX "overwrite_all"
#include <stdio.h> #include "test_macros.h"
#include <stdlib.h> #include "overwrite_all.c"
#include <string.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_over.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " 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, &rc);
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_unsafe (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file which is open in UNSAFE mode*/
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be written
int num = 5;
double coord[15] = {
0.00000000 , 666.666 , 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
};
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, 'u', backend, &rc);
assert (file != NULL);
// check that the previously written data cannot be overwritten
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, 16);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_label(file, labels, 4);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_overwrite_safe (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file which is open in SAFE mode*/
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, &rc);
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 test_read(const char* file_name, const back_end_t backend) {
/*========= Test read ===========*/
trexio_t* file = NULL;
trexio_exit_code rc;
int num;
double* coord;
char** label;
char* point_group;
/*================= START OF TEST ==================*/
// open existing file on 'read' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read nucleus_num
rc = trexio_read_nucleus_num(file,&num);
assert (rc == TREXIO_SUCCESS);
assert (num == 5);
// read nucleus_coord
coord = (double*) calloc(3*num, sizeof(double));
rc = trexio_read_nucleus_coord(file,coord);
assert (rc == TREXIO_SUCCESS);
double x = coord[1] - 666.666;
assert( x*x < 1.e-14);
free(coord);
// read nucleus_label
label = (char**) malloc(num*sizeof(char*));
for (int i=0; i<num; i++){
label[i] = (char*) malloc(32*sizeof(char));
}
rc = trexio_read_nucleus_label(file, label, 2);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(label[0], "Ru") == 0);
assert (strcmp(label[3], "Na") == 0);
for (int i=0; i<num; i++){
free(label[i]);
}
free(label);
point_group = (char*) malloc(32*sizeof(char));
rc = trexio_read_nucleus_point_group(file, point_group, 10);
assert (rc == TREXIO_SUCCESS);
char * pch;
pch = strtok(point_group, " ");
assert (strcmp(pch, "Unknown") == 0);
/* alternative test when 3 symbols are read from the file to point_group */
/*rc = trexio_read_nucleus_point_group(file, point_group, 3);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(point_group, "B3U") == 0 );*/
free(point_group);
// 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_safe (TREXIO_FILE, TEST_BACKEND);
test_overwrite_unsafe (TREXIO_FILE, TEST_BACKEND);
test_read (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

5
tests/template_hdf5.c Normal file
View File

@ -0,0 +1,5 @@
#define TEST_BACKEND_HDF5
#define TREXIO_FILE_PREFIX "REPLACE"
#include "test_macros.h"
#include "REPLACE.c"

5
tests/template_text.c Normal file
View File

@ -0,0 +1,5 @@
#define TEST_BACKEND_TEXT
#define TREXIO_FILE_PREFIX "REPLACE"
#include "test_macros.h"
#include "REPLACE.c"

20
tests/test_macros.h Normal file
View File

@ -0,0 +1,20 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef TEST_BACKEND_HDF5
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE TREXIO_FILE_PREFIX ".h5"
#define RM_COMMAND "rm -f -- " TREXIO_FILE
#endif
#ifdef TEST_BACKEND_TEXT
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE TREXIO_FILE_PREFIX ".dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " TREXIO_FILE
#endif