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

Merge pull request #52 from TREX-CoE/unit-testing

Unit testing
This commit is contained in:
Anthony Scemama 2021-06-22 22:31:58 +02:00 committed by GitHub
commit 6637cfd6ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 1983 additions and 88 deletions

View File

@ -30,8 +30,11 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# =============== SETTINGS =============== #
ACLOCAL_AMFLAGS = -I m4
CLEANFILES = trexio.mod
BUILT_SOURCES = trexio.mod
VERSION_MAJOR = @VERSION_MAJOR@
VERSION_MINOR = @VERSION_MINOR@
@ -42,6 +45,7 @@ SUBDIRS =
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = pkgconfig/trexio.pc
# =============== BUILD =============== #
trexio_h = $(srcdir)/include/trexio.h
trexio_f = $(srcdir)/include/trexio_f.f90
@ -54,14 +58,14 @@ lib_LTLIBRARIES = src/libtrexio.la
SOURCES = \
$(trexio_h) \
src/trexio.c \
src/trexio_private.h \
src/trexio_s.h \
src/trexio_hdf5.c \
src/trexio_hdf5.h \
src/trexio_text.c \
src/trexio_text.h
$(trexio_h) \
src/trexio.c \
src/trexio_private.h \
src/trexio_s.h \
src/trexio_hdf5.c \
src/trexio_hdf5.h \
src/trexio_text.c \
src/trexio_text.h
ORG_FILES = \
src/templates_front/templator_front.org \
@ -72,21 +76,67 @@ ORG_FILES = \
src_libtrexio_la_SOURCES = $(SOURCES)
TESTS = tests/test_c tests/test_f
# =============== TESTS =============== #
TESTS_C = \
tests/open_hdf5 \
tests/open_text \
tests/io_num_hdf5 \
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/test_f
TESTS = $(TESTS_C) $(TESTS_F)
check_PROGRAMS = $(TESTS)
tests_test_c_DEPENDENCIES = $(trexio_h)
tests_test_c_SOURCES = tests/test.c $(trexio_h)
tests_test_c_LDADD = src/libtrexio.la
tests_test_c_LDFLAGS = -no-install
# specify common LDADD options for all tests
LDADD = src/libtrexio.la
# in principal, specifying -no-install (see below) is not mandatory
# for the tests to compile and pass, but the produced test binaries differ
tests_open_hdf5_LDFLAGS = -no-install
tests_open_text_LDFLAGS = -no-install
tests_io_num_hdf5_LDFLAGS = -no-install
tests_io_num_text_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
tests_test_f_DEPENDENCIES = $(test_trexio_f)
$(test_trexio_f): $(trexio_f)
cp $(trexio_f) $(test_trexio_f)
trexio.mod: tests/trexio_f.o
tests_test_f_SOURCES = $(test_trexio_f) tests/test_f.f90
tests_test_f_LDADD = src/libtrexio.la
tests_test_f_LDFLAGS = -no-install
clean-local:
-rm -rf -- *.dir/ *.h5
# =============== DOCUMENTATION =============== #
HTML_FILES = docs/trexio.css \
docs/index.html \
docs/Sparse.html \
@ -101,17 +151,13 @@ dist_html_DATA = $(HTML_FILES)
$(HTML_FILES): docs/index.html
$(test_trexio_f): $(trexio_f)
cp $(trexio_f) $(test_trexio_f)
clean-local:
-rm -rf -- test_write.dir/ test_write_f.dir/ test_write.h5 test_write_f.h5
# =============== DEVELOPER MODE =============== #
if TREXIO_DEVEL
CLEANFILES += $(SOURCES) $(trexio_f) $(trexio_h)
BUILT_SOURCES = $(SOURCES) $(trexio_f) $(test_trexio_f)
BUILT_SOURCES += $(SOURCES) $(trexio_f) $(test_trexio_f)
$(SOURCES): $(trexio_f)
@ -120,7 +166,7 @@ src/trexio.c: $(trexio_h)
$(trexio_f): $(ORG_FILES)
cd $(srcdir)/tools && ./build_trexio.sh
docs/index.html: $(SOURCES) $(srcdir)/src/README.org
docs/index.html: $(ORG_FILES) $(srcdir)/src/README.org
cd $(srcdir)/tools && ./build_doc.sh
cppcheck: cppcheck.out
@ -136,3 +182,4 @@ cppcheck.out: $(trexio_h)
.PHONY: cppcheck
endif

