1 /* Test of fwritable() function.
2 Copyright (C) 2007-2008 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. */
21 #include "fwritable.h"
26 #define ASSERT(expr) \
31 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38 #define TESTFILE "t-fwritable.tmp"
45 /* Create a file with some contents. */
46 fp = fopen (TESTFILE, "w");
49 ASSERT (fwritable (fp));
50 if (fwrite ("foobarsh", 1, 8, fp) < 8)
52 ASSERT (fwritable (fp));
56 /* Open it in read-only mode. */
57 fp = fopen (TESTFILE, "r");
60 ASSERT (!fwritable (fp));
61 if (fgetc (fp) != 'f')
63 ASSERT (!fwritable (fp));
64 if (fseek (fp, 2, SEEK_CUR))
66 ASSERT (!fwritable (fp));
67 if (fgetc (fp) != 'b')
69 ASSERT (!fwritable (fp));
71 ASSERT (!fwritable (fp));
72 if (fgetc (fp) != 'a')
74 ASSERT (!fwritable (fp));
75 if (fseek (fp, 0, SEEK_END))
77 ASSERT (!fwritable (fp));
81 /* Open it in read-write mode. */
82 fp = fopen (TESTFILE, "r+");
85 ASSERT (fwritable (fp));
86 if (fgetc (fp) != 'f')
88 ASSERT (fwritable (fp));
89 if (fseek (fp, 2, SEEK_CUR))
91 ASSERT (fwritable (fp));
92 if (fgetc (fp) != 'b')
94 ASSERT (fwritable (fp));
96 ASSERT (fwritable (fp));
97 if (fgetc (fp) != 'a')
99 ASSERT (fwritable (fp));
100 if (fputc ('z', fp) != 'z')
102 ASSERT (fwritable (fp));
103 if (fseek (fp, 0, SEEK_END))
105 ASSERT (fwritable (fp));
109 /* Open it in append mode. */
110 fp = fopen (TESTFILE, "a");
113 ASSERT (fwritable (fp));
114 if (fwrite ("bla", 1, 3, fp) < 3)
116 ASSERT (fwritable (fp));
123 fprintf (stderr, "Skipping test: file operations failed.\n");