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