[3/3] : C++20 P0482R6 and C2X N2653: Tests for mbrtoc8, c8rtomb, char8_t

Message ID 895dc28e-d2d6-6c6a-1637-f84184e84190@honermann.net
State New
Headers show
Series
  • : C++20 P0482R6 and C2X N2653: support for char8_t, mbrtoc8(), and c8rtomb().
Related show

Commit Message

Adhemerval Zanella via Libc-alpha June 7, 2021, 2:08 a.m.
This patch adds tests for the mbrtoc8 and c8rtomb functions adopted for 
C++20 via WG21 P0482R6 [1] and proposed for C2X via WG14 N2653 [2], and 
for the char8_t typedef from WG14 N2653 [2].

The tests for mbrtoc8 and c8rtomb specifically exercise conversion 
from/to Big5-HKSCS because of special cases that arise with that 
encoding.  Big5-HKSCS defines some double byte sequences that convert to 
more than one Unicode code point.  In order to test this, the locale 
dependencies for running tests under wcsmbs is expanded to include 
zh_HK.BIG5-HKSCS.

Tested on Linux x86_64.

Tom.

[1]: WG21 P0482R6
      "char8_t: A type for UTF-8 characters and strings (Revision 6)"
      https://wg21.link/p0482r6

[2]: WG14 N2653
      "char8_t: A type for UTF-8 characters and strings (Revision 1)"
      http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2653.htm

Comments

Joseph Myers June 7, 2021, 7:35 p.m. | #1
New tests should use support/test-driver.c not the older test-skeleton.c 
(see support/README-testing.c) and should preferably use the interfaces in 
support/check.h in place of plain assert.  Very few tests should need to 
define any feature test macros, because they're built with _GNU_SOURCE by 
default.

-- 
Joseph S. Myers
joseph@codesourcery.com
Adhemerval Zanella via Libc-alpha June 11, 2021, 11:25 a.m. | #2
On 6/7/21 3:35 PM, Joseph Myers wrote:
> New tests should use support/test-driver.c not the older test-skeleton.c

> (see support/README-testing.c) and should preferably use the interfaces in

> support/check.h in place of plain assert.  Very few tests should need to

> define any feature test macros, because they're built with _GNU_SOURCE by

> default.

>

Thank you.  I'll correct and provide an updated patch.

Tom.

Patch

commit d3cb93e0dffbd32b24307b416a3f3bf1de23dcde
Author: Tom Honermann <tom@honermann.net>
Date:   Fri Feb 12 23:09:41 2021 -0500

    Tests for mbrtoc8(), c8rtomb(), and the char8_t typedef.
    
    This change adds tests for the mbrtoc8 and c8rtomb functions adopted for
    C++20 via WG21 P0482R6 and proposed for C2X via WG14 N2653, and for the
    char8_t typedef from WG14 N2653.
    
    The tests for mbrtoc8 and c8rtomb specifically exercise conversion
    from/to Big5-HKSCS because of special cases that arise with that encoding.
    Big5-HKSCS defines some double byte sequences that convert to more than
    one Unicode code point.  In order to test this, the locale dependencies
    for running tests under wcsmbs is expanded to include zh_HK.BIG5-HKSCS.

diff --git a/wcsmbs/Makefile b/wcsmbs/Makefile
index e0c8acf591..a0716069ce 100644
--- a/wcsmbs/Makefile
+++ b/wcsmbs/Makefile
@@ -52,13 +52,14 @@  tests := tst-wcstof wcsmbs-tst1 tst-wcsnlen tst-btowc tst-mbrtowc \
 	 tst-c16c32-1 wcsatcliff tst-wcstol-locale tst-wcstod-nan-locale \
 	 tst-wcstod-round test-char-types tst-fgetwc-after-eof \
 	 tst-wcstod-nan-sign tst-c16-surrogate tst-c32-state \
