Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-fpurge.c
1 /* Test of fpurge() 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 <stdio.h>
22
23 #include <stdlib.h>
24 #include <string.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-fpurge.tmp"
45
46 int
47 main (void)
48 {
49   int check_filepos;
50
51   for (check_filepos = 0; check_filepos <= 1; check_filepos++)
52     {
53       FILE *fp;
54
55       /* Create a file with some contents.  */
56       fp = fopen (TESTFILE, "w");
57       if (fp == NULL)
58         goto skip;
59       if (fwrite ("foobarsh", 1, 8, fp) < 8)
60         goto skip;
61       if (fclose (fp))
62         goto skip;
63
64       /* The file's contents is now "foobarsh".  */
65
66       /* Open it in read-write mode.  */
67       fp = fopen (TESTFILE, "r+");
68       if (fp == NULL)
69         goto skip;
70       if (fseek (fp, 3, SEEK_CUR))
71         goto skip;
72       if (fwrite ("g", 1, 1, fp) < 1)
73         goto skip;
74       if (fflush (fp))
75         goto skip;
76       if (fwrite ("bz", 1, 2, fp) < 2)
77         goto skip;
78       /* Discard pending write.  */
79       ASSERT (fpurge (fp) == 0);
80       /* Verify that when discarding pending output, the file position is set
81          back to where it was before the write calls.  */
82       if (check_filepos)
83         ASSERT (ftell (fp) == 4);
84       ASSERT (fclose (fp) == 0);
85
86       /* Open it in read-only mode.  */
87       fp = fopen (TESTFILE, "r");
88       if (fp == NULL)
89         goto skip;
90       /* Verify that the pending writes before the fpurge were really
91          discarded.  */
92       {
93         char buf[8];
94         if (fread (buf, 1, 7, fp) < 7)
95           goto skip;
96         ASSERT (memcmp (buf, "foogars", 7) == 0);
97       }
98       /* Discard the buffered 'h'.  */
99       if (check_filepos)
100         ASSERT (ftell (fp) == 7);
101       ASSERT (fpurge (fp) == 0);
102       /* Verify that when discarding pending input, the file position is
103          advanced to match the end of the previously read input.  */
104       if (check_filepos)
105         ASSERT (ftell (fp) == 8);
106       ASSERT (getc (fp) == EOF);
107       ASSERT (fclose (fp) == 0);
108
109       /* The file's contents is now "foogarsh".  */
110
111       /* Ensure that purging a read does not corrupt subsequent writes.  */
112       fp = fopen (TESTFILE, "r+");
113       if (fp == NULL)
114         goto skip;
115       if (fseek (fp, -1, SEEK_END))
116         goto skip;
117       ASSERT (getc (fp) == 'h');
118       ASSERT (getc (fp) == EOF);
119       if (check_filepos)
120         ASSERT (ftell (fp) == 8);
121       ASSERT (fpurge (fp) == 0);
122       if (check_filepos)
123         ASSERT (ftell (fp) == 8);
124       ASSERT (putc ('!', fp) == '!');
125       ASSERT (fclose (fp) == 0);
126       fp = fopen (TESTFILE, "r");
127       if (fp == NULL)
128         goto skip;
129       {
130         char buf[10];
131         ASSERT (fread (buf, 1, 10, fp) == 9);
132         ASSERT (memcmp (buf, "foogarsh!", 9) == 0);
133       }
134       ASSERT (fclose (fp) == 0);
135
136       /* The file's contents is now "foogarsh!".  */
137     }
138
139   remove (TESTFILE);
140   return 0;
141
142  skip:
143   fprintf (stderr, "Skipping test: prerequisite file operations failed.\n");
144   remove (TESTFILE);
145   return 77;
146 }