1 /* Test of freading() 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. */
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-freading.tmp"
40 /* Create a file with some contents. Write-only file is never reading. */
41 fp = fopen (TESTFILE, "w");
43 ASSERT (!freading (fp));
44 ASSERT (fwrite ("foobarsh", 1, 8, fp) == 8);
45 ASSERT (!freading (fp));
46 ASSERT (fclose (fp) == 0);
48 /* Open it in read-only mode. Read-only file is always reading. */
49 fp = fopen (TESTFILE, "r");
51 ASSERT (freading (fp));
52 ASSERT (fgetc (fp) == 'f');
53 ASSERT (freading (fp));
54 ASSERT (fseek (fp, 2, SEEK_CUR) == 0);
55 ASSERT (freading (fp));
56 ASSERT (fgetc (fp) == 'b');
57 ASSERT (freading (fp));
59 ASSERT (freading (fp));
60 ASSERT (fgetc (fp) == 'a');
61 ASSERT (freading (fp));
62 ASSERT (fseek (fp, 0, SEEK_END) == 0);
63 ASSERT (freading (fp));
64 ASSERT (fclose (fp) == 0);
66 /* Open it in read-write mode. POSIX requires a reposition (fseek,
67 fsetpos, rewind) or EOF when transitioning from read to write;
68 freading is only deterministic after input or output, but this
69 test case should be portable even on open, after reposition, and
71 /* First a scenario with only fgetc, fseek, fputc. */
72 fp = fopen (TESTFILE, "r+");
74 ASSERT (!freading (fp));
75 ASSERT (fgetc (fp) == 'f');
76 ASSERT (freading (fp));
77 ASSERT (fseek (fp, 2, SEEK_CUR) == 0);
78 /* freading (fp) is undefined here, but fwriting (fp) is false. */
79 ASSERT (fgetc (fp) == 'b');
80 ASSERT (freading (fp));
81 /* This fseek call is necessary when switching from reading to writing.
82 See the description of fopen(), ISO C 99 7.19.5.3.(6). */
83 ASSERT (fseek (fp, 0, SEEK_CUR) == 0);
84 /* freading (fp) is undefined here, but fwriting (fp) is false. */
85 ASSERT (fputc ('x', fp) == 'x');
86 ASSERT (!freading (fp));
87 ASSERT (fseek (fp, 0, SEEK_END) == 0);
88 /* freading (fp) is undefined here, because on some implementations (e.g.
89 glibc) fseek causes a buffer to be read.
90 fwriting (fp) is undefined as well. */
91 ASSERT (fclose (fp) == 0);
93 /* Open it in read-write mode. POSIX requires a reposition (fseek,
94 fsetpos, rewind) or EOF when transitioning from read to write;
95 freading is only deterministic after input or output, but this
96 test case should be portable even on open, after reposition, and
98 /* Here a scenario that includes fflush. */
99 fp = fopen (TESTFILE, "r+");
101 ASSERT (!freading (fp));
102 ASSERT (fgetc (fp) == 'f');
103 ASSERT (freading (fp));
104 ASSERT (fseek (fp, 2, SEEK_CUR) == 0);
105 /* freading (fp) is undefined here, but fwriting (fp) is false. */
106 ASSERT (fgetc (fp) == 'b');
107 ASSERT (freading (fp));
109 /* freading (fp) is undefined here, but fwriting (fp) is false. */
110 ASSERT (fgetc (fp) == 'x');
111 ASSERT (freading (fp));
112 /* This fseek call is necessary when switching from reading to writing.
113 See the description of fopen(), ISO C 99 7.19.5.3.(6). */
114 ASSERT (fseek (fp, 0, SEEK_CUR) == 0);
115 /* freading (fp) is undefined here, but fwriting (fp) is false. */
116 ASSERT (fputc ('z', fp) == 'z');
117 ASSERT (!freading (fp));
118 ASSERT (fseek (fp, 0, SEEK_END) == 0);
119 /* freading (fp) is undefined here, because on some implementations (e.g.
120 glibc) fseek causes a buffer to be read.
121 fwriting (fp) is undefined as well. */
122 ASSERT (fclose (fp) == 0);
124 /* Open it in append mode. Write-only file is never reading. */
125 fp = fopen (TESTFILE, "a");
127 ASSERT (!freading (fp));
128 ASSERT (fwrite ("bla", 1, 3, fp) == 3);
129 ASSERT (!freading (fp));
130 ASSERT (fclose (fp) == 0);
131 ASSERT (remove (TESTFILE) == 0);