diff options
| author | cvs2svn <admin@example.com> | 2025-08-02 06:16:35 +0000 |
|---|---|---|
| committer | cvs2svn <admin@example.com> | 2025-08-02 06:16:35 +0000 |
| commit | a397c95f8aea58533e72379d6e0de12683ecd0dc (patch) | |
| tree | 45ccaccd0ce2ea01650f0a9f5e9268a656e17e51 /src/regress/lib/libc/stdio/test_fflush.c | |
| parent | a79e90e7342954ae2287db505811ca3c1cd336d7 (diff) | |
| download | openbsd-tb_20250802.tar.gz openbsd-tb_20250802.tar.bz2 openbsd-tb_20250802.zip | |
This commit was manufactured by cvs2git to create tag 'tb_20250802'.tb_20250802
Diffstat (limited to 'src/regress/lib/libc/stdio/test_fflush.c')
| -rw-r--r-- | src/regress/lib/libc/stdio/test_fflush.c | 345 |
1 files changed, 0 insertions, 345 deletions
diff --git a/src/regress/lib/libc/stdio/test_fflush.c b/src/regress/lib/libc/stdio/test_fflush.c deleted file mode 100644 index a0586b7d14..0000000000 --- a/src/regress/lib/libc/stdio/test_fflush.c +++ /dev/null | |||
| @@ -1,345 +0,0 @@ | |||
| 1 | /* $OpenBSD: test_fflush.c,v 1.3 2025/06/08 08:53:53 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 <locale.h> | ||
| 21 | #include <stdio.h> | ||
| 22 | #include <stdlib.h> | ||
| 23 | #include <string.h> | ||
| 24 | #include <unistd.h> | ||
| 25 | #include <wchar.h> | ||
| 26 | |||
| 27 | /* we use assert() */ | ||
| 28 | #undef NDEBUG | ||
| 29 | |||
| 30 | #define TMPFILENAME "test_fflush.tmp" | ||
| 31 | |||
| 32 | void setup(void); | ||
| 33 | |||
| 34 | void test_fflush_read0(void); | ||
| 35 | void test_fflush_read1(void); | ||
| 36 | void test_fflush_read2(void); | ||
| 37 | void test_fflush_read3(void); | ||
| 38 | void test_fflush_read4(void); | ||
| 39 | void setupw(void); | ||
| 40 | void test_fflush_read5(void); | ||
| 41 | void test_fflush_read6(void); | ||
| 42 | |||
| 43 | void | ||
| 44 | setup(void) | ||
| 45 | { | ||
| 46 | FILE *fp; | ||
| 47 | |||
| 48 | /* common setup */ | ||
| 49 | unlink(TMPFILENAME); | ||
| 50 | fp = fopen(TMPFILENAME, "w+"); | ||
| 51 | assert(fp != NULL); | ||
| 52 | fputs("Hello world\n", fp); | ||
| 53 | fclose(fp); | ||
| 54 | } | ||
| 55 | |||
| 56 | /* fflush work with reading file and seekable */ | ||
| 57 | void | ||
| 58 | test_fflush_read0(void) | ||
| 59 | { | ||
| 60 | int r; | ||
| 61 | char buf[80]; | ||
| 62 | FILE *fp; | ||
| 63 | |||
| 64 | setup(); | ||
| 65 | |||
| 66 | /* In POSIX 2008, fflush() must work with the file object for reading */ | ||
| 67 | fp = fopen(TMPFILENAME, "r"); | ||
| 68 | assert(fp != NULL); | ||
| 69 | assert(fgetc(fp) == 'H'); | ||
| 70 | r = fflush(fp); | ||
| 71 | assert(r == 0); | ||
| 72 | |||
| 73 | /* the position is moved to 1 */ | ||
| 74 | assert(ftell(fp) == 1); | ||
| 75 | |||
| 76 | /* can read rest of that */ | ||
| 77 | fgets(buf, sizeof(buf), fp); | ||
| 78 | assert(strcmp(buf, "ello world\n") == 0); | ||
| 79 | r = fclose(fp); | ||
| 80 | assert(r == 0); | ||
| 81 | } | ||
| 82 | |||
| 83 | /* fflush work with reading file and seekable + unget */ | ||
| 84 | void | ||
| 85 | test_fflush_read1(void) | ||
| 86 | { | ||
| 87 | int r; | ||
| 88 | char buf[80]; | ||
| 89 | FILE *fp; | ||
| 90 | |||
| 91 | setup(); | ||
| 92 | |||
| 93 | fp = fopen(TMPFILENAME, "r"); | ||
| 94 | assert(fp != NULL); | ||
| 95 | assert(fgetc(fp) == 'H'); | ||
| 96 | assert(fgetc(fp) == 'e'); | ||
| 97 | assert(fgetc(fp) == 'l'); | ||
| 98 | assert(fgetc(fp) == 'l'); | ||
| 99 | assert(fgetc(fp) == 'o'); | ||
| 100 | |||
| 101 | /* push the 'AAAA' back */ | ||
| 102 | ungetc('A', fp); | ||
| 103 | ungetc('A', fp); | ||
| 104 | ungetc('A', fp); | ||
| 105 | ungetc('A', fp); | ||
| 106 | |||
| 107 | /* can read rest of that */ | ||
| 108 | fgets(buf, sizeof(buf), fp); | ||
| 109 | assert(strcmp(buf, "AAAA world\n") == 0); | ||
| 110 | r = fclose(fp); | ||
| 111 | assert(r == 0); | ||
| 112 | |||
| 113 | /* do the same thing + fflush */ | ||
| 114 | |||
| 115 | fp = fopen(TMPFILENAME, "r"); | ||
| 116 | assert(fp != NULL); | ||
| 117 | assert(fgetc(fp) == 'H'); | ||
| 118 | assert(fgetc(fp) == 'e'); | ||
| 119 | assert(fgetc(fp) == 'l'); | ||
| 120 | assert(fgetc(fp) == 'l'); | ||
| 121 | assert(fgetc(fp) == 'o'); | ||
| 122 | |||
| 123 | /* push 'AAAA' back */ | ||
| 124 | ungetc('A', fp); | ||
| 125 | ungetc('A', fp); | ||
| 126 | ungetc('A', fp); | ||
| 127 | ungetc('A', fp); | ||
| 128 | |||
| 129 | /* then fflush */ | ||
| 130 | r = fflush(fp); | ||
| 131 | assert(r == 0); | ||
| 132 | |||
| 133 | /* fllush() clears the all pushed back chars */ | ||
| 134 | |||
| 135 | /* can read rest of that */ | ||
| 136 | fgets(buf, sizeof(buf), fp); | ||
| 137 | assert(strcmp(buf, " world\n") == 0); | ||
| 138 | r = fclose(fp); | ||
| 139 | assert(r == 0); | ||
| 140 | } | ||
| 141 | |||
| 142 | /* fflush() to reading and non-seekable stream */ | ||
| 143 | void | ||
| 144 | test_fflush_read2(void) | ||
| 145 | { | ||
| 146 | int r; | ||
| 147 | FILE *fp; | ||
| 148 | char buf[80]; | ||
| 149 | |||
| 150 | /* In POSIX-2008, fflush() must work with the file object for reading */ | ||
| 151 | fp = popen("echo Hello world", "r"); | ||
| 152 | assert(fp != NULL); | ||
| 153 | assert(fgetc(fp) == 'H'); | ||
| 154 | r = fflush(fp); | ||
| 155 | assert(r == 0); | ||
| 156 | |||
| 157 | /* | ||
| 158 | * FILE object for read and NOT seekable. In that case, fflush does | ||
| 159 | * nothing, but must keep the buffer. | ||
| 160 | */ | ||
| 161 | |||
| 162 | /* can read rest of that */ | ||
| 163 | fgets(buf, sizeof(buf), fp); | ||
| 164 | assert(strcmp(buf, "ello world\n") == 0); | ||
| 165 | r = pclose(fp); | ||
| 166 | assert(r == 0); | ||
| 167 | } | ||
| 168 | |||
| 169 | /* fflush() to the file which doesn't have any buffer */ | ||
| 170 | void | ||
| 171 | test_fflush_read3(void) | ||
| 172 | { | ||
| 173 | int r; | ||
| 174 | FILE *fp; | ||
| 175 | |||
| 176 | setup(); | ||
| 177 | |||
| 178 | /* In POSIX-2008, fflush() must work with the file object for reading */ | ||
| 179 | fp = fopen(TMPFILENAME, "r"); | ||
| 180 | assert(fp != NULL); | ||
| 181 | r = fflush(fp); | ||
| 182 | assert(r == 0); | ||
| 183 | r = fclose(fp); | ||
| 184 | assert(r == 0); | ||
| 185 | } | ||
| 186 | |||
| 187 | /* freopen() should call fflush() internal */ | ||
| 188 | void | ||
| 189 | test_fflush_read4(void) | ||
| 190 | { | ||
| 191 | int r; | ||
| 192 | FILE *fp; | ||
| 193 | off_t pos; | ||
| 194 | char buf[80]; | ||
| 195 | |||
| 196 | setup(); | ||
| 197 | |||
| 198 | /* In POSIX-2008, fflush() must work with the file object for reading */ | ||
| 199 | fp = fopen(TMPFILENAME, "r"); | ||
| 200 | assert(fp != NULL); | ||
| 201 | |||
| 202 | assert(fgetc(fp) == 'H'); /* read 1 */ | ||
| 203 | |||
| 204 | pos = lseek(fileno(fp), 0, SEEK_CUR); | ||
| 205 | assert(pos >= 1); | ||
| 206 | assert(pos > 1); /* this test assume the buffer is used */ | ||
| 207 | |||
| 208 | /* freopen() should call fflush() internal */ | ||
| 209 | fp = freopen(TMPFILENAME, "r", fp); | ||
| 210 | assert(fp != NULL); | ||
| 211 | |||
| 212 | /* can read rest of that on fp */ | ||
| 213 | fgets(buf, sizeof(buf), fp); | ||
| 214 | assert(strcmp(buf, "Hello world\n") == 0); | ||
| 215 | |||
| 216 | r = fclose(fp); | ||
| 217 | assert(r == 0); | ||
| 218 | } | ||
| 219 | |||
| 220 | void | ||
| 221 | setupw(void) | ||
| 222 | { | ||
| 223 | FILE *fp; | ||
| 224 | |||
| 225 | /* common setup */ | ||
| 226 | unlink(TMPFILENAME); | ||
| 227 | fp = fopen(TMPFILENAME, "w+"); | ||
| 228 | assert(fp != NULL); | ||
| 229 | /* Konnitiwa Sekai(in Kanji) */ | ||
| 230 | fputws(L"\u3053\u3093\u306b\u3061\u308f \u4e16\u754c\n", fp); | ||
| 231 | fclose(fp); | ||
| 232 | } | ||
| 233 | |||
| 234 | /* fflush work with reading file and seekable + ungetwc */ | ||
| 235 | void | ||
| 236 | test_fflush_read5(void) | ||
| 237 | { | ||
| 238 | int r; | ||
| 239 | wchar_t buf[80]; | ||
| 240 | FILE *fp; | ||
| 241 | |||
| 242 | setupw(); | ||
| 243 | |||
| 244 | fp = fopen(TMPFILENAME, "r"); | ||
| 245 | |||
| 246 | assert(fp != NULL); | ||
| 247 | assert(fgetwc(fp) == L'\u3053'); /* Ko */ | ||
| 248 | assert(fgetwc(fp) == L'\u3093'); /* N */ | ||
| 249 | assert(fgetwc(fp) == L'\u306b'); /* Ni */ | ||
| 250 | assert(fgetwc(fp) == L'\u3061'); /* Ti */ | ||
| 251 | assert(fgetwc(fp) == L'\u308f'); /* Wa */ | ||
| 252 | |||
| 253 | /* push 263A(smile) back */ | ||
| 254 | assert(ungetwc(L'\u263a', fp)); | ||
| 255 | |||
| 256 | /* we support 1 push back wchar_t */ | ||
| 257 | assert(fgetwc(fp) == L'\u263a'); | ||
| 258 | |||
| 259 | /* can read reset of that */ | ||
| 260 | fgetws(buf, sizeof(buf), fp); | ||
| 261 | assert(wcscmp(buf, L" \u4e16\u754c\n") == 0); | ||
| 262 | |||
| 263 | r = fclose(fp); | ||
| 264 | assert(r == 0); | ||
| 265 | |||
| 266 | /* do the same thing + fflush */ | ||
| 267 | fp = fopen(TMPFILENAME, "r"); | ||
| 268 | |||
| 269 | assert(fp != NULL); | ||
| 270 | assert(fgetwc(fp) == L'\u3053'); /* Ko */ | ||
| 271 | assert(fgetwc(fp) == L'\u3093'); /* N */ | ||
| 272 | assert(fgetwc(fp) == L'\u306b'); /* Ni */ | ||
| 273 | assert(fgetwc(fp) == L'\u3061'); /* Ti */ | ||
| 274 | assert(fgetwc(fp) == L'\u308f'); /* Wa */ | ||
| 275 | |||
| 276 | /* push 263A(smile) back */ | ||
| 277 | assert(ungetwc(L'\u263a', fp)); | ||
| 278 | |||
| 279 | /* we support 1 push back wchar_t */ | ||
| 280 | assert(fgetwc(fp) == L'\u263a'); | ||
| 281 | |||
| 282 | /* then fflush */ | ||
| 283 | r = fflush(fp); | ||
| 284 | assert(r == 0); | ||
| 285 | |||
| 286 | /* fllush() clears the all pushed back chars */ | ||
| 287 | |||
| 288 | /* can read rest of that */ | ||
| 289 | fgetws(buf, sizeof(buf), fp); | ||
| 290 | assert(wcscmp(buf, L" \u4e16\u754c\n") == 0); | ||
| 291 | r = fclose(fp); | ||
| 292 | assert(r == 0); | ||
| 293 | } | ||
| 294 | |||
| 295 | void | ||
| 296 | test_fflush_read6(void) | ||
| 297 | { | ||
| 298 | int r, c; | ||
| 299 | FILE *fp; | ||
| 300 | |||
| 301 | setup(); | ||
| 302 | fp = fopen(TMPFILENAME, "r"); | ||
| 303 | assert(fp != NULL); | ||
| 304 | |||
| 305 | /* | ||
| 306 | * https://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html | ||
| 307 | * .. any characters pushed back onto the stream by ungetc() or ungetwc() | ||
| 308 | * that have not subsequently been read from the stream shall be discarded | ||
| 309 | * (without further changing the file offset). | ||
| 310 | */ | ||
| 311 | |||
| 312 | assert(fgetc(fp) == 'H'); | ||
| 313 | c = getc(fp); | ||
| 314 | ungetc(c, fp); /* push back the character has been read */ | ||
| 315 | r = fflush(fp); | ||
| 316 | assert(r == 0); | ||
| 317 | assert(getc(fp) == c); | ||
| 318 | |||
| 319 | fseek(fp, 0, SEEK_SET); | ||
| 320 | assert(fgetc(fp) == 'H'); | ||
| 321 | c = getc(fp); | ||
| 322 | ungetc('X', fp); /* push back the character has not been read */ | ||
| 323 | r = fflush(fp); | ||
| 324 | assert(r == 0); | ||
| 325 | assert(getc(fp) == 'l'); | ||
| 326 | |||
| 327 | r = fclose(fp); | ||
| 328 | assert(r == 0); | ||
| 329 | } | ||
| 330 | |||
| 331 | int | ||
| 332 | main(int argc, char *argv[]) | ||
| 333 | { | ||
| 334 | setlocale(LC_ALL, "C.UTF-8"); | ||
| 335 | |||
| 336 | test_fflush_read0(); | ||
| 337 | test_fflush_read1(); | ||
| 338 | test_fflush_read2(); | ||
| 339 | test_fflush_read3(); | ||
| 340 | test_fflush_read4(); | ||
| 341 | test_fflush_read5(); | ||
| 342 | test_fflush_read6(); | ||
| 343 | |||
| 344 | exit(0); | ||
| 345 | } | ||
