1 /* Test of POSIX compatible fflush() function.
2 Copyright (C) 2007, 2009-2011 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 3 of the License, or
7 (at your option) 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake, 2007. */
21 /* None of the files accessed by this test are large, so disable the
22 ftell link warning if we are not using the gnulib ftell module. */
23 #define _GL_NO_LARGE_FILES
26 #include "signature.h"
27 SIGNATURE_CHECK (fflush, int, (FILE *));
38 /* Create test file. */
39 f = fopen ("test-fflush.txt", "w");
40 if (!f || fwrite ("1234567890ABCDEFG", 1, 17, f) != 17 || fclose (f) != 0)
42 fputs ("Failed to create sample file.\n", stderr);
43 unlink ("test-fflush.txt");
48 f = fopen ("test-fflush.txt", "r");
50 if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
52 fputs ("Failed initial read of sample file.\n", stderr);
55 unlink ("test-fflush.txt");
58 /* For deterministic results, ensure f read a bigger buffer.
59 This is not the case on BeOS, nor on uClibc. */
60 #if !(defined __BEOS__ || defined __UCLIBC__)
61 if (lseek (fd, 0, SEEK_CUR) == 5)
63 fputs ("Sample file was not buffered after fread.\n", stderr);
65 unlink ("test-fflush.txt");
69 /* POSIX requires fflush-fseek to set file offset of fd. */
70 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
72 fputs ("Failed to flush-fseek sample file.\n", stderr);
74 unlink ("test-fflush.txt");
77 /* Check that offset is correct. */
78 if (lseek (fd, 0, SEEK_CUR) != 5)
80 fprintf (stderr, "File offset is wrong after fseek: %ld.\n",
81 (long) lseek (fd, 0, SEEK_CUR));
83 unlink ("test-fflush.txt");
88 fprintf (stderr, "ftell result is wrong after fseek: %ld.\n",
91 unlink ("test-fflush.txt");
94 /* Check that file reading resumes at correct location. */
97 fputs ("Failed to read next byte after fseek.\n", stderr);
99 unlink ("test-fflush.txt");
102 /* For deterministic results, ensure f read a bigger buffer. */
103 if (lseek (fd, 0, SEEK_CUR) == 6)
105 fputs ("Sample file was not buffered after fgetc.\n", stderr);
107 unlink ("test-fflush.txt");
110 /* POSIX requires fflush-fseeko to set file offset of fd. */
111 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
113 fputs ("Failed to flush-fseeko sample file.\n", stderr);
115 unlink ("test-fflush.txt");
118 /* Check that offset is correct. */
119 if (lseek (fd, 0, SEEK_CUR) != 6)
121 fprintf (stderr, "File offset is wrong after fseeko: %ld.\n",
122 (long) lseek (fd, 0, SEEK_CUR));
124 unlink ("test-fflush.txt");
129 fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n",
132 unlink ("test-fflush.txt");
135 /* Check that file reading resumes at correct location. */
136 if (fgetc (f) != '7')
138 fputs ("Failed to read next byte after fseeko.\n", stderr);
140 unlink ("test-fflush.txt");
144 unlink ("test-fflush.txt");