1 /* Test of POSIX compatible fflush() function.
2 Copyright (C) 2007, 2009 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. */
31 /* Create test file. */
32 f = fopen ("test-fflush.txt", "w");
33 if (!f || fwrite ("1234567890ABCDEFG", 1, 17, f) != 17 || fclose (f) != 0)
35 fputs ("Failed to create sample file.\n", stderr);
36 unlink ("test-fflush.txt");
41 f = fopen ("test-fflush.txt", "r");
43 if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
45 fputs ("Failed initial read of sample file.\n", stderr);
47 unlink ("test-fflush.txt");
50 /* For deterministic results, ensure f read a bigger buffer.
51 This is not the case on BeOS, nor on uClibc. */
52 #if !(defined __BEOS__ || defined __UCLIBC__)
53 if (lseek (fd, 0, SEEK_CUR) == 5)
55 fputs ("Sample file was not buffered after fread.\n", stderr);
57 unlink ("test-fflush.txt");
61 /* POSIX requires fflush-fseek to set file offset of fd. */
62 if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
64 fputs ("Failed to flush-fseek sample file.\n", stderr);
66 unlink ("test-fflush.txt");
69 /* Check that offset is correct. */
70 if (lseek (fd, 0, SEEK_CUR) != 5)
72 fprintf (stderr, "File offset is wrong after fseek: %ld.\n",
73 (long) lseek (fd, 0, SEEK_CUR));
75 unlink ("test-fflush.txt");
80 fprintf (stderr, "ftell result is wrong after fseek: %ld.\n",
83 unlink ("test-fflush.txt");
86 /* Check that file reading resumes at correct location. */
89 fputs ("Failed to read next byte after fseek.\n", stderr);
91 unlink ("test-fflush.txt");
94 /* For deterministic results, ensure f read a bigger buffer. */
95 if (lseek (fd, 0, SEEK_CUR) == 6)
97 fputs ("Sample file was not buffered after fgetc.\n", stderr);
99 unlink ("test-fflush.txt");
102 /* POSIX requires fflush-fseeko to set file offset of fd. */
103 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
105 fputs ("Failed to flush-fseeko sample file.\n", stderr);
107 unlink ("test-fflush.txt");
110 /* Check that offset is correct. */
111 if (lseek (fd, 0, SEEK_CUR) != 6)
113 fprintf (stderr, "File offset is wrong after fseeko: %ld.\n",
114 (long) lseek (fd, 0, SEEK_CUR));
116 unlink ("test-fflush.txt");
121 fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n",
124 unlink ("test-fflush.txt");
127 /* Check that file reading resumes at correct location. */
128 if (fgetc (f) != '7')
130 fputs ("Failed to read next byte after fseeko.\n", stderr);
132 unlink ("test-fflush.txt");
136 unlink ("test-fflush.txt");