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. */
26 #define ASSERT(expr) if (!(expr)) abort ();
28 #define TESTFILE "t-fwriting.tmp"
35 /* Create a file with some contents. Write-only file is always writing. */
36 fp = fopen (TESTFILE, "w");
39 ASSERT (fwriting (fp));
40 if (fwrite ("foobarsh", 1, 8, fp) < 8)
42 ASSERT (fwriting (fp));
46 /* Open it in read-only mode. Read-only file is never writing. */
47 fp = fopen (TESTFILE, "r");
50 ASSERT (!fwriting (fp));
51 if (fgetc (fp) != 'f')
53 ASSERT (!fwriting (fp));
54 if (fseek (fp, 2, SEEK_CUR))
56 ASSERT (!fwriting (fp));
57 if (fgetc (fp) != 'b')
59 ASSERT (!fwriting (fp));
60 if (fseek (fp, 0, SEEK_END))
62 ASSERT (!fwriting (fp));
66 /* Open it in read-write mode. POSIX requires a reposition (fseek,
67 fsetpos, rewind) or fflush when transitioning from write to read,
68 fwriting is only deterministic after input or output, but this
69 test case should be portable even on open, after reposition, and
71 fp = fopen (TESTFILE, "r+");
74 ASSERT (!fwriting (fp));
75 if (fgetc (fp) != 'f')
77 ASSERT (!fwriting (fp));
78 if (fseek (fp, 2, SEEK_CUR))
80 ASSERT (!fwriting (fp));
81 if (fgetc (fp) != 'b')
83 ASSERT (!fwriting (fp));
84 if (fseek (fp, 0, SEEK_CUR) != 0)
86 if (fputc ('z', fp) != 'z')
88 ASSERT (fwriting (fp));
89 if (fseek (fp, 0, SEEK_END))
91 /* fwriting (fp) is undefined here, but freading is false. */
95 /* Open it in append mode. */
96 fp = fopen (TESTFILE, "a");
99 ASSERT (fwriting (fp));
100 if (fwrite ("bla", 1, 3, fp) < 3)
102 ASSERT (fwriting (fp));
109 fprintf (stderr, "Skipping test: file operations failed.\n");