test-freading: clean up temporary file
[gnulib.git] / tests / test-freading.c
1 /* Test of freading() 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 Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include "freading.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 /* None of the files accessed by this test are large, so disable the
27    fseek link warning if we are not using the gnulib fseek module.  */
28 #if !GNULIB_FSEEK
29 # undef fseek
30 #endif
31
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           fflush (stderr);                                                   \
39           abort ();                                                          \
40         }                                                                    \
41     }                                                                        \
42   while (0)
43
44 #define TESTFILE "t-freading.tmp"
45
46 int
47 main (void)
48 {
49   FILE *fp;
50
51   /* Create a file with some contents.  Write-only file is never reading.  */
52   fp = fopen (TESTFILE, "w");
53   ASSERT (fp);
54   ASSERT (!freading (fp));
55   ASSERT (fwrite ("foobarsh", 1, 8, fp) == 8);
56   ASSERT (!freading (fp));
57   ASSERT (fclose (fp) == 0);
58
59   /* Open it in read-only mode.  Read-only file is always reading.  */
60   fp = fopen (TESTFILE, "r");
61   ASSERT (fp);
62   ASSERT (freading (fp));
63   ASSERT (fgetc (fp) == 'f');
64   ASSERT (freading (fp));
65   ASSERT (fseek (fp, 2, SEEK_CUR) == 0);
66   ASSERT (freading (fp));
67   ASSERT (fgetc (fp) == 'b');
68   ASSERT (freading (fp));
69   fflush (fp);
70   ASSERT (freading (fp));
71   ASSERT (fgetc (fp) == 'a');
72   ASSERT (freading (fp));
73   ASSERT (fseek (fp, 0, SEEK_END) == 0);
74   ASSERT (freading (fp));
75   ASSERT (fclose (fp) == 0);
76
77   /* Open it in read-write mode.  POSIX requires a reposition (fseek,
78      fsetpos, rewind) or EOF when transitioning from read to write;
79      freading is only deterministic after input or output, but this
80      test case should be portable even on open, after reposition, and
81      at EOF.  */
82   /* First a scenario with only fgetc, fseek, fputc.  */
83   fp = fopen (TESTFILE, "r+");
84   ASSERT (fp);
85   ASSERT (!freading (fp));
86   ASSERT (fgetc (fp) == 'f');
87   ASSERT (freading (fp));
88   ASSERT (fseek (fp, 2, SEEK_CUR) ==  0);
89   /* freading (fp) is undefined here, but fwriting (fp) is false.  */
90   ASSERT (fgetc (fp) == 'b');
91   ASSERT (freading (fp));
92   /* This fseek call is necessary when switching from reading to writing.
93      See the description of fopen(), ISO C 99 7.19.5.3.(6).  */
94   ASSERT (fseek (fp, 0, SEEK_CUR) == 0);
95   /* freading (fp) is undefined here, but fwriting (fp) is false.  */
96   ASSERT (fputc ('x', fp) == 'x');
97   ASSERT (!freading (fp));
98   ASSERT (fseek (fp, 0, SEEK_END) == 0);
99   /* freading (fp) is undefined here, because on some implementations (e.g.
100      glibc) fseek causes a buffer to be read.
101      fwriting (fp) is undefined as well.  */
102   ASSERT (fclose (fp) == 0);
103
104   /* Open it in read-write mode.  POSIX requires a reposition (fseek,
105      fsetpos, rewind) or EOF when transitioning from read to write;
106      freading is only deterministic after input or output, but this
107      test case should be portable even on open, after reposition, and
108      at EOF.  */
109   /* Here a scenario that includes fflush.  */
110   fp = fopen (TESTFILE, "r+");
111   ASSERT (fp);
112   ASSERT (!freading (fp));
113   ASSERT (fgetc (fp) == 'f');
114   ASSERT (freading (fp));
115   ASSERT (fseek (fp, 2, SEEK_CUR) == 0);
116   /* freading (fp) is undefined here, but fwriting (fp) is false.  */
117   ASSERT (fgetc (fp) == 'b');
118   ASSERT (freading (fp));
119   fflush (fp);
120   /* freading (fp) is undefined here, but fwriting (fp) is false.  */
121   ASSERT (fgetc (fp) == 'x');
122   ASSERT (freading (fp));
123   /* This fseek call is necessary when switching from reading to writing.
124      See the description of fopen(), ISO C 99 7.19.5.3.(6).  */
125   ASSERT (fseek (fp, 0, SEEK_CUR) == 0);
126   /* freading (fp) is undefined here, but fwriting (fp) is false.  */
127   ASSERT (fputc ('z', fp) == 'z');
128   ASSERT (!freading (fp));
129   ASSERT (fseek (fp, 0, SEEK_END) == 0);
130   /* freading (fp) is undefined here, because on some implementations (e.g.
131      glibc) fseek causes a buffer to be read.
132      fwriting (fp) is undefined as well.  */
133   ASSERT (fclose (fp) == 0);
134
135   /* Open it in append mode.  Write-only file is never reading.  */
136   fp = fopen (TESTFILE, "a");
137   ASSERT (fp);
138   ASSERT (!freading (fp));
139   ASSERT (fwrite ("bla", 1, 3, fp) == 3);
140   ASSERT (!freading (fp));
141   ASSERT (fclose (fp) == 0);
142   ASSERT (remove (TESTFILE) == 0);
143   return 0;
144 }