summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/stdio/test_fflush.c
diff options
context:
space:
mode:
authoryasuoka <>2025-05-24 11:07:21 +0000
committeryasuoka <>2025-05-24 11:07:21 +0000
commit1874b023f58612fb7b0d26839af9561babb2e1d1 (patch)
tree86f35cc2a6761c8d1bca8159fc60d45221a20a7e /src/regress/lib/libc/stdio/test_fflush.c
parent27bf6f27cb6450c0c6da4e75b4fc00aa0cc44713 (diff)
downloadopenbsd-1874b023f58612fb7b0d26839af9561babb2e1d1.tar.gz
openbsd-1874b023f58612fb7b0d26839af9561babb2e1d1.tar.bz2
openbsd-1874b023f58612fb7b0d26839af9561babb2e1d1.zip
Add regress/lib/libc/stdio/test_fflush.c to test fflush() behavior for
reading FILE objects. It will fail until fflush() complies POSIX-2008. ok tb asou
Diffstat (limited to 'src/regress/lib/libc/stdio/test_fflush.c')
-rw-r--r--src/regress/lib/libc/stdio/test_fflush.c225
1 files changed, 225 insertions, 0 deletions
diff --git a/src/regress/lib/libc/stdio/test_fflush.c b/src/regress/lib/libc/stdio/test_fflush.c
new file mode 100644
index 0000000000..fe1772bdfc
--- /dev/null
+++ b/src/regress/lib/libc/stdio/test_fflush.c
@@ -0,0 +1,225 @@
1/* $OpenBSD: test_fflush.c,v 1.1 2025/05/24 11:07:21 yasuoka Exp $ */
2
3/*
4 * Copyright (c) 2025 YASUOKA Masahiko <yasuoka@yasuoka.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <assert.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25/* we use assert() */
26#undef NDEBUG
27
28#define TMPFILENAME "test_fflush.tmp"
29
30void setup(void);
31
32void test_fflush_read0(void);
33void test_fflush_read1(void);
34void test_fflush_read2(void);
35void test_fflush_read3(void);
36void test_fflush_read4(void);
37
38void
39setup(void)
40{
41 FILE *fp;
42
43 /* common setup */
44 unlink(TMPFILENAME);
45 fp = fopen(TMPFILENAME, "w+");
46 assert(fp != NULL);
47 fputs("Hello world\n", fp);
48 fclose(fp);
49}
50
51/* fflush work with reading file and seekable */
52void
53test_fflush_read0(void)
54{
55 int r;
56 char buf[80];
57 FILE *fp;
58
59 setup();
60
61 /* In POSIX 2008, fflush() must work with the file object for reading */
62 fp = fopen(TMPFILENAME, "r");
63 assert(fp != NULL);
64 assert(fgetc(fp) == 'H');
65 r = fflush(fp);
66 assert(r == 0);
67
68 /* the position is moved to 1 */
69 assert(ftell(fp) == 1);
70
71 /* can read rest of that */
72 fgets(buf, sizeof(buf), fp);
73 assert(strcmp(buf, "ello world\n") == 0);
74 r = fclose(fp);
75 assert(r == 0);
76}
77
78/* fflush work with reading file and seekable + unget */
79void
80test_fflush_read1(void)
81{
82 int r;
83 char buf[80];
84 FILE *fp;
85
86 setup();
87
88 fp = fopen(TMPFILENAME, "r");
89 assert(fp != NULL);
90 assert(fgetc(fp) == 'H');
91 assert(fgetc(fp) == 'e');
92 assert(fgetc(fp) == 'l');
93 assert(fgetc(fp) == 'l');
94 assert(fgetc(fp) == 'o');
95
96 /* push the 'AAAA' back */
97 ungetc('A', fp);
98 ungetc('A', fp);
99 ungetc('A', fp);
100 ungetc('A', fp);
101
102 /* can read rest of that */
103 fgets(buf, sizeof(buf), fp);
104 assert(strcmp(buf, "AAAA world\n") == 0);
105 r = fclose(fp);
106 assert(r == 0);
107
108 /* do the same thing + fflush */
109
110 fp = fopen(TMPFILENAME, "r");
111 assert(fp != NULL);
112 assert(fgetc(fp) == 'H');
113 assert(fgetc(fp) == 'e');
114 assert(fgetc(fp) == 'l');
115 assert(fgetc(fp) == 'l');
116 assert(fgetc(fp) == 'o');
117
118 /* push 'AAAA' back */
119 ungetc('A', fp);
120 ungetc('A', fp);
121 ungetc('A', fp);
122 ungetc('A', fp);
123
124 /* then fflush */
125 r = fflush(fp);
126 assert(r == 0);
127
128 /* fllush() clears the all pushed back chars */
129
130 /* can read rest of that */
131 fgets(buf, sizeof(buf), fp);
132 assert(strcmp(buf, " world\n") == 0);
133 r = fclose(fp);
134 assert(r == 0);
135}
136
137/* fflush() to reading and non-seekable stream */
138void
139test_fflush_read2(void)
140{
141 int r;
142 FILE *fp;
143 char buf[80];
144
145 /* In POSIX-2008, fflush() must work with the file object for reading */
146 fp = popen("echo Hello world", "r");
147 assert(fp != NULL);
148 assert(fgetc(fp) == 'H');
149 r = fflush(fp);
150 assert(r == 0);
151
152 /*
153 * FILE object for read and NOT seekable. In that case, fflush does
154 * nothing, but must keep the buffer.
155 */
156
157 /* can read rest of that */
158 fgets(buf, sizeof(buf), fp);
159 assert(strcmp(buf, "ello world\n") == 0);
160 r = pclose(fp);
161 assert(r == 0);
162}
163
164/* fflush() to the file which doesn't have any buffer */
165void
166test_fflush_read3(void)
167{
168 int r;
169 FILE *fp;
170
171 setup();
172
173 /* In POSIX-2008, fflush() must work with the file object for reading */
174 fp = fopen(TMPFILENAME, "r");
175 assert(fp != NULL);
176 r = fflush(fp);
177 assert(r == 0);
178 r = fclose(fp);
179 assert(r == 0);
180}
181
182/* freopen() should call fflush() internal */
183void
184test_fflush_read4(void)
185{
186 int r;
187 FILE *fp;
188 off_t pos;
189 char buf[80];
190
191 setup();
192
193 /* In POSIX-2008, fflush() must work with the file object for reading */
194 fp = fopen(TMPFILENAME, "r");
195 assert(fp != NULL);
196
197 assert(fgetc(fp) == 'H'); /* read 1 */
198
199 pos = lseek(fileno(fp), 0, SEEK_CUR);
200 assert(pos >= 1);
201 assert(pos > 1); /* this test assume the buffer is used */
202
203 /* freopen() should call fflush() internal */
204 fp = freopen(TMPFILENAME, "r", fp);
205 assert(fp != NULL);
206
207 /* can read rest of that on fp */
208 fgets(buf, sizeof(buf), fp);
209 assert(strcmp(buf, "Hello world\n") == 0);
210
211 r = fclose(fp);
212 assert(r == 0);
213}
214
215int
216main(int argc, char *argv[])
217{
218 test_fflush_read0();
219 test_fflush_read1();
220 test_fflush_read2();
221 test_fflush_read3();
222 test_fflush_read4();
223
224 exit(0);
225}