1 /* Test of fpurge() function.
2 Copyright (C) 2007-2010 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. */
21 /* None of the files accessed by this test are large, so disable the
22 fseek link warning if we are not using the gnulib fseek module. */
23 #define _GL_NO_LARGE_FILES
30 #define TESTFILE "t-fpurge.tmp"
37 for (check_filepos = 0; check_filepos <= 1; check_filepos++)
41 /* Create a file with some contents. */
42 fp = fopen (TESTFILE, "w");
45 if (fwrite ("foobarsh", 1, 8, fp) < 8)
50 /* The file's contents is now "foobarsh". */
52 /* Open it in read-write mode. */
53 fp = fopen (TESTFILE, "r+");
56 if (fseek (fp, 3, SEEK_CUR))
58 if (fwrite ("g", 1, 1, fp) < 1)
62 if (fwrite ("bz", 1, 2, fp) < 2)
64 /* Discard pending write. */
65 ASSERT (fpurge (fp) == 0);
66 /* Verify that when discarding pending output, the file position is set
67 back to where it was before the write calls. */
69 ASSERT (ftell (fp) == 4);
70 ASSERT (fclose (fp) == 0);
72 /* Open it in read-only mode. */
73 fp = fopen (TESTFILE, "r");
76 /* Verify that the pending writes before the fpurge were really
80 if (fread (buf, 1, 7, fp) < 7)
82 ASSERT (memcmp (buf, "foogars", 7) == 0);
84 /* Discard the buffered 'h'. */
86 ASSERT (ftell (fp) == 7);
87 ASSERT (fpurge (fp) == 0);
88 /* Verify that when discarding pending input, the file position is
89 advanced to match the end of the previously read input. */
91 ASSERT (ftell (fp) == 8);
92 ASSERT (getc (fp) == EOF);
93 ASSERT (fclose (fp) == 0);
95 /* The file's contents is now "foogarsh". */
97 /* Ensure that purging a read does not corrupt subsequent writes. */
98 fp = fopen (TESTFILE, "r+");
101 if (fseek (fp, -1, SEEK_END))
103 ASSERT (getc (fp) == 'h');
104 ASSERT (getc (fp) == EOF);
106 ASSERT (ftell (fp) == 8);
107 ASSERT (fpurge (fp) == 0);
109 ASSERT (ftell (fp) == 8);
110 ASSERT (putc ('!', fp) == '!');
111 ASSERT (fclose (fp) == 0);
112 fp = fopen (TESTFILE, "r");
117 ASSERT (fread (buf, 1, 10, fp) == 9);
118 ASSERT (memcmp (buf, "foogarsh!", 9) == 0);
120 ASSERT (fclose (fp) == 0);
122 /* The file's contents is now "foogarsh!". */
129 fprintf (stderr, "Skipping test: prerequisite file operations failed.\n");