]> Savannah Git Hosting - gnulib.git/commitdiff
sfl-istream: Add tests.
authorBruno Haible <bruno@clisp.org>
Tue, 24 Sep 2024 10:31:07 +0000 (12:31 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 24 Sep 2024 10:31:07 +0000 (12:31 +0200)
* tests/test-sfl-istream.c: New file, based on tests/test-sf-istream.c.
* modules/sfl-istream-tests: New file.

ChangeLog
modules/sfl-istream-tests [new file with mode: 0644]
tests/test-sfl-istream.c [new file with mode: 0644]

index 52a498383f0c2f8fb4b1f92f745b9c1601cf5c6c..934ed7233d11060807452af6738138acbe6d27a2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2024-09-24  Bruno Haible  <bruno@clisp.org>
 
+       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 (file)
index 0000000..eba2aa0
--- /dev/null
@@ -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 (file)
index 0000000..bf8eeec
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2024.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include "sfl-istream.h"
+
+#include <unistd.h>
+
+#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;
+}