diff options
Diffstat (limited to '')
-rw-r--r-- | src/regress/lib/libc/stdio/test___fwriting.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/regress/lib/libc/stdio/test___fwriting.c b/src/regress/lib/libc/stdio/test___fwriting.c new file mode 100644 index 0000000000..eb4671d3cf --- /dev/null +++ b/src/regress/lib/libc/stdio/test___fwriting.c | |||
@@ -0,0 +1,83 @@ | |||
1 | /* $OpenBSD: test___fwriting.c,v 1.1 2025/05/25 00:20:54 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 <stdio_ext.h> | ||
22 | #include <stdlib.h> | ||
23 | |||
24 | /* we use assert() */ | ||
25 | #undef NDEBUG | ||
26 | |||
27 | #define TMPFILENAME "test___fwriting.tmp" | ||
28 | |||
29 | void test___fwriting0(void); | ||
30 | void test___fwriting1(void); | ||
31 | |||
32 | void | ||
33 | test___fwriting0(void) | ||
34 | { | ||
35 | FILE *fp; | ||
36 | int r; | ||
37 | |||
38 | fp = fopen(TMPFILENAME, "w"); /* write only */ | ||
39 | assert(fp != NULL); | ||
40 | assert(__fwriting(fp) != 0); /* writing is true immediately */ | ||
41 | r = fputs("Hello world\n", fp); | ||
42 | assert(r >= 0); | ||
43 | r = fclose(fp); | ||
44 | assert(r == 0); | ||
45 | |||
46 | fp = fopen(TMPFILENAME, "a"); /* append only */ | ||
47 | assert(fp != NULL); | ||
48 | assert(__fwriting(fp) != 0); /* writing immediately */ | ||
49 | r = fclose(fp); | ||
50 | assert(r == 0); | ||
51 | } | ||
52 | |||
53 | void | ||
54 | test___fwriting1(void) | ||
55 | { | ||
56 | FILE *fp; | ||
57 | int r; | ||
58 | |||
59 | fp = fopen(TMPFILENAME, "w+"); /* read / write */ | ||
60 | assert(fp != NULL); | ||
61 | r = fputs("Hello world\n", fp); | ||
62 | assert(r >= 0); | ||
63 | assert(__fwriting(fp) != 0); | ||
64 | rewind(fp); | ||
65 | assert(fgetc(fp) == 'H'); /* read */ | ||
66 | assert(__fwriting(fp) == 0); /* writing becomes false */ | ||
67 | fputc('e', fp); | ||
68 | assert(__fwriting(fp) != 0); /* writing becomes true */ | ||
69 | ungetc('e', fp); | ||
70 | assert(__fwriting(fp) == 0); /* ungetc -> writing becomes false */ | ||
71 | |||
72 | r = fclose(fp); | ||
73 | assert(r == 0); | ||
74 | } | ||
75 | |||
76 | int | ||
77 | main(int argc, char *argv[]) | ||
78 | { | ||
79 | test___fwriting0(); | ||
80 | test___fwriting1(); | ||
81 | |||
82 | exit(0); | ||
83 | } | ||