Make test a bit easier to debug.
[gnulib.git] / tests / test-fflush.c
1 /* Test of POSIX compatible fflush() function.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Written by Eric Blake, 2007.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <unistd.h>
26
27 int
28 main (int argc, char *argv[])
29 {
30   FILE *f;
31   char buffer[10];
32   int fd;
33
34   /* Create test file.  */
35   f = fopen ("test-fflush.txt", "w");
36   if (!f || fwrite ("1234567890ABCDEFG", 1, 17, f) != 17 || fclose (f) != 0)
37     {
38       fputs ("Failed to create sample file.\n", stderr);
39       unlink ("test-fflush.txt");
40       return 1;
41     }
42
43   /* Test fflush.  */
44   f = fopen ("test-fflush.txt", "r");
45   fd = fileno (f);
46   if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
47     {
48       fputs ("Failed initial read of sample file.\n", stderr);
49       fclose (f);
50       unlink ("test-fflush.txt");
51       return 1;
52     }
53   /* For deterministic results, ensure f read a bigger buffer.
54      This is not the case on BeOS.  */
55 #if !defined __BEOS__
56   if (lseek (fd, 0, SEEK_CUR) == 5)
57     {
58       fputs ("Sample file was not buffered after fread.\n", stderr);
59       fclose (f);
60       unlink ("test-fflush.txt");
61       return 1;
62     }
63 #endif
64   /* POSIX requires fflush-fseek to set file offset of fd.  */
65   if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
66     {
67       fputs ("Failed to flush-fseek sample file.\n", stderr);
68       fclose (f);
69       unlink ("test-fflush.txt");
70       return 1;
71     }
72   /* Check that offset is correct.  */
73   if (lseek (fd, 0, SEEK_CUR) != 5)
74     {
75       fprintf (stderr, "File offset is wrong after fseek: %ld.\n",
76                (long) lseek (fd, 0, SEEK_CUR));
77       fclose (f);
78       unlink ("test-fflush.txt");
79       return 1;
80     }
81   if (ftell (f) != 5)
82     {
83       fprintf (stderr, "ftell result is wrong after fseek: %ld.\n",
84                (long) ftell (f));
85       fclose (f);
86       unlink ("test-fflush.txt");
87       return 1;
88     }
89   /* Check that file reading resumes at correct location.  */
90   if (fgetc (f) != '6')
91     {
92       fputs ("Failed to read next byte after fseek.\n", stderr);
93       fclose (f);
94       unlink ("test-fflush.txt");
95       return 1;
96     }
97   /* For deterministic results, ensure f read a bigger buffer.  */
98   if (lseek (fd, 0, SEEK_CUR) == 6)
99     {
100       fputs ("Sample file was not buffered after fgetc.\n", stderr);
101       fclose (f);
102       unlink ("test-fflush.txt");
103       return 1;
104     }
105   /* POSIX requires fflush-fseeko to set file offset of fd.  */
106   if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
107     {
108       fputs ("Failed to flush-fseeko sample file.\n", stderr);
109       fclose (f);
110       unlink ("test-fflush.txt");
111       return 1;
112     }
113   /* Check that offset is correct.  */
114   if (lseek (fd, 0, SEEK_CUR) != 6)
115     {
116       fprintf (stderr, "File offset is wrong after fseeko: %ld.\n",
117                (long) lseek (fd, 0, SEEK_CUR));
118       fclose (f);
119       unlink ("test-fflush.txt");
120       return 1;
121     }
122   if (ftell (f) != 6)
123     {
124       fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n",
125                (long) ftell (f));
126       fclose (f);
127       unlink ("test-fflush.txt");
128       return 1;
129     }
130   /* Check that file reading resumes at correct location.  */
131   if (fgetc (f) != '7')
132     {
133       fputs ("Failed to read next byte after fseeko.\n", stderr);
134       fclose (f);
135       unlink ("test-fflush.txt");
136       return 1;
137     }
138   fclose (f);
139   unlink ("test-fflush.txt");
140   return 0;
141 }