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 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-fwriting.tmp"
45 /* Create a file with some contents. Write-only file is always writing. */
46 fp = fopen (TESTFILE, "w");
49 ASSERT (fwriting (fp));
50 if (fwrite ("foobarsh", 1, 8, fp) < 8)
52 ASSERT (fwriting (fp));
56 /* Open it in read-only mode. Read-only file is never writing. */
57 fp = fopen (TESTFILE, "r");
60 ASSERT (!fwriting (fp));
61 if (fgetc (fp) != 'f')
63 ASSERT (!fwriting (fp));
64 if (fseek (fp, 2, SEEK_CUR))
66 ASSERT (!fwriting (fp));
67 if (fgetc (fp) != 'b')
69 ASSERT (!fwriting (fp));
71 ASSERT (!fwriting (fp));
72 if (fgetc (fp) != 'a')
74 ASSERT (!fwriting (fp));
75 if (fseek (fp, 0, SEEK_END))
77 ASSERT (!fwriting (fp));
81 /* Open it in read-write mode. POSIX requires a reposition (fseek,
82 fsetpos, rewind) or fflush when transitioning from write to read,
83 fwriting 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 (!fwriting (fp));
91 if (fgetc (fp) != 'f')
93 ASSERT (!fwriting (fp));
94 if (fseek (fp, 2, SEEK_CUR))
96 ASSERT (!fwriting (fp));
97 if (fgetc (fp) != 'b')
99 ASSERT (!fwriting (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 ASSERT (!fwriting (fp));
105 if (fputc ('x', fp) != 'x')
107 ASSERT (fwriting (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 fflush when transitioning from write to read,
118 fwriting 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 (!fwriting (fp));
126 if (fgetc (fp) != 'f')
128 ASSERT (!fwriting (fp));
129 if (fseek (fp, 2, SEEK_CUR))
131 ASSERT (!fwriting (fp));
132 if (fgetc (fp) != 'b')
134 ASSERT (!fwriting (fp));
136 ASSERT (!fwriting (fp));
137 if (fgetc (fp) != 'x')
139 ASSERT (!fwriting (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 ASSERT (!fwriting (fp));
145 if (fputc ('z', fp) != 'z')
147 ASSERT (fwriting (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. */
157 fp = fopen (TESTFILE, "a");
160 ASSERT (fwriting (fp));
161 if (fwrite ("bla", 1, 3, fp) < 3)
163 ASSERT (fwriting (fp));
170 fprintf (stderr, "Skipping test: file operations failed.\n");