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"
26 #define ASSERT(expr) if (!(expr)) abort ();
28 #define TESTFILE "t-freadable.tmp"
35 /* Create a file with some contents. */
36 fp = fopen (TESTFILE, "w");
39 ASSERT (!freadable (fp));
40 if (fwrite ("foobarsh", 1, 8, fp) < 8)
42 ASSERT (!freadable (fp));
46 /* Open it in read-only mode. */
47 fp = fopen (TESTFILE, "r");
50 ASSERT (freadable (fp));
51 if (fgetc (fp) != 'f')
53 ASSERT (freadable (fp));
54 if (fseek (fp, 2, SEEK_CUR))
56 ASSERT (freadable (fp));
57 if (fgetc (fp) != 'b')
59 ASSERT (freadable (fp));
61 ASSERT (freadable (fp));
62 if (fgetc (fp) != 'a')
64 ASSERT (freadable (fp));
65 if (fseek (fp, 0, SEEK_END))
67 ASSERT (freadable (fp));
71 /* Open it in read-write mode. */
72 fp = fopen (TESTFILE, "r+");
75 ASSERT (freadable (fp));
76 if (fgetc (fp) != 'f')
78 ASSERT (freadable (fp));
79 if (fseek (fp, 2, SEEK_CUR))
81 ASSERT (freadable (fp));
82 if (fgetc (fp) != 'b')
84 ASSERT (freadable (fp));
86 ASSERT (freadable (fp));
87 if (fgetc (fp) != 'a')
89 ASSERT (freadable (fp));
90 if (fputc ('z', fp) != 'z')
92 ASSERT (freadable (fp));
93 if (fseek (fp, 0, SEEK_END))
95 ASSERT (freadable (fp));
99 /* Open it in append mode. */
100 fp = fopen (TESTFILE, "a");
103 ASSERT (!freadable (fp));
104 if (fwrite ("bla", 1, 3, fp) < 3)
106 ASSERT (!freadable (fp));
113 fprintf (stderr, "Skipping test: file operations failed.\n");