From 5a7754d11ecaf369987b7074333f2b392238d04a Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 24 Sep 2024 12:31:07 +0200 Subject: [PATCH] sfl-istream: Add tests. * tests/test-sfl-istream.c: New file, based on tests/test-sf-istream.c. * modules/sfl-istream-tests: New file. --- ChangeLog | 4 ++ modules/sfl-istream-tests | 12 ++++ tests/test-sfl-istream.c | 133 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 modules/sfl-istream-tests create mode 100644 tests/test-sfl-istream.c diff --git a/ChangeLog b/ChangeLog index 52a498383f..934ed7233d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2024-09-24 Bruno Haible + sfl-istream: Add tests. + * tests/test-sfl-istream.c: New file, based on tests/test-sf-istream.c. + * modules/sfl-istream-tests: New file. + sfl-istream: New module. * lib/sfl-istream.h: New file. * lib/sfl-istream.c: New file. diff --git a/modules/sfl-istream-tests b/modules/sfl-istream-tests new file mode 100644 index 0000000000..eba2aa0abb --- /dev/null +++ b/modules/sfl-istream-tests @@ -0,0 +1,12 @@ +Files: +tests/test-sfl-istream.c +tests/macros.h + +Depends-on: +unlink + +configure.ac: + +Makefile.am: +TESTS += test-sfl-istream +check_PROGRAMS += test-sfl-istream diff --git a/tests/test-sfl-istream.c b/tests/test-sfl-istream.c new file mode 100644 index 0000000000..bf8eeeccb6 --- /dev/null +++ b/tests/test-sfl-istream.c @@ -0,0 +1,133 @@ +/* Test of string or file based input stream with line number. + 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 "sfl-istream.h" + +#include + +#include "macros.h" + +#define CONTENTS_LEN 9 +#define CONTENTS "Hello\377\n\nW" + +static void +test_open_stream (sfl_istream_t *stream) +{ + int c; + + ASSERT (sfl_get_line_number (stream) == 1); + c = sfl_getc (stream); + ASSERT (c == 'H'); + ASSERT (sfl_get_line_number (stream) == 1); + c = sfl_getc (stream); + ASSERT (c == 'e'); + ASSERT (sfl_get_line_number (stream) == 1); + c = sfl_getc (stream); + ASSERT (c == 'l'); + ASSERT (sfl_get_line_number (stream) == 1); + c = sfl_getc (stream); + ASSERT (c == 'l'); + ASSERT (sfl_get_line_number (stream) == 1); + c = sfl_getc (stream); + ASSERT (c == 'o'); + ASSERT (sfl_get_line_number (stream) == 1); + sfl_ungetc (stream, c); + c = sfl_getc (stream); + ASSERT (c == 'o'); + ASSERT (sfl_get_line_number (stream) == 1); + c = sfl_getc (stream); + ASSERT (c == 0xff); + ASSERT (sfl_get_line_number (stream) == 1); + sfl_ungetc (stream, c); + c = sfl_getc (stream); + ASSERT (c == 0xff); + ASSERT (sfl_get_line_number (stream) == 1); + c = sfl_getc (stream); + ASSERT (c == '\n'); + ASSERT (sfl_get_line_number (stream) == 2); + c = sfl_getc (stream); + ASSERT (c == '\n'); + ASSERT (sfl_get_line_number (stream) == 3); + sfl_ungetc (stream, c); + ASSERT (sfl_get_line_number (stream) == 2); + c = sfl_getc (stream); + ASSERT (c == '\n'); + ASSERT (sfl_get_line_number (stream) == 3); + c = sfl_getc (stream); + ASSERT (c == 'W'); + ASSERT (sfl_get_line_number (stream) == 3); + c = sfl_getc (stream); + ASSERT (c == EOF); + ASSERT (sfl_get_line_number (stream) == 3); + c = sfl_getc (stream); + ASSERT (c == EOF); + ASSERT (sfl_get_line_number (stream) == 3); + sfl_ungetc (stream, c); + c = sfl_getc (stream); + ASSERT (c == EOF); + ASSERT (sfl_get_line_number (stream) == 3); + ASSERT (!sfl_ferror (stream)); +} + +int +main () +{ + char const contents[CONTENTS_LEN] = CONTENTS; + + /* Test reading from a file. */ + { + const char *filename = "test-sfl-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"); + sfl_istream_t stream; + sfl_istream_init_from_file (&stream, fp); + test_open_stream (&stream); + sfl_free (&stream); + } + unlink (filename); + } + + /* Test reading from a string in memory. */ + { + sfl_istream_t stream; + sfl_istream_init_from_string_desc (&stream, + string_desc_new_addr (CONTENTS_LEN, + (char *) contents)); + test_open_stream (&stream); + sfl_free (&stream); + } + + /* Test reading from a NUL-terminated string in memory. */ + { + sfl_istream_t stream; + sfl_istream_init_from_string (&stream, CONTENTS); + test_open_stream (&stream); + sfl_free (&stream); + } + + return 0; +} -- 2.39.5