1 /* Test of fwriting() 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 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 #define ASSERT(expr) \
31 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37 #define TESTFILE "t-fwriting.tmp"
44 /* Create a file with some contents. Write-only file is always writing. */
45 fp = fopen (TESTFILE, "w");
48 ASSERT (fwriting (fp));
49 if (fwrite ("foobarsh", 1, 8, fp) < 8)
51 ASSERT (fwriting (fp));
55 /* Open it in read-only mode. Read-only file is never writing. */
56 fp = fopen (TESTFILE, "r");
59 ASSERT (!fwriting (fp));
60 if (fgetc (fp) != 'f')
62 ASSERT (!fwriting (fp));
63 if (fseek (fp, 2, SEEK_CUR))
65 ASSERT (!fwriting (fp));
66 if (fgetc (fp) != 'b')
68 ASSERT (!fwriting (fp));
70 ASSERT (!fwriting (fp));
71 if (fgetc (fp) != 'a')
73 ASSERT (!fwriting (fp));
74 if (fseek (fp, 0, SEEK_END))
76 ASSERT (!fwriting (fp));
80 /* Open it in read-write mode. POSIX requires a reposition (fseek,
81 fsetpos, rewind) or fflush when transitioning from write to read,
82 fwriting is only deterministic after input or output, but this
83 test case should be portable even on open, after reposition, and
85 /* First a scenario with only fgetc, fseek, fputc. */
86 fp = fopen (TESTFILE, "r+");
89 ASSERT (!fwriting (fp));
90 if (fgetc (fp) != 'f')
92 ASSERT (!fwriting (fp));
93 if (fseek (fp, 2, SEEK_CUR))
95 ASSERT (!fwriting (fp));
96 if (fgetc (fp) != 'b')
98 ASSERT (!fwriting (fp));
99 /* This fseek call is necessary when switching from reading to writing.
100 See the description of fopen(), ISO C 99 7.19.5.3.(6). */
101 if (fseek (fp, 0, SEEK_CUR) != 0)
103 ASSERT (!fwriting (fp));
104 if (fputc ('x', fp) != 'x')
106 ASSERT (fwriting (fp));
107 if (fseek (fp, 0, SEEK_END))
109 /* freading (fp) is undefined here, because on some implementations (e.g.
110 glibc) fseek causes a buffer to be read.
111 fwriting (fp) is undefined as well. */
115 /* Open it in read-write mode. POSIX requires a reposition (fseek,
116 fsetpos, rewind) or fflush when transitioning from write to read,
117 fwriting is only deterministic after input or output, but this
118 test case should be portable even on open, after reposition, and
120 /* Here a scenario that includes fflush. */
121 fp = fopen (TESTFILE, "r+");
124 ASSERT (!fwriting (fp));
125 if (fgetc (fp) != 'f')
127 ASSERT (!fwriting (fp));
128 if (fseek (fp, 2, SEEK_CUR))
130 ASSERT (!fwriting (fp));
131 if (fgetc (fp) != 'b')
133 ASSERT (!fwriting (fp));
135 ASSERT (!fwriting (fp));
136 if (fgetc (fp) != 'x')
138 ASSERT (!fwriting (fp));
139 /* This fseek call is necessary when switching from reading to writing.
140 See the description of fopen(), ISO C 99 7.19.5.3.(6). */
141 if (fseek (fp, 0, SEEK_CUR) != 0)
143 ASSERT (!fwriting (fp));
144 if (fputc ('z', fp) != 'z')
146 ASSERT (fwriting (fp));
147 if (fseek (fp, 0, SEEK_END))
149 /* freading (fp) is undefined here, because on some implementations (e.g.
150 glibc) fseek causes a buffer to be read.
151 fwriting (fp) is undefined as well. */
155 /* Open it in append mode. */
156 fp = fopen (TESTFILE, "a");
159 ASSERT (fwriting (fp));
160 if (fwrite ("bla", 1, 3, fp) < 3)
162 ASSERT (fwriting (fp));
169 fprintf (stderr, "Skipping test: file operations failed.\n");