1 /* Test of POSIX compatible fflush() function.
2 Copyright (C) 2007 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Eric Blake, 2007. */
28 main (int argc, char *argv[])
34 /* Create test file. */
35 f = fopen ("test-fflush.txt", "w");
36 if (!f || fwrite ("1234567890ABCDEFG", 1, 17, f) != 17 || fclose (f) != 0)
38 fputs ("Failed to create sample file.\n", stderr);
39 unlink ("test-fflush.txt");
44 f = fopen ("test-fflush.txt", "r");
46 if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
48 fputs ("Failed initial read of sample file.\n", stderr);
50 unlink ("test-fflush.txt");
53 /* For deterministic results, ensure f read a bigger buffer.
54 This is not the case on BeOS. */
56 if (lseek (fd, 0, SEEK_CUR) == 5)
58 fputs ("Sample file was not buffered after fread.\n", stderr);
60 unlink ("test-fflush.txt");
64 /* POSIX requires fflush-fseek to set file offset of fd. */
65 if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
67 fputs ("Failed to flush-fseek sample file.\n", stderr);
69 unlink ("test-fflush.txt");
72 /* Check that offset is correct. */
73 if (lseek (fd, 0, SEEK_CUR) != 5)
75 fprintf (stderr, "File offset is wrong after fseek: %ld.\n",
76 (long) lseek (fd, 0, SEEK_CUR));
78 unlink ("test-fflush.txt");
83 fprintf (stderr, "ftell result is wrong after fseek: %ld.\n",
86 unlink ("test-fflush.txt");
89 /* Check that file reading resumes at correct location. */
92 fputs ("Failed to read next byte after fseek.\n", stderr);
94 unlink ("test-fflush.txt");
97 /* For deterministic results, ensure f read a bigger buffer. */
98 if (lseek (fd, 0, SEEK_CUR) == 6)
100 fputs ("Sample file was not buffered after fgetc.\n", stderr);
102 unlink ("test-fflush.txt");
105 /* POSIX requires fflush-fseeko to set file offset of fd. */
106 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
108 fputs ("Failed to flush-fseeko sample file.\n", stderr);
110 unlink ("test-fflush.txt");
113 /* Check that offset is correct. */
114 if (lseek (fd, 0, SEEK_CUR) != 6)
116 fprintf (stderr, "File offset is wrong after fseeko: %ld.\n",
117 (long) lseek (fd, 0, SEEK_CUR));
119 unlink ("test-fflush.txt");
124 fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n",
127 unlink ("test-fflush.txt");
130 /* Check that file reading resumes at correct location. */
131 if (fgetc (f) != '7')
133 fputs ("Failed to read next byte after fseeko.\n", stderr);
135 unlink ("test-fflush.txt");
139 unlink ("test-fflush.txt");