PowerPC: remove wrong roundl implementation for PowerPC64
[glibc.git] / libio / tst-ftell-active-handler.c
blob54bfe6380f9b7a084841b99c9ad3f243d044e2e9
1 /* Verify that ftell returns the correct value at various points before and
2 after the handler on which it is called becomes active.
3 Copyright (C) 2014 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <locale.h>
27 #include <wchar.h>
29 static int do_test (void);
31 #define TEST_FUNCTION do_test ()
32 #include "../test-skeleton.c"
34 #define get_handles_fdopen(filename, fd, fp, fd_mode, mode) \
35 ({ \
36 int ret = 0; \
37 (fd) = open ((filename), (fd_mode), 0); \
38 if ((fd) == -1) \
39 { \
40 printf ("open failed: %m\n"); \
41 ret = 1; \
42 } \
43 else \
44 { \
45 (fp) = fdopen ((fd), (mode)); \
46 if ((fp) == NULL) \
47 { \
48 printf ("fdopen failed: %m\n"); \
49 close (fd); \
50 ret = 1; \
51 } \
52 } \
53 ret; \
56 #define get_handles_fopen(filename, fd, fp, mode) \
57 ({ \
58 int ret = 0; \
59 (fp) = fopen ((filename), (mode)); \
60 if ((fp) == NULL) \
61 { \
62 printf ("fopen failed: %m\n"); \
63 ret = 1; \
64 } \
65 else \
66 { \
67 (fd) = fileno (fp); \
68 if ((fd) == -1) \
69 { \
70 printf ("fileno failed: %m\n"); \
71 ret = 1; \
72 } \
73 } \
74 ret; \
77 /* data points to either char_data or wide_data, depending on whether we're
78 testing regular file mode or wide mode respectively. Similarly,
79 fputs_func points to either fputs or fputws. data_len keeps track of the
80 length of the current data and file_len maintains the current file
81 length. */
82 static const void *data;
83 static const char *char_data = "abcdef";
84 static const wchar_t *wide_data = L"abcdef";
85 static size_t data_len;
86 static size_t file_len;
88 typedef int (*fputs_func_t) (const void *data, FILE *fp);
89 fputs_func_t fputs_func;
91 /* Test that the value of ftell is not cached when the stream handle is not
92 active. */
93 static int
94 do_ftell_test (const char *filename)
96 int ret = 0;
97 struct test
99 const char *mode;
100 int fd_mode;
101 size_t old_off;
102 size_t new_off;
103 } test_modes[] = {
104 /* In w, w+ and r+ modes, the file position should be at the
105 beginning of the file. After the write, the offset should be
106 updated to data_len. */
107 {"w", O_WRONLY, 0, data_len},
108 {"w+", O_RDWR, 0, data_len},
109 {"r+", O_RDWR, 0, data_len},
110 /* For 'a' and 'a+' modes, the initial file position should be the
111 current end of file. After the write, the offset has data_len
112 added to the old value. */
113 {"a", O_WRONLY, data_len, 2 * data_len},
114 {"a+", O_RDWR, 2 * data_len, 3 * data_len},
116 for (int j = 0; j < 2; j++)
118 for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
120 FILE *fp;
121 int fd;
122 printf ("\tftell: %s (file, \"%s\"): ", j == 0 ? "fdopen" : "fopen",
123 test_modes[i].mode);
125 if (j == 0)
126 ret = get_handles_fdopen (filename, fd, fp, test_modes[i].fd_mode,
127 test_modes[i].mode);
128 else
129 ret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
131 if (ret != 0)
132 return ret;
134 long off = ftell (fp);
135 if (off != test_modes[i].old_off)
137 printf ("Incorrect old offset. Expected %zu but got %ld, ",
138 test_modes[i].old_off, off);
139 ret |= 1;
141 else
142 printf ("old offset = %ld, ", off);
144 /* The effect of this write on the offset should be seen in the ftell
145 call that follows it. */
146 int ret = write (fd, data, data_len);
147 off = ftell (fp);
149 if (off != test_modes[i].new_off)
151 printf ("Incorrect new offset. Expected %zu but got %ld\n",
152 test_modes[i].old_off, off);
153 ret |= 1;
155 else
156 printf ("new offset = %ld\n", off);
158 fclose (fp);
162 return ret;
165 /* This test opens the file for writing, moves the file offset of the
166 underlying file, writes out data and then checks if ftell trips on it. */
167 static int
168 do_write_test (const char *filename)
170 FILE *fp = NULL;
171 int fd;
172 int ret = 0;
173 struct test
175 const char *mode;
176 int fd_mode;
177 } test_modes[] = {
178 {"w", O_WRONLY},
179 {"w+", O_RDWR},
180 {"r+", O_RDWR}
183 for (int j = 0; j < 2; j++)
185 for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
187 printf ("\twrite: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
188 test_modes[i].mode);
190 if (j == 0)
191 ret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
192 else
193 ret = get_handles_fdopen (filename, fd, fp, test_modes[i].fd_mode,
194 test_modes[i].mode);
196 if (ret != 0)
197 return ret;
199 /* Move offset to just before the end of the file. */
200 off_t ret = lseek (fd, file_len - 1, SEEK_SET);
201 if (ret == -1)
203 printf ("lseek failed: %m\n");
204 ret |= 1;
207 /* Write some data. */
208 size_t written = fputs_func (data, fp);
210 if (written == EOF)
212 printf ("fputs[1] failed to write data\n");
213 ret |= 1;
216 /* Verify that the offset points to the end of the file. The length
217 of the file would be the original length + the length of data
218 written to it - the amount by which we moved the offset using
219 lseek. */
220 long offset = ftell (fp);
221 file_len = file_len - 1 + data_len;
223 if (offset != file_len)
225 printf ("Incorrect offset. Expected %zu, but got %ld\n",
226 file_len, offset);
228 ret |= 1;
231 printf ("offset = %ld\n", offset);
232 fclose (fp);
236 return ret;
239 /* This test opens a file in append mode, writes some data, and then verifies
240 that ftell does not trip over it. */
241 static int
242 do_append_test (const char *filename)
244 FILE *fp = NULL;
245 int ret = 0;
246 int fd;
248 struct test
250 const char *mode;
251 int fd_mode;
252 } test_modes[] = {
253 {"a", O_WRONLY},
254 {"a+", O_RDWR}
257 for (int j = 0; j < 2; j++)
259 for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
261 printf ("\tappend: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
262 test_modes[i].mode);
264 if (j == 0)
265 ret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
266 else
267 ret = get_handles_fdopen (filename, fd, fp, test_modes[i].fd_mode,
268 test_modes[i].mode);
270 if (ret != 0)
271 return ret;
273 /* Write some data. */
274 size_t written = fputs_func (data, fp);
276 if (written == EOF)
278 printf ("fputs[1] failed to write all data\n");
279 ret |= 1;
282 /* Verify that the offset points to the end of the file. The file
283 len is maintained by adding data_len each time to reflect the data
284 written to it. */
285 long offset = ftell (fp);
286 file_len += data_len;
288 if (offset != file_len)
290 printf ("Incorrect offset. Expected %zu, but got %ld\n",
291 file_len, offset);
293 ret |= 1;
296 printf ("offset = %ld\n", offset);
297 fclose (fp);
301 return ret;
304 static int
305 do_one_test (const char *filename)
307 int ret = 0;
309 ret |= do_ftell_test (filename);
310 ret |= do_write_test (filename);
311 ret |= do_append_test (filename);
313 return ret;
316 /* Run a set of tests for ftell for regular files and wide mode files. */
317 static int
318 do_test (void)
320 int ret = 0;
321 FILE *fp = NULL;
322 char *filename;
323 size_t written;
324 int fd = create_temp_file ("tst-active-handler-tmp.", &filename);
326 if (fd == -1)
328 printf ("create_temp_file: %m\n");
329 return 1;
332 fp = fdopen (fd, "w");
333 if (fp == NULL)
335 printf ("fdopen[0]: %m\n");
336 close (fd);
337 return 1;
340 data = char_data;
341 data_len = strlen (char_data);
342 file_len = strlen (char_data);
343 written = fputs (data, fp);
345 if (written == EOF)
347 printf ("fputs[1] failed to write data\n");
348 ret = 1;
351 fclose (fp);
352 if (ret)
353 return ret;
355 /* Tests for regular files. */
356 puts ("Regular mode:");
357 fputs_func = (fputs_func_t) fputs;
358 data = char_data;
359 data_len = strlen (char_data);
360 ret |= do_one_test (filename);
362 /* Truncate the file before repeating the tests in wide mode. */
363 fp = fopen (filename, "w");
364 if (fp == NULL)
366 printf ("fopen failed %m\n");
367 return 1;
369 fclose (fp);
371 /* Tests for wide files. */
372 puts ("Wide mode:");
373 if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
375 printf ("Cannot set en_US.UTF-8 locale.\n");
376 return 1;
378 fputs_func = (fputs_func_t) fputws;
379 data = wide_data;
380 data_len = wcslen (wide_data);
381 ret |= do_one_test (filename);
383 return ret;