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)
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 "sfl-istream.h"
28 #define CONTENTS_LEN 9
29 #define CONTENTS "Hello\377\n\nW"
32 test_open_stream (sfl_istream_t
*stream
)
36 ASSERT (sfl_get_line_number (stream
) == 1);
37 c
= sfl_getc (stream
);
39 ASSERT (sfl_get_line_number (stream
) == 1);
40 c
= sfl_getc (stream
);
42 ASSERT (sfl_get_line_number (stream
) == 1);
43 c
= sfl_getc (stream
);
45 ASSERT (sfl_get_line_number (stream
) == 1);
46 c
= sfl_getc (stream
);
48 ASSERT (sfl_get_line_number (stream
) == 1);
49 c
= sfl_getc (stream
);
51 ASSERT (sfl_get_line_number (stream
) == 1);
52 sfl_ungetc (stream
, c
);
53 c
= sfl_getc (stream
);
55 ASSERT (sfl_get_line_number (stream
) == 1);
56 c
= sfl_getc (stream
);
58 ASSERT (sfl_get_line_number (stream
) == 1);
59 sfl_ungetc (stream
, c
);
60 c
= sfl_getc (stream
);
62 ASSERT (sfl_get_line_number (stream
) == 1);
63 c
= sfl_getc (stream
);
65 ASSERT (sfl_get_line_number (stream
) == 2);
66 c
= sfl_getc (stream
);
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
);
73 ASSERT (sfl_get_line_number (stream
) == 3);
74 c
= sfl_getc (stream
);
76 ASSERT (sfl_get_line_number (stream
) == 3);
77 c
= sfl_getc (stream
);
79 ASSERT (sfl_get_line_number (stream
) == 3);
80 c
= sfl_getc (stream
);
82 ASSERT (sfl_get_line_number (stream
) == 3);
83 sfl_ungetc (stream
, c
);
84 c
= sfl_getc (stream
);
86 ASSERT (sfl_get_line_number (stream
) == 3);
87 ASSERT (!sfl_ferror (stream
));
93 char const contents
[CONTENTS_LEN
] = CONTENTS
;
95 /* Test reading from a file. */
97 const char *filename
= "test-sfl-istream.tmp";
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
);
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
,
120 test_open_stream (&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
);