From: Bruno Haible Date: Tue, 24 Sep 2024 10:30:58 +0000 (+0200) Subject: sf-istream: Add tests. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=357c5d521d7af3c56fb23cb2a98db562017468ed;p=gnulib.git sf-istream: Add tests. * tests/test-sf-istream.c: New file. * modules/sf-istream-tests: New file. --- diff --git a/ChangeLog b/ChangeLog index 2b0ffa4da0..6012c6e7e0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2024-09-24 Bruno Haible + sf-istream: Add tests. + * tests/test-sf-istream.c: New file. + * modules/sf-istream-tests: New file. + sf-istream: New module. * lib/sf-istream.h: New file. * lib/sf-istream.c: New file. diff --git a/modules/sf-istream-tests b/modules/sf-istream-tests new file mode 100644 index 0000000000..dc0f80d541 --- /dev/null +++ b/modules/sf-istream-tests @@ -0,0 +1,12 @@ +Files: +tests/test-sf-istream.c +tests/macros.h + +Depends-on: +unlink + +configure.ac: + +Makefile.am: +TESTS += test-sf-istream +check_PROGRAMS += test-sf-istream diff --git a/tests/test-sf-istream.c b/tests/test-sf-istream.c new file mode 100644 index 0000000000..61d096f55f --- /dev/null +++ b/tests/test-sf-istream.c @@ -0,0 +1,109 @@ +/* Test of string or file based input stream. + Copyright (C) 2024 Free Software Foundation, Inc. + + 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 2, 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 . */ + +/* Written by Bruno Haible , 2024. */ + +#include + +/* Specification. */ +#include "sf-istream.h" + +#include + +#include "macros.h" + +#define CONTENTS_LEN 7 +#define CONTENTS "Hello\377\n" + +static void +test_open_stream (sf_istream_t *stream) +{ + int c; + + c = sf_getc (stream); + ASSERT (c == 'H'); + c = sf_getc (stream); + ASSERT (c == 'e'); + c = sf_getc (stream); + ASSERT (c == 'l'); + c = sf_getc (stream); + ASSERT (c == 'l'); + c = sf_getc (stream); + ASSERT (c == 'o'); + sf_ungetc (stream, c); + c = sf_getc (stream); + ASSERT (c == 'o'); + c = sf_getc (stream); + ASSERT (c == 0xff); + sf_ungetc (stream, c); + c = sf_getc (stream); + ASSERT (c == 0xff); + c = sf_getc (stream); + ASSERT (c == '\n'); + c = sf_getc (stream); + ASSERT (c == EOF); + c = sf_getc (stream); + ASSERT (c == EOF); + sf_ungetc (stream, c); + c = sf_getc (stream); + ASSERT (c == EOF); + ASSERT (!sf_ferror (stream)); +} + +int +main () +{ + char const contents[CONTENTS_LEN] = CONTENTS; + + /* Test reading from a file. */ + { + const char *filename = "test-sf-istream.tmp"; + unlink (filename); + { + FILE *fp = fopen (filename, "wb"); + ASSERT (fwrite (contents, 1, CONTENTS_LEN, fp) == CONTENTS_LEN); + ASSERT (fclose (fp) == 0); + } + { + FILE *fp = fopen (filename, "rb"); + sf_istream_t stream; + sf_istream_init_from_file (&stream, fp); + test_open_stream (&stream); + sf_free (&stream); + } + unlink (filename); + } + + /* Test reading from a string in memory. */ + { + sf_istream_t stream; + sf_istream_init_from_string_desc (&stream, + string_desc_new_addr (CONTENTS_LEN, + (char *) contents)); + test_open_stream (&stream); + sf_free (&stream); + } + + /* Test reading from a NUL-terminated string in memory. */ + { + sf_istream_t stream; + sf_istream_init_from_string (&stream, CONTENTS); + test_open_stream (&stream); + sf_free (&stream); + } + + return 0; +}