1 /* Test the __libc_readline_unlocked function.
2 Copyright (C) 2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* Exercise __libc_readline_unlocked with various combinations of line
20 lengths, stdio buffer sizes, and line read buffer sizes. */
26 #include <support/check.h>
27 #include <support/support.h>
28 #include <support/temp_file.h>
29 #include <support/test-driver.h>
30 #include <support/xmemstream.h>
31 #include <support/xstdio.h>
32 #include <support/xunistd.h>
36 maximum_line_length
= 7,
40 /* -1: Do not set buffer size. 0: unbuffered. Otherwise, use this as
41 the size of the buffer. */
42 static int buffer_size
;
44 /* These size of the buffer used for reading. Must be at least 2. */
47 /* If a read files with ERANGE, increase the buffer size by this
48 amount. Must be positive. */
49 static int read_size_increment
;
51 /* If non-zero, do not reset the read size after an ERANGE error. */
52 static int read_size_preserve
;
54 /* If non-zero, no '\n' at the end of the file. */
55 static int no_newline_at_eof
;
57 /* Length of the line, or -1 if the line is not present. */
58 static int line_lengths
[number_of_lines
];
60 /* The name of the test file. */
61 static char *test_file_path
;
63 /* The contents of the test file. */
64 static char expected_contents
[(maximum_line_length
+ 2) * number_of_lines
+ 1];
65 static size_t expected_length
;
67 /* Returns a random byte which is not zero or the line terminator. */
71 static unsigned int rand_state
= 1;
74 char result
= rand_r (&rand_state
) >> 16;
75 if (result
!= 0 && result
!= '\n')
80 /* Create the test file. */
82 prepare (int argc
, char **argv
)
84 int fd
= create_temp_file ("tst-readline-", &test_file_path
);
85 TEST_VERIFY_EXIT (fd
>= 0);
89 /* Prepare the test file. Return false if the test parameters are
90 incongruent and the test should be skipped. */
92 write_test_file (void)
95 char *p
= expected_contents
;
96 for (int lineno
= 0; lineno
< number_of_lines
; ++lineno
)
97 for (int i
= 0; i
< line_lengths
[lineno
]; ++i
)
98 *p
++ = random_char ();
99 expected_length
= p
- &expected_contents
[0];
100 if (no_newline_at_eof
)
102 if (expected_length
== 0)
107 if (test_verbose
> 0)
109 printf ("info: writing test file of %zu bytes:\n", expected_length
);
110 for (int i
= 0; i
< number_of_lines
; ++i
)
111 printf (" line %d: %d\n", i
, line_lengths
[i
]);
112 if (no_newline_at_eof
)
113 puts (" (no newline at EOF)");
115 TEST_VERIFY_EXIT (expected_length
< sizeof (expected_contents
));
117 support_write_file_string (test_file_path
, expected_contents
);
121 /* Run a single test (a combination of a test file and read
126 TEST_VERIFY_EXIT (read_size_increment
> 0);
127 if (test_verbose
> 0)
129 printf ("info: running test: buffer_size=%d read_size=%d\n"
130 " read_size_increment=%d read_size_preserve=%d\n",
131 buffer_size
, read_size
, read_size_increment
, read_size_preserve
);
134 struct xmemstream result
;
135 xopen_memstream (&result
);
137 FILE *fp
= xfopen (test_file_path
, "rce");
138 char *fp_buffer
= NULL
;
139 if (buffer_size
== 0)
140 TEST_VERIFY_EXIT (setvbuf (fp
, NULL
, _IONBF
, 0) == 0);
143 fp_buffer
= xmalloc (buffer_size
);
144 TEST_VERIFY_EXIT (setvbuf (fp
, fp_buffer
, _IOFBF
, buffer_size
) == 0);
147 char *line_buffer
= xmalloc (read_size
);
148 size_t line_buffer_size
= read_size
;
152 ssize_t ret
= __libc_readline_unlocked
153 (fp
, line_buffer
, line_buffer_size
);
156 TEST_VERIFY (ret
== -1);
158 FAIL_EXIT1 ("__libc_readline_unlocked: %m");
159 line_buffer_size
+= read_size_increment
;
161 line_buffer
= xmalloc (line_buffer_size
);
162 /* Try reading this line again. */
168 /* A line has been read. Save it. */
169 TEST_VERIFY (ret
== strlen (line_buffer
));
170 const char *pnl
= strchr (line_buffer
, '\n');
171 /* If there is a \n, it must be at the end. */
172 TEST_VERIFY (pnl
== NULL
|| pnl
== line_buffer
+ ret
- 1);
173 fputs (line_buffer
, result
.out
);
175 /* Restore the original read size if required. */
176 if (line_buffer_size
> read_size
&& !read_size_preserve
)
178 line_buffer_size
= read_size
;
180 line_buffer
= xmalloc (line_buffer_size
);
189 xfclose_memstream (&result
);
190 TEST_VERIFY (result
.length
== expected_length
);
191 TEST_VERIFY (strcmp (result
.buffer
, expected_contents
) == 0);
192 if (test_verbose
> 0)
194 printf ("info: expected (%zu): [[%s]]\n",
195 expected_length
, expected_contents
);
196 printf ("info: actual (%zu): [[%s]]\n", result
.length
, result
.buffer
);
198 free (result
.buffer
);
201 /* Test one test file with multiple read parameters. */
205 for (buffer_size
= -1; buffer_size
<= maximum_line_length
+ 1; ++buffer_size
)
206 for (read_size
= 2; read_size
<= maximum_line_length
+ 2; ++read_size
)
207 for (read_size_increment
= 1; read_size_increment
<= 4;
208 ++read_size_increment
)
209 for (read_size_preserve
= 0; read_size_preserve
< 2;
210 ++read_size_preserve
)
218 /* Set up the test file contents. */
219 for (line_lengths
[0] = -1; line_lengths
[0] <= maximum_line_length
;
221 for (line_lengths
[1] = -1; line_lengths
[1] <= maximum_line_length
;
223 for (line_lengths
[2] = -1; line_lengths
[2] <= maximum_line_length
;
225 for (no_newline_at_eof
= 0; no_newline_at_eof
< 2; ++no_newline_at_eof
)
227 if (!write_test_file ())
231 free (test_file_path
);
235 #define PREPARE prepare
236 #include <support/test-driver.c>