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