From 8b4626f0b3c47fb4ea0aa9adc84cb658c244ec2b Mon Sep 17 00:00:00 2001 From: yasuoka <> Date: Sun, 25 May 2025 05:35:13 +0000 Subject: Add test whether fflush() complies POSIX for the handling of pushed-back wchar_t chars. --- src/regress/lib/libc/stdio/test_fflush.c | 83 +++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) (limited to 'src/regress/lib/libc/stdio') diff --git a/src/regress/lib/libc/stdio/test_fflush.c b/src/regress/lib/libc/stdio/test_fflush.c index fe1772bdfc..76f2d88aba 100644 --- a/src/regress/lib/libc/stdio/test_fflush.c +++ b/src/regress/lib/libc/stdio/test_fflush.c @@ -1,4 +1,4 @@ -/* $OpenBSD: test_fflush.c,v 1.1 2025/05/24 11:07:21 yasuoka Exp $ */ +/* $OpenBSD: test_fflush.c,v 1.2 2025/05/25 05:35:13 yasuoka Exp $ */ /* * Copyright (c) 2025 YASUOKA Masahiko @@ -17,10 +17,12 @@ */ #include +#include #include #include #include #include +#include /* we use assert() */ #undef NDEBUG @@ -34,6 +36,8 @@ 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 setup(void) @@ -212,14 +216,91 @@ test_fflush_read4(void) 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); +} 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(); exit(0); } -- cgit v1.2.3-55-g6feb