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)
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. */
22 #include "sf-istream.h"
28 #define CONTENTS_LEN 7
29 #define CONTENTS "Hello\377\n"
32 test_open_stream (sf_istream_t
*stream
)
46 sf_ungetc (stream
, c
);
51 sf_ungetc (stream
, c
);
60 sf_ungetc (stream
, c
);
63 ASSERT (!sf_ferror (stream
));
69 char const contents
[CONTENTS_LEN
] = CONTENTS
;
71 /* Test reading from a file. */
73 const char *filename
= "test-sf-istream.tmp";
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");
83 sf_istream_init_from_file (&stream
, fp
);
84 test_open_stream (&stream
);
90 /* Test reading from a string in memory. */
93 sf_istream_init_from_string_desc (&stream
,
94 sd_new_addr (CONTENTS_LEN
,
96 test_open_stream (&stream
);
100 /* Test reading from a NUL-terminated string in memory. */
103 sf_istream_init_from_string (&stream
, CONTENTS
);
104 test_open_stream (&stream
);