1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-23 04:43:57 +01:00
trexio/tests/io_all.c

228 lines
6.0 KiB
C
Raw Normal View History

2021-11-01 11:57:03 +01:00
2021-03-02 13:56:30 +01:00
#include "trexio.h"
2021-02-20 18:16:10 +01:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
2021-05-31 16:37:00 +02:00
#include <string.h>
2021-02-20 18:16:10 +01:00
2021-06-02 10:27:11 +02:00
int test_write(const char* file_name, const back_end_t backend);
int test_read(const char* file_name, const back_end_t backend);
2021-02-20 18:16:10 +01:00
int main() {
2021-03-03 02:28:00 +01:00
/*============== Main test launcher ================*/
2021-06-01 15:03:37 +02:00
int rc;
2022-01-20 14:22:32 +01:00
trexio_info();
bool have_hdf5 = trexio_has_backend(TREXIO_HDF5);
if(have_hdf5) {
rc = system("rm -f -- test_all.h5");
assert (rc == 0);
test_write("test_all.h5", TREXIO_HDF5);
test_read ("test_all.h5", TREXIO_HDF5);
rc = system("rm -f -- test_all.h5");
assert (rc == 0);
}
2021-03-03 02:28:00 +01:00
2021-06-21 12:51:21 +02:00
rc = system("rm -rf test_all.dir");
2021-06-01 15:03:37 +02:00
assert (rc == 0);
2021-06-21 12:51:21 +02:00
test_write("test_all.dir", TREXIO_TEXT);
test_read ("test_all.dir", TREXIO_TEXT);
rc = system("rm -rf test_all.dir");
2021-06-04 18:14:45 +02:00
assert (rc == 0);
2021-06-01 15:03:37 +02:00
return 0;
2021-02-20 18:16:10 +01:00
}
2021-06-01 15:03:37 +02:00
int test_write(const char* file_name, const back_end_t backend) {
2021-03-02 11:27:09 +01:00
2021-06-01 15:03:37 +02:00
/*======== Test write ===========*/
2021-03-02 13:56:30 +01:00
trexio_t* file = NULL;
trexio_exit_code rc;
2021-03-02 11:27:09 +01:00
// parameters to be written
int num = 12;
double charge[12] = {6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.};
2021-03-02 11:27:09 +01:00
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 ,
};
2021-05-31 16:37:00 +02:00
const char* label[] = {"C" ,
"Na",
"ClH asdasdasdas" ,
2021-05-31 16:37:00 +02:00
"C" ,
"C 666" ,
2021-05-31 16:37:00 +02:00
"C" ,
"H" ,
"Ru",
"H" ,
"H 999asdasd" ,
2021-05-31 16:37:00 +02:00
"H" ,
"H" };
2022-01-20 14:22:32 +01:00
2021-06-21 12:51:21 +02:00
const char* sym = "B3U with some comments";
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
2021-03-03 02:28:00 +01:00
assert (file != NULL);
2021-03-02 11:27:09 +01:00
// check that certain data does not exist in the file
2021-03-30 11:56:04 +02:00
rc = trexio_has_nucleus_num(file);
assert (rc == TREXIO_HAS_NOT);
rc = trexio_has_nucleus_coord(file);
assert (rc == TREXIO_HAS_NOT);
// write info in an empty file
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_num(file,num);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_coord(file,coord);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-06-15 11:26:22 +02:00
rc = trexio_write_nucleus_label(file, label, 32);
2021-05-31 16:37:00 +02:00
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_point_group(file, sym, 32);
2021-06-04 15:36:56 +02:00
assert (rc == TREXIO_SUCCESS);
2022-01-20 14:22:32 +01:00
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
2022-01-20 14:22:32 +01:00
// reopen file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// check if the written data exists in the file
2021-03-30 11:56:04 +02:00
rc = trexio_has_nucleus_num(file);
assert (rc == TREXIO_SUCCESS);
rc = trexio_has_nucleus_coord(file);
assert (rc == TREXIO_SUCCESS);
rc = trexio_has_nucleus_label(file);
2021-05-31 16:37:00 +02:00
assert (rc == TREXIO_SUCCESS);
rc = trexio_has_nucleus_point_group(file);
2021-06-04 15:36:56 +02:00
assert (rc == TREXIO_SUCCESS);
2021-03-30 11:56:04 +02:00
// should not work: try to overwrite the num variable
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_num(file,25);
2021-06-01 15:03:37 +02:00
printf("Test error message: %s\n", trexio_string_of_error(rc));
2021-06-21 12:51:21 +02:00
assert (rc == TREXIO_ATTR_ALREADY_EXISTS);
2021-03-02 11:27:09 +01:00
// should not work: try to overwrite the dset
2021-03-02 11:41:25 +01:00
coord[0] = 666.666;
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_coord(file,coord);
2021-06-02 14:24:27 +02:00
assert (rc == TREXIO_DSET_ALREADY_EXISTS);
2021-03-02 11:27:09 +01:00
2021-03-03 11:49:42 +01:00
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// open file again in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
2021-03-03 11:49:42 +01:00
assert (file != NULL);
// write some missing blocks (e.g. if forgot last time)
rc = trexio_write_nucleus_charge(file,charge);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 11:27:09 +01:00
// close current session
2021-03-03 02:28:00 +01:00
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
2021-03-02 11:27:09 +01:00
/*================= END OF TEST ==================*/
2021-03-02 11:27:09 +01:00
return 0;
}
2021-06-01 15:03:37 +02:00
int test_read(const char* file_name, const back_end_t backend) {
2021-06-01 15:03:37 +02:00
/*========= Test read ===========*/
2021-03-02 13:56:30 +01:00
trexio_t* file = NULL;
trexio_exit_code rc;
int num;
double* coord;
2021-05-31 16:37:00 +02:00
char** label;
2021-06-04 18:42:52 +02:00
char* point_group;
/*================= START OF TEST ==================*/
2021-06-01 15:03:37 +02:00
// open existing file on 'read' mode [created by test_write]
file = trexio_open(file_name, 'r', TREXIO_AUTO, &rc);
2021-03-03 02:28:00 +01:00
assert (file != NULL);
// read nucleus_num
2021-03-02 13:56:30 +01:00
rc = trexio_read_nucleus_num(file,&num);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 11:41:25 +01:00
assert (num == 12);
// read nucleus_coord
coord = (double*) calloc(3*num, sizeof(double));
2021-03-02 13:56:30 +01:00
rc = trexio_read_nucleus_coord(file,coord);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 13:56:30 +01:00
2021-03-02 11:41:25 +01:00
double x = coord[30] - 2.14171677;
assert( x*x < 1.e-14);
2021-06-15 11:26:22 +02:00
free(coord);
2021-05-31 16:37:00 +02:00
// read nucleus_label
label = (char**) malloc(num*sizeof(char*));
for (int i=0; i<num; i++){
label[i] = (char*) malloc(32*sizeof(char));
}
2021-06-15 11:26:22 +02:00
rc = trexio_read_nucleus_label(file, label, 2);
2021-06-15 11:26:22 +02:00
assert (rc == TREXIO_SUCCESS);
assert (strcmp(label[0], "C") == 0);
assert (strcmp(label[1], "Na") == 0);
2022-01-20 14:22:32 +01:00
for (int i=0; i<num; i++){
2021-06-15 11:26:22 +02:00
free(label[i]);
}
2021-06-15 11:26:22 +02:00
free(label);
2021-05-31 16:37:00 +02:00
2021-06-04 18:42:52 +02:00
point_group = (char*) malloc(32*sizeof(char));
2021-06-04 15:36:56 +02:00
rc = trexio_read_nucleus_point_group(file, point_group, 6);
2021-06-04 15:36:56 +02:00
assert (rc == TREXIO_SUCCESS);
2021-06-15 11:26:22 +02:00
char * pch;
pch = strtok(point_group, " ");
2021-06-15 11:26:22 +02:00
assert (strcmp(pch, "B3U") == 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);
2021-06-15 11:26:22 +02:00
assert (strcmp(point_group, "B3U") == 0 );*/
2021-06-04 18:42:52 +02:00
free(point_group);
2021-06-04 15:36:56 +02:00
// close current session
2021-03-03 02:28:00 +01:00
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
2021-02-20 18:16:10 +01:00
// read non-existing file, should fail and return NULL
2021-06-01 15:03:37 +02:00
const char* file_name2 = "test_nonexisting";
trexio_t* file2 = NULL;
2021-02-20 18:16:10 +01:00
file2 = trexio_open(file_name2, 'r', backend, &rc);
assert (file2 == NULL);
2021-02-20 18:16:10 +01:00
/*================= END OF TEST =====================*/
2022-01-20 14:22:32 +01:00
2021-02-20 18:16:10 +01:00
return 0;
}