From: Bruno Haible Date: Mon, 3 Apr 2023 12:24:50 +0000 (+0200) Subject: mbstoc32s tests: Check behaviour in the C locale. X-Git-Tag: v1.0~1522 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=144cf2bd1009ecc82ea289fa2b05d89d7555f458;p=gnulib.git mbstoc32s tests: Check behaviour in the C locale. * tests/test-mbstoc32s.c (main): Test behaviour in the C locale. Based on tests/test-mbstowcs.c. * tests/test-mbstoc32s-5.sh: New file, based on tests/test-mbstowcs5.sh. * modules/mbstoc32s-tests (Files): Add it. (Depends-on): Add btoc32. (Makefile.am): Run test-mbstoc32s-5.sh. --- diff --git a/ChangeLog b/ChangeLog index 4699781f9f..5c916c133b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2023-04-03 Bruno Haible + + mbstoc32s tests: Check behaviour in the C locale. + * tests/test-mbstoc32s.c (main): Test behaviour in the C locale. Based + on tests/test-mbstowcs.c. + * tests/test-mbstoc32s-5.sh: New file, based on tests/test-mbstowcs5.sh. + * modules/mbstoc32s-tests (Files): Add it. + (Depends-on): Add btoc32. + (Makefile.am): Run test-mbstoc32s-5.sh. + 2023-04-03 Bruno Haible mbsnrtoc32s tests: Check behaviour in the C locale. diff --git a/modules/mbstoc32s-tests b/modules/mbstoc32s-tests index 565796a06f..9e6dd61d26 100644 --- a/modules/mbstoc32s-tests +++ b/modules/mbstoc32s-tests @@ -3,6 +3,7 @@ tests/test-mbstoc32s-1.sh tests/test-mbstoc32s-2.sh tests/test-mbstoc32s-3.sh tests/test-mbstoc32s-4.sh +tests/test-mbstoc32s-5.sh tests/test-mbstoc32s.c tests/signature.h tests/macros.h @@ -12,6 +13,7 @@ m4/locale-zh.m4 m4/codeset.m4 Depends-on: +btoc32 c32tob setlocale @@ -22,7 +24,9 @@ gt_LOCALE_JA gt_LOCALE_ZH_CN Makefile.am: -TESTS += test-mbstoc32s-1.sh test-mbstoc32s-2.sh test-mbstoc32s-3.sh test-mbstoc32s-4.sh +TESTS += \ + test-mbstoc32s-1.sh test-mbstoc32s-2.sh test-mbstoc32s-3.sh \ + test-mbstoc32s-4.sh test-mbstoc32s-5.sh TESTS_ENVIRONMENT += \ LOCALE_FR='@LOCALE_FR@' \ LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \ diff --git a/tests/test-mbstoc32s-5.sh b/tests/test-mbstoc32s-5.sh new file mode 100755 index 0000000000..edb36432b8 --- /dev/null +++ b/tests/test-mbstoc32s-5.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +# Test whether the POSIX locale has encoding errors. +LC_ALL=C \ +${CHECKER} ./test-mbstoc32s${EXEEXT} 5 || exit 1 +LC_ALL=POSIX \ +${CHECKER} ./test-mbstoc32s${EXEEXT} 5 || exit 1 + +exit 0 diff --git a/tests/test-mbstoc32s.c b/tests/test-mbstoc32s.c index 5358118906..e638999bb6 100644 --- a/tests/test-mbstoc32s.c +++ b/tests/test-mbstoc32s.c @@ -64,6 +64,15 @@ main (int argc, char *argv[]) ASSERT (wc == 0); } +#ifdef __ANDROID__ + /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the + "C" locale. Furthermore, when you attempt to set the "C" or "POSIX" + locale via setlocale(), what you get is a "C" locale with UTF-8 encoding, + that is, effectively the "C.UTF-8" locale. */ + if (argc > 1 && strcmp (argv[1], "5") == 0 && MB_CUR_MAX > 1) + argv[1] = "2"; +#endif + if (argc > 1) { int unlimited; @@ -232,6 +241,63 @@ main (int argc, char *argv[]) } break; + case '5': + /* C or POSIX locale. */ + { + char input[] = "n/a"; + + src = input; + ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 1); + ASSERT (ret == 3); + + src = input; + ret = mbstoc32s (buf, src, unlimited ? BUFSIZE : 1); + ASSERT (ret == (unlimited ? 3 : 1)); + ASSERT (buf[0] == 'n'); + if (unlimited) + { + ASSERT (buf[1] == '/'); + ASSERT (buf[2] == 'a'); + ASSERT (buf[3] == 0); + ASSERT (buf[4] == (char32_t) 0xBADFACE); + } + else + ASSERT (buf[1] == (char32_t) 0xBADFACE); + } + { + int c; + char input[2]; + + for (c = 0; c < 0x100; c++) + if (c != 0) + { + /* We are testing all nonnull bytes. */ + input[0] = c; + input[1] = '\0'; + + src = input; + ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 1); + ASSERT (ret == 1); + + buf[0] = buf[1] = (char32_t) 0xBADFACE; + src = input; + ret = mbstoc32s (buf, src, unlimited ? BUFSIZE : 1); + /* POSIX:2018 says regarding mbstowcs: "In the POSIX locale an + [EILSEQ] error cannot occur since all byte values are valid + characters." It is reasonable to expect mbstoc32s to behave + in the same way. */ + ASSERT (ret == 1); + if (c < 0x80) + /* c is an ASCII character. */ + ASSERT (buf[0] == c); + else + /* On most platforms, the bytes 0x80..0xFF map to U+0080..U+00FF. + But on musl libc, the bytes 0x80..0xFF map to U+DF80..U+DFFF. */ + ASSERT (buf[0] == (btoc32 (c) == 0xDF00 + c ? btoc32 (c) : c)); + } + } + break; + default: return 1; }