rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

test_text_reader.c (5014B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "mem_allocator.h"
     17 #include "test_utils.h"
     18 #include "text_reader.h"
     19 
     20 #include <string.h>
     21 
     22 static const char* text[] = {
     23   "Znxr rnpu cebtenz qb bar guvat jryy. Gb qb n arj wbo, ohvyq nserfu engure"
     24   "guna pbzcyvpngr byq cebtenzf ol nqqvat arj \"srngherf\"\n",
     25   "   \t\t # rzcgl yvar\n",
     26   "Rkcrpg gur bhgchg bs rirel cebtenz gb orpbzr gur vachg gb nabgure, nf lrg"
     27   "haxabja, cebtenz. Qba'g pyhggre bhgchg jvgu rkgenarbhf vasbezngvba. Nibvq"
     28   "fgevatragyl pbyhzane be ovanel vachg sbezngf. Qba'g vafvfg ba vagrenpgvir"
     29   "vachg.\n",
     30   "# pbzzrag\n",
     31   "Qrfvta naq ohvyq fbsgjner, rira bcrengvat flfgrzf, gb or gevrq rneyl, vqrnyyl"
     32   "jvguva jrrxf. Qba'g urfvgngr gb guebj njnl gur pyhzfl cnegf naq erohvyq gurz.\n",
     33   "\n",
     34   "Hfr gbbyf va cersrerapr gb hafxvyyrq uryc gb yvtugra n cebtenzzvat gnfx, rira"
     35   "vs lbh unir gb qrgbhe gb ohvyq gur gbbyf naq rkcrpg gb guebj fbzr bs gurz bhg"
     36   "nsgre lbh'ir svavfurq hfvat gurz.\n"
     37 };
     38 static const size_t nlines = sizeof(text)/sizeof(const char*);
     39 
     40 static void
     41 check_text_reader(struct txtrdr* txtrdr)
     42 {
     43   size_t iline = 0;
     44 
     45   CHK(txtrdr_read_line(txtrdr) == RES_OK);
     46   while(txtrdr_get_line(txtrdr)) {
     47 
     48     /* Discard empty line */
     49     while(strcspn(text[iline], "#\n") == strspn(text[iline], " \t")) ++iline;
     50 
     51     CHK(!strncmp(txtrdr_get_line(txtrdr), text[iline], strcspn(text[iline], "#\n")));
     52     CHK(txtrdr_get_line_num(txtrdr) == iline+1);
     53     CHK(txtrdr_read_line(txtrdr) == RES_OK);
     54     ++iline;
     55   }
     56   CHK(txtrdr_get_line_num(txtrdr) == nlines);
     57   CHK(txtrdr_read_line(txtrdr) == RES_OK);
     58   CHK(txtrdr_get_line(txtrdr) == NULL);
     59   CHK(txtrdr_get_line_num(txtrdr) == nlines);
     60   CHK(txtrdr_read_line(txtrdr) == RES_OK);
     61   CHK(txtrdr_get_line(txtrdr) == NULL);
     62   txtrdr_set_line_num(txtrdr, nlines/2);
     63   CHK(txtrdr_get_line_num(txtrdr) == nlines/2);
     64 }
     65 
     66 int
     67 main(int argc, char** argv)
     68 {
     69   struct mem_allocator allocator;
     70   struct txtrdr* txtrdr = NULL;
     71   size_t i;
     72   FILE* stream;
     73   const char* filename = "test_text_reader.txt";
     74   const char* stream_name = "my_stream";
     75   (void)argc, (void)argv;
     76 
     77   mem_init_proxy_allocator(&allocator, &mem_default_allocator);
     78 
     79   /* Write the text into a stream */
     80   stream = fopen(filename, "w+");
     81   CHK(stream);
     82   FOR_EACH(i, 0, nlines) {
     83     const size_t len = strlen(text[i]);
     84     CHK(fwrite(text[i], 1, len, stream) == len);
     85   }
     86 
     87   rewind(stream);
     88   CHK(txtrdr_stream(NULL, stream, NULL, '#', &txtrdr) == RES_OK);
     89   check_text_reader(txtrdr);
     90   txtrdr_ref_get(txtrdr);
     91   txtrdr_ref_put(txtrdr);
     92   txtrdr_ref_put(txtrdr);
     93 
     94   rewind(stream);
     95   CHK(txtrdr_stream(&allocator, stream, stream_name, '#', &txtrdr) == RES_OK);
     96   CHK(!strcmp(txtrdr_get_name(txtrdr), stream_name));
     97   CHK(txtrdr_get_stream(txtrdr) == stream);
     98   check_text_reader(txtrdr);
     99   txtrdr_ref_put(txtrdr);
    100 
    101   CHK(txtrdr_file(&allocator, filename, '#', &txtrdr) == RES_OK);
    102   CHK(!strcmp(txtrdr_get_name(txtrdr), filename));
    103   check_text_reader(txtrdr);
    104   txtrdr_ref_put(txtrdr);
    105 
    106   stream = freopen(filename, "w+", stream);
    107   CHK(stream);
    108   fprintf(stream, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    109   fprintf(stream, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
    110   rewind(stream);
    111   CHK(txtrdr_stream(&allocator, stream, filename, -1, &txtrdr) == RES_OK);
    112   CHK(txtrdr_read_line(txtrdr) == RES_OK);
    113   txtrdr_ref_put(txtrdr);
    114 
    115   stream = freopen(filename, "w+", stream);
    116   CHK(stream);
    117   fprintf(stream, "\r\n");
    118   rewind(stream);
    119   CHK(txtrdr_stream(&allocator, stream, filename, '#', &txtrdr) == RES_OK);
    120   CHK(txtrdr_read_line(txtrdr) == RES_OK);
    121   CHK(txtrdr_get_line(txtrdr) == NULL);
    122   txtrdr_ref_put(txtrdr);
    123 
    124   stream = freopen(filename, "w+", stream);
    125   CHK(stream);
    126   fprintf(stream, "   AAAA\nbbb\n");
    127   rewind(stream);
    128   CHK(txtrdr_stream(&allocator, stream, stream_name, 0, &txtrdr) == RES_OK);
    129   CHK(txtrdr_read_line(txtrdr) == RES_OK);
    130   CHK(txtrdr_get_line(txtrdr) != NULL);
    131   CHK(!strcmp(txtrdr_get_line(txtrdr), "   AAAA"));
    132   CHK(txtrdr_read_line(txtrdr) == RES_OK);
    133   CHK(txtrdr_get_line(txtrdr) != NULL);
    134   CHK(!strcmp(txtrdr_get_line(txtrdr), "bbb"));
    135   CHK(txtrdr_read_line(txtrdr) == RES_OK);
    136   CHK(txtrdr_get_line(txtrdr) == NULL);
    137   txtrdr_ref_put(txtrdr);
    138 
    139   fclose(stream);
    140   check_memory_allocator(&allocator);
    141   mem_shutdown_proxy_allocator(&allocator);
    142   CHK(mem_allocated_size() == 0);
    143   return 0;
    144 }