Refactor common macros used in tests.
[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 <string.h>
24
25 #include "macros.h"
26
27 /* None of the files accessed by this test are large, so disable the
28    fseek link warning if we are not using the gnulib fseek module.  */
29 #if !GNULIB_FSEEK
30 # undef fseek
31 #endif
32
33 #define TESTFILE "t-fpurge.tmp"
34
35 int
36 main (void)
37 {
38   int check_filepos;
39
40   for (check_filepos = 0; check_filepos <= 1; check_filepos++)
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       if (fwrite ("foobarsh", 1, 8, fp) < 8)
49         goto skip;
50       if (fclose (fp))
51         goto skip;
52
53       /* The file's contents is now "foobarsh".  */
54
55       /* Open it in read-write mode.  */
56       fp = fopen (TESTFILE, "r+");
57       if (fp == NULL)
58         goto skip;
59       if (fseek (fp, 3, SEEK_CUR))
60         goto skip;
61       if (fwrite ("g", 1, 1, fp) < 1)
62         goto skip;
63       if (fflush (fp))
64         goto skip;
65       if (fwrite ("bz", 1, 2, fp) < 2)
66         goto skip;
67       /* Discard pending write.  */
68       ASSERT (fpurge (fp) == 0);
69       /* Verify that when discarding pending output, the file position is set
70          back to where it was before the write calls.  */
71       if (check_filepos)
72         ASSERT (ftell (fp) == 4);
73       ASSERT (fclose (fp) == 0);
74
75       /* Open it in read-only mode.  */
76       fp = fopen (TESTFILE, "r");
77       if (fp == NULL)
78         goto skip;
79       /* Verify that the pending writes before the fpurge were really
80          discarded.  */
81       {
82         char buf[8];
83         if (fread (buf, 1, 7, fp) < 7)
84           goto skip;
85         ASSERT (memcmp (buf, "foogars", 7) == 0);
86       }
87       /* Discard the buffered 'h'.  */
88       if (check_filepos)
89         ASSERT (ftell (fp) == 7);
90       ASSERT (fpurge (fp) == 0);
91       /* Verify that when discarding pending input, the file position is
92          advanced to match the end of the previously read input.  */
93       if (check_filepos)
94         ASSERT (ftell (fp) == 8);
95       ASSERT (getc (fp) == EOF);
96       ASSERT (fclose (fp) == 0);
97
98       /* The file's contents is now "foogarsh".  */
99
100       /* Ensure that purging a read does not corrupt subsequent writes.  */
101       fp = fopen (TESTFILE, "r+");
102       if (fp == NULL)
103         goto skip;
104       if (fseek (fp, -1, SEEK_END))
105         goto skip;
106       ASSERT (getc (fp) == 'h');
107       ASSERT (getc (fp) == EOF);
108       if (check_filepos)
109         ASSERT (ftell (fp) == 8);
110       ASSERT (fpurge (fp) == 0);
111       if (check_filepos)
112         ASSERT (ftell (fp) == 8);
113       ASSERT (putc ('!', fp) == '!');
114       ASSERT (fclose (fp) == 0);
115       fp = fopen (TESTFILE, "r");
116       if (fp == NULL)
117         goto skip;
118       {
119         char buf[10];
120         ASSERT (fread (buf, 1, 10, fp) == 9);
121         ASSERT (memcmp (buf, "foogarsh!", 9) == 0);
122       }
123       ASSERT (fclose (fp) == 0);
124
125       /* The file's contents is now "foogarsh!".  */
126     }
127
128   remove (TESTFILE);
129   return 0;
130
131  skip:
132   fprintf (stderr, "Skipping test: prerequisite file operations failed.\n");
133   remove (TESTFILE);
134   return 77;
135 }