From: Bruno Haible Date: Fri, 27 Feb 2026 05:41:31 +0000 (+0100) Subject: mbs_startswith: Add tests. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3574d0a5262e59fa63547b0b29eb7b02b2ff145f;p=thirdparty%2Fgnulib.git mbs_startswith: Add tests. * tests/test-mbs_startswith1.c: New file, based on tests/test-mbs_endswith1.c. * tests/test-mbs_startswith2.c: New file, based on tests/test-mbs_endswith2.c. * tests/test-mbs_startswith2.sh: New file, based on tests/test-mbs_endswith2.sh. * tests/test-mbs_startswith3.c: New file, based on tests/test-mbs_endswith3.c. * tests/test-mbs_startswith3.sh: New file, based on tests/test-mbs_endswith3.sh. * modules/mbs_startswith-tests: New file. --- diff --git a/ChangeLog b/ChangeLog index 6b2d4d01cf..f31e19cb4b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,18 @@ 2026-02-27 Bruno Haible + mbs_startswith: Add tests. + * tests/test-mbs_startswith1.c: New file, based on + tests/test-mbs_endswith1.c. + * tests/test-mbs_startswith2.c: New file, based on + tests/test-mbs_endswith2.c. + * tests/test-mbs_startswith2.sh: New file, based on + tests/test-mbs_endswith2.sh. + * tests/test-mbs_startswith3.c: New file, based on + tests/test-mbs_endswith3.c. + * tests/test-mbs_startswith3.sh: New file, based on + tests/test-mbs_endswith3.sh. + * modules/mbs_startswith-tests: New file. + mbs_startswith: Fix handling of incomplete characters. * lib/string.in.h (mbs_startswith): Remove macro definition. * lib/mbs_startswith.c: New file. diff --git a/modules/mbs_startswith-tests b/modules/mbs_startswith-tests new file mode 100644 index 0000000000..ac922ed450 --- /dev/null +++ b/modules/mbs_startswith-tests @@ -0,0 +1,30 @@ +Files: +tests/test-mbs_startswith1.c +tests/test-mbs_startswith2.sh +tests/test-mbs_startswith2.c +tests/test-mbs_startswith3.sh +tests/test-mbs_startswith3.c +tests/macros.h +m4/locale-en.m4 +m4/locale-fr.m4 +m4/locale-zh.m4 +m4/codeset.m4 + +Depends-on: +setlocale + +configure.ac: +gt_LOCALE_EN_UTF8 +gt_LOCALE_FR_UTF8 +gt_LOCALE_ZH_CN + +Makefile.am: +TESTS += test-mbs_startswith1 test-mbs_startswith2.sh test-mbs_startswith3.sh +TESTS_ENVIRONMENT += \ + LOCALE_EN_UTF8='@LOCALE_EN_UTF8@' \ + LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \ + LOCALE_ZH_CN='@LOCALE_ZH_CN@' +check_PROGRAMS += test-mbs_startswith1 test-mbs_startswith2 test-mbs_startswith3 +test_mbs_startswith1_LDADD = $(LDADD) $(LIBUNISTRING) $(MBRTOWC_LIB) $(LIBC32CONV) +test_mbs_startswith2_LDADD = $(LDADD) $(LIBUNISTRING) $(SETLOCALE_LIB) $(MBRTOWC_LIB) $(LIBC32CONV) +test_mbs_startswith3_LDADD = $(LDADD) $(LIBUNISTRING) $(SETLOCALE_LIB) $(MBRTOWC_LIB) $(LIBC32CONV) diff --git a/tests/test-mbs_startswith1.c b/tests/test-mbs_startswith1.c new file mode 100644 index 0000000000..a1b89fa4a5 --- /dev/null +++ b/tests/test-mbs_startswith1.c @@ -0,0 +1,48 @@ +/* Test of mbs_startswith() function. + Copyright (C) 2025-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 . */ + +/* Written by Bruno Haible , 2026. */ + +#include + +#include + +#include + +#include "macros.h" + +int +main () +{ + /* This test is executed in the C locale. */ + + ASSERT (mbs_startswith ("", "")); + ASSERT (mbs_startswith ("abc", "")); + + ASSERT (!mbs_startswith ("", "a")); + ASSERT (!mbs_startswith ("x", "a")); + ASSERT (mbs_startswith ("a", "a")); + ASSERT (mbs_startswith ("abc", "a")); + + ASSERT (!mbs_startswith ("", "xyz")); + ASSERT (!mbs_startswith ("x", "xyz")); + ASSERT (!mbs_startswith ("a", "xyz")); + ASSERT (!mbs_startswith ("abc", "xyz")); + ASSERT (mbs_startswith ("xyz", "xyz")); + ASSERT (mbs_startswith ("xyzzy", "xyz")); + + return test_exit_status; +} diff --git a/tests/test-mbs_startswith2.c b/tests/test-mbs_startswith2.c new file mode 100644 index 0000000000..38c53dd12b --- /dev/null +++ b/tests/test-mbs_startswith2.c @@ -0,0 +1,95 @@ +/* Test of mbs_startswith() function. + Copyright (C) 2025-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 . */ + +/* Written by Bruno Haible , 2026. */ + +#include + +#include + +#include +#include + +#include "macros.h" + +int +main () +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + ASSERT (mbs_startswith ("", "")); + ASSERT (mbs_startswith ("abc", "")); + + ASSERT (!mbs_startswith ("", "a")); + ASSERT (!mbs_startswith ("x", "a")); + ASSERT (mbs_startswith ("a", "a")); + ASSERT (mbs_startswith ("abc", "a")); + + ASSERT (!mbs_startswith ("", "xyz")); + ASSERT (!mbs_startswith ("x", "xyz")); + ASSERT (!mbs_startswith ("a", "xyz")); + ASSERT (!mbs_startswith ("abc", "xyz")); + ASSERT (mbs_startswith ("xyz", "xyz")); + ASSERT (mbs_startswith ("xyzzy", "xyz")); + + ASSERT (mbs_startswith ("", "")); + ASSERT (mbs_startswith ("\303\244\306\200\303\247", "")); /* "äƀç" */ + + ASSERT (!mbs_startswith ("", "\303\244")); /* "ä" */ + ASSERT (!mbs_startswith ("\341\272\213", "\303\244")); /* "ẋ" "ä" */ + ASSERT (mbs_startswith ("\303\244", "\303\244")); /* "ä" "ä" */ + ASSERT (mbs_startswith ("\303\244\306\200\303\247", "\303\244")); /* "äƀç" "ä" */ + /* Test a prefix that ends in an incomplete character. */ + ASSERT (!mbs_startswith ("\303\244\306\200\303\247", "\303")); /* "äƀç" */ + + ASSERT (!mbs_startswith ("", "\341\272\213\303\277\341\272\221")); /* "ẋÿẑ" */ + ASSERT (!mbs_startswith ("\341\272\213", "\341\272\213\303\277\341\272\221")); /* "ẋ" "ẋÿẑ" */ + ASSERT (!mbs_startswith ("\303\244", "\341\272\213\303\277\341\272\221")); /* "ä" "ẋÿẑ" */ + ASSERT (!mbs_startswith ("\303\244\306\200\303\247", "\341\272\213\303\277\341\272\221")); /* "äƀç" "ẋÿẑ" */ + ASSERT (mbs_startswith ("\341\272\213\303\277\341\272\221", "\341\272\213\303\277\341\272\221")); /* "ẋÿẑ" "ẋÿẑ" */ + ASSERT (mbs_startswith ("\341\272\213\303\277\341\272\221\341\272\221\303\277", "\341\272\213\303\277\341\272\221")); /* "ẋÿẑẑÿ" "ẋÿẑ" */ + /* Test a prefix that ends in an incomplete character. */ + ASSERT (!mbs_startswith ("\341\272\213\303\277\341\272\221\341\272\221\303\277", "\341\272")); /* "ẋÿẑẑÿ" */ + ASSERT (!mbs_startswith ("\341\272\213\303\277\341\272\221\341\272\221\303\277", "\341")); /* "ẋÿẑẑÿ" */ + + /* Test cases with invalid or incomplete characters. */ + + /* A valid character should not match an invalid character. */ + ASSERT (!mbs_startswith ("\303\247", "\301\247")); + ASSERT (!mbs_startswith ("\301\247", "\303\247")); + + /* A valid character should not match an incomplete character. */ + ASSERT (!mbs_startswith ("\303\247", "\343\247")); + ASSERT (!mbs_startswith ("\343\247", "\303\247")); + + /* An invalid character should not match an incomplete character. */ + ASSERT (!mbs_startswith ("\301\247", "\343\247")); + ASSERT (!mbs_startswith ("\343\247", "\301\247")); + + /* Two invalid characters should match only if they are identical. */ + ASSERT (!mbs_startswith ("\301\246", "\301\247")); + ASSERT (!mbs_startswith ("\301\247", "\301\246")); + ASSERT (mbs_startswith ("\301\247", "\301\247")); + + /* Two incomplete characters should match only if they are identical. */ + ASSERT (!mbs_startswith ("\343\246", "\343\247")); + ASSERT (!mbs_startswith ("\343\247", "\343\246")); + ASSERT (mbs_startswith ("\343\247", "\343\247")); + + return test_exit_status; +} diff --git a/tests/test-mbs_startswith2.sh b/tests/test-mbs_startswith2.sh new file mode 100755 index 0000000000..8ff20abefb --- /dev/null +++ b/tests/test-mbs_startswith2.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +# 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 + if test -f /usr/bin/localedef; then + echo "Skipping test: no english or french Unicode locale is installed" + else + echo "Skipping test: no english or french Unicode locale is supported" + fi + exit 77 +fi + +# 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-mbs_startswith2${EXEEXT} diff --git a/tests/test-mbs_startswith3.c b/tests/test-mbs_startswith3.c new file mode 100644 index 0000000000..1965070401 --- /dev/null +++ b/tests/test-mbs_startswith3.c @@ -0,0 +1,73 @@ +/* Test of mbs_startswith() function. + Copyright (C) 2025-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 . */ + +/* Written by Bruno Haible , 2026. */ + +#include + +#include + +#include + +#include "macros.h" + +int +main () +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + ASSERT (mbs_startswith ("", "")); + ASSERT (mbs_startswith ("abc", "")); + + ASSERT (!mbs_startswith ("", "a")); + ASSERT (!mbs_startswith ("x", "a")); + ASSERT (mbs_startswith ("a", "a")); + ASSERT (mbs_startswith ("abc", "a")); + + ASSERT (!mbs_startswith ("", "xyz")); + ASSERT (!mbs_startswith ("x", "xyz")); + ASSERT (!mbs_startswith ("a", "xyz")); + ASSERT (!mbs_startswith ("abc", "xyz")); + ASSERT (mbs_startswith ("xyz", "xyz")); + ASSERT (mbs_startswith ("xyzzy", "xyz")); + + ASSERT (mbs_startswith ("", "")); + ASSERT (mbs_startswith ("\201\060\212\061\201\060\227\070\201\060\212\064", "")); /* "äƀç" */ + + ASSERT (!mbs_startswith ("", "\201\060\212\061")); /* "ä" */ + ASSERT (!mbs_startswith ("\201\065\374\063", "\201\060\212\061")); /* "ẋ" "ä" */ + ASSERT (mbs_startswith ("\201\060\212\061", "\201\060\212\061")); /* "ä" "ä" */ + ASSERT (mbs_startswith ("\201\060\212\061\201\060\227\070\201\060\212\064", "\201\060\212\061")); /* "äƀç" "ä" */ + /* Test a prefix that ends in an incomplete character. */ + ASSERT (!mbs_startswith ("\201\060\212\061\201\060\227\070\201\060\212\064", "\201\060\212")); /* "äƀç" */ + ASSERT (!mbs_startswith ("\201\060\212\061\201\060\227\070\201\060\212\064", "\201\060")); /* "äƀç" */ + ASSERT (!mbs_startswith ("\201\060\212\061\201\060\227\070\201\060\212\064", "\201")); /* "äƀç" */ + + ASSERT (!mbs_startswith ("", "\201\065\374\063\201\060\213\067\201\065\374\071")); /* "ẋÿẑ" */ + ASSERT (!mbs_startswith ("\201\065\374\063", "\201\065\374\063\201\060\213\067\201\065\374\071")); /* "ẋ" "ẋÿẑ" */ + ASSERT (!mbs_startswith ("\201\060\212\061", "\201\065\374\063\201\060\213\067\201\065\374\071")); /* "ä" "ẋÿẑ" */ + ASSERT (!mbs_startswith ("\201\060\212\061\201\060\227\070\201\060\212\064", "\201\065\374\063\201\060\213\067\201\065\374\071")); /* "äƀç" "ẋÿẑ" */ + ASSERT (mbs_startswith ("\201\065\374\063\201\060\213\067\201\065\374\071", "\201\065\374\063\201\060\213\067\201\065\374\071")); /* "ẋÿẑ" "ẋÿẑ" */ + ASSERT (mbs_startswith ("\201\065\374\063\201\060\213\067\201\065\374\071\201\065\374\071\201\060\213\067", "\201\065\374\063\201\060\213\067\201\065\374\071")); /* "ẋÿẑẑÿ" "ẋÿẑ" */ + /* Test a prefix that ends in an incomplete character. */ + ASSERT (!mbs_startswith ("\201\065\374\063\201\060\213\067\201\065\374\071", "\201\065\374")); /* "ẋÿẑ" */ + ASSERT (!mbs_startswith ("\201\065\374\063\201\060\213\067\201\065\374\071", "\201\065")); /* "ẋÿẑ" */ + ASSERT (!mbs_startswith ("\201\065\374\063\201\060\213\067\201\065\374\071", "\201")); /* "ẋÿẑ" */ + + return test_exit_status; +} diff --git a/tests/test-mbs_startswith3.sh b/tests/test-mbs_startswith3.sh new file mode 100755 index 0000000000..80a39f32bc --- /dev/null +++ b/tests/test-mbs_startswith3.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +# Test whether a specific GB18030 locale is installed. +: "${LOCALE_ZH_CN=zh_CN.GB18030}" +if test $LOCALE_ZH_CN = none; then + if test -f /usr/bin/localedef; then + echo "Skipping test: no chinese GB18030 locale is installed" + else + echo "Skipping test: no chinese GB18030 locale is supported" + fi + exit 77 +fi + +LC_ALL=$LOCALE_ZH_CN \ +${CHECKER} ./test-mbs_startswith3${EXEEXT}