rnsl

Load a list of strings expanded by the shell
git clone git://git.meso-star.fr/rnsl.git
Log | Files | Refs | README | LICENSE

commit 0ff08b907e114ff2da1f3405738bfce5dc026594
parent 2498b39a057ff8b136541d3ad80c9b275923e121
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Tue, 20 Sep 2022 09:57:45 +0200

Test loading

Diffstat:
Mcmake/CMakeLists.txt | 4++--
Msrc/rnsl.c | 4++--
Asrc/test_rnsl_load.c | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 127 insertions(+), 4 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -86,8 +86,8 @@ if(NOT NO_TEST) add_test(${_name} ${_name}) endfunction() - new_test(test_rnfl) - # new_test(test_rnfl_load) + new_test(test_rnsl) + new_test(test_rnsl_load) endif() ################################################################################ diff --git a/src/rnsl.c b/src/rnsl.c @@ -39,7 +39,7 @@ parse_string(struct rnsl* rnsl, struct txtrdr* txtrdr, struct str* str) wordexp_t wexp; char* tk = NULL; char* tk_ctx = NULL; - int wexp_is_allocated = 1; + int wexp_is_allocated = 0; res_T res = RES_OK; int err = 0; ASSERT(rnsl && txtrdr && str); @@ -57,7 +57,7 @@ parse_string(struct rnsl* rnsl, struct txtrdr* txtrdr, struct str* str) const size_t nparsed = (size_t)(str - darray_str_cdata_get(&rnsl->strings)); log_err(rnsl, "%s:%lu: missing a string. " - "Expecting %lu string%s while %lu %s parsed.\n", + "Expecting %lu string%swhile %lu %s parsed.\n", txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), (unsigned long)nexpect, nexpect == 1 ? " " : "s ", (unsigned long)nparsed, nparsed > 1 ? "were" : "was"); diff --git a/src/test_rnsl_load.c b/src/test_rnsl_load.c @@ -0,0 +1,123 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université de Reims Champagne-Ardenne + * Copyright (C) 2022 Université de Versaille Saint-Quentin + * Copyright (C) 2022 Université Paul Sabatier (contact@laplace.univ-tlse.fr) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#define _POSIX_C_SOURCE 200112L /* setenv */ + +#include "rnsl.h" +#include <rsys/mem_allocator.h> + +#include <stdlib.h> +#include <string.h> + +static void +test_load(struct rnsl* rnsl) +{ + const char* path = "file.txt"; + FILE* fp = NULL; + + CHK(fp = fopen(path, "w+")); + fprintf(fp, "8\n"); + fprintf(fp, "string0\n"); + fprintf(fp, "my/string/1\n"); + fprintf(fp, "my string\t2\n"); + fprintf(fp, "\"my string\t3\"\n"); + fprintf(fp, "\"${VARIABLE}/hello\"\n"); + fprintf(fp, "world!\n"); + fprintf(fp, "\n"); + fprintf(fp, "# Comment\n"); + fprintf(fp, " foo\n"); + fprintf(fp, "\tbar\n"); + fprintf(fp, "unreachable string\n"); + rewind(fp); + + CHK(rnsl_load(NULL, path) == RES_BAD_ARG); + CHK(rnsl_load(rnsl, NULL) == RES_BAD_ARG); + CHK(rnsl_load(rnsl, path) == RES_OK); + CHK(rnsl_get_strings_count(rnsl) == 8); + CHK(!strcmp(rnsl_get_string(rnsl, 0), "string0")); + CHK(!strcmp(rnsl_get_string(rnsl, 1), "my/string/1")); + CHK(!strcmp(rnsl_get_string(rnsl, 2), "my")); + CHK(!strcmp(rnsl_get_string(rnsl, 3), "my string\t3")); + CHK(!strcmp(rnsl_get_string(rnsl, 4), "/hello")); + CHK(!strcmp(rnsl_get_string(rnsl, 5), "world!")); + CHK(!strcmp(rnsl_get_string(rnsl, 6), "foo")); + CHK(!strcmp(rnsl_get_string(rnsl, 7), "bar")); + + CHK(setenv("VARIABLE", "my path", 0) == 0); + CHK(rnsl_load_stream(NULL, fp, path) == RES_BAD_ARG); + CHK(rnsl_load_stream(rnsl, NULL, path) == RES_BAD_ARG); + CHK(rnsl_load_stream(rnsl, fp, NULL) == RES_OK); + CHK(rnsl_get_strings_count(rnsl) == 8); + CHK(!strcmp(rnsl_get_string(rnsl, 3), "my string\t3")); + CHK(!strcmp(rnsl_get_string(rnsl, 4), "my path/hello")); + CHK(!strcmp(rnsl_get_string(rnsl, 5), "world!")); + rewind(fp); + + CHK(rnsl_load_stream(rnsl, fp, path) == RES_OK); + CHK(rnsl_get_strings_count(rnsl) == 8); + CHK(!strcmp(rnsl_get_string(rnsl, 0), "string0")); + CHK(!strcmp(rnsl_get_string(rnsl, 2), "my")); + CHK(!strcmp(rnsl_get_string(rnsl, 7), "bar")); + + fclose(fp); +} + +static void +test_load_failure(struct rnsl* rnsl) +{ + FILE* fp; + + CHK(fp = tmpfile()); + + /* Empty file */ + CHK(rnsl_load_stream(rnsl, fp, NULL) == RES_BAD_ARG); + + /* Invalid #lines */ + fprintf(fp, "2\n"); + rewind(fp); + CHK(rnsl_load_stream(rnsl, fp, NULL) == RES_BAD_ARG); + + /* Invalid #lines */ + rewind(fp); + fprintf(fp, "2\n"); + fprintf(fp, "\"string 0\"\n"); + rewind(fp); + CHK(rnsl_load_stream(rnsl, fp, NULL) == RES_BAD_ARG); + + CHK(fclose(fp) == 0); +} + +int +main(int argc, char** argv) +{ + struct rnsl_create_args args = RNSL_CREATE_ARGS_DEFAULT; + struct rnsl* rnsl = NULL; + (void)argc, (void)argv; + + args.verbose = 1; + CHK(rnsl_create(&args, &rnsl) == RES_OK); + + test_load(rnsl); + test_load_failure(rnsl); + + CHK(rnsl_ref_put(rnsl) == RES_OK); + CHK(mem_allocated_size() == 0); + return 0; +}