+	 test-char8-type test-mbrtoc8 test-c8rtomb \
 	 $(addprefix test-,$(strop-tests)) tst-mbstowcs
 
 include ../Rules
 
 ifeq ($(run-built-tests),yes)
 LOCALES := de_DE.ISO-8859-1 de_DE.UTF-8 en_US.ANSI_X3.4-1968 hr_HR.ISO-8859-2 \
-	   ja_JP.EUC-JP zh_TW.EUC-TW tr_TR.UTF-8 tr_TR.ISO-8859-9
+	   ja_JP.EUC-JP zh_TW.EUC-TW tr_TR.UTF-8 tr_TR.ISO-8859-9 zh_HK.BIG5-HKSCS
 include ../gen-locales.mk
 
 $(objpfx)tst-btowc.out: $(gen-locales)
diff --git a/wcsmbs/test-c8rtomb.c b/wcsmbs/test-c8rtomb.c
new file mode 100644
index 0000000000..2a65fb8b9c
--- /dev/null
+++ b/wcsmbs/test-c8rtomb.c
@@ -0,0 +1,552 @@ 
+/* Test c8rtomb.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Tom Honermann <tom@honermann.net>, 2020.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* Ensure that char8_t support is enabled so that the char8_t typedef and
+   c8rtomb function are declared.  */
+#define _CHAR8_T_SOURCE
+
+/* We always want assert to be fully defined.  */
+#undef NDEBUG
+
+#include <features.h>
+#include <assert.h>
+#include <errno.h>
+#include <limits.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <uchar.h>
+#include <wchar.h>
+
+static int
+test_truncated_code_unit_sequence (void)
+{
+  const char8_t *u8s;
+  char buf[MB_LEN_MAX];
+  mbstate_t s;
+
+  /* Missing trailing code unit for a two code byte unit sequence.  */
+  u8s = (const char8_t*) u8"\xC2";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* No trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Missing first trailing code unit for a three byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xE0";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* No trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Missing second trailing code unit for a three byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xE0\xA0";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t)  0); /* 2nd byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) -1); /* No trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Missing first trailing code unit for a four byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xF0";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* No trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Missing second trailing code unit for a four byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xF0\x90";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t)  0); /* 2nd byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) -1); /* No trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Missing third trailing code unit for a four byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xF0\x90\x80";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t)  0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t)  0); /* 3rd byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[3], &s) == (size_t) -1); /* No trailing code unit */
+  assert (errno == EILSEQ);
+
+  return 0;
+}
+
+static int
+test_invalid_trailing_code_unit_sequence (void)
+{
+  const char8_t *u8s;
+  char buf[MB_LEN_MAX];
+  mbstate_t s;
+
+  /* Invalid trailing code unit for a two code byte unit sequence.  */
+  u8s = (const char8_t*) u8"\xC2\xC0";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Invalid first trailing code unit for a three byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xE0\xC0";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Invalid second trailing code unit for a three byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xE0\xA0\xC0";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t)  0); /* 2nd byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Invalid first trailing code unit for a four byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xF0\xC0";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Invalid second trailing code unit for a four byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xF0\x90\xC0";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t)  0); /* 2nd byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Invalid third trailing code unit for a four byte code unit sequence.  */
+  u8s = (const char8_t*) u8"\xF0\x90\x80\xC0";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t)  0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t)  0); /* 3rd byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[3], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  return 0;
+}
+
+static int
+test_lone_trailing_code_units (void)
+{
+  const char8_t *u8s;
+  char buf[MB_LEN_MAX];
+  mbstate_t s;
+
+  /* Lone trailing code unit.  */
+  u8s = (const char8_t*) u8"\x80";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  errno = 0;
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) -1); /* Lone trailing code unit */
+  assert (errno == EILSEQ);
+
+  return 0;
+}
+
+static int
+test_overlong_encoding (void)
+{
+  const char8_t *u8s;
+  char buf[MB_LEN_MAX];
+  mbstate_t s;
+
+  /* Two byte overlong encoding.  */
+  u8s = (const char8_t*) u8"\xC0\x80";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  errno = 0;
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) -1); /* Invalid lead code unit */
+  assert (errno == EILSEQ);
+
+  /* Two byte overlong encoding.  */
+  u8s = (const char8_t*) u8"\xC1\x80";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  errno = 0;
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) -1); /* Invalid lead code unit */
+  assert (errno == EILSEQ);
+
+  /* Three byte overlong encoding.  */
+  u8s = (const char8_t*) u8"\xE0\x9F\xBF";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* First byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Four byte overlong encoding.  */
+  u8s = (const char8_t*) u8"\xF0\x8F\xBF\xBF";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* First byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  return 0;
+}
+
+static int
+test_surrogate_range (void)
+{
+  const char8_t *u8s;
+  char buf[MB_LEN_MAX];
+  mbstate_t s;
+
+  /* Would encode U+D800.  */
+  u8s = (const char8_t*) u8"\xED\xA0\x80";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* First byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Would encode U+DFFF.  */
+  u8s = (const char8_t*) u8"\xED\xBF\xBF";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* First byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  return 0;
+}
+
+static int
+test_out_of_range_encoding (void)
+{
+  const char8_t *u8s;
+  char buf[MB_LEN_MAX];
+  mbstate_t s;
+
+  /* Would encode U+00110000.  */
+  u8s = (const char8_t*) u8"\xF4\x90\x80\x80";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t)  0); /* First byte processed */
+  errno = 0;
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) -1); /* Invalid trailing code unit */
+  assert (errno == EILSEQ);
+
+  /* Would encode U+00140000.  */
+  u8s = (const char8_t*) u8"\xF5\x90\x80\x80";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  errno = 0;
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) -1); /* Invalid lead code unit */
+  assert (errno == EILSEQ);
+
+  return 0;
+}
+
+static int
+test_invalid_utf8 (void)
+{
+  int result = 0;
+
+  result |= test_truncated_code_unit_sequence ();
+  result |= test_invalid_trailing_code_unit_sequence ();
+  result |= test_lone_trailing_code_units ();
+  result |= test_overlong_encoding ();
+  result |= test_surrogate_range ();
+  result |= test_out_of_range_encoding ();
+
+  return result;
+}
+
+static int
+test_null_output_buffer (void)
+{
+  char buf[MB_LEN_MAX];
+  mbstate_t s;
+
+  /* Null character with an initial state.  */
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (NULL, u8"X"[0], &s) == (size_t) 1); /* null byte processed */
+  assert (mbsinit (&s));    /* Assert the state is now an initial state.  */
+
+  /* Null buffer with a state corresponding to an incompletely read code
+     unit sequence.  In this case, an error occurs since insufficient
+     information is available to complete the already started code unit
+     sequence and return to the initial state.  */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8"\xC2"[0], &s) == (size_t)  0);  /* 1st byte processed */
+  errno = 0;
+  assert (c8rtomb (NULL, u8"\x80"[0], &s) == (size_t) -1); /* No trailing code unit */
+  assert (errno == EILSEQ);
+
+  return 0;
+}
+
+static int
+test_utf8 (void)
+{
+  const char *locale = "de_DE.UTF-8";
+  const char8_t *u8s;
+  char buf[MB_LEN_MAX];
+  mbstate_t s;
+
+  if (!setlocale (LC_ALL, locale))
+    {
+      fprintf (stderr, "locale '%s' not available!\n", locale);
+      exit (1);
+    }
+
+  /* Null character.  */
+  u8s = (const char8_t*) u8"\x00"; /* U+0000 => 0x00 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 1); /* 1st byte processed */
+  assert (buf[0] == (char) 0x00);
+  assert (mbsinit (&s));
+
+  /* First non-null character in the code point range that maps to a single
+     code unit.  */
+  u8s = (const char8_t*) u8"\x01"; /* U+0001 => 0x01 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 1); /* 1st byte processed */
+  assert (buf[0] == (char) 0x01);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to a single code unit.  */
+  u8s = (const char8_t*) u8"\x7F"; /* U+007F => 0x7F */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 1); /* 1st byte processed */
+  assert (buf[0] == (char) 0x7F);
+  assert (mbsinit (&s));
+
+  /* First character in the code point range that maps to two code units.  */
+  u8s = (const char8_t*) u8"\xC2\x80"; /* U+0080 => 0xC2 0x80 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 2); /* 2nd byte processed */
+  assert (buf[0] == (char) 0xC2);
+  assert (buf[1] == (char) 0x80);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to two code units.  */
+  u8s = (const char8_t*) u8"\u07FF"; /* U+07FF => 0xDF 0xBF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 2); /* 2nd byte processed */
+  assert (buf[0] == (char) 0xDF);
+  assert (buf[1] == (char) 0xBF);
+  assert (mbsinit (&s));
+
+  /* First character in the code point range that maps to three code units.  */
+  u8s = (const char8_t*) u8"\u0800"; /* U+0800 => 0xE0 0xA0 0x80 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 3); /* 3rd byte processed */
+  assert (buf[0] == (char) 0xE0);
+  assert (buf[1] == (char) 0xA0);
+  assert (buf[2] == (char) 0x80);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to three code units
+     before the surrogate code point range.  */
+  u8s = (const char8_t*) u8"\uD7FF"; /* U+D7FF => 0xED 0x9F 0xBF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 3); /* 3rd byte processed */
+  assert (buf[0] == (char) 0xED);
+  assert (buf[1] == (char) 0x9F);
+  assert (buf[2] == (char) 0xBF);
+  assert (mbsinit (&s));
+
+  /* First character in the code point range that maps to three code units
+     after the surrogate code point range.  */
+  u8s = (const char8_t*) u8"\uE000"; /* U+E000 => 0xEE 0x80 0x80 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 3); /* 3rd byte processed */
+  assert (buf[0] == (char) 0xEE);
+  assert (buf[1] == (char) 0x80);
+  assert (buf[2] == (char) 0x80);
+  assert (mbsinit (&s));
+
+  /* Not a BOM.  */
+  u8s = (const char8_t*) u8"\uFEFF"; /* U+FEFF => 0xEF 0xBB 0xBF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 3); /* 3rd byte processed */
+  assert (buf[0] == (char) 0xEF);
+  assert (buf[1] == (char) 0xBB);
+  assert (buf[2] == (char) 0xBF);
+  assert (mbsinit (&s));
+
+  /* Replacement character.  */
+  u8s = (const char8_t*) u8"\uFFFD"; /* U+FFFD => 0xEF 0xBF 0xBD */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 3); /* 3rd byte processed */
+  assert (buf[0] == (char) 0xEF);
+  assert (buf[1] == (char) 0xBF);
+  assert (buf[2] == (char) 0xBD);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to three code units.  */
+  u8s = (const char8_t*) u8"\uFFFF"; /* U+FFFF => 0xEF 0xBF 0xBF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 3); /* 3rd byte processed */
+  assert (buf[0] == (char) 0xEF);
+  assert (buf[1] == (char) 0xBF);
+  assert (buf[2] == (char) 0xBF);
+  assert (mbsinit (&s));
+
+  /* First character in the code point range that maps to four code units.  */
+  u8s = (const char8_t*) u8"\U00010000"; /* U+10000 => 0xF0 0x90 0x80 0x80 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 0); /* 3rd byte processed */
+  assert (c8rtomb (buf, u8s[3], &s) == (size_t) 4); /* 4th byte processed */
+  assert (buf[0] == (char) 0xF0);
+  assert (buf[1] == (char) 0x90);
+  assert (buf[2] == (char) 0x80);
+  assert (buf[3] == (char) 0x80);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to four code units.  */
+  u8s = (const char8_t*) u8"\U0010FFFF"; /* U+10FFFF => 0xF4 0x8F 0xBF 0xBF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 0); /* 3rd byte processed */
+  assert (c8rtomb (buf, u8s[3], &s) == (size_t) 4); /* 4th byte processed */
+  assert (buf[0] == (char) 0xF4);
+  assert (buf[1] == (char) 0x8F);
+  assert (buf[2] == (char) 0xBF);
+  assert (buf[3] == (char) 0xBF);
+  assert (mbsinit (&s));
+
+  return 0;
+}
+
+static int
+test_big5_hkscs (void)
+{
+  const char *locale = "zh_HK.BIG5-HKSCS";
+  const char8_t *u8s;
+  char buf[MB_LEN_MAX];
+  mbstate_t s;
+
+  if (!setlocale (LC_ALL, locale))
+    {
+      fprintf (stderr, "locale '%s' not available!\n", locale);
+      exit (1);
+    }
+
+  /* A pair of two byte UTF-8 code unit sequences that map a Unicode code
+     point and combining character to a single double byte character.  */
+  u8s = (const char8_t*) u8"\u00CA\u0304"; /* U+00CA U+0304 => 0x88 0x62 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 0); /* 3rd byte processed */
+  assert (c8rtomb (buf, u8s[3], &s) == (size_t) 2); /* 4th byte processed */
+  assert (buf[0] == (char) 0x88);
+  assert (buf[1] == (char) 0x62);
+  assert (mbsinit (&s));
+
+  /* Another pair of two byte UTF-8 code unit sequences that map a Unicode code
+     point and combining character to a single double byte character.  */
+  u8s = (const char8_t*) u8"\u00EA\u030C"; /* U+00EA U+030C => 0x88 0xA5 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (c8rtomb (buf, u8s[0], &s) == (size_t) 0); /* 1st byte processed */
+  assert (c8rtomb (buf, u8s[1], &s) == (size_t) 0); /* 2nd byte processed */
+  assert (c8rtomb (buf, u8s[2], &s) == (size_t) 0); /* 3rd byte processed */
+  assert (c8rtomb (buf, u8s[3], &s) == (size_t) 2); /* 4th byte processed */
+  assert (buf[0] == (char) 0x88);
+  assert (buf[1] == (char) 0xA5);
+  assert (mbsinit (&s));
+
+  return 0;
+}
+
+static int
+do_test (void)
+{
+  int result = 0;
+
+  result |= test_invalid_utf8 ();
+  result |= test_null_output_buffer ();
+  result |= test_utf8 ();
+  result |= test_big5_hkscs ();
+
+  return result;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/wcsmbs/test-char8-type.c b/wcsmbs/test-char8-type.c
new file mode 100644
index 0000000000..220dea787e
--- /dev/null
+++ b/wcsmbs/test-char8-type.c
@@ -0,0 +1,36 @@ 
+/* Test char8_t types consistent with compiler.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* Ensure that char8_t support is enabled so that the char8_t typedef is
+   declared.  */
+#define _CHAR8_T_SOURCE
+
+#include <features.h>
+#include <uchar.h>
+
+/* Verify that the char8_t type is recognized.  */
+char8_t c8;
+
+static int
+do_test (void)
+{
+  /* This is a compilation test.  */
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/wcsmbs/test-mbrtoc8.c b/wcsmbs/test-mbrtoc8.c
new file mode 100644
index 0000000000..7bacf48166
--- /dev/null
+++ b/wcsmbs/test-mbrtoc8.c
@@ -0,0 +1,485 @@ 
+/* Test mbrtoc8.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Tom Honermann <tom@honermann.net>, 2020.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* Ensure that char8_t support is enabled so that the char8_t typedef and
+   mbrtoc8 function are declared.  */
+#define _CHAR8_T_SOURCE
+
+/* We always want assert to be fully defined.  */
+#undef NDEBUG
+
+#include <features.h>
+#include <assert.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <uchar.h>
+#include <wchar.h>
+
+static int
+test_utf8 (void)
+{
+  const char *locale = "de_DE.UTF-8";
+  const char *mbs;
+  char8_t buf[1];
+  mbstate_t s;
+
+  if (!setlocale (LC_ALL, locale))
+    {
+      fprintf (stderr, "locale '%s' not available!\n", locale);
+      exit (1);
+    }
+
+  /* No inputs.  */
+  mbs = "";
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 0, &s) == (size_t) -2); /* no input */
+  assert (mbsinit (&s));
+
+  /* Null character.  */
+  mbs = "\x00"; /* 0x00 => U+0000 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 0); /* null byte written */
+  mbs += 1;
+  assert (buf[0] == 0x00);
+  assert (mbsinit (&s));
+
+  /* First non-null character in the code point range that maps to a single
+     code unit.  */
+  mbs = "\x01"; /* 0x01 => U+0001 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 1); /* 1st byte processed */
+  mbs += 1;
+  assert (buf[0] == 0x01);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to a single code unit.  */
+  mbs = "\x7F"; /* 0x7F => U+007F */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 1);  /* 1st byte processed */
+  mbs += 1;
+  assert (buf[0] == 0x7F);
+  assert (mbsinit (&s));
+
+  /* First character in the code point range that maps to two code units.  */
+  mbs = "\xC2\x80"; /* 0xC2 0x80 => U+0080 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 2);  /* 1st byte written */
+  mbs += 2;
+  assert (buf[0] == 0xC2);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\xC2\x80"; /* 0xC2 0x80 => U+0080 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xC2);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to two code units.  */
+  mbs = "\xDF\xBF"; /* 0xDF 0xBF => U+07FF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 2);  /* 1st byte written */
+  mbs += 2;
+  assert (buf[0] == 0xDF);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\xDF\xBF"; /* 0xDF 0xBF => U+07FF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xDF);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  /* First character in the code point range that maps to three code units.  */
+  mbs = u8"\xE0\xA0\x80"; /* 0xE0 0xA0 0x80 => U+0800 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 3);  /* 1st byte written */
+  mbs += 3;
+  assert (buf[0] == 0xE0);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0xA0);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = u8"\xE0\xA0\x80"; /* 0xE0 0xA0 0x80 => U+0800 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xE0);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0xA0);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to three code units
+     before the surrogate code point range.  */
+  mbs = "\xED\x9F\xBF"; /* 0xED 0x9F 0xBF => U+D7FF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 3);  /* 1st byte written */
+  mbs += 3;
+  assert (buf[0] == 0xED);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0x9F);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\xED\x9F\xBF"; /* 0xED 0x9F 0xBF => U+D7FF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xED);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0x9F);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  /* First character in the code point range that maps to three code units
+     after the surrogate code point range.  */
+  mbs = "\xEE\x80\x80"; /* 0xEE 0x80 0x80 => U+E000 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 3);  /* 1st byte written */
+  mbs += 3;
+  assert (buf[0] == 0xEE);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\xEE\x80\x80"; /* 0xEE 0x80 0x80 => U+E000 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xEE);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbsinit (&s));
+
+  /* Not a BOM.  */
+  mbs = "\xEF\xBB\xBF"; /* 0xEF 0xBB 0xBF => U+FEFF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 3);  /* 1st byte written */
+  mbs += 3;
+  assert (buf[0] == 0xEF);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0xBB);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\xEF\xBB\xBF"; /* 0xEF 0xBB 0xBF => U+FEFF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xEF);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0xBB);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  /* Replacement character.  */
+  mbs = "\xEF\xBF\xBD"; /* 0xEF 0xBF 0xBD => U+FFFD */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 3);  /* 1st byte written */
+  mbs += 3;
+  assert (buf[0] == 0xEF);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte written */
+  assert (buf[0] == 0xBD);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\xEF\xBF\xBD"; /* 0xEF 0xBF 0xBD => U+FFFD */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xEF);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0xBD);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to three code units.  */
+  mbs = "\xEF\xBF\xBF"; /* 0xEF 0xBF 0xBF => U+FFFF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 3);  /* 1st byte processed */
+  mbs += 3;
+  assert (buf[0] == 0xEF);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte processed */
+  assert (buf[0] == 0xBF);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte processed */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\xEF\xBF\xBF"; /* 0xEF 0xBF 0xBF => U+FFFF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xEF);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  /* First character in the code point range that maps to four code units.  */
+  mbs = "\xF0\x90\x80\x80"; /* 0xF0 0x90 0x80 0x80 => U+10000 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 4);  /* 1st byte written */
+  mbs += 4;
+  assert (buf[0] == 0xF0);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0x90);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 4th byte written */
+  assert (buf[0] == 0x80);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\xF0\x90\x80\x80"; /* 0xF0 0x90 0x80 0x80 => U+10000 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xF0);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0x90);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0x80);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 4th byte written */
+  assert (buf[0] == 0x80);
+  assert (mbsinit (&s));
+
+  /* Last character in the code point range that maps to four code units.  */
+  mbs = "\xF4\x8F\xBF\xBF"; /* 0xF4 0x8F 0xBF 0xBF => U+10FFFF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 4);  /* 1st byte written */
+  mbs += 4;
+  assert (buf[0] == 0xF4);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0x8F);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 4th byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\xF4\x8F\xBF\xBF"; /* 0xF4 0x8F 0xBF 0xBF => U+10FFFF */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xF4);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0x8F);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 4th byte written */
+  assert (buf[0] == 0xBF);
+  assert (mbsinit (&s));
+
+  return 0;
+}
+
+static int
+test_big5_hkscs (void)
+{
+  const char *locale = "zh_HK.BIG5-HKSCS";
+  const char *mbs;
+  char8_t buf[1];
+  mbstate_t s;
+
+  if (!setlocale (LC_ALL, locale))
+    {
+      fprintf (stderr, "locale '%s' not available!\n", locale);
+      exit (1);
+    }
+
+  /* A double byte character that maps to a pair of two byte UTF-8 code unit
+     sequences.  */
+  mbs = "\x88\x62"; /* 0x88 0x62 => U+00CA U+0304 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 2);  /* 1st byte written */
+  mbs += 2;
+  assert (buf[0] == 0xC3);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0x8A);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte written */
+  assert (buf[0] == 0xCC);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 4th byte written */
+  assert (buf[0] == 0x84);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\x88\x62"; /* 0x88 0x62 => U+00CA U+0304 */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xC3);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0x8A);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0xCC);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 4th byte written */
+  assert (buf[0] == 0x84);
+  assert (mbsinit (&s));
+
+  /* Another double byte character that maps to a pair of two byte UTF-8 code
+     unit sequences.  */
+  mbs = "\x88\xA5"; /* 0x88 0xA5 => U+00EA U+030C */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) 2);  /* 1st byte written */
+  mbs += 2;
+  assert (buf[0] == 0xC3);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 2nd byte written */
+  assert (buf[0] == 0xAA);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 3rd byte written */
+  assert (buf[0] == 0xCC);
+  assert (mbrtoc8 (buf, mbs, strlen(mbs)+1, &s) == (size_t) -3); /* 4th byte written */
+  assert (buf[0] == 0x8C);
+  assert (mbsinit (&s));
+
+  /* Same as last test, but one code unit at a time.  */
+  mbs = "\x88\xA5"; /* 0x88 0xA5 => U+00EA U+030C */
+  memset (buf, 0, sizeof (buf));
+  memset (&s, 0, sizeof (s));
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -2);             /* incomplete */
+  mbs += 1;
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) 1);              /* 1st byte written */
+  mbs += 1;
+  assert (buf[0] == 0xC3);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 2nd byte written */
+  assert (buf[0] == 0xAA);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 3rd byte written */
+  assert (buf[0] == 0xCC);
+  assert (mbrtoc8 (buf, mbs, 1, &s) == (size_t) -3);             /* 4th byte written */
+  assert (buf[0] == 0x8C);
+  assert (mbsinit (&s));
+
+  return 0;
+}
+
+static int
+do_test (void)
+{
+  int result = 0;
+
+  result |= test_utf8 ();
+  result |= test_big5_hkscs ();
+
+  return result;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"