View File

@ -120,33 +120,33 @@ typedef int32_t trexio_exit_code;
** Error handling
#+NAME: table-exit-codes
| Macro | Code | Description |
|------------------------------+------+----------------------------------------|
| ~TREXIO_FAILURE~ | -1 | 'Unknown failure' |
| ~TREXIO_SUCCESS~ | 0 | 'Success' |
| ~TREXIO_INVALID_ARG_1~ | 1 | 'Invalid argument 1' |
| ~TREXIO_INVALID_ARG_2~ | 2 | 'Invalid argument 2' |
| ~TREXIO_INVALID_ARG_3~ | 3 | 'Invalid argument 3' |
| ~TREXIO_INVALID_ARG_4~ | 4 | 'Invalid argument 4' |
| ~TREXIO_INVALID_ARG_5~ | 5 | 'Invalid argument 5' |
| ~TREXIO_END~ | 6 | 'End of file' |
| ~TREXIO_READONLY~ | 7 | 'Read-only file' |
| ~TREXIO_ERRNO~ | 8 | strerror(errno) |
| ~TREXIO_INVALID_ID~ | 9 | 'Invalid ID' |
| ~TREXIO_ALLOCATION_FAILED~ | 10 | 'Allocation failed' |
| ~TREXIO_HAS_NOT~ | 11 | 'Element absent' |
| ~TREXIO_INVALID_NUM~ | 12 | 'Invalid dimensions' |
| ~TREXIO_NUM_ALREADY_EXISTS~ | 13 | 'Dimensioning variable already exists' |
| ~TREXIO_DSET_ALREADY_EXISTS~ | 14 | 'Dataset already exists' |
| ~TREXIO_OPEN_ERROR~ | 15 | 'Error opening file' |
| ~TREXIO_LOCK_ERROR~ | 16 | 'Error locking file' |
| ~TREXIO_UNLOCK_ERROR~ | 17 | 'Error unlocking file' |
| ~TREXIO_FILE_ERROR~ | 18 | 'Invalid file handle' |
| ~TREXIO_GROUP_READ_ERROR~ | 19 | 'Error reading group' |
| ~TREXIO_GROUP_WRITE_ERROR~ | 20 | 'Error writing group' |
| ~TREXIO_ELEM_READ_ERROR~ | 21 | 'Error reading element' |
| ~TREXIO_ELEM_WRITE_ERROR~ | 22 | 'Error writing element' |
| ~TREXIO_INVALID_STR_LEN~ | 30 | 'Invalid max_str_len' |
| Macro | Code | Description |
|------------------------------+------+--------------------------------------|
| ~TREXIO_FAILURE~ | -1 | 'Unknown failure' |
| ~TREXIO_SUCCESS~ | 0 | 'Success' |
| ~TREXIO_INVALID_ARG_1~ | 1 | 'Invalid argument 1' |
| ~TREXIO_INVALID_ARG_2~ | 2 | 'Invalid argument 2' |
| ~TREXIO_INVALID_ARG_3~ | 3 | 'Invalid argument 3' |
| ~TREXIO_INVALID_ARG_4~ | 4 | 'Invalid argument 4' |
| ~TREXIO_INVALID_ARG_5~ | 5 | 'Invalid argument 5' |
| ~TREXIO_END~ | 6 | 'End of file' |
| ~TREXIO_READONLY~ | 7 | 'Read-only file' |
| ~TREXIO_ERRNO~ | 8 | strerror(errno) |
| ~TREXIO_INVALID_ID~ | 9 | 'Invalid ID' |
| ~TREXIO_ALLOCATION_FAILED~ | 10 | 'Allocation failed' |
| ~TREXIO_HAS_NOT~ | 11 | 'Element absent' |
| ~TREXIO_INVALID_NUM~ | 12 | 'Invalid dimensions' |
| ~TREXIO_ATTR_ALREADY_EXISTS~ | 13 | 'Attribute (num/str) already exists' |
| ~TREXIO_DSET_ALREADY_EXISTS~ | 14 | 'Dataset already exists' |
| ~TREXIO_OPEN_ERROR~ | 15 | 'Error opening file' |
| ~TREXIO_LOCK_ERROR~ | 16 | 'Error locking file' |
| ~TREXIO_UNLOCK_ERROR~ | 17 | 'Error unlocking file' |
| ~TREXIO_FILE_ERROR~ | 18 | 'Invalid file handle' |
| ~TREXIO_GROUP_READ_ERROR~ | 19 | 'Error reading group' |
| ~TREXIO_GROUP_WRITE_ERROR~ | 20 | 'Error writing group' |
| ~TREXIO_ELEM_READ_ERROR~ | 21 | 'Error reading element' |
| ~TREXIO_ELEM_WRITE_ERROR~ | 22 | 'Error writing element' |
| ~TREXIO_INVALID_STR_LEN~ | 30 | 'Invalid max_str_len' |
# We need to force Emacs not to indent the Python code:
# -*- org-src-preserve-indentation: t
@ -192,7 +192,7 @@ return '\n'.join(result)
#define TREXIO_ALLOCATION_FAILED ((trexio_exit_code) 10)
#define TREXIO_HAS_NOT ((trexio_exit_code) 11)
#define TREXIO_INVALID_NUM ((trexio_exit_code) 12)
#define TREXIO_NUM_ALREADY_EXISTS ((trexio_exit_code) 13)
#define TREXIO_ATTR_ALREADY_EXISTS ((trexio_exit_code) 13)
#define TREXIO_DSET_ALREADY_EXISTS ((trexio_exit_code) 14)
#define TREXIO_OPEN_ERROR ((trexio_exit_code) 15)
#define TREXIO_LOCK_ERROR ((trexio_exit_code) 16)
@ -220,7 +220,7 @@ return '\n'.join(result)
integer(trexio_exit_code), parameter :: TREXIO_ALLOCATION_FAILED = 10
integer(trexio_exit_code), parameter :: TREXIO_HAS_NOT = 11
integer(trexio_exit_code), parameter :: TREXIO_INVALID_NUM = 12
integer(trexio_exit_code), parameter :: TREXIO_NUM_ALREADY_EXISTS = 13
integer(trexio_exit_code), parameter :: TREXIO_ATTR_ALREADY_EXISTS = 13
integer(trexio_exit_code), parameter :: TREXIO_DSET_ALREADY_EXISTS = 14
integer(trexio_exit_code), parameter :: TREXIO_OPEN_ERROR = 15
integer(trexio_exit_code), parameter :: TREXIO_LOCK_ERROR = 16
@ -860,7 +860,7 @@ trexio_write_$group_num$_64 (trexio_t* const file, const int64_t num)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num < 0 ) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_NUM_ALREADY_EXISTS;
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS;
switch (file->back_end) {
@ -921,7 +921,7 @@ trexio_write_$group_num$_32 (trexio_t* const file, const int32_t num)
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num < 0 ) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_NUM_ALREADY_EXISTS;
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS;
switch (file->back_end) {
@ -1981,7 +1981,7 @@ trexio_write_$group_str$ (trexio_t* const file, const char* str, const uint32_t
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (str == NULL) return TREXIO_INVALID_ARG_2;
if (max_str_len <= 0) return TREXIO_INVALID_ARG_3;
if (trexio_has_$group_str$(file) == TREXIO_SUCCESS) return TREXIO_NUM_ALREADY_EXISTS;
if (trexio_has_$group_str$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS;
size_t len_write = strlen(str);
if (max_str_len < len_write) return TREXIO_INVALID_STR_LEN;

View File

@ -233,25 +233,16 @@ trexio_hdf5_write_$group_num$ (trexio_t* const file, const uint64_t num)
trexio_exit_code rc = trexio_hdf5_read_$group_num$(file, &(infile_num));
if (rc != TREXIO_SUCCESS) return rc;
if (infile_num != num) {
const hid_t dtype = H5Tcopy(H5T_NATIVE_UINT64);
const hid_t num_id = H5Aopen(f->$group$_group, $GROUP_NUM$_NAME, H5P_DEFAULT);
if (num_id <= 0) return TREXIO_INVALID_ID;
const herr_t status = H5Awrite(num_id, dtype, &(num));
if (status < 0) return TREXIO_FAILURE;
H5Aclose(num_id);
H5Tclose(dtype);
if (infile_num != 0) {
return TREXIO_NUM_ALREADY_EXISTS;
} else {
const hid_t dtype = H5Tcopy(H5T_NATIVE_UINT64);
const hid_t num_id = H5Aopen(f->$group$_group, $GROUP_NUM$_NAME, H5P_DEFAULT);
if (num_id <= 0) return TREXIO_INVALID_ID;
const herr_t status = H5Awrite(num_id, dtype, &(num));
if (status < 0) return TREXIO_FAILURE;
H5Aclose(num_id);
H5Tclose(dtype);
}
}
return TREXIO_SUCCESS;
}

13
tests/.gitignore vendored
View File

@ -1,14 +1,15 @@
libtrexio.so
trexio_f.o
# Ignore all
*
# Unignore all with extensions
!*.*
trexio_f.f90
trexio.mod
test_c
test_f
*.o
*.log
*.trs
.dirstamp
*.h5
*/

View File

@ -12,18 +12,18 @@ int main() {
/*============== Main test launcher ================*/
int rc;
rc = system("rm -rf test_write.h5");
rc = system("rm -rf test_all.h5");
assert (rc == 0);
test_write("test_write.h5", TREXIO_HDF5);
test_read ("test_write.h5", TREXIO_HDF5);
rc = system("rm -rf test_write.h5");
test_write("test_all.h5", TREXIO_HDF5);
test_read ("test_all.h5", TREXIO_HDF5);
rc = system("rm -rf test_all.h5");
assert (rc == 0);
rc = system("rm -rf test_write.dir");
rc = system("rm -rf test_all.dir");
assert (rc == 0);
test_write("test_write.dir", TREXIO_TEXT);
test_read ("test_write.dir", TREXIO_TEXT);
rc = system("rm -rf test_write.dir");
test_write("test_all.dir", TREXIO_TEXT);
test_read ("test_all.dir", TREXIO_TEXT);
rc = system("rm -rf test_all.dir");
assert (rc == 0);
return 0;
@ -68,7 +68,7 @@ int test_write(const char* file_name, const back_end_t backend) {
"H" ,
"H" };
const char* sym = "B3U and some stuff";
const char* sym = "B3U with some comments";
/*================= START OF TEST ==================*/
// open file in 'write' mode
@ -113,7 +113,7 @@ int test_write(const char* file_name, const back_end_t backend) {
// should not work: try to overwrite the num variable
rc = trexio_write_nucleus_num(file,25);
printf("Test error message: %s\n", trexio_string_of_error(rc));
assert (rc == TREXIO_NUM_ALREADY_EXISTS);
assert (rc == TREXIO_ATTR_ALREADY_EXISTS);
// should not work: try to overwrite the dset
coord[0] = 666.666;

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_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);
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_dset_f.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_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] = {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_dset_i.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_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);
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_dset_s.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;
}

120
tests/io_num_hdf5.c Normal file
View File

@ -0,0 +1,120 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#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);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_num(file, num);
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);
assert (file != NULL);
// check that the previously written num variable exists
rc = trexio_has_nucleus_num(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;
/*================= 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);
// 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;
}

120
tests/io_num_text.c Normal file
View File

@ -0,0 +1,120 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_num.dir"
#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);
assert (file != NULL);
// write numerical attribute in an empty file
rc = trexio_write_nucleus_num(file, num);
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);
assert (file != NULL);
// check that the previously written num variable exists
rc = trexio_has_nucleus_num(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;
/*================= 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);
// 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;
}

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_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);
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_str.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;
}

95
tests/open_hdf5.c Normal file
View File

@ -0,0 +1,95 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#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);
assert (file != NULL);
// 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);
assert (file != NULL);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_void (const char* file_name, const back_end_t backend) {
/* Try to open the non-existing TREXIO file in 'read' mode */
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);
/*================= 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_void (TREXIO_VOID, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}

95
tests/open_text.c Normal file
View File

@ -0,0 +1,95 @@
#include "trexio.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_open.dir"
#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);
assert (file != NULL);
// 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);
assert (file != NULL);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_open_void (const char* file_name, const back_end_t backend) {
/* Try to open the non-existing TREXIO file in 'read' mode */
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);
/*================= 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_void (TREXIO_VOID, 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_over.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_over.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;
}