]> Savannah Git Hosting - gnulib.git/commitdiff
mbsrtoc32s tests: Check behaviour in the C locale.
authorBruno Haible <bruno@clisp.org>
Mon, 3 Apr 2023 12:24:45 +0000 (14:24 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 3 Apr 2023 12:24:45 +0000 (14:24 +0200)
* tests/test-mbsrtoc32s.c (main): Test behaviour in the C locale. Based
on tests/test-mbsrtowcs.c.
* tests/test-mbsrtoc32s-5.sh: New file, based on
tests/test-mbsrtowcs5.sh.
* modules/mbsrtoc32s-tests (Files): Add it.
(Depends-on): Add btoc32.
(Makefile.am): Run test-mbsrtoc32s-5.sh.

ChangeLog
modules/mbsrtoc32s-tests
tests/test-mbsrtoc32s-5.sh [new file with mode: 0755]
tests/test-mbsrtoc32s.c

index b05eb8f8467e58128f49d80370cd65cad77bfb15..7fbb423791495fcd68aa3163d10f4a1ce5057e55 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2023-04-03  Bruno Haible  <bruno@clisp.org>
+
+       mbsrtoc32s tests: Check behaviour in the C locale.
+       * tests/test-mbsrtoc32s.c (main): Test behaviour in the C locale. Based
+       on tests/test-mbsrtowcs.c.
+       * tests/test-mbsrtoc32s-5.sh: New file, based on
+       tests/test-mbsrtowcs5.sh.
+       * modules/mbsrtoc32s-tests (Files): Add it.
+       (Depends-on): Add btoc32.
+       (Makefile.am): Run test-mbsrtoc32s-5.sh.
+
 2023-04-03  Bruno Haible  <bruno@clisp.org>
 
        btoc32 tests: Check behaviour in the C locale.
index f794c5ae3230339ea096dd27d2592764187d4982..0adc62368ed7e820fa2eb3e438046abbe0e202df 100644 (file)
@@ -3,6 +3,7 @@ tests/test-mbsrtoc32s-1.sh
 tests/test-mbsrtoc32s-2.sh
 tests/test-mbsrtoc32s-3.sh
 tests/test-mbsrtoc32s-4.sh
+tests/test-mbsrtoc32s-5.sh
 tests/test-mbsrtoc32s.c
 tests/signature.h
 tests/macros.h
@@ -14,6 +15,7 @@ m4/codeset.m4
 Depends-on:
 mbrtoc32
 mbsinit
+btoc32
 c32tob
 setlocale
 
@@ -24,7 +26,9 @@ gt_LOCALE_JA
 gt_LOCALE_ZH_CN
 
 Makefile.am:
-TESTS += test-mbsrtoc32s-1.sh test-mbsrtoc32s-2.sh test-mbsrtoc32s-3.sh test-mbsrtoc32s-4.sh
+TESTS += \
+  test-mbsrtoc32s-1.sh test-mbsrtoc32s-2.sh test-mbsrtoc32s-3.sh \
+  test-mbsrtoc32s-4.sh test-mbsrtoc32s-5.sh
 TESTS_ENVIRONMENT += \
   LOCALE_FR='@LOCALE_FR@' \
   LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \
diff --git a/tests/test-mbsrtoc32s-5.sh b/tests/test-mbsrtoc32s-5.sh
new file mode 100755 (executable)
index 0000000..fb922b2
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+# Test whether the POSIX locale has encoding errors.
+LC_ALL=C \
+${CHECKER} ./test-mbsrtoc32s${EXEEXT} 5 || exit 1
+LC_ALL=POSIX \
+${CHECKER} ./test-mbsrtoc32s${EXEEXT} 5 || exit 1
+
+exit 0
index ef985fdc03ce31732a12d282c6bb4c9f49496d0d..aed9d7b2fbffed55b9d6db3e3185ddafc037897a 100644 (file)
@@ -72,6 +72,15 @@ main (int argc, char *argv[])
     ASSERT (mbsinit (&state));
   }
 
+#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;
@@ -281,6 +290,74 @@ main (int argc, char *argv[])
               }
               break;
 
+            case '5':
+              /* C or POSIX locale.  */
+              {
+                char input[] = "n/a";
+                memset (&state, '\0', sizeof (mbstate_t));
+
+                src = input;
+                temp_state = state;
+                ret = mbsrtoc32s (NULL, &src, unlimited ? BUFSIZE : 1, &temp_state);
+                ASSERT (ret == 3);
+                ASSERT (src == input);
+                ASSERT (mbsinit (&state));
+
+                src = input;
+                ret = mbsrtoc32s (buf, &src, unlimited ? BUFSIZE : 1, &state);
+                ASSERT (ret == (unlimited ? 3 : 1));
+                ASSERT (src == (unlimited ? NULL : input + 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);
+                ASSERT (mbsinit (&state));
+              }
+              {
+                int c;
+                char input[2];
+
+                memset (&state, '\0', sizeof (mbstate_t));
+                for (c = 0; c < 0x100; c++)
+                  if (c != 0)
+                    {
+                      /* We are testing all nonnull bytes.  */
+                      input[0] = c;
+                      input[1] = '\0';
+
+                      src = input;
+                      ret = mbsrtoc32s (NULL, &src, unlimited ? BUFSIZE : 1, &state);
+                      ASSERT (ret == 1);
+                      ASSERT (src == input);
+                      ASSERT (mbsinit (&state));
+
+                      buf[0] = buf[1] = (char32_t) 0xBADFACE;
+                      src = input;
+                      ret = mbsrtoc32s (buf, &src, unlimited ? BUFSIZE : 1, &state);
+                      /* POSIX:2018 says regarding mbsrtowcs: "In the POSIX locale an
+                         [EILSEQ] error cannot occur since all byte values are valid
+                         characters."  It is reasonable to expect mbsrtoc32s to behave
+                         in the same way.  */
+                      ASSERT (ret == 1);
+                      ASSERT (src == (unlimited ? NULL : input + 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));
+                      ASSERT (mbsinit (&state));
+                    }
+              }
+              break;
+
             default:
               return 1;
             }