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