rnsl

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

test_rnsl_load.c (4197B)


      1 /* Copyright (C) 2022, 2023 Centre National de la Recherche Scientifique
      2  * Copyright (C) 2022, 2023 Institut Pierre-Simon Laplace
      3  * Copyright (C) 2022, 2023 Institut de Physique du Globe de Paris
      4  * Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com)
      5  * Copyright (C) 2022, 2023 Observatoire de Paris
      6  * Copyright (C) 2022, 2023 Université de Reims Champagne-Ardenne
      7  * Copyright (C) 2022, 2023 Université de Versaille Saint-Quentin
      8  * Copyright (C) 2022, 2023 Université Paul Sabatier
      9  *
     10  * This program is free software: you can redistribute it and/or modify
     11  * it under the terms of the GNU General Public License as published by
     12  * the Free Software Foundation, either version 3 of the License, or
     13  * (at your option) any later version.
     14  *
     15  * This program is distributed in the hope that it will be useful,
     16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     18  * GNU General Public License for more details.
     19  *
     20  * You should have received a copy of the GNU General Public License
     21  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     22 
     23 #define _POSIX_C_SOURCE 200112L /* setenv */
     24 
     25 #include "rnsl.h"
     26 #include <rsys/mem_allocator.h>
     27 
     28 #include <stdlib.h>
     29 #include <string.h>
     30 
     31 static void
     32 test_load(struct rnsl* rnsl)
     33 {
     34   const char* path = "file.txt";
     35   FILE* fp = NULL;
     36 
     37   CHK(fp = fopen(path, "w+"));
     38   fprintf(fp, "8\n");
     39   fprintf(fp, "string0\n");
     40   fprintf(fp, "my/string/1\n");
     41   fprintf(fp, "my string\t2\n");
     42   fprintf(fp, "\"my string\t3\"\n");
     43   fprintf(fp, "\"${VARIABLE}/hello\"\n");
     44   fprintf(fp, "world!\n");
     45   fprintf(fp, "\n");
     46   fprintf(fp, "# Comment\n");
     47   fprintf(fp, "  foo\n");
     48   fprintf(fp, "\tbar\n");
     49   fprintf(fp, "unreachable string\n");
     50   rewind(fp);
     51 
     52   CHK(rnsl_load(NULL, path) == RES_BAD_ARG);
     53   CHK(rnsl_load(rnsl, NULL) == RES_BAD_ARG);
     54   CHK(rnsl_load(rnsl, path) == RES_OK);
     55   CHK(rnsl_get_strings_count(rnsl) == 8);
     56   CHK(!strcmp(rnsl_get_string(rnsl, 0), "string0"));
     57   CHK(!strcmp(rnsl_get_string(rnsl, 1), "my/string/1"));
     58   CHK(!strcmp(rnsl_get_string(rnsl, 2), "my"));
     59   CHK(!strcmp(rnsl_get_string(rnsl, 3), "my string\t3"));
     60   CHK(!strcmp(rnsl_get_string(rnsl, 4), "/hello"));
     61   CHK(!strcmp(rnsl_get_string(rnsl, 5), "world!"));
     62   CHK(!strcmp(rnsl_get_string(rnsl, 6), "foo"));
     63   CHK(!strcmp(rnsl_get_string(rnsl, 7), "bar"));
     64 
     65   CHK(setenv("VARIABLE", "my path", 0) == 0);
     66   CHK(rnsl_load_stream(NULL, fp, path) == RES_BAD_ARG);
     67   CHK(rnsl_load_stream(rnsl, NULL, path) == RES_BAD_ARG);
     68   CHK(rnsl_load_stream(rnsl, fp, NULL) == RES_OK);
     69   CHK(rnsl_get_strings_count(rnsl) == 8);
     70   CHK(!strcmp(rnsl_get_string(rnsl, 3), "my string\t3"));
     71   CHK(!strcmp(rnsl_get_string(rnsl, 4), "my path/hello"));
     72   CHK(!strcmp(rnsl_get_string(rnsl, 5), "world!"));
     73   rewind(fp);
     74 
     75   CHK(rnsl_load_stream(rnsl, fp, path) == RES_OK);
     76   CHK(rnsl_get_strings_count(rnsl) == 8);
     77   CHK(!strcmp(rnsl_get_string(rnsl, 0), "string0"));
     78   CHK(!strcmp(rnsl_get_string(rnsl, 2), "my"));
     79   CHK(!strcmp(rnsl_get_string(rnsl, 7), "bar"));
     80 
     81   fclose(fp);
     82 }
     83 
     84 static void
     85 test_load_failure(struct rnsl* rnsl)
     86 {
     87   FILE* fp;
     88 
     89   CHK(fp = tmpfile());
     90 
     91   /* Empty file */
     92   CHK(rnsl_load_stream(rnsl, fp, NULL) == RES_BAD_ARG);
     93 
     94   /* Invalid #lines */
     95   fprintf(fp, "2\n");
     96   rewind(fp);
     97   CHK(rnsl_load_stream(rnsl, fp, NULL) == RES_BAD_ARG);
     98 
     99   /* Invalid #lines */
    100   rewind(fp);
    101   fprintf(fp, "2\n");
    102   fprintf(fp, "\"string 0\"\n");
    103   rewind(fp);
    104   CHK(rnsl_load_stream(rnsl, fp, NULL) == RES_BAD_ARG);
    105 
    106   CHK(fclose(fp) == 0);
    107 }
    108 
    109 static void
    110 test_load_file(struct rnsl* rnsl, int argc, char** argv)
    111 {
    112   int i;
    113   ASSERT(rnsl && argc && argv);
    114 
    115   FOR_EACH(i, 1, argc) {
    116     printf("Load %s\n", argv[i]);
    117     CHK(rnsl_load(rnsl, argv[i]) == RES_OK);
    118   }
    119 }
    120 
    121 int
    122 main(int argc, char** argv)
    123 {
    124   struct rnsl_create_args args = RNSL_CREATE_ARGS_DEFAULT;
    125   struct rnsl* rnsl = NULL;
    126   (void)argc, (void)argv;
    127 
    128   args.verbose = 1;
    129   CHK(rnsl_create(&args, &rnsl) == RES_OK);
    130 
    131   if(argc > 1) {
    132     test_load_file(rnsl, argc, argv);
    133   } else {
    134     test_load(rnsl);
    135     test_load_failure(rnsl);
    136   }
    137 
    138   CHK(rnsl_ref_put(rnsl) == RES_OK);
    139   CHK(mem_allocated_size() == 0);
    140   return 0;
    141 }