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