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

add C unit tests

This commit is contained in:
q-posev 2021-06-21 12:55:16 +02:00
parent c010331114
commit 5cc1d13c36
11 changed files with 1454 additions and 8 deletions

View File

@ -75,9 +75,19 @@ src_libtrexio_la_SOURCES = $(SOURCES)
# section related to tests # section related to tests
TESTS_C = \ TESTS_C = \
tests/io_all \
tests/io_num_hdf5 \ tests/io_num_hdf5 \
tests/io_num_text tests/io_num_text \
tests/io_dset_float_hdf5 \
tests/io_dset_float_text \
tests/io_dset_int_hdf5 \
tests/io_dset_int_text \
tests/io_str_hdf5 \
tests/io_str_text \
tests/io_dset_str_hdf5 \
tests/io_dset_str_text \
tests/overwrite_all_hdf5 \
tests/overwrite_all_text \
tests/io_all
TESTS_F = \ TESTS_F = \
tests/test_f tests/test_f
@ -89,19 +99,29 @@ check_PROGRAMS = $(TESTS)
# specify common options for all tests # specify common options for all tests
LDADD = src/libtrexio.la LDADD = src/libtrexio.la
# in principal, specifying -no-install (see example below) is not mandatory
# in principal, specifying -no-install (see example below) for each test provides better # for the tests to compile and pass, but the compilations itself differs
# and faster (?) `make check` but becomes tedious as the number of tests increases tests_io_num_hdf5_LDFLAGS = -no-install
tests_io_num_text_LDFLAGS = -no-install
#tests_io_all_LDFLAGS = -no-install tests_io_dset_float_hdf5_LDFLAGS = -no-install
tests_io_dset_float_text_LDFLAGS = -no-install
tests_io_dset_int_hdf5_LDFLAGS = -no-install
tests_io_dset_int_text_LDFLAGS = -no-install
tests_io_str_hdf5_LDFLAGS = -no-install
tests_io_str_text_LDFLAGS = -no-install
tests_io_dset_str_hdf5_LDFLAGS = -no-install
tests_io_dset_str_text_LDFLAGS = -no-install
tests_overwrite_all_hdf5_LDFLAGS = -no-install
tests_overwrite_all_text_LDFLAGS = -no-install
tests_io_all_LDFLAGS = -no-install
test_trexio_f = $(srcdir)/tests/trexio_f.f90 test_trexio_f = $(srcdir)/tests/trexio_f.f90
$(test_trexio_f): $(trexio_f) $(test_trexio_f): $(trexio_f)
cp $(trexio_f) $(test_trexio_f) cp $(trexio_f) $(test_trexio_f)
tests_test_f_SOURCES = $(test_trexio_f) tests/test_f.f90 tests_test_f_SOURCES = $(test_trexio_f) tests/test_f.f90
tests_test_f_LDFLAGS = -no-install
if TREXIO_DEVEL if TREXIO_DEVEL

148
tests/io_dset_float_hdf5.c Normal file
View File

@ -0,0 +1,148 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test.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);
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);
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);
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;
}

148
tests/io_dset_float_text.c Normal file
View File

@ -0,0 +1,148 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test.dir"
#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);
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);
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);
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;
}

134
tests/io_dset_int_hdf5.c Normal file
View File

@ -0,0 +1,134 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test.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] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_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);
assert (file != NULL);
// 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);
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 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 );
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;
}

134
tests/io_dset_int_text.c Normal file
View File

@ -0,0 +1,134 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test.dir"
#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] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_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);
assert (file != NULL);
// 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);
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 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 );
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;
}

157
tests/io_dset_str_hdf5.c Normal file
View File

@ -0,0 +1,157 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test.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);
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);
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);
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;
}

157
tests/io_dset_str_text.c Normal file
View File

@ -0,0 +1,157 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test.dir"
#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);
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);
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);
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;
}

133
tests/io_str_hdf5.c Normal file
View File

@ -0,0 +1,133 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test.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);
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);
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);
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;
}

133
tests/io_str_text.c Normal file
View File

@ -0,0 +1,133 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test.dir"
#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);
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);
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);
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;
}

141
tests/overwrite_all_hdf5.c Normal file
View File

@ -0,0 +1,141 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_BACKEND TREXIO_HDF5
#define TREXIO_FILE "test.h5"
#define RM_COMMAND "rm -rf " 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);
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 (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file */
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);
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 main(void) {
/*============== Test launcher ================*/
int rc;
rc = system(RM_COMMAND);
assert (rc == 0);
test_write (TREXIO_FILE, TEST_BACKEND);
test_overwrite (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

141
tests/overwrite_all_text.c Normal file
View File

@ -0,0 +1,141 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test.dir"
#define RM_COMMAND "rm -rf " 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);
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 (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file */
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);
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 main(void) {
/*============== Test launcher ================*/
int rc;
rc = system(RM_COMMAND);
assert (rc == 0);
test_write (TREXIO_FILE, TEST_BACKEND);
test_overwrite (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}