From f68a59417eee3e285f2da8bad1e7cb0333473a1b Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 24 Jan 2022 16:13:27 +0100 Subject: [PATCH] add overwriting functionality for unsafe mode [TEXT] --- src/templates_front/templator_front.org | 18 +-- src/templates_text/templator_text.org | 2 +- tests/overwrite_all_text.c | 147 ++++++++++++++++++++++-- 3 files changed, 146 insertions(+), 21 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 356e480..6f0622d 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -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; <> @@ -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; <> @@ -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; diff --git a/src/templates_text/templator_text.org b/src/templates_text/templator_text.org index 96f945a..7ad0ff9 100644 --- a/src/templates_text/templator_text.org +++ b/src/templates_text/templator_text.org @@ -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; diff --git a/tests/overwrite_all_text.c b/tests/overwrite_all_text.c index 11e5518..5baf99e 100644 --- a/tests/overwrite_all_text.c +++ b/tests/overwrite_all_text.c @@ -2,6 +2,7 @@ #include #include #include +#include #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