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