1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-08-25 06:31:43 +02:00

add overwriting functionality for unsafe mode [TEXT]

This commit is contained in:
q-posev 2022-01-24 16:13:27 +01:00
parent 179e0c8dfc
commit f68a59417e
3 changed files with 146 additions and 21 deletions

View File

@ -1426,7 +1426,7 @@ trexio_write_$group_num$_64 (trexio_t* const file, const $group_num_dtype_double
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
//if (num <= 0L) return TREXIO_INVALID_NUM; /* this line is uncommented by the generator for dimensioning variables; do NOT remove! */
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS;
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS && file->mode != 'u') return TREXIO_ATTR_ALREADY_EXISTS;
switch (file->back_end) {
@ -1496,7 +1496,7 @@ trexio_write_$group_num$_32 (trexio_t* const file, const $group_num_dtype_single
if (file == NULL) return TREXIO_INVALID_ARG_1;
//if (num <= 0) return TREXIO_INVALID_NUM; /* this line is uncommented by the generator for dimensioning variables; do NOT remove! */
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS;
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS && file->mode != 'u') return TREXIO_ATTR_ALREADY_EXISTS;
switch (file->back_end) {
@ -1844,7 +1844,7 @@ trexio_write_$group_dset$_64 (trexio_t* const file, const $group_dset_dtype_doub
assert(file->back_end < TREXIO_INVALID_BACK_END);
if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) return TREXIO_DSET_ALREADY_EXISTS;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS && file->mode != 'u') return TREXIO_DSET_ALREADY_EXISTS;
trexio_exit_code rc;
@ -2015,7 +2015,7 @@ trexio_write_$group_dset$_32 (trexio_t* const file, const $group_dset_dtype_sing
if (file == NULL) return TREXIO_INVALID_ARG_1;
if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) return TREXIO_DSET_ALREADY_EXISTS;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS && file->mode != 'u') return TREXIO_DSET_ALREADY_EXISTS;
trexio_exit_code rc;
int64_t $group_dset_dim$ = 0;
@ -2127,7 +2127,7 @@ trexio_write_safe_$group_dset$_32 (trexio_t* const file, const $group_dset_dtype
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset_in == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) return TREXIO_DSET_ALREADY_EXISTS;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS && file->mode != 'u') return TREXIO_DSET_ALREADY_EXISTS;
<<dimCheck>>
@ -2162,7 +2162,7 @@ trexio_write_safe_$group_dset$_64 (trexio_t* const file, const $group_dset_dtype
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset_in == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) return TREXIO_DSET_ALREADY_EXISTS;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS && file->mode != 'u') return TREXIO_DSET_ALREADY_EXISTS;
<<dimCheck>>
@ -3288,7 +3288,7 @@ trexio_write_$group_dset$_low (trexio_t* const file, char* dset_in, const int32_
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset_in == NULL) return TREXIO_INVALID_ARG_2;
if (max_str_len <= 0) return TREXIO_INVALID_ARG_3;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) return TREXIO_DSET_ALREADY_EXISTS;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS && file->mode != 'u') return TREXIO_DSET_ALREADY_EXISTS;
trexio_exit_code rc;
int64_t $group_dset_dim$ = 0;
@ -3372,7 +3372,7 @@ trexio_write_$group_dset$ (trexio_t* const file, const char** dset_in, const int
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset_in == NULL) return TREXIO_INVALID_ARG_2;
if (max_str_len <= 0) return TREXIO_INVALID_ARG_3;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) return TREXIO_DSET_ALREADY_EXISTS;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS && file->mode != 'u') return TREXIO_DSET_ALREADY_EXISTS;
assert(file->back_end < TREXIO_INVALID_BACK_END);
@ -3694,7 +3694,7 @@ trexio_write_$group_str$ (trexio_t* const file, const char* str, const int32_t m
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_ATTR_ALREADY_EXISTS;
if (trexio_has_$group_str$(file) == TREXIO_SUCCESS && file->mode != 'u') return TREXIO_ATTR_ALREADY_EXISTS;
size_t len_write = strlen(str);
if ((size_t) max_str_len < len_write) return TREXIO_INVALID_STR_LEN;

View File

@ -578,7 +578,7 @@ trexio_text_flush_$group$ (trexio_text_t* const file)
if ($group$->to_flush == 0) return TREXIO_SUCCESS;
assert (file->parent.mode == 'w');
assert (file->parent.mode == 'w' || file->parent.mode == 'u');
FILE* f = fopen($group$->file_name, "w");
if (f == NULL) return TREXIO_INVALID_ARG_1;

View File

@ -2,6 +2,7 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_over.dir"
@ -56,13 +57,13 @@ static int test_write (const char* file_name, const back_end_t backend) {
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);
@ -73,9 +74,61 @@ static int test_write (const char* file_name, const back_end_t backend) {
}
static int test_overwrite (const char* file_name, const back_end_t backend) {
static int test_overwrite_unsafe (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file */
/* Try to overwrite the data that already exists in the TREXIO file which is open in UNSAFE mode*/
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be written
int num = 5;
double coord[15] = {
0.00000000 , 666.666 , 0.00000000 ,
-1.20594314 , 0.69625160 , 0.00000000 ,
-1.20594314 , -0.69625160 , 0.00000000 ,
0.00000000 , -1.39250319 , 0.00000000 ,
1.20594314 , -0.69625160 , 0.00000000
};
const char* sym = "Unknown";
const char* labels[] = {"Ru" ,
"U" ,
"Cl" ,
"Na" ,
"H" };
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'u', backend, &rc);
assert (file != NULL);
// check that the previously written data cannot be overwritten
rc = trexio_write_nucleus_num(file, num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_coord(file, coord);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_point_group(file, sym, 16);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_label(file, labels, 4);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_overwrite_safe (const char* file_name, const back_end_t backend) {
/* Try to overwrite the data that already exists in the TREXIO file which is open in SAFE mode*/
trexio_t* file = NULL;
trexio_exit_code rc;
@ -104,10 +157,10 @@ static int test_overwrite (const char* file_name, const back_end_t backend) {
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);
@ -121,6 +174,78 @@ static int test_overwrite (const char* file_name, const back_end_t backend) {
}
int test_read(const char* file_name, const back_end_t backend) {
/*========= Test read ===========*/
trexio_t* file = NULL;
trexio_exit_code rc;
int num;
double* coord;
char** label;
char* point_group;
/*================= START OF TEST ==================*/
// open existing file on 'read' mode
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read nucleus_num
rc = trexio_read_nucleus_num(file,&num);
assert (rc == TREXIO_SUCCESS);
assert (num == 5);
// read nucleus_coord
coord = (double*) calloc(3*num, sizeof(double));
rc = trexio_read_nucleus_coord(file,coord);
assert (rc == TREXIO_SUCCESS);
double x = coord[1] - 666.666;
assert( x*x < 1.e-14);
free(coord);
// read nucleus_label
label = (char**) malloc(num*sizeof(char*));
for (int i=0; i<num; i++){
label[i] = (char*) malloc(32*sizeof(char));
}
rc = trexio_read_nucleus_label(file, label, 2);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(label[0], "Ru") == 0);
assert (strcmp(label[3], "Na") == 0);
for (int i=0; i<num; i++){
free(label[i]);
}
free(label);
point_group = (char*) malloc(32*sizeof(char));
rc = trexio_read_nucleus_point_group(file, point_group, 10);
assert (rc == TREXIO_SUCCESS);
char * pch;
pch = strtok(point_group, " ");
assert (strcmp(pch, "Unknown") == 0);
/* alternative test when 3 symbols are read from the file to point_group */
/*rc = trexio_read_nucleus_point_group(file, point_group, 3);
assert (rc == TREXIO_SUCCESS);
assert (strcmp(point_group, "B3U") == 0 );*/
free(point_group);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST =====================*/
return 0;
}
int main(void) {
/*============== Test launcher ================*/
@ -129,13 +254,13 @@ int main(void) {
rc = system(RM_COMMAND);
assert (rc == 0);
test_write (TREXIO_FILE, TEST_BACKEND);
test_overwrite (TREXIO_FILE, TEST_BACKEND);
test_write (TREXIO_FILE, TEST_BACKEND);
test_overwrite_safe (TREXIO_FILE, TEST_BACKEND);
test_overwrite_unsafe (TREXIO_FILE, TEST_BACKEND);
test_read (TREXIO_FILE, TEST_BACKEND);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}