1 /* Test of freading() function.
2 Copyright (C) 2007 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
27 #define ASSERT(expr) \
32 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38 #define TESTFILE "t-freading.tmp"
45 /* Create a file with some contents. Write-only file is never reading. */
46 fp = fopen (TESTFILE, "w");
49 ASSERT (!freading (fp));
50 if (fwrite ("foobarsh", 1, 8, fp) < 8)
52 ASSERT (!freading (fp));
56 /* Open it in read-only mode. Read-only file is always reading. */
57 fp = fopen (TESTFILE, "r");
60 ASSERT (freading (fp));
61 if (fgetc (fp) != 'f')
63 ASSERT (freading (fp));
64 if (fseek (fp, 2, SEEK_CUR))
66 ASSERT (freading (fp));
67 if (fgetc (fp) != 'b')
69 ASSERT (freading (fp));
71 ASSERT (freading (fp));
72 if (fgetc (fp) != 'a')
74 ASSERT (freading (fp));
75 if (fseek (fp, 0, SEEK_END))
77 ASSERT (freading (fp));
81 /* Open it in read-write mode. POSIX requires a reposition (fseek,
82 fsetpos, rewind) or EOF when transitioning from read to write;
83 freading is only deterministic after input or output, but this
84 test case should be portable even on open, after reposition, and
86 /* First a scenario with only fgetc, fseek, fputc. */
87 fp = fopen (TESTFILE, "r+");
90 ASSERT (!freading (fp));
91 if (fgetc (fp) != 'f')
93 ASSERT (freading (fp));
94 if (fseek (fp, 2, SEEK_CUR))
96 /* freading (fp) is undefined here, but fwriting (fp) is false. */
97 if (fgetc (fp) != 'b')
99 ASSERT (freading (fp));
100 /* This fseek call is necessary when switching from reading to writing.
101 See the description of fopen(), ISO C 99 7.19.5.3.(6). */
102 if (fseek (fp, 0, SEEK_CUR) != 0)
104 /* freading (fp) is undefined here, but fwriting (fp) is false. */
105 if (fputc ('x', fp) != 'x')
107 ASSERT (!freading (fp));
108 if (fseek (fp, 0, SEEK_END))
110 /* freading (fp) is undefined here, because on some implementations (e.g.
111 glibc) fseek causes a buffer to be read.
112 fwriting (fp) is undefined as well. */
116 /* Open it in read-write mode. POSIX requires a reposition (fseek,
117 fsetpos, rewind) or EOF when transitioning from read to write;
118 freading is only deterministic after input or output, but this
119 test case should be portable even on open, after reposition, and
121 /* Here a scenario that includes fflush. */
122 fp = fopen (TESTFILE, "r+");
125 ASSERT (!freading (fp));
126 if (fgetc (fp) != 'f')
128 ASSERT (freading (fp));
129 if (fseek (fp, 2, SEEK_CUR))
131 /* freading (fp) is undefined here, but fwriting (fp) is false. */
132 if (fgetc (fp) != 'b')
134 ASSERT (freading (fp));
136 /* freading (fp) is undefined here, but fwriting (fp) is false. */
137 if (fgetc (fp) != 'x')
139 ASSERT (freading (fp));
140 /* This fseek call is necessary when switching from reading to writing.
141 See the description of fopen(), ISO C 99 7.19.5.3.(6). */
142 if (fseek (fp, 0, SEEK_CUR) != 0)
144 /* freading (fp) is undefined here, but fwriting (fp) is false. */
145 if (fputc ('z', fp) != 'z')
147 ASSERT (!freading (fp));
148 if (fseek (fp, 0, SEEK_END))
150 /* freading (fp) is undefined here, because on some implementations (e.g.
151 glibc) fseek causes a buffer to be read.
152 fwriting (fp) is undefined as well. */
156 /* Open it in append mode. Write-only file is never reading. */
157 fp = fopen (TESTFILE, "a");
160 ASSERT (!freading (fp));
161 if (fwrite ("bla", 1, 3, fp) < 3)
163 ASSERT (!freading (fp));
170 fprintf (stderr, "Skipping test: file operations failed.\n");