summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/stdio/test_fflush.c
blob: a0586b7d14e0e78f578682072a8191dc4cb1591b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*	$OpenBSD: test_fflush.c,v 1.3 2025/06/08 08:53:53 yasuoka Exp $	*/

/*
 * Copyright (c) 2025 YASUOKA Masahiko <yasuoka@yasuoka.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <assert.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>

/* we use assert() */
#undef	NDEBUG

#define	TMPFILENAME	"test_fflush.tmp"

void setup(void);

void test_fflush_read0(void);
void test_fflush_read1(void);
void test_fflush_read2(void);
void test_fflush_read3(void);
void test_fflush_read4(void);
void setupw(void);
void test_fflush_read5(void);
void test_fflush_read6(void);

void
setup(void)
{
	FILE	*fp;

	/* common setup */
	unlink(TMPFILENAME);
	fp = fopen(TMPFILENAME, "w+");
	assert(fp != NULL);
	fputs("Hello world\n", fp);
	fclose(fp);
}

/* fflush work with reading file and seekable */
void
test_fflush_read0(void)
{
	int	 r;
	char	 buf[80];
	FILE	*fp;

	setup();

	/* In POSIX 2008, fflush() must work with the file object for reading */
	fp = fopen(TMPFILENAME, "r");
	assert(fp != NULL);
	assert(fgetc(fp) == 'H');
	r = fflush(fp);
	assert(r == 0);

	/* the position is moved to 1 */
	assert(ftell(fp) == 1);

	/* can read rest of that */
	fgets(buf, sizeof(buf), fp);
	assert(strcmp(buf, "ello world\n") == 0);
	r = fclose(fp);
	assert(r == 0);
}

/* fflush work with reading file and seekable + unget */
void
test_fflush_read1(void)
{
	int	 r;
	char	 buf[80];
	FILE	*fp;

	setup();

	fp = fopen(TMPFILENAME, "r");
	assert(fp != NULL);
	assert(fgetc(fp) == 'H');
	assert(fgetc(fp) == 'e');
	assert(fgetc(fp) == 'l');
	assert(fgetc(fp) == 'l');
	assert(fgetc(fp) == 'o');

	/* push the 'AAAA' back */
	ungetc('A', fp);
	ungetc('A', fp);
	ungetc('A', fp);
	ungetc('A', fp);

	/* can read rest of that */
	fgets(buf, sizeof(buf), fp);
	assert(strcmp(buf, "AAAA world\n") == 0);
	r = fclose(fp);
	assert(r == 0);

	/* do the same thing + fflush */

	fp = fopen(TMPFILENAME, "r");
	assert(fp != NULL);
	assert(fgetc(fp) == 'H');
	assert(fgetc(fp) == 'e');
	assert(fgetc(fp) == 'l');
	assert(fgetc(fp) == 'l');
	assert(fgetc(fp) == 'o');

	/* push 'AAAA' back */
	ungetc('A', fp);
	ungetc('A', fp);
	ungetc('A', fp);
	ungetc('A', fp);

	/* then fflush */
	r = fflush(fp);
	assert(r == 0);

	/* fllush() clears the all pushed back chars */

	/* can read rest of that */
	fgets(buf, sizeof(buf), fp);
	assert(strcmp(buf, " world\n") == 0);
	r = fclose(fp);
	assert(r == 0);
}

/* fflush() to reading and non-seekable stream */
void
test_fflush_read2(void)
{
	int	 r;
	FILE	*fp;
	char	 buf[80];

	/* In POSIX-2008, fflush() must work with the file object for reading */
	fp = popen("echo Hello world", "r");
	assert(fp != NULL);
	assert(fgetc(fp) == 'H');
	r = fflush(fp);
	assert(r == 0);

	/*
	 * FILE object for read and NOT seekable.  In that case, fflush does
	 * nothing, but must keep the buffer.
	 */

	/* can read rest of that */
	fgets(buf, sizeof(buf), fp);
	assert(strcmp(buf, "ello world\n") == 0);
	r = pclose(fp);
	assert(r == 0);
}

/* fflush() to the file which doesn't have any buffer */
void
test_fflush_read3(void)
{
	int	 r;
	FILE	*fp;

	setup();

	/* In POSIX-2008, fflush() must work with the file object for reading */
	fp = fopen(TMPFILENAME, "r");
	assert(fp != NULL);
	r = fflush(fp);
	assert(r == 0);
	r = fclose(fp);
	assert(r == 0);
}

/* freopen() should call fflush() internal */
void
test_fflush_read4(void)
{
	int	 r;
	FILE	*fp;
	off_t	 pos;
	char	 buf[80];

	setup();

	/* In POSIX-2008, fflush() must work with the file object for reading */
	fp = fopen(TMPFILENAME, "r");
	assert(fp != NULL);

	assert(fgetc(fp) == 'H');	/* read 1 */

	pos = lseek(fileno(fp), 0, SEEK_CUR);
	assert(pos >= 1);
	assert(pos > 1);	/* this test assume the buffer is used */

	/* freopen() should call fflush() internal */
	fp = freopen(TMPFILENAME, "r", fp);
	assert(fp != NULL);

	/* can read rest of that on fp */
	fgets(buf, sizeof(buf), fp);
	assert(strcmp(buf, "Hello world\n") == 0);

	r = fclose(fp);
	assert(r == 0);
}

void
setupw(void)
{
	FILE	*fp;

	/* common setup */
	unlink(TMPFILENAME);
	fp = fopen(TMPFILENAME, "w+");
	assert(fp != NULL);
	/* Konnitiwa Sekai(in Kanji) */
	fputws(L"\u3053\u3093\u306b\u3061\u308f \u4e16\u754c\n", fp);
	fclose(fp);
}

/* fflush work with reading file and seekable + ungetwc */
void
test_fflush_read5(void)
{
	int	 r;
	wchar_t	 buf[80];
	FILE	*fp;

	setupw();

	fp = fopen(TMPFILENAME, "r");

	assert(fp != NULL);
	assert(fgetwc(fp) == L'\u3053');	/* Ko */
	assert(fgetwc(fp) == L'\u3093');	/* N  */
	assert(fgetwc(fp) == L'\u306b');	/* Ni */
	assert(fgetwc(fp) == L'\u3061');	/* Ti */
	assert(fgetwc(fp) == L'\u308f');	/* Wa */

	/* push 263A(smile) back */
	assert(ungetwc(L'\u263a', fp));

	/* we support 1 push back wchar_t */
	assert(fgetwc(fp) == L'\u263a');

	/* can read reset of that */
	fgetws(buf, sizeof(buf), fp);
	assert(wcscmp(buf, L" \u4e16\u754c\n") == 0);

	r = fclose(fp);
	assert(r == 0);

	/* do the same thing + fflush */
	fp = fopen(TMPFILENAME, "r");

	assert(fp != NULL);
	assert(fgetwc(fp) == L'\u3053');	/* Ko */
	assert(fgetwc(fp) == L'\u3093');	/* N  */
	assert(fgetwc(fp) == L'\u306b');	/* Ni */
	assert(fgetwc(fp) == L'\u3061');	/* Ti */
	assert(fgetwc(fp) == L'\u308f');	/* Wa */

	/* push 263A(smile) back */
	assert(ungetwc(L'\u263a', fp));

	/* we support 1 push back wchar_t */
	assert(fgetwc(fp) == L'\u263a');

	/* then fflush */
	r = fflush(fp);
	assert(r == 0);

	/* fllush() clears the all pushed back chars */

	/* can read rest of that */
	fgetws(buf, sizeof(buf), fp);
	assert(wcscmp(buf, L" \u4e16\u754c\n") == 0);
	r = fclose(fp);
	assert(r == 0);
}

void
test_fflush_read6(void)
{
	int	 r, c;
	FILE	*fp;

	setup();
	fp = fopen(TMPFILENAME, "r");
	assert(fp != NULL);

	/*
	 * https://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html
	 * .. any characters pushed back onto the stream by ungetc() or ungetwc()
	 * that have not subsequently been read from the stream shall be discarded
	 * (without further changing the file offset).
	 */

	assert(fgetc(fp) == 'H');
	c = getc(fp);
	ungetc(c, fp);	/* push back the character has been read */
	r = fflush(fp);
	assert(r == 0);
	assert(getc(fp) == c);

	fseek(fp, 0, SEEK_SET);
	assert(fgetc(fp) == 'H');
	c = getc(fp);
	ungetc('X', fp); /* push back the character has not been read */
	r = fflush(fp);
	assert(r == 0);
	assert(getc(fp) == 'l');

	r = fclose(fp);
	assert(r == 0);
}

int
main(int argc, char *argv[])
{
	setlocale(LC_ALL, "C.UTF-8");

	test_fflush_read0();
	test_fflush_read1();
	test_fflush_read2();
	test_fflush_read3();
	test_fflush_read4();
	test_fflush_read5();
	test_fflush_read6();

	exit(0);
}