1 /* Test of fpurge() function.
2 Copyright (C) 2007-2009 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
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. */
32 #define ASSERT(expr) \
37 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
44 #define TESTFILE "t-fpurge.tmp"
51 for (check_filepos = 0; check_filepos <= 1; check_filepos++)
55 /* Create a file with some contents. */
56 fp = fopen (TESTFILE, "w");
59 if (fwrite ("foobarsh", 1, 8, fp) < 8)
64 /* The file's contents is now "foobarsh". */
66 /* Open it in read-write mode. */
67 fp = fopen (TESTFILE, "r+");
70 if (fseek (fp, 3, SEEK_CUR))
72 if (fwrite ("g", 1, 1, fp) < 1)
76 if (fwrite ("bz", 1, 2, fp) < 2)
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. */
83 ASSERT (ftell (fp) == 4);
84 ASSERT (fclose (fp) == 0);
86 /* Open it in read-only mode. */
87 fp = fopen (TESTFILE, "r");
90 /* Verify that the pending writes before the fpurge were really
94 if (fread (buf, 1, 7, fp) < 7)
96 ASSERT (memcmp (buf, "foogars", 7) == 0);
98 /* Discard the buffered 'h'. */
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. */
105 ASSERT (ftell (fp) == 8);
106 ASSERT (getc (fp) == EOF);
107 ASSERT (fclose (fp) == 0);
109 /* The file's contents is now "foogarsh". */
111 /* Ensure that purging a read does not corrupt subsequent writes. */
112 fp = fopen (TESTFILE, "r+");
115 if (fseek (fp, -1, SEEK_END))
117 ASSERT (getc (fp) == 'h');
118 ASSERT (getc (fp) == EOF);
120 ASSERT (ftell (fp) == 8);
121 ASSERT (fpurge (fp) == 0);
123 ASSERT (ftell (fp) == 8);
124 ASSERT (putc ('!', fp) == '!');
125 ASSERT (fclose (fp) == 0);
126 fp = fopen (TESTFILE, "r");
131 ASSERT (fread (buf, 1, 10, fp) == 9);
132 ASSERT (memcmp (buf, "foogarsh!", 9) == 0);
134 ASSERT (fclose (fp) == 0);
136 /* The file's contents is now "foogarsh!". */
143 fprintf (stderr, "Skipping test: prerequisite file operations failed.\n");