rsys

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

commit 52c2b9d8035ffeebd47a8e28ede76b541dba9214
parent 7f0bf5323b951e61ce3ef2e8f0932e72158b0793
Author: vaplv <vaplv@free.fr>
Date:   Thu, 28 Nov 2019 18:37:57 +0100

Test the text_reader API

Diffstat:
Mcmake/CMakeLists.txt | 1+
Asrc/test_text_reader.c | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/text_reader.h | 3++-
3 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -231,6 +231,7 @@ if(NOT NO_TEST) new_test(test_signal rsys) new_test(test_str rsys) new_test(test_stretchy_array rsys) + new_test(test_text_reader rsys) new_test(test_time rsys) add_library(test_lib SHARED ${RSYS_SOURCE_DIR}/test_library.c) diff --git a/src/test_text_reader.c b/src/test_text_reader.c @@ -0,0 +1,92 @@ +/* Copyright (C) 2013-2019 Vincent Forest (vaplv@free.fr) + * + * The RSys library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The RSys library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ + +#include "mem_allocator.h" +#include "test_utils.h" +#include "text_reader.h" + +#include <string.h> + +static const char* text[] = { + "Znxr rnpu cebtenz qb bar guvat jryy. Gb qb n arj wbo, ohvyq nserfu engure" + "guna pbzcyvpngr byq cebtenzf ol nqqvat arj \"srngherf\"\n", + " \t\t # rzcgl yvar\n", + "Rkcrpg gur bhgchg bs rirel cebtenz gb orpbzr gur vachg gb nabgure, nf lrg" + "haxabja, cebtenz. Qba'g pyhggre bhgchg jvgu rkgenarbhf vasbezngvba. Nibvq" + "fgevatragyl pbyhzane be ovanel vachg sbezngf. Qba'g vafvfg ba vagrenpgvir" + "vachg.\n", + "# pbzzrag\n", + "Qrfvta naq ohvyq fbsgjner, rira bcrengvat flfgrzf, gb or gevrq rneyl, vqrnyyl" + "jvguva jrrxf. Qba'g urfvgngr gb guebj njnl gur pyhzfl cnegf naq erohvyq gurz.\n", + "\n", + "Hfr gbbyf va cersrerapr gb hafxvyyrq uryc gb yvtugra n cebtenzzvat gnfx, rira" + "vs lbh unir gb qrgbhe gb ohvyq gur gbbyf naq rkcrpg gb guebj fbzr bs gurz bhg" + "nsgre lbh'ir svavfurq hfvat gurz.\n" +}; +static const size_t nlines = sizeof(text)/sizeof(const char*); + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct txtrdr* txtrdr = NULL; + size_t i; + FILE* stream; + (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + /* Write the text into a stream */ + CHK(stream = tmpfile()); + FOR_EACH(i, 0, nlines) { + const size_t len = strlen(text[i]); + CHK(fwrite(text[i], 1, len, stream) == len); + } + rewind(stream); + + CHK(txtrdr_create(NULL, stream, NULL, '#', &txtrdr) == RES_OK); + txtrdr_ref_get(txtrdr); + txtrdr_ref_put(txtrdr); + txtrdr_ref_put(txtrdr); + + CHK(txtrdr_create(&allocator, stream, "my_stream", '#', &txtrdr) == RES_OK); + CHK(!strcmp(txtrdr_get_name(txtrdr), "my_stream")); + + i = 0; + CHK(txtrdr_read_line(txtrdr) == RES_OK); + while(txtrdr_get_line(txtrdr)) { + + while(strcspn(text[i], "#\n") == strspn(text[i], " \t")) ++i; + + CHK(!strncmp(txtrdr_get_line(txtrdr), text[i], strcspn(text[i], "#\n"))); + CHK(txtrdr_get_line_num(txtrdr) == i); + CHK(txtrdr_read_line(txtrdr) == RES_OK); + ++i; + } + CHK(txtrdr_get_line_num(txtrdr) == nlines-1); + CHK(txtrdr_read_line(txtrdr) == RES_OK); + CHK(txtrdr_get_line(txtrdr) == NULL); + CHK(txtrdr_get_line_num(txtrdr) == nlines-1); + CHK(txtrdr_read_line(txtrdr) == RES_OK); + + txtrdr_ref_put(txtrdr); + + fclose(stream); + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} + diff --git a/src/text_reader.h b/src/text_reader.h @@ -39,7 +39,8 @@ RSYS_API void txtrdr_ref_put (struct txtrdr* txtrdr); -/* Read a non empty line, i.e. line with text that is not a comment */ +/* Read a non empty line, i.e. line with text that is not a comment. The new + * line character as well as the comments are skipped from the reading */ RSYS_API res_T txtrdr_read_line (struct txtrdr* txtrdr);