Fix last ChangeLog entry.
[gnulib.git] / tests / test-sf-istream.c
blobe3063c34d6aeecdf170c5bad79f770872db16073
1 /* Test of string or file based input stream.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2024. */
19 #include <config.h>
21 /* Specification. */
22 #include "sf-istream.h"
24 #include <unistd.h>
26 #include "macros.h"
28 #define CONTENTS_LEN 7
29 #define CONTENTS "Hello\377\n"
31 static void
32 test_open_stream (sf_istream_t *stream)
34 int c;
36 c = sf_getc (stream);
37 ASSERT (c == 'H');
38 c = sf_getc (stream);
39 ASSERT (c == 'e');
40 c = sf_getc (stream);
41 ASSERT (c == 'l');
42 c = sf_getc (stream);
43 ASSERT (c == 'l');
44 c = sf_getc (stream);
45 ASSERT (c == 'o');
46 sf_ungetc (stream, c);
47 c = sf_getc (stream);
48 ASSERT (c == 'o');
49 c = sf_getc (stream);
50 ASSERT (c == 0xff);
51 sf_ungetc (stream, c);
52 c = sf_getc (stream);
53 ASSERT (c == 0xff);
54 c = sf_getc (stream);
55 ASSERT (c == '\n');
56 c = sf_getc (stream);
57 ASSERT (c == EOF);
58 c = sf_getc (stream);
59 ASSERT (c == EOF);
60 sf_ungetc (stream, c);
61 c = sf_getc (stream);
62 ASSERT (c == EOF);
63 ASSERT (!sf_ferror (stream));
66 int
67 main ()
69 char const contents[CONTENTS_LEN] = CONTENTS;
71 /* Test reading from a file. */
73 const char *filename = "test-sf-istream.tmp";
74 unlink (filename);
76 FILE *fp = fopen (filename, "wb");
77 ASSERT (fwrite (contents, 1, CONTENTS_LEN, fp) == CONTENTS_LEN);
78 ASSERT (fclose (fp) == 0);
81 FILE *fp = fopen (filename, "rb");
82 sf_istream_t stream;
83 sf_istream_init_from_file (&stream, fp);
84 test_open_stream (&stream);
85 sf_free (&stream);
87 unlink (filename);
90 /* Test reading from a string in memory. */
92 sf_istream_t stream;
93 sf_istream_init_from_string_desc (&stream,
94 sd_new_addr (CONTENTS_LEN,
95 (char *) contents));
96 test_open_stream (&stream);
97 sf_free (&stream);
100 /* Test reading from a NUL-terminated string in memory. */
102 sf_istream_t stream;
103 sf_istream_init_from_string (&stream, CONTENTS);
104 test_open_stream (&stream);
105 sf_free (&stream);
108 return 0;