Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-fwritable.c
1 /* Test of fwritable() function.
2    Copyright (C) 2007-2009 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 "fwritable.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 /* None of the files accessed by this test are large, so disable the
27    fseek link warning if we are not using the gnulib fseek module.  */
28 #if !GNULIB_FSEEK
29 # undef fseek
30 #endif
31
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           fflush (stderr);                                                   \
39           abort ();                                                          \
40         }                                                                    \
41     }                                                                        \
42   while (0)
43
44 #define TESTFILE "t-fwritable.tmp"
45
46 int
47 main ()
48 {
49   FILE *fp;
50
51   /* Create a file with some contents.  */
52   fp = fopen (TESTFILE, "w");
53   if (fp == NULL)
54     goto skip;
55   ASSERT (fwritable (fp));
56   if (fwrite ("foobarsh", 1, 8, fp) < 8)
57     goto skip;
58   ASSERT (fwritable (fp));
59   if (fclose (fp))
60     goto skip;
61
62   /* Open it in read-only mode.  */
63   fp = fopen (TESTFILE, "r");
64   if (fp == NULL)
65     goto skip;
66   ASSERT (!fwritable (fp));
67   if (fgetc (fp) != 'f')
68     goto skip;
69   ASSERT (!fwritable (fp));
70   if (fseek (fp, 2, SEEK_CUR))
71     goto skip;
72   ASSERT (!fwritable (fp));
73   if (fgetc (fp) != 'b')
74     goto skip;
75   ASSERT (!fwritable (fp));
76   fflush (fp);
77   ASSERT (!fwritable (fp));
78   if (fgetc (fp) != 'a')
79     goto skip;
80   ASSERT (!fwritable (fp));
81   if (fseek (fp, 0, SEEK_END))
82     goto skip;
83   ASSERT (!fwritable (fp));
84   if (fclose (fp))
85     goto skip;
86
87   /* Open it in read-write mode.  */
88   fp = fopen (TESTFILE, "r+");
89   if (fp == NULL)
90     goto skip;
91   ASSERT (fwritable (fp));
92   if (fgetc (fp) != 'f')
93     goto skip;
94   ASSERT (fwritable (fp));
95   if (fseek (fp, 2, SEEK_CUR))
96     goto skip;
97   ASSERT (fwritable (fp));
98   if (fgetc (fp) != 'b')
99     goto skip;
100   ASSERT (fwritable (fp));
101   fflush (fp);
102   ASSERT (fwritable (fp));
103   if (fgetc (fp) != 'a')
104     goto skip;
105   ASSERT (fwritable (fp));
106   if (fputc ('z', fp) != 'z')
107     goto skip;
108   ASSERT (fwritable (fp));
109   if (fseek (fp, 0, SEEK_END))
110     goto skip;
111   ASSERT (fwritable (fp));
112   if (fclose (fp))
113     goto skip;
114
115   /* Open it in append mode.  */
116   fp = fopen (TESTFILE, "a");
117   if (fp == NULL)
118     goto skip;
119   ASSERT (fwritable (fp));
120   if (fwrite ("bla", 1, 3, fp) < 3)
121     goto skip;
122   ASSERT (fwritable (fp));
123   if (fclose (fp))
124     goto skip;
125
126   return 0;
127
128  skip:
129   fprintf (stderr, "Skipping test: file operations failed.\n");
130   return 77;
131 }