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. */
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. */
33 #define TESTFILE "t-fpurge.tmp"
40 for (check_filepos = 0; check_filepos <= 1; check_filepos++)
44 /* Create a file with some contents. */
45 fp = fopen (TESTFILE, "w");
48 if (fwrite ("foobarsh", 1, 8, fp) < 8)
53 /* The file's contents is now "foobarsh". */
55 /* Open it in read-write mode. */
56 fp = fopen (TESTFILE, "r+");
59 if (fseek (fp, 3, SEEK_CUR))
61 if (fwrite ("g", 1, 1, fp) < 1)
65 if (fwrite ("bz", 1, 2, fp) < 2)
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. */
72 ASSERT (ftell (fp) == 4);
73 ASSERT (fclose (fp) == 0);
75 /* Open it in read-only mode. */
76 fp = fopen (TESTFILE, "r");
79 /* Verify that the pending writes before the fpurge were really
83 if (fread (buf, 1, 7, fp) < 7)
85 ASSERT (memcmp (buf, "foogars", 7) == 0);
87 /* Discard the buffered 'h'. */
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. */
94 ASSERT (ftell (fp) == 8);
95 ASSERT (getc (fp) == EOF);
96 ASSERT (fclose (fp) == 0);
98 /* The file's contents is now "foogarsh". */
100 /* Ensure that purging a read does not corrupt subsequent writes. */
101 fp = fopen (TESTFILE, "r+");
104 if (fseek (fp, -1, SEEK_END))
106 ASSERT (getc (fp) == 'h');
107 ASSERT (getc (fp) == EOF);
109 ASSERT (ftell (fp) == 8);
110 ASSERT (fpurge (fp) == 0);
112 ASSERT (ftell (fp) == 8);
113 ASSERT (putc ('!', fp) == '!');
114 ASSERT (fclose (fp) == 0);
115 fp = fopen (TESTFILE, "r");
120 ASSERT (fread (buf, 1, 10, fp) == 9);
121 ASSERT (memcmp (buf, "foogarsh!", 9) == 0);
123 ASSERT (fclose (fp) == 0);
125 /* The file's contents is now "foogarsh!". */
132 fprintf (stderr, "Skipping test: prerequisite file operations failed.\n");