1 /* Test of freading() function.
2 Copyright (C) 2007-2009 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
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. */
32 #define ASSERT(expr) \
37 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
44 #define TESTFILE "t-freading.tmp"
51 /* Create a file with some contents. Write-only file is never reading. */
52 fp = fopen (TESTFILE, "w");
54 ASSERT (!freading (fp));
55 ASSERT (fwrite ("foobarsh", 1, 8, fp) == 8);
56 ASSERT (!freading (fp));
57 ASSERT (fclose (fp) == 0);
59 /* Open it in read-only mode. Read-only file is always reading. */
60 fp = fopen (TESTFILE, "r");
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));
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);
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
82 /* First a scenario with only fgetc, fseek, fputc. */
83 fp = fopen (TESTFILE, "r+");
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);
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
109 /* Here a scenario that includes fflush. */
110 fp = fopen (TESTFILE, "r+");
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));
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);
135 /* Open it in append mode. Write-only file is never reading. */
136 fp = fopen (TESTFILE, "a");
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);