From c11a2e675ccc8637e6322b98d878b0315a8bb7e6 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 23 Dec 2024 16:59:20 +0100 Subject: [PATCH 7/7] mbrtowc tests: Test in the UTF-8 environment on native Windows. * tests/test-mbrtowc-w32utf8.sh: New file. * tests/test-mbrtowc-w32utf8.c: New file. * modules/mbrtowc-tests (Files): Add these files and m4/windows-rc.m4, tests/windows-utf8.rc, tests/windows-utf8.manifest. (Depends-on): Add test-xfail. (configure.ac): Invoke gl_WINDOWS_RC. (Makefile.am): Arrange to compile test-mbrtowc-w32utf8 and run test-mbrtowc-w32utf8.sh. --- ChangeLog | 12 +++ modules/mbrtowc-tests | 16 ++++ tests/test-mbrtowc-w32utf8.c | 166 ++++++++++++++++++++++++++++++++++ tests/test-mbrtowc-w32utf8.sh | 12 +++ 4 files changed, 206 insertions(+) create mode 100644 tests/test-mbrtowc-w32utf8.c create mode 100755 tests/test-mbrtowc-w32utf8.sh diff --git a/ChangeLog b/ChangeLog index c5e2e8b1b2..e6d2e1d592 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2024-12-23 Bruno Haible + + mbrtowc tests: Test in the UTF-8 environment on native Windows. + * tests/test-mbrtowc-w32utf8.sh: New file. + * tests/test-mbrtowc-w32utf8.c: New file. + * modules/mbrtowc-tests (Files): Add these files and + m4/windows-rc.m4, tests/windows-utf8.rc, tests/windows-utf8.manifest. + (Depends-on): Add test-xfail. + (configure.ac): Invoke gl_WINDOWS_RC. + (Makefile.am): Arrange to compile test-mbrtowc-w32utf8 and run + test-mbrtowc-w32utf8.sh. + 2024-12-23 Bruno Haible setlocale tests: Test in the UTF-8 environment on native Windows. diff --git a/modules/mbrtowc-tests b/modules/mbrtowc-tests index d152e2e472..d9add89fee 100644 --- a/modules/mbrtowc-tests +++ b/modules/mbrtowc-tests @@ -13,6 +13,10 @@ tests/test-mbrtowc-w32-6.sh tests/test-mbrtowc-w32-7.sh tests/test-mbrtowc-w32-8.sh tests/test-mbrtowc-w32.c +tests/test-mbrtowc-w32utf8.sh +tests/test-mbrtowc-w32utf8.c +tests/windows-utf8.rc +tests/windows-utf8.manifest tests/signature.h tests/macros.h m4/locale-en.m4 @@ -20,12 +24,14 @@ m4/locale-fr.m4 m4/locale-ja.m4 m4/locale-zh.m4 m4/codeset.m4 +m4/windows-rc.m4 Depends-on: mbsinit wctob setlocale localcharset +test-xfail configure.ac: gt_LOCALE_EN_UTF8 @@ -33,6 +39,7 @@ gt_LOCALE_FR gt_LOCALE_FR_UTF8 gt_LOCALE_JA gt_LOCALE_ZH_CN +gl_WINDOWS_RC Makefile.am: TESTS += \ @@ -49,3 +56,12 @@ TESTS_ENVIRONMENT += \ LOCALE_ZH_CN='@LOCALE_ZH_CN@' check_PROGRAMS += test-mbrtowc test-mbrtowc-w32 test_mbrtowc_LDADD = $(LDADD) $(SETLOCALE_LIB) $(MBRTOWC_LIB) + +if OS_IS_NATIVE_WINDOWS +TESTS += test-mbrtowc-w32utf8.sh +noinst_PROGRAMS += test-mbrtowc-w32utf8 +test_mbrtowc_w32utf8_LDADD = $(LDADD) test-mbrtowc-windows-utf8.res $(SETLOCALE_LIB) +test-mbrtowc-windows-utf8.res : $(srcdir)/windows-utf8.rc + $(WINDRES) -i $(srcdir)/windows-utf8.rc -o test-mbrtowc-windows-utf8.res --output-format=coff +MOSTLYCLEANFILES += test-mbrtowc-windows-utf8.res +endif diff --git a/tests/test-mbrtowc-w32utf8.c b/tests/test-mbrtowc-w32utf8.c new file mode 100644 index 0000000000..803c1638c0 --- /dev/null +++ b/tests/test-mbrtowc-w32utf8.c @@ -0,0 +1,166 @@ +/* Test of conversion of multibyte character to wide character + on native Windows in the UTF-8 environment. + Copyright (C) 2024 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible , 2024. */ + +#include + +#include + +#include +#include +#include +#include +#include + +#include "macros.h" + +int +main (void) +{ +#ifdef _UCRT + /* Test that MB_CUR_MAX and mbrtowc() work as expected in a UTF-8 locale. */ + mbstate_t state; + wchar_t wc; + size_t ret; + + if (setlocale (LC_ALL, "") == NULL) + return 1; + + ASSERT (MB_CUR_MAX >= 4); + + { + char input[] = "B\303\274\303\237er"; /* "B????er" */ + memset (&state, '\0', sizeof (mbstate_t)); + + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, input, 1, &state); + ASSERT (ret == 1); + ASSERT (wc == 'B'); + ASSERT (mbsinit (&state)); + input[0] = '\0'; + + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, input + 1, 1, &state); + ASSERT (ret == (size_t)(-2)); + ASSERT (wc == (wchar_t) 0xBADFACE); + ASSERT (!mbsinit (&state)); + input[1] = '\0'; + + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, input + 2, 5, &state); + ASSERT (ret == 1); + ASSERT (wctob (wc) == EOF); + ASSERT (wc == 0x00FC); + ASSERT (mbsinit (&state)); + input[2] = '\0'; + + /* Test support of NULL first argument. */ + ret = mbrtowc (NULL, input + 3, 4, &state); + ASSERT (ret == 2); + ASSERT (mbsinit (&state)); + + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, input + 3, 4, &state); + ASSERT (ret == 2); + ASSERT (wctob (wc) == EOF); + ASSERT (wc == 0x00DF); + ASSERT (mbsinit (&state)); + input[3] = '\0'; + input[4] = '\0'; + + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, input + 5, 2, &state); + ASSERT (ret == 1); + ASSERT (wc == 'e'); + ASSERT (mbsinit (&state)); + input[5] = '\0'; + + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, input + 6, 1, &state); + ASSERT (ret == 1); + ASSERT (wc == 'r'); + ASSERT (mbsinit (&state)); + + /* Test some invalid input. */ + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\377", 1, &state); /* 0xFF */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\303\300", 2, &state); /* 0xC3 0xC0 */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\343\300", 2, &state); /* 0xE3 0xC0 */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\343\300\200", 3, &state); /* 0xE3 0xC0 0x80 */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\343\200\300", 3, &state); /* 0xE3 0x80 0xC0 */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\363\300", 2, &state); /* 0xF3 0xC0 */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\363\300\200\200", 4, &state); /* 0xF3 0xC0 0x80 0x80 */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\363\200\300", 3, &state); /* 0xF3 0x80 0xC0 */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\363\200\300\200", 4, &state); /* 0xF3 0x80 0xC0 0x80 */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + + memset (&state, '\0', sizeof (mbstate_t)); + wc = (wchar_t) 0xBADFACE; + ret = mbrtowc (&wc, "\363\200\200\300", 4, &state); /* 0xF3 0x80 0x80 0xC0 */ + ASSERT (ret == (size_t)-1); + ASSERT (errno == EILSEQ); + } + + return test_exit_status; +#else + fputs ("Skipping test: not using the UCRT runtime\n", stderr); + return 77; +#endif +} diff --git a/tests/test-mbrtowc-w32utf8.sh b/tests/test-mbrtowc-w32utf8.sh new file mode 100755 index 0000000000..d0a953486c --- /dev/null +++ b/tests/test-mbrtowc-w32utf8.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +# Test the UTF-8 environment on native Windows. +unset LC_ALL +unset LC_CTYPE +unset LC_MESSAGES +unset LC_NUMERIC +unset LC_COLLATE +unset LC_MONETARY +unset LC_TIME +unset LANG +${CHECKER} ./test-mbrtowc-w32utf8${EXEEXT} -- 2.43.0