From: Bruno Haible Date: Tue, 18 Jun 2024 17:21:53 +0000 (+0200) Subject: u*-vasnprintf tests: Add tests of %ls directive. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=937c40c83e3286d7d6e20c6c7f6fbddc49160a3a;p=gnulib.git u*-vasnprintf tests: Add tests of %ls directive. * tests/unistdio/test-u8-vasnprintf2.c (test_function): Add tests of the %ls directive with non-ASCII argument. * tests/unistdio/test-u8-vasnprintf3.c (test_function): Likewise. * tests/unistdio/test-u16-vasnprintf2.c (test_function): Likewise. * tests/unistdio/test-u16-vasnprintf3.c (test_function): Likewise. * tests/unistdio/test-u32-vasnprintf2.c (test_function): Likewise. * tests/unistdio/test-u32-vasnprintf3.c (test_function): Likewise. * tests/unistdio/test-ulc-vasnprintf2.c (test_function): Likewise. * tests/unistdio/test-ulc-vasnprintf3.c (test_function): Likewise. --- diff --git a/ChangeLog b/ChangeLog index 5d5acb7de9..43b8424b6b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2024-06-18 Bruno Haible + u*-vasnprintf tests: Add tests of %ls directive. + * tests/unistdio/test-u8-vasnprintf2.c (test_function): Add tests of + the %ls directive with non-ASCII argument. + * tests/unistdio/test-u8-vasnprintf3.c (test_function): Likewise. + * tests/unistdio/test-u16-vasnprintf2.c (test_function): Likewise. + * tests/unistdio/test-u16-vasnprintf3.c (test_function): Likewise. + * tests/unistdio/test-u32-vasnprintf2.c (test_function): Likewise. + * tests/unistdio/test-u32-vasnprintf3.c (test_function): Likewise. + * tests/unistdio/test-ulc-vasnprintf2.c (test_function): Likewise. + * tests/unistdio/test-ulc-vasnprintf3.c (test_function): Likewise. + u*-vasnprintf: Fix handling of %ls, %lc directives on Solaris, MSVC. * lib/vasnprintf.c (VASNPRINTF): When implementing the %ls, %lc directives ourselves: Rename variable 'characters' to 'bytes'. Also diff --git a/tests/unistdio/test-u16-vasnprintf2.c b/tests/unistdio/test-u16-vasnprintf2.c index c0da1e00cf..4c061c6684 100644 --- a/tests/unistdio/test-u16-vasnprintf2.c +++ b/tests/unistdio/test-u16-vasnprintf2.c @@ -87,6 +87,69 @@ test_function (uint16_t * (*my_asnprintf) (uint16_t *, size_t *, const char *, . free (result); } } + + /* Test the support of the 'ls' conversion specifier for wide strings. */ + + { + const char *locale_string = "h\351t\351rog\351n\351it\351"; /* hétérogénéité */ + wchar_t wide_string[20]; + ASSERT (mbstowcs (wide_string, locale_string, SIZEOF (wide_string)) == 13); + { + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%ls %d", wide_string, 33, 44, 55); + static const uint16_t expected[] = + { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, + 'i', 't', 0x00e9, ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%20ls %d", wide_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't', + 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9, + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%-20ls %d", wide_string, 33, 44, 55); + static const uint16_t expected[] = + { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, + 'i', 't', 0x00e9, ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%020ls %d", wide_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't', + 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9, + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + } } static uint16_t * diff --git a/tests/unistdio/test-u16-vasnprintf3.c b/tests/unistdio/test-u16-vasnprintf3.c index f1156791bc..d5924f8bad 100644 --- a/tests/unistdio/test-u16-vasnprintf3.c +++ b/tests/unistdio/test-u16-vasnprintf3.c @@ -87,6 +87,69 @@ test_function (uint16_t * (*my_asnprintf) (uint16_t *, size_t *, const char *, . free (result); } } + + /* Test the support of the 'ls' conversion specifier for wide strings. */ + + { + const char *locale_string = "h\303\251t\303\251rog\303\251n\303\251it\303\251"; /* hétérogénéité */ + wchar_t wide_string[20]; + ASSERT (mbstowcs (wide_string, locale_string, SIZEOF (wide_string)) == 13); + { + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%ls %d", wide_string, 33, 44, 55); + static const uint16_t expected[] = + { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, + 'i', 't', 0x00e9, ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%20ls %d", wide_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't', + 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9, + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%-20ls %d", wide_string, 33, 44, 55); + static const uint16_t expected[] = + { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, + 'i', 't', 0x00e9, ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%020ls %d", wide_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't', + 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9, + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + } } static uint16_t * diff --git a/tests/unistdio/test-u32-vasnprintf2.c b/tests/unistdio/test-u32-vasnprintf2.c index 64c91f0c46..70336c7c49 100644 --- a/tests/unistdio/test-u32-vasnprintf2.c +++ b/tests/unistdio/test-u32-vasnprintf2.c @@ -87,6 +87,69 @@ test_function (uint32_t * (*my_asnprintf) (uint32_t *, size_t *, const char *, . free (result); } } + + /* Test the support of the 'ls' conversion specifier for wide strings. */ + + { + const char *locale_string = "h\351t\351rog\351n\351it\351"; /* hétérogénéité */ + wchar_t wide_string[20]; + ASSERT (mbstowcs (wide_string, locale_string, SIZEOF (wide_string)) == 13); + { + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%ls %d", wide_string, 33, 44, 55); + static const uint32_t expected[] = + { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, + 'i', 't', 0x00e9, ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%20ls %d", wide_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't', + 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9, + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%-20ls %d", wide_string, 33, 44, 55); + static const uint32_t expected[] = + { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, + 'i', 't', 0x00e9, ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%020ls %d", wide_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't', + 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9, + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + } } static uint32_t * diff --git a/tests/unistdio/test-u32-vasnprintf3.c b/tests/unistdio/test-u32-vasnprintf3.c index 9e4eb11f92..816eb9fd8b 100644 --- a/tests/unistdio/test-u32-vasnprintf3.c +++ b/tests/unistdio/test-u32-vasnprintf3.c @@ -87,6 +87,69 @@ test_function (uint32_t * (*my_asnprintf) (uint32_t *, size_t *, const char *, . free (result); } } + + /* Test the support of the 'ls' conversion specifier for wide strings. */ + + { + const char *locale_string = "h\303\251t\303\251rog\303\251n\303\251it\303\251"; /* hétérogénéité */ + wchar_t wide_string[20]; + ASSERT (mbstowcs (wide_string, locale_string, SIZEOF (wide_string)) == 13); + { + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%ls %d", wide_string, 33, 44, 55); + static const uint32_t expected[] = + { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, + 'i', 't', 0x00e9, ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%20ls %d", wide_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't', + 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9, + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%-20ls %d", wide_string, 33, 44, 55); + static const uint32_t expected[] = + { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, + 'i', 't', 0x00e9, ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%020ls %d", wide_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't', + 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9, + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + } } static uint32_t * diff --git a/tests/unistdio/test-u8-vasnprintf2.c b/tests/unistdio/test-u8-vasnprintf2.c index aa11e47437..3c0a00f13f 100644 --- a/tests/unistdio/test-u8-vasnprintf2.c +++ b/tests/unistdio/test-u8-vasnprintf2.c @@ -77,6 +77,54 @@ test_function (uint8_t * (*my_asnprintf) (uint8_t *, size_t *, const char *, ... free (result); } } + + /* Test the support of the 'ls' conversion specifier for wide strings. */ + + { + const char *locale_string = "h\351t\351rog\351n\351it\351"; /* hétérogénéité */ + wchar_t wide_string[20]; + ASSERT (mbstowcs (wide_string, locale_string, SIZEOF (wide_string)) == 13); + { + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%ls %d", wide_string, 33, 44, 55); + static const uint8_t expected[] = "h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%20ls %d", wide_string, 33, 44, 55); + static const uint8_t expected[] = " h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%-20ls %d", wide_string, 33, 44, 55); + static const uint8_t expected[] = "h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%020ls %d", wide_string, 33, 44, 55); + static const uint8_t expected[] = " h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + } } static uint8_t * diff --git a/tests/unistdio/test-u8-vasnprintf3.c b/tests/unistdio/test-u8-vasnprintf3.c index 98b851aa27..72efa60ae6 100644 --- a/tests/unistdio/test-u8-vasnprintf3.c +++ b/tests/unistdio/test-u8-vasnprintf3.c @@ -77,6 +77,54 @@ test_function (uint8_t * (*my_asnprintf) (uint8_t *, size_t *, const char *, ... free (result); } } + + /* Test the support of the 'ls' conversion specifier for wide strings. */ + + { + const char *locale_string = "h\303\251t\303\251rog\303\251n\303\251it\303\251"; /* hétérogénéité */ + wchar_t wide_string[20]; + ASSERT (mbstowcs (wide_string, locale_string, SIZEOF (wide_string)) == 13); + { + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%ls %d", wide_string, 33, 44, 55); + static const uint8_t expected[] = "h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%20ls %d", wide_string, 33, 44, 55); + static const uint8_t expected[] = " h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%-20ls %d", wide_string, 33, 44, 55); + static const uint8_t expected[] = "h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%020ls %d", wide_string, 33, 44, 55); + static const uint8_t expected[] = " h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + } } static uint8_t * diff --git a/tests/unistdio/test-ulc-vasnprintf2.c b/tests/unistdio/test-ulc-vasnprintf2.c index 1904aa76f1..d91fa86e5a 100644 --- a/tests/unistdio/test-ulc-vasnprintf2.c +++ b/tests/unistdio/test-ulc-vasnprintf2.c @@ -214,6 +214,50 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) free (result); } } + + /* Test the support of the 'ls' conversion specifier for wide strings. */ + + { + const char *locale_string = "h\351t\351rog\351n\351it\351"; /* hétérogénéité */ + wchar_t wide_string[20]; + ASSERT (mbstowcs (wide_string, locale_string, SIZEOF (wide_string)) == 13); + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%ls %d", wide_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "h\351t\351rog\351n\351it\351 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%20ls %d", wide_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " h\351t\351rog\351n\351it\351 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-20ls %d", wide_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "h\351t\351rog\351n\351it\351 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%020ls %d", wide_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " h\351t\351rog\351n\351it\351 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } } static char * diff --git a/tests/unistdio/test-ulc-vasnprintf3.c b/tests/unistdio/test-ulc-vasnprintf3.c index 12ef2bfc66..7fc6171952 100644 --- a/tests/unistdio/test-ulc-vasnprintf3.c +++ b/tests/unistdio/test-ulc-vasnprintf3.c @@ -202,6 +202,50 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) free (result); } } + + /* Test the support of the 'ls' conversion specifier for wide strings. */ + + { + const char *locale_string = "h\303\251t\303\251rog\303\251n\303\251it\303\251"; /* hétérogénéité */ + wchar_t wide_string[20]; + ASSERT (mbstowcs (wide_string, locale_string, SIZEOF (wide_string)) == 13); + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%ls %d", wide_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "h\303\251t\303\251rog\303\251n\303\251it\303\251 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%20ls %d", wide_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " h\303\251t\303\251rog\303\251n\303\251it\303\251 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-20ls %d", wide_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "h\303\251t\303\251rog\303\251n\303\251it\303\251 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%020ls %d", wide_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " h\303\251t\303\251rog\303\251n\303\251it\303\251 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } } static char *