Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / tests / test-freadable.c
1 /* Test of freadable() function.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include "freadable.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #define ASSERT(expr) \
27   do                                                                         \
28     {                                                                        \
29       if (!(expr))                                                           \
30         {                                                                    \
31           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32           abort ();                                                          \
33         }                                                                    \
34     }                                                                        \
35   while (0)
36
37 #define TESTFILE "t-freadable.tmp"
38
39 int
40 main ()
41 {
42   FILE *fp;
43
44   /* Create a file with some contents.  */
45   fp = fopen (TESTFILE, "w");
46   if (fp == NULL)
47     goto skip;
48   ASSERT (!freadable (fp));
49   if (fwrite ("foobarsh", 1, 8, fp) < 8)
50     goto skip;
51   ASSERT (!freadable (fp));
52   if (fclose (fp))
53     goto skip;
54
55   /* Open it in read-only mode.  */
56   fp = fopen (TESTFILE, "r");
57   if (fp == NULL)
58     goto skip;
59   ASSERT (freadable (fp));
60   if (fgetc (fp) != 'f')
61     goto skip;
62   ASSERT (freadable (fp));
63   if (fseek (fp, 2, SEEK_CUR))
64     goto skip;
65   ASSERT (freadable (fp));
66   if (fgetc (fp) != 'b')
67     goto skip;
68   ASSERT (freadable (fp));
69   fflush (fp);
70   ASSERT (freadable (fp));
71   if (fgetc (fp) != 'a')
72     goto skip;
73   ASSERT (freadable (fp));
74   if (fseek (fp, 0, SEEK_END))
75     goto skip;
76   ASSERT (freadable (fp));
77   if (fclose (fp))
78     goto skip;
79
80   /* Open it in read-write mode.  */
81   fp = fopen (TESTFILE, "r+");
82   if (fp == NULL)
83     goto skip;
84   ASSERT (freadable (fp));
85   if (fgetc (fp) != 'f')
86     goto skip;
87   ASSERT (freadable (fp));
88   if (fseek (fp, 2, SEEK_CUR))
89     goto skip;
90   ASSERT (freadable (fp));
91   if (fgetc (fp) != 'b')
92     goto skip;
93   ASSERT (freadable (fp));
94   fflush (fp);
95   ASSERT (freadable (fp));
96   if (fgetc (fp) != 'a')
97     goto skip;
98   ASSERT (freadable (fp));
99   if (fputc ('z', fp) != 'z')
100     goto skip;
101   ASSERT (freadable (fp));
102   if (fseek (fp, 0, SEEK_END))
103     goto skip;
104   ASSERT (freadable (fp));
105   if (fclose (fp))
106     goto skip;
107
108   /* Open it in append mode.  */
109   fp = fopen (TESTFILE, "a");
110   if (fp == NULL)
111     goto skip;
112   ASSERT (!freadable (fp));
113   if (fwrite ("bla", 1, 3, fp) < 3)
114     goto skip;
115   ASSERT (!freadable (fp));
116   if (fclose (fp))
117     goto skip;
118
119   return 0;
120
121  skip:
122   fprintf (stderr, "Skipping test: file operations failed.\n");
123   return 77;
124 }