1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2026-10-24 00:48:56 +03:00

wctob: Add tests.

* tests/test-wctob.c: New file.
* tests/test-wctob.sh: New file.
* modules/wctob-tests: New file.
This commit is contained in:
Bruno Haible
2026-09-20 17:23:31 +02:00
parent b09ea20030
commit 1052c15fa3
4 changed files with 208 additions and 0 deletions

View File

@@ -1,5 +1,10 @@
2026-09-20 Bruno Haible <bruno@clisp.org>
wctob: Add tests.
* tests/test-wctob.c: New file.
* tests/test-wctob.sh: New file.
* modules/wctob-tests: New file.
wctob: Make consistent with wcrtomb.
* m4/wctob.m4 (gl_FUNC_WCTOB): Require gl_MBRTOWC_C_LOCALE and, if
needed, set REPLACE_WCTOB.

33
modules/wctob-tests Normal file
View File

@@ -0,0 +1,33 @@
Files:
tests/test-wctob.sh
tests/test-wctob.c
tests/signature.h
tests/macros.h
m4/locale-en.m4
m4/locale-fr.m4
m4/locale-ja.m4
m4/locale-zh.m4
m4/codeset.m4
Depends-on:
btowc
setlocale
configure.ac:
gt_LOCALE_EN_UTF8
gt_LOCALE_FR
gt_LOCALE_FR_UTF8
gt_LOCALE_JA
gt_LOCALE_ZH_CN
Makefile.am:
TESTS += \
test-wctob.sh
TESTS_ENVIRONMENT += \
LOCALE_EN_UTF8='@LOCALE_EN_UTF8@' \
LOCALE_FR='@LOCALE_FR@' \
LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \
LOCALE_JA='@LOCALE_JA@' \
LOCALE_ZH_CN='@LOCALE_ZH_CN@'
check_PROGRAMS += test-wctob
test_wctob_LDADD = $(LDADD) $(SETLOCALE_LIB)

121
tests/test-wctob.c Normal file
View File

@@ -0,0 +1,121 @@
/* Test of conversion of wide character to unibyte character.
Copyright (C) 2008-2026 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 <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2026. */
#include <config.h>
#include <wchar.h>
#include "signature.h"
SIGNATURE_CHECK (wctob, int, (wint_t));
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include "macros.h"
/* Check the unibyte character c. */
static void
check_character (unsigned char c)
{
wint_t wc = btowc (c);
ASSERT (wc != WEOF);
int cc = wctob (wc);
ASSERT (cc == c);
}
int
main (int argc, char *argv[])
{
/* configure should already have checked that the locale is supported. */
if (setlocale (LC_ALL, "") == NULL)
return 1;
/* Test NUL character. */
ASSERT (wctob (0) == 0);
/* Test single bytes. */
for (int c = 0; c < 0x100; c++)
switch (c)
{
case '\t': case '\v': case '\f':
case ' ': case '!': case '"': case '#': case '%':
case '&': case '\'': case '(': case ')': case '*':
case '+': case ',': case '-': case '.': case '/':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case ':': case ';': case '<': case '=': case '>':
case '?':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z':
case '[': case '\\': case ']': case '^': case '_':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y':
case 'z': case '{': case '|': case '}': case '~':
/* c is in the ISO C "basic character set". */
check_character (c);
break;
}
if (argc > 1)
switch (argv[1][0])
{
case '1':
/* C locale; tested above. */
#if !defined __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. */
/* Check that wctob does the inverse of btowc, in the C locale.
Above we have only tested the ISO C "basic character set". */
for (int c = 0; c < 0x100; c++)
check_character (c);
#endif
return test_exit_status;
case '2':
/* Locale encoding is ISO-8859-1 or ISO-8859-15. */
return test_exit_status;
case '3':
/* Locale encoding is UTF-8. */
ASSERT (wctob (0x0091) == EOF);
ASSERT (wctob (0x00DF) == EOF);
ASSERT (wctob (0x00FC) == EOF);
ASSERT (wctob (0x1F60B) == EOF);
return test_exit_status;
case '4':
/* Locale encoding is EUC-JP. */
return test_exit_status;
case '5':
/* Locale encoding is GB18030. */
return test_exit_status;
}
return 1;
}

49
tests/test-wctob.sh Executable file
View File

@@ -0,0 +1,49 @@
#!/bin/sh
# Test in the POSIX locale.
LC_ALL=C ${CHECKER} ./test-wctob${EXEEXT} 1 || exit 1
LC_ALL=POSIX ${CHECKER} ./test-wctob${EXEEXT} 1 || exit 1
# Test in an ISO-8859-1 or ISO-8859-15 locale.
: "${LOCALE_FR=fr_FR}"
if test $LOCALE_FR != none; then
LC_ALL=$LOCALE_FR \
${CHECKER} ./test-wctob${EXEEXT} 2 \
|| exit 1
fi
# Test whether a specific UTF-8 locale is installed.
: "${LOCALE_EN_UTF8=en_US.UTF-8}"
: "${LOCALE_FR_UTF8=fr_FR.UTF-8}"
if test "$LOCALE_EN_UTF8" != none || test $LOCALE_FR_UTF8 != none; then
# It's sufficient to test in one of the two locales.
if test $LOCALE_FR_UTF8 != none; then
testlocale=$LOCALE_FR_UTF8
else
testlocale="$LOCALE_EN_UTF8"
fi
LC_ALL="$testlocale" \
${CHECKER} ./test-wctob${EXEEXT} 3 \
|| exit 1
fi
# Test whether a specific EUC-JP locale is installed.
: "${LOCALE_JA=ja_JP}"
if test $LOCALE_JA != none; then
LC_ALL=$LOCALE_JA \
${CHECKER} ./test-wctob${EXEEXT} 4 \
|| exit 1
fi
# Test whether a specific GB18030 locale is installed.
: "${LOCALE_ZH_CN=zh_CN.GB18030}"
if test $LOCALE_ZH_CN != none; then
LC_ALL=$LOCALE_ZH_CN \
${CHECKER} ./test-wctob${EXEEXT} 5
case $? in
0 | 77) ;;
*) exit 1 ;;
esac
fi
exit 0