1 /* Test of POSIX compatible fflush() function.
2 Copyright (C) 2007, 2009, 2010 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);
54 unlink ("test-fflush.txt");
57 /* For deterministic results, ensure f read a bigger buffer.
58 This is not the case on BeOS, nor on uClibc. */
59 #if !(defined __BEOS__ || defined __UCLIBC__)
60 if (lseek (fd, 0, SEEK_CUR) == 5)
62 fputs ("Sample file was not buffered after fread.\n", stderr);
64 unlink ("test-fflush.txt");
68 /* POSIX requires fflush-fseek to set file offset of fd. */
69 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
71 fputs ("Failed to flush-fseek sample file.\n", stderr);
73 unlink ("test-fflush.txt");
76 /* Check that offset is correct. */
77 if (lseek (fd, 0, SEEK_CUR) != 5)
79 fprintf (stderr, "File offset is wrong after fseek: %ld.\n",
80 (long) lseek (fd, 0, SEEK_CUR));
82 unlink ("test-fflush.txt");
87 fprintf (stderr, "ftell result is wrong after fseek: %ld.\n",
90 unlink ("test-fflush.txt");
93 /* Check that file reading resumes at correct location. */
96 fputs ("Failed to read next byte after fseek.\n", stderr);
98 unlink ("test-fflush.txt");
101 /* For deterministic results, ensure f read a bigger buffer. */
102 if (lseek (fd, 0, SEEK_CUR) == 6)
104 fputs ("Sample file was not buffered after fgetc.\n", stderr);
106 unlink ("test-fflush.txt");
109 /* POSIX requires fflush-fseeko to set file offset of fd. */
110 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
112 fputs ("Failed to flush-fseeko sample file.\n", stderr);
114 unlink ("test-fflush.txt");
117 /* Check that offset is correct. */
118 if (lseek (fd, 0, SEEK_CUR) != 6)
120 fprintf (stderr, "File offset is wrong after fseeko: %ld.\n",
121 (long) lseek (fd, 0, SEEK_CUR));
123 unlink ("test-fflush.txt");
128 fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n",
131 unlink ("test-fflush.txt");
134 /* Check that file reading resumes at correct location. */
135 if (fgetc (f) != '7')
137 fputs ("Failed to read next byte after fseeko.\n", stderr);
139 unlink ("test-fflush.txt");
143 unlink ("test-fflush.txt");