1 /* Test of freading() function.
2 Copyright (C) 2007 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
26 #define ASSERT(expr) if (!(expr)) abort ();
28 #define TESTFILE "t-freading.tmp"
35 /* Create a file with some contents. Write-only file is never reading. */
36 fp = fopen (TESTFILE, "w");
39 ASSERT (!freading (fp));
40 if (fwrite ("foobarsh", 1, 8, fp) < 8)
42 ASSERT (!freading (fp));
46 /* Open it in read-only mode. Read-only file is always reading. */
47 fp = fopen (TESTFILE, "r");
50 ASSERT (freading (fp));
51 if (fgetc (fp) != 'f')
53 ASSERT (freading (fp));
54 if (fseek (fp, 2, SEEK_CUR))
56 ASSERT (freading (fp));
57 if (fgetc (fp) != 'b')
59 ASSERT (freading (fp));
61 ASSERT (freading (fp));
62 if (fgetc (fp) != 'a')
64 ASSERT (freading (fp));
65 if (fseek (fp, 0, SEEK_END))
67 ASSERT (freading (fp));
71 /* Open it in read-write mode. POSIX requires a reposition (fseek,
72 fsetpos, rewind) or EOF when transitioning from read to write;
73 freading is only deterministic after input or output, but this
74 test case should be portable even on open, after reposition, and
76 fp = fopen (TESTFILE, "r+");
79 ASSERT (!freading (fp));
80 if (fgetc (fp) != 'f')
82 ASSERT (freading (fp));
83 if (fseek (fp, 2, SEEK_CUR))
85 /* freading (fp) is undefined here, but fwriting (fp) is false. */
86 if (fgetc (fp) != 'b')
88 ASSERT (freading (fp));
89 /* This fseek call is necessary when switching from reading to writing.
90 See the description of fopen(), ISO C 99 7.19.5.3.(6). */
91 if (fseek (fp, 0, SEEK_CUR) != 0)
93 /* freading (fp) is undefined here, but fwriting (fp) is false. */
94 if (fputc ('z', fp) != 'z')
96 ASSERT (!freading (fp));
97 if (fseek (fp, 0, SEEK_END))
99 ASSERT (!freading (fp));
103 /* Open it in read-write mode. POSIX requires a reposition (fseek,
104 fsetpos, rewind) or EOF when transitioning from read to write;
105 freading is only deterministic after input or output, but this
106 test case should be portable even on open, after reposition, and
108 fp = fopen (TESTFILE, "r+");
111 ASSERT (!freading (fp));
112 if (fgetc (fp) != 'f')
114 ASSERT (freading (fp));
115 if (fseek (fp, 2, SEEK_CUR))
117 /* freading (fp) is undefined here, but fwriting (fp) is false. */
118 if (fgetc (fp) != 'b')
120 ASSERT (freading (fp));
122 /* freading (fp) is undefined here, but fwriting (fp) is false. */
123 if (fgetc (fp) != 'a')
125 ASSERT (freading (fp));
126 /* This fseek call is necessary when switching from reading to writing.
127 See the description of fopen(), ISO C 99 7.19.5.3.(6). */
128 if (fseek (fp, 0, SEEK_CUR) != 0)
130 /* freading (fp) is undefined here, but fwriting (fp) is false. */
131 if (fputc ('z', fp) != 'z')
133 ASSERT (!freading (fp));
134 if (fseek (fp, 0, SEEK_END))
136 ASSERT (!freading (fp));
140 /* Open it in append mode. Write-only file is never reading. */
141 fp = fopen (TESTFILE, "a");
144 ASSERT (!freading (fp));
145 if (fwrite ("bla", 1, 3, fp) < 3)
147 ASSERT (!freading (fp));
154 fprintf (stderr, "Skipping test: file operations failed.\n");