Make Makefile.am example code more cut-and-paste friendly.
[gnulib.git] / tests / test-fflush.c
1 /* Test of POSIX compatible fflush() function.
2    Copyright (C) 2007, 2009, 2010 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       fclose (f);
54       unlink ("test-fflush.txt");
55       return 1;
56     }
57   /* For deterministic results, ensure f read a bigger buffer.
58      This is not the case on BeOS, nor on uClibc.  */
59 #if !(defined __BEOS__ || defined __UCLIBC__)
60   if (lseek (fd, 0, SEEK_CUR) == 5)
61     {
62       fputs ("Sample file was not buffered after fread.\n", stderr);
63       fclose (f);
64       unlink ("test-fflush.txt");
65       return 1;
66     }
67 #endif
68   /* POSIX requires fflush-fseek to set file offset of fd.  */
69   if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
70     {
71       fputs ("Failed to flush-fseek sample file.\n", stderr);
72       fclose (f);
73       unlink ("test-fflush.txt");
74       return 1;
75     }
76   /* Check that offset is correct.  */
77   if (lseek (fd, 0, SEEK_CUR) != 5)
78     {
79       fprintf (stderr, "File offset is wrong after fseek: %ld.\n",
80                (long) lseek (fd, 0, SEEK_CUR));
81       fclose (f);
82       unlink ("test-fflush.txt");
83       return 1;
84     }
85   if (ftell (f) != 5)
86     {
87       fprintf (stderr, "ftell result is wrong after fseek: %ld.\n",
88                (long) ftell (f));
89       fclose (f);
90       unlink ("test-fflush.txt");
91       return 1;
92     }
93   /* Check that file reading resumes at correct location.  */
94   if (fgetc (f) != '6')
95     {
96       fputs ("Failed to read next byte after fseek.\n", stderr);
97       fclose (f);
98       unlink ("test-fflush.txt");
99       return 1;
100     }
101   /* For deterministic results, ensure f read a bigger buffer.  */
102   if (lseek (fd, 0, SEEK_CUR) == 6)
103     {
104       fputs ("Sample file was not buffered after fgetc.\n", stderr);
105       fclose (f);
106       unlink ("test-fflush.txt");
107       return 1;
108     }
109   /* POSIX requires fflush-fseeko to set file offset of fd.  */
110   if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
111     {
112       fputs ("Failed to flush-fseeko sample file.\n", stderr);
113       fclose (f);
114       unlink ("test-fflush.txt");
115       return 1;
116     }
117   /* Check that offset is correct.  */
118   if (lseek (fd, 0, SEEK_CUR) != 6)
119     {
120       fprintf (stderr, "File offset is wrong after fseeko: %ld.\n",
121                (long) lseek (fd, 0, SEEK_CUR));
122       fclose (f);
123       unlink ("test-fflush.txt");
124       return 1;
125     }
126   if (ftell (f) != 6)
127     {
128       fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n",
129                (long) ftell (f));
130       fclose (f);
131       unlink ("test-fflush.txt");
132       return 1;
133     }
134   /* Check that file reading resumes at correct location.  */
135   if (fgetc (f) != '7')
136     {
137       fputs ("Failed to read next byte after fseeko.\n", stderr);
138       fclose (f);
139       unlink ("test-fflush.txt");
140       return 1;
141     }
142   fclose (f);
143   unlink ("test-fflush.txt");
144   return 0;
145 }