doc: Document version-etc, version-etc, and argp-version-etc.
[gnulib.git] / tests / test-sfl-istream.c
blobe0e9a02a9e6ce4013aa28a84bfaf7f7183a530c5
1 /* Test of string or file based input stream with line number.
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 "sfl-istream.h"
24 #include <unistd.h>
26 #include "macros.h"
28 #define CONTENTS_LEN 9
29 #define CONTENTS "Hello\377\n\nW"
31 static void
32 test_open_stream (sfl_istream_t *stream)
34 int c;
36 ASSERT (sfl_get_line_number (stream) == 1);
37 c = sfl_getc (stream);
38 ASSERT (c == 'H');
39 ASSERT (sfl_get_line_number (stream) == 1);
40 c = sfl_getc (stream);
41 ASSERT (c == 'e');
42 ASSERT (sfl_get_line_number (stream) == 1);
43 c = sfl_getc (stream);
44 ASSERT (c == 'l');
45 ASSERT (sfl_get_line_number (stream) == 1);
46 c = sfl_getc (stream);
47 ASSERT (c == 'l');
48 ASSERT (sfl_get_line_number (stream) == 1);
49 c = sfl_getc (stream);
50 ASSERT (c == 'o');
51 ASSERT (sfl_get_line_number (stream) == 1);
52 sfl_ungetc (stream, c);
53 c = sfl_getc (stream);
54 ASSERT (c == 'o');
55 ASSERT (sfl_get_line_number (stream) == 1);
56 c = sfl_getc (stream);
57 ASSERT (c == 0xff);
58 ASSERT (sfl_get_line_number (stream) == 1);
59 sfl_ungetc (stream, c);
60 c = sfl_getc (stream);
61 ASSERT (c == 0xff);
62 ASSERT (sfl_get_line_number (stream) == 1);
63 c = sfl_getc (stream);
64 ASSERT (c == '\n');
65 ASSERT (sfl_get_line_number (stream) == 2);
66 c = sfl_getc (stream);
67 ASSERT (c == '\n');
68 ASSERT (sfl_get_line_number (stream) == 3);
69 sfl_ungetc (stream, c);
70 ASSERT (sfl_get_line_number (stream) == 2);
71 c = sfl_getc (stream);
72 ASSERT (c == '\n');
73 ASSERT (sfl_get_line_number (stream) == 3);
74 c = sfl_getc (stream);
75 ASSERT (c == 'W');
76 ASSERT (sfl_get_line_number (stream) == 3);
77 c = sfl_getc (stream);
78 ASSERT (c == EOF);
79 ASSERT (sfl_get_line_number (stream) == 3);
80 c = sfl_getc (stream);
81 ASSERT (c == EOF);
82 ASSERT (sfl_get_line_number (stream) == 3);
83 sfl_ungetc (stream, c);
84 c = sfl_getc (stream);
85 ASSERT (c == EOF);
86 ASSERT (sfl_get_line_number (stream) == 3);
87 ASSERT (!sfl_ferror (stream));
90 int
91 main ()
93 char const contents[CONTENTS_LEN] = CONTENTS;
95 /* Test reading from a file. */
97 const char *filename = "test-sfl-istream.tmp";
98 unlink (filename);
100 FILE *fp = fopen (filename, "wb");
101 ASSERT (fwrite (contents, 1, CONTENTS_LEN, fp) == CONTENTS_LEN);
102 ASSERT (fclose (fp) == 0);
105 FILE *fp = fopen (filename, "rb");
106 sfl_istream_t stream;
107 sfl_istream_init_from_file (&stream, fp);
108 test_open_stream (&stream);
109 sfl_free (&stream);
111 unlink (filename);
114 /* Test reading from a string in memory. */
116 sfl_istream_t stream;
117 sfl_istream_init_from_string_desc (&stream,
118 sd_new_addr (CONTENTS_LEN,
119 (char *) contents));
120 test_open_stream (&stream);
121 sfl_free (&stream);
124 /* Test reading from a NUL-terminated string in memory. */
126 sfl_istream_t stream;
127 sfl_istream_init_from_string (&stream, CONTENTS);
128 test_open_stream (&stream);
129 sfl_free (&stream);
132 return 0;