1 /* Test of freadable() 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. */
21 #include "freadable.h"
26 #define ASSERT(expr) \
31 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37 #define TESTFILE "t-freadable.tmp"
44 /* Create a file with some contents. */
45 fp = fopen (TESTFILE, "w");
48 ASSERT (!freadable (fp));
49 if (fwrite ("foobarsh", 1, 8, fp) < 8)
51 ASSERT (!freadable (fp));
55 /* Open it in read-only mode. */
56 fp = fopen (TESTFILE, "r");
59 ASSERT (freadable (fp));
60 if (fgetc (fp) != 'f')
62 ASSERT (freadable (fp));
63 if (fseek (fp, 2, SEEK_CUR))
65 ASSERT (freadable (fp));
66 if (fgetc (fp) != 'b')
68 ASSERT (freadable (fp));
70 ASSERT (freadable (fp));
71 if (fgetc (fp) != 'a')
73 ASSERT (freadable (fp));
74 if (fseek (fp, 0, SEEK_END))
76 ASSERT (freadable (fp));
80 /* Open it in read-write mode. */
81 fp = fopen (TESTFILE, "r+");
84 ASSERT (freadable (fp));
85 if (fgetc (fp) != 'f')
87 ASSERT (freadable (fp));
88 if (fseek (fp, 2, SEEK_CUR))
90 ASSERT (freadable (fp));
91 if (fgetc (fp) != 'b')
93 ASSERT (freadable (fp));
95 ASSERT (freadable (fp));
96 if (fgetc (fp) != 'a')
98 ASSERT (freadable (fp));
99 if (fputc ('z', fp) != 'z')
101 ASSERT (freadable (fp));
102 if (fseek (fp, 0, SEEK_END))
104 ASSERT (freadable (fp));
108 /* Open it in append mode. */
109 fp = fopen (TESTFILE, "a");
112 ASSERT (!freadable (fp));
113 if (fwrite ("bla", 1, 3, fp) < 3)
115 ASSERT (!freadable (fp));
122 fprintf (stderr, "Skipping test: file operations failed.\n");