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 ("1234567890", 1, 10, f) != 10 || 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 if (lseek (fd, 0, SEEK_CUR) == 5)
56 fputs ("Sample file was not buffered after fread.\n", stderr);
58 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 fputs ("File offset is wrong after fseek.\n", stderr);
74 unlink ("test-fflush.txt");
77 /* Check that file reading resumes at correct location. */
80 fputs ("Failed to read next byte after fseek.\n", stderr);
82 unlink ("test-fflush.txt");
85 /* For deterministic results, ensure f read a bigger buffer. */
86 if (lseek (fd, 0, SEEK_CUR) == 6)
88 fputs ("Sample file was not buffered after fgetc.\n", stderr);
90 unlink ("test-fflush.txt");
93 /* POSIX requires fflush-fseeko to set file offset of fd. */
94 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
96 fputs ("Failed to flush-fseeko sample file.\n", stderr);
98 unlink ("test-fflush.txt");
101 /* Check that offset is correct. */
102 if (lseek (fd, 0, SEEK_CUR) != 6)
104 fputs ("File offset is wrong after fseeko.\n", stderr);
106 unlink ("test-fflush.txt");
109 /* Check that file reading resumes at correct location. */
110 if (fgetc (f) != '7')
112 fputs ("Failed to read next byte after fseeko.\n", stderr);
114 unlink ("test-fflush.txt");
118 unlink ("test-fflush.txt");