diff options
author | Andreas Rottmann <a.rottmann@gmx.at> | 2009-09-14 12:32:44 +0200 |
---|---|---|
committer | Andreas Rottmann <a.rottmann@gmx.at> | 2009-09-14 12:32:44 +0200 |
commit | fa095a4504cbe668e4244547e2c141597bea4ecf (patch) | |
tree | 06135820a286ffec47804e75fbf8a147e92acd2e /tests/unistdio |
Imported Upstream version 0.9.1upstream/0.9.1
Diffstat (limited to 'tests/unistdio')
44 files changed, 6296 insertions, 0 deletions
diff --git a/tests/unistdio/test-u16-asnprintf1.c b/tests/unistdio/test-u16-asnprintf1.c new file mode 100644 index 00000000..53fedf34 --- /dev/null +++ b/tests/unistdio/test-u16-asnprintf1.c @@ -0,0 +1,59 @@ +/* Test of u16_asnprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u16-asnprintf1.h" + +static void +test_asnprintf () +{ + test_function (u16_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + test_asnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u16-asnprintf1.h b/tests/unistdio/test-u16-asnprintf1.h new file mode 100644 index 00000000..34d4f821 --- /dev/null +++ b/tests/unistdio/test-u16-asnprintf1.h @@ -0,0 +1,60 @@ +/* Test of u16_[v]asnprintf() function. + Copyright (C) 2007 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +static void +test_function (uint16_t * (*my_asnprintf) (uint16_t *, size_t *, const char *, ...)) +{ + uint16_t buf[8]; + int size; + + /* Test return value convention. */ + + for (size = 0; size <= 8; size++) + { + size_t length = size; + uint16_t *result = my_asnprintf (NULL, &length, "%d", 12345); + static const uint16_t expected[] = + { '1', '2', '3', '4', '5', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == 5); + free (result); + } + + for (size = 0; size <= 8; size++) + { + static const uint16_t initializer[] = + { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F', 0 }; + static const uint16_t expected[] = + { '1', '2', '3', '4', '5', 0 }; + size_t length; + uint16_t *result; + + u16_cpy (buf, initializer, 8); + length = size; + result = my_asnprintf (buf, &length, "%d", 12345); + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == 5); + if (size < 6) + ASSERT (result != buf); + ASSERT (u16_cmp (buf + size, initializer + size, 8 - size) == 0); + if (result != buf) + free (result); + } +} diff --git a/tests/unistdio/test-u16-printf1.h b/tests/unistdio/test-u16-printf1.h new file mode 100644 index 00000000..199ece90 --- /dev/null +++ b/tests/unistdio/test-u16-printf1.h @@ -0,0 +1,941 @@ +/* Test of u16_v[a]s[n]printf() function. + Copyright (C) 2007 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +static void +test_xfunction (uint16_t * (*my_xasprintf) (const char *, ...)) +{ + /* Test support of size specifiers as in C99. */ + + { + uint16_t *result = + my_xasprintf ("%ju %d", (uintmax_t) 12345671, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '3', '4', '5', '6', '7', '1', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { + uint16_t *result = + my_xasprintf ("%zu %d", (size_t) 12345672, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '3', '4', '5', '6', '7', '2', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { + uint16_t *result = + my_xasprintf ("%tu %d", (ptrdiff_t) 12345673, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '3', '4', '5', '6', '7', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { + uint16_t *result = + my_xasprintf ("%Lg %d", (long double) 1.5, 33, 44, 55); + static const uint16_t expected[] = + { '1', '.', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the 'U' conversion specifier for Unicode strings. */ + + { + static const uint8_t unicode_string[] = "Hello"; + { + uint16_t *result = + my_xasprintf ("%U %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint16_t *result = + my_xasprintf ("%10U %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint16_t *result = + my_xasprintf ("%-10U %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint16_t *result = + my_xasprintf ("%010U %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint16_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 }; + { + uint16_t *result = + my_xasprintf ("%lU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint16_t *result = + my_xasprintf ("%10lU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint16_t *result = + my_xasprintf ("%-10lU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint16_t *result = + my_xasprintf ("%010lU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint32_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 }; + { + uint16_t *result = + my_xasprintf ("%llU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint16_t *result = + my_xasprintf ("%10llU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint16_t *result = + my_xasprintf ("%-10llU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint16_t *result = + my_xasprintf ("%010llU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + } + + /* Test the support of the 's' conversion specifier for strings. */ + + { + uint16_t *result = + my_xasprintf ("Mr. %s %d", "Ronald Reagan", 33, 44, 55); + static const uint16_t expected[] = + { 'M', 'r', '.', ' ', 'R', 'o', 'n', 'a', 'l', 'd', + ' ', 'R', 'e', 'a', 'g', 'a', 'n', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint16_t *result = + my_xasprintf ("Mr. %20s %d", "Ronald Reagan", 33, 44, 55); + static const uint16_t expected[] = + { 'M', 'r', '.', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', 'R', 'o', 'n', 'a', 'l', 'd', ' ', 'R', 'e', + 'a', 'g', 'a', 'n', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* FLAG_LEFT. */ + uint16_t *result = + my_xasprintf ("Mr. %-20s %d", "Ronald Reagan", 33, 44, 55); + static const uint16_t expected[] = + { 'M', 'r', '.', ' ', 'R', 'o', 'n', 'a', 'l', 'd', + ' ', 'R', 'e', 'a', 'g', 'a', 'n', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* FLAG_ZERO: no effect. */ + uint16_t *result = + my_xasprintf ("Mr. %020s %d", "Ronald Reagan", 33, 44, 55); + static const uint16_t expected[] = + { 'M', 'r', '.', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', 'R', 'o', 'n', 'a', 'l', 'd', ' ', 'R', 'e', + 'a', 'g', 'a', 'n', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal + output of floating-point numbers. */ + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%a %d", 3.1416015625, 33, 44, 55); + static const uint16_t expected1[] = + { '0', 'x', '1', '.', '9', '2', '2', 'p', '+', '1', ' ', '3', '3', 0 }; + static const uint16_t expected2[] = + { '0', 'x', '3', '.', '2', '4', '4', 'p', '+', '0', ' ', '3', '3', 0 }; + static const uint16_t expected3[] = + { '0', 'x', '6', '.', '4', '8', '8', 'p', '-', '1', ' ', '3', '3', 0 }; + static const uint16_t expected4[] = + { '0', 'x', 'c', '.', '9', '1', 'p', '-', '2', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0 + || u16_strcmp (result, expected3) == 0 + || u16_strcmp (result, expected4) == 0); + free (result); + } + + { /* Width. */ + uint16_t *result = + my_xasprintf ("%10a %d", 1.75, 33, 44, 55); + static const uint16_t expected1[] = + { ' ', ' ', '0', 'x', '1', '.', 'c', 'p', '+', '0', ' ', '3', '3', 0 }; + static const uint16_t expected2[] = + { ' ', ' ', '0', 'x', '3', '.', '8', 'p', '-', '1', ' ', '3', '3', 0 }; + static const uint16_t expected3[] = + { ' ', ' ', ' ', ' ', '0', 'x', '7', 'p', '-', '2', ' ', '3', '3', 0 }; + static const uint16_t expected4[] = + { ' ', ' ', ' ', ' ', '0', 'x', 'e', 'p', '-', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0 + || u16_strcmp (result, expected3) == 0 + || u16_strcmp (result, expected4) == 0); + free (result); + } + + { /* Small precision. */ + uint16_t *result = + my_xasprintf ("%.10a %d", 1.75, 33, 44, 55); + static const uint16_t expected1[] = + { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3', + 0 + }; + static const uint16_t expected2[] = + { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3', + 0 + }; + static const uint16_t expected3[] = + { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3', + 0 + }; + static const uint16_t expected4[] = + { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0 + || u16_strcmp (result, expected3) == 0 + || u16_strcmp (result, expected4) == 0); + free (result); + } + + { /* Large precision. */ + uint16_t *result = + my_xasprintf ("%.50a %d", 1.75, 33, 44, 55); + static const uint16_t expected1[] = + { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3', + 0 + }; + static const uint16_t expected2[] = + { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3', + 0 + }; + static const uint16_t expected3[] = + { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3', + 0 + }; + static const uint16_t expected4[] = + { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0 + || u16_strcmp (result, expected3) == 0 + || u16_strcmp (result, expected4) == 0); + free (result); + } + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%La %d", 3.1416015625L, 33, 44, 55); + static const uint16_t expected1[] = + { '0', 'x', '1', '.', '9', '2', '2', 'p', '+', '1', + ' ', '3', '3', 0 + }; + static const uint16_t expected2[] = + { '0', 'x', '3', '.', '2', '4', '4', 'p', '+', '0', + ' ', '3', '3', 0 + }; + static const uint16_t expected3[] = + { '0', 'x', '6', '.', '4', '8', '8', 'p', '-', '1', + ' ', '3', '3', 0 + }; + static const uint16_t expected4[] = + { '0', 'x', 'c', '.', '9', '1', 'p', '-', '2', ' ', + '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0 + || u16_strcmp (result, expected3) == 0 + || u16_strcmp (result, expected4) == 0); + free (result); + } + + { /* Width. */ + uint16_t *result = + my_xasprintf ("%10La %d", 1.75L, 33, 44, 55); + static const uint16_t expected1[] = + { ' ', ' ', '0', 'x', '1', '.', 'c', 'p', '+', '0', ' ', '3', '3', 0 }; + static const uint16_t expected2[] = + { ' ', ' ', '0', 'x', '3', '.', '8', 'p', '-', '1', ' ', '3', '3', 0 }; + static const uint16_t expected3[] = + { ' ', ' ', ' ', ' ', '0', 'x', '7', 'p', '-', '2', ' ', '3', '3', 0 }; + static const uint16_t expected4[] = + { ' ', ' ', ' ', ' ', '0', 'x', 'e', 'p', '-', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0 + || u16_strcmp (result, expected3) == 0 + || u16_strcmp (result, expected4) == 0); + free (result); + } + + { /* Small precision. */ + uint16_t *result = + my_xasprintf ("%.10La %d", 1.75L, 33, 44, 55); + static const uint16_t expected1[] = + { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3', + 0 + }; + static const uint16_t expected2[] = + { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3', + 0 + }; + static const uint16_t expected3[] = + { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3', + 0 + }; + static const uint16_t expected4[] = + { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0 + || u16_strcmp (result, expected3) == 0 + || u16_strcmp (result, expected4) == 0); + free (result); + } + + { /* Large precision. */ + uint16_t *result = + my_xasprintf ("%.50La %d", 1.75L, 33, 44, 55); + static const uint16_t expected1[] = + { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3', + 0 + }; + static const uint16_t expected2[] = + { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3', + 0 + }; + static const uint16_t expected3[] = + { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3', + 0 + }; + static const uint16_t expected4[] = + { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0 + || u16_strcmp (result, expected3) == 0 + || u16_strcmp (result, expected4) == 0); + free (result); + } + + /* Test the support of the %f format directive. */ + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%f %d", 12.75, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint16_t *result = + my_xasprintf ("%10f %d", 1.75, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint16_t *result = + my_xasprintf ("%.f %d", 1234.0, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '3', '4', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%Lf %d", 12.75L, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint16_t *result = + my_xasprintf ("%10Lf %d", 1.75L, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint16_t *result = + my_xasprintf ("%.Lf %d", 1234.0L, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '3', '4', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %F format directive. */ + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%F %d", 12.75, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint16_t *result = + my_xasprintf ("%.F %d", 1234.0, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '3', '4', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%LF %d", 12.75L, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint16_t *result = + my_xasprintf ("%.LF %d", 1234.0L, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '3', '4', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %e format directive. */ + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%e %d", 12.75, 33, 44, 55); + static const uint16_t expected1[] = + { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+', + '0', '1', ' ', '3', '3', 0 + }; + static const uint16_t expected2[] = + { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+', + '0', '0', '1', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0); + free (result); + } + + { /* Width. */ + uint16_t *result = + my_xasprintf ("%15e %d", 1.75, 33, 44, 55); + static const uint16_t expected1[] = + { ' ', ' ', ' ', '1', '.', '7', '5', '0', '0', '0', + '0', 'e', '+', '0', '0', ' ', '3', '3', 0 + }; + static const uint16_t expected2[] = + { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', + 'e', '+', '0', '0', '0', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0); + free (result); + } + + { /* Precision. */ + uint16_t *result = + my_xasprintf ("%.e %d", 1234.0, 33, 44, 55); + static const uint16_t expected1[] = + { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 }; + static const uint16_t expected2[] = + { '1', 'e', '+', '0', '0', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0); + free (result); + } + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%Le %d", 12.75L, 33, 44, 55); + static const uint16_t expected[] = + { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+', + '0', '1', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint16_t *result = + my_xasprintf ("%15Le %d", 1.75L, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', '1', '.', '7', '5', '0', '0', '0', + '0', 'e', '+', '0', '0', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint16_t *result = + my_xasprintf ("%.Le %d", 1234.0L, 33, 44, 55); + static const uint16_t expected[] = + { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %g format directive. */ + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%g %d", 12.75, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '.', '7', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint16_t *result = + my_xasprintf ("%10g %d", 1.75, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', '1', '.', '7', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint16_t *result = + my_xasprintf ("%.g %d", 1234.0, 33, 44, 55); + static const uint16_t expected1[] = + { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 }; + static const uint16_t expected2[] = + { '1', 'e', '+', '0', '0', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected1) == 0 + || u16_strcmp (result, expected2) == 0); + free (result); + } + + { /* A positive number. */ + uint16_t *result = + my_xasprintf ("%Lg %d", 12.75L, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '.', '7', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint16_t *result = + my_xasprintf ("%10Lg %d", 1.75L, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', '1', '.', '7', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint16_t *result = + my_xasprintf ("%.Lg %d", 1234.0L, 33, 44, 55); + static const uint16_t expected[] = + { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %n format directive. */ + + { + int count = -1; + uint16_t *result = + my_xasprintf ("%d %n", 123, &count, 33, 44, 55); + static const uint16_t expected[] = + { '1', '2', '3', ' ', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (count == 4); + free (result); + } + + /* Test the support of the POSIX/XSI format strings with positions. */ + + { + uint16_t *result = + my_xasprintf ("%2$d %1$d", 33, 55); + static const uint16_t expected[] = + { '5', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the grouping flag. */ + + { + uint16_t *result = + my_xasprintf ("%'d %d", 1234567, 99); + ASSERT (result != NULL); + ASSERT (result[u16_strlen (result) - 1] == '9'); + free (result); + } + + /* Test the support of the 'U' conversion specifier for Unicode strings. */ + + { + static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */ + { + uint16_t *result = + my_xasprintf ("%U %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint16_t *result = + my_xasprintf ("%20U %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint16_t *result = + my_xasprintf ("%-20U %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint16_t *result = + my_xasprintf ("%020U %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint16_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + uint16_t *result = + my_xasprintf ("%lU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint16_t *result = + my_xasprintf ("%20lU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint16_t *result = + my_xasprintf ("%-20lU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint16_t *result = + my_xasprintf ("%020lU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint32_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + uint16_t *result = + my_xasprintf ("%llU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint16_t *result = + my_xasprintf ("%20llU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint16_t *result = + my_xasprintf ("%-20llU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint16_t *result = + my_xasprintf ("%020llU %d", unicode_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + free (result); + } + } + + /* Test non-ASCII characters in the format string. */ + + { + uint16_t *result = + my_xasprintf ("\304rger", 33, 44, 55); + ASSERT (result == NULL && errno == EINVAL); + } +} diff --git a/tests/unistdio/test-u16-vasnprintf1.c b/tests/unistdio/test-u16-vasnprintf1.c new file mode 100644 index 00000000..b236e3fe --- /dev/null +++ b/tests/unistdio/test-u16-vasnprintf1.c @@ -0,0 +1,88 @@ +/* Test of u16_vasnprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u16-asnprintf1.h" +#include "test-u16-printf1.h" + +static uint16_t * +my_asnprintf (uint16_t *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + uint16_t *ret; + + va_start (args, format); + ret = u16_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static uint16_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + size_t length; + uint16_t *ret; + + va_start (args, format); + ret = u16_vasnprintf (NULL, &length, format, args); + va_end (args); + if (ret != NULL) + ASSERT (length == u16_strlen (ret)); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u16-vasnprintf2.c b/tests/unistdio/test-u16-vasnprintf2.c new file mode 100644 index 00000000..6f7b7cf6 --- /dev/null +++ b/tests/unistdio/test-u16-vasnprintf2.c @@ -0,0 +1,132 @@ +/* Test of u16_vasnprintf() function in an ISO-8859-1 locale. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +static void +test_function (uint16_t * (*my_asnprintf) (uint16_t *, size_t *, const char *, ...)) +{ + /* Test the support of the 's' conversion specifier for strings. */ + + { + const char *locale_string = "\304rger"; /* Ärger */ + { + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); + static const uint16_t expected[] = + { 0x00c4, 'r', 'g', 'e', 'r', ' ', '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, "%10s %d", locale_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', + ' ', '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, "%-10s %d", locale_string, 33, 44, 55); + static const uint16_t expected[] = + { 0x00c4, 'r', 'g', 'e', 'r', ' ', ' ', ' ', ' ', ' ', + ' ', '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, "%010s %d", locale_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + } +} + +static uint16_t * +my_asnprintf (uint16_t *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + uint16_t *ret; + + va_start (args, format); + ret = u16_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u16-vasnprintf2.sh b/tests/unistdio/test-u16-vasnprintf2.sh new file mode 100755 index 00000000..418b8aac --- /dev/null +++ b/tests/unistdio/test-u16-vasnprintf2.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Test whether a traditional french locale is installed. +: ${LOCALE_FR=fr_FR} +if test $LOCALE_FR != none; then + testlocale=$LOCALE_FR +else + if test -f /usr/bin/localedef; then + echo "Skipping test: no traditional french locale is installed" + else + echo "Skipping test: no traditional french locale is supported" + fi + exit 77 +fi + +LC_ALL=$testlocale \ +./test-u16-vasnprintf1${EXEEXT} \ + || exit 1 + +LC_ALL=$testlocale \ +./test-u16-vasnprintf2${EXEEXT} diff --git a/tests/unistdio/test-u16-vasnprintf3.c b/tests/unistdio/test-u16-vasnprintf3.c new file mode 100644 index 00000000..14021819 --- /dev/null +++ b/tests/unistdio/test-u16-vasnprintf3.c @@ -0,0 +1,132 @@ +/* Test of u16_vasnprintf() function in an UTF-8 locale. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +static void +test_function (uint16_t * (*my_asnprintf) (uint16_t *, size_t *, const char *, ...)) +{ + /* Test the support of the 's' conversion specifier for strings. */ + + { + const char *locale_string = "\303\204rger"; /* Ärger */ + { + size_t length; + uint16_t *result = + my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); + static const uint16_t expected[] = + { 0x00c4, 'r', 'g', 'e', 'r', ' ', '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, "%10s %d", locale_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', + ' ', '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, "%-10s %d", locale_string, 33, 44, 55); + static const uint16_t expected[] = + { 0x00c4, 'r', 'g', 'e', 'r', ' ', ' ', ' ', ' ', ' ', + ' ', '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, "%010s %d", locale_string, 33, 44, 55); + static const uint16_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u16_strcmp (result, expected) == 0); + ASSERT (length == u16_strlen (result)); + free (result); + } + } +} + +static uint16_t * +my_asnprintf (uint16_t *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + uint16_t *ret; + + va_start (args, format); + ret = u16_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u16-vasnprintf3.sh b/tests/unistdio/test-u16-vasnprintf3.sh new file mode 100755 index 00000000..57488c06 --- /dev/null +++ b/tests/unistdio/test-u16-vasnprintf3.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Test whether a french Unicode locale is installed. +: ${LOCALE_FR_UTF8=fr_FR.UTF-8} +if test $LOCALE_FR_UTF8 != none; then + testlocale=$LOCALE_FR_UTF8 +else + if test -f /usr/bin/localedef; then + echo "Skipping test: no french Unicode locale is installed" + else + echo "Skipping test: no french Unicode locale is supported" + fi + exit 77 +fi + +LC_ALL=$testlocale \ +./test-u16-vasnprintf1${EXEEXT} \ + || exit 1 + +LC_ALL=$testlocale \ +./test-u16-vasnprintf3${EXEEXT} diff --git a/tests/unistdio/test-u16-vasprintf1.c b/tests/unistdio/test-u16-vasprintf1.c new file mode 100644 index 00000000..8b961268 --- /dev/null +++ b/tests/unistdio/test-u16-vasprintf1.c @@ -0,0 +1,75 @@ +/* Test of u16_vasprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u16-printf1.h" + +static uint16_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + uint16_t *result; + int retval; + + va_start (args, format); + retval = u16_vasprintf (&result, format, args); + va_end (args); + if (retval < 0) + return NULL; + ASSERT (result != NULL); + return result; +} + +static void +test_vasprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + test_vasprintf (); + return 0; +} diff --git a/tests/unistdio/test-u16-vsnprintf1.c b/tests/unistdio/test-u16-vsnprintf1.c new file mode 100644 index 00000000..886e95b3 --- /dev/null +++ b/tests/unistdio/test-u16-vsnprintf1.c @@ -0,0 +1,84 @@ +/* Test of u16_vsnprintf() function. + Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" +#include "xalloc.h" +#include "progname.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u16-printf1.h" + +static uint16_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + uint16_t buf[1000]; + int retval; + size_t length; + uint16_t *result; + + va_start (args, format); + retval = u16_vsnprintf (buf, sizeof (buf), format, args); + va_end (args); + if (retval < 0 || retval >= (int) sizeof (buf)) + return NULL; + length = u16_strlen (buf); + result = XNMALLOC (length + 1, uint16_t); + u16_cpy (result, buf, length + 1); + return result; +} + +static void +test_vsnprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + set_program_name (argv[0]); + + test_vsnprintf (); + + return 0; +} diff --git a/tests/unistdio/test-u16-vsprintf1.c b/tests/unistdio/test-u16-vsprintf1.c new file mode 100644 index 00000000..e6b6eab7 --- /dev/null +++ b/tests/unistdio/test-u16-vsprintf1.c @@ -0,0 +1,84 @@ +/* Test of u16_vsprintf() function. + Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" +#include "xalloc.h" +#include "progname.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u16-printf1.h" + +static uint16_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + uint16_t buf[1000]; + int retval; + size_t length; + uint16_t *result; + + va_start (args, format); + retval = u16_vsprintf (buf, format, args); + va_end (args); + if (retval < 0 || retval >= (int) sizeof (buf)) + return NULL; + length = u16_strlen (buf); + result = XNMALLOC (length + 1, uint16_t); + u16_cpy (result, buf, length + 1); + return result; +} + +static void +test_vsprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + set_program_name (argv[0]); + + test_vsprintf (); + + return 0; +} diff --git a/tests/unistdio/test-u32-asnprintf1.c b/tests/unistdio/test-u32-asnprintf1.c new file mode 100644 index 00000000..252e919f --- /dev/null +++ b/tests/unistdio/test-u32-asnprintf1.c @@ -0,0 +1,59 @@ +/* Test of u32_asnprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u32-asnprintf1.h" + +static void +test_asnprintf () +{ + test_function (u32_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + test_asnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u32-asnprintf1.h b/tests/unistdio/test-u32-asnprintf1.h new file mode 100644 index 00000000..dfaed98d --- /dev/null +++ b/tests/unistdio/test-u32-asnprintf1.h @@ -0,0 +1,60 @@ +/* Test of u32_[v]asnprintf() function. + Copyright (C) 2007 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +static void +test_function (uint32_t * (*my_asnprintf) (uint32_t *, size_t *, const char *, ...)) +{ + uint32_t buf[8]; + int size; + + /* Test return value convention. */ + + for (size = 0; size <= 8; size++) + { + size_t length = size; + uint32_t *result = my_asnprintf (NULL, &length, "%d", 12345); + static const uint32_t expected[] = + { '1', '2', '3', '4', '5', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == 5); + free (result); + } + + for (size = 0; size <= 8; size++) + { + static const uint32_t initializer[] = + { 'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F', 0 }; + static const uint32_t expected[] = + { '1', '2', '3', '4', '5', 0 }; + size_t length; + uint32_t *result; + + u32_cpy (buf, initializer, 8); + length = size; + result = my_asnprintf (buf, &length, "%d", 12345); + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == 5); + if (size < 6) + ASSERT (result != buf); + ASSERT (u32_cmp (buf + size, initializer + size, 8 - size) == 0); + if (result != buf) + free (result); + } +} diff --git a/tests/unistdio/test-u32-printf1.h b/tests/unistdio/test-u32-printf1.h new file mode 100644 index 00000000..240232fd --- /dev/null +++ b/tests/unistdio/test-u32-printf1.h @@ -0,0 +1,941 @@ +/* Test of u32_v[a]s[n]printf() function. + Copyright (C) 2007 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +static void +test_xfunction (uint32_t * (*my_xasprintf) (const char *, ...)) +{ + /* Test support of size specifiers as in C99. */ + + { + uint32_t *result = + my_xasprintf ("%ju %d", (uintmax_t) 12345671, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '3', '4', '5', '6', '7', '1', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { + uint32_t *result = + my_xasprintf ("%zu %d", (size_t) 12345672, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '3', '4', '5', '6', '7', '2', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { + uint32_t *result = + my_xasprintf ("%tu %d", (ptrdiff_t) 12345673, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '3', '4', '5', '6', '7', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { + uint32_t *result = + my_xasprintf ("%Lg %d", (long double) 1.5, 33, 44, 55); + static const uint32_t expected[] = + { '1', '.', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the 'U' conversion specifier for Unicode strings. */ + + { + static const uint8_t unicode_string[] = "Hello"; + { + uint32_t *result = + my_xasprintf ("%U %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint32_t *result = + my_xasprintf ("%10U %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint32_t *result = + my_xasprintf ("%-10U %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint32_t *result = + my_xasprintf ("%010U %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint16_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 }; + { + uint32_t *result = + my_xasprintf ("%lU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint32_t *result = + my_xasprintf ("%10lU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint32_t *result = + my_xasprintf ("%-10lU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint32_t *result = + my_xasprintf ("%010lU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint32_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 }; + { + uint32_t *result = + my_xasprintf ("%llU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint32_t *result = + my_xasprintf ("%10llU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint32_t *result = + my_xasprintf ("%-10llU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'H', 'e', 'l', 'l', 'o', ' ', ' ', ' ', ' ', ' ', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint32_t *result = + my_xasprintf ("%010llU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + } + + /* Test the support of the 's' conversion specifier for strings. */ + + { + uint32_t *result = + my_xasprintf ("Mr. %s %d", "Ronald Reagan", 33, 44, 55); + static const uint32_t expected[] = + { 'M', 'r', '.', ' ', 'R', 'o', 'n', 'a', 'l', 'd', + ' ', 'R', 'e', 'a', 'g', 'a', 'n', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint32_t *result = + my_xasprintf ("Mr. %20s %d", "Ronald Reagan", 33, 44, 55); + static const uint32_t expected[] = + { 'M', 'r', '.', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', 'R', 'o', 'n', 'a', 'l', 'd', ' ', 'R', 'e', + 'a', 'g', 'a', 'n', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* FLAG_LEFT. */ + uint32_t *result = + my_xasprintf ("Mr. %-20s %d", "Ronald Reagan", 33, 44, 55); + static const uint32_t expected[] = + { 'M', 'r', '.', ' ', 'R', 'o', 'n', 'a', 'l', 'd', + ' ', 'R', 'e', 'a', 'g', 'a', 'n', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* FLAG_ZERO: no effect. */ + uint32_t *result = + my_xasprintf ("Mr. %020s %d", "Ronald Reagan", 33, 44, 55); + static const uint32_t expected[] = + { 'M', 'r', '.', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', 'R', 'o', 'n', 'a', 'l', 'd', ' ', 'R', 'e', + 'a', 'g', 'a', 'n', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal + output of floating-point numbers. */ + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%a %d", 3.1416015625, 33, 44, 55); + static const uint32_t expected1[] = + { '0', 'x', '1', '.', '9', '2', '2', 'p', '+', '1', ' ', '3', '3', 0 }; + static const uint32_t expected2[] = + { '0', 'x', '3', '.', '2', '4', '4', 'p', '+', '0', ' ', '3', '3', 0 }; + static const uint32_t expected3[] = + { '0', 'x', '6', '.', '4', '8', '8', 'p', '-', '1', ' ', '3', '3', 0 }; + static const uint32_t expected4[] = + { '0', 'x', 'c', '.', '9', '1', 'p', '-', '2', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0 + || u32_strcmp (result, expected3) == 0 + || u32_strcmp (result, expected4) == 0); + free (result); + } + + { /* Width. */ + uint32_t *result = + my_xasprintf ("%10a %d", 1.75, 33, 44, 55); + static const uint32_t expected1[] = + { ' ', ' ', '0', 'x', '1', '.', 'c', 'p', '+', '0', ' ', '3', '3', 0 }; + static const uint32_t expected2[] = + { ' ', ' ', '0', 'x', '3', '.', '8', 'p', '-', '1', ' ', '3', '3', 0 }; + static const uint32_t expected3[] = + { ' ', ' ', ' ', ' ', '0', 'x', '7', 'p', '-', '2', ' ', '3', '3', 0 }; + static const uint32_t expected4[] = + { ' ', ' ', ' ', ' ', '0', 'x', 'e', 'p', '-', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0 + || u32_strcmp (result, expected3) == 0 + || u32_strcmp (result, expected4) == 0); + free (result); + } + + { /* Small precision. */ + uint32_t *result = + my_xasprintf ("%.10a %d", 1.75, 33, 44, 55); + static const uint32_t expected1[] = + { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3', + 0 + }; + static const uint32_t expected2[] = + { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3', + 0 + }; + static const uint32_t expected3[] = + { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3', + 0 + }; + static const uint32_t expected4[] = + { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0 + || u32_strcmp (result, expected3) == 0 + || u32_strcmp (result, expected4) == 0); + free (result); + } + + { /* Large precision. */ + uint32_t *result = + my_xasprintf ("%.50a %d", 1.75, 33, 44, 55); + static const uint32_t expected1[] = + { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3', + 0 + }; + static const uint32_t expected2[] = + { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3', + 0 + }; + static const uint32_t expected3[] = + { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3', + 0 + }; + static const uint32_t expected4[] = + { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0 + || u32_strcmp (result, expected3) == 0 + || u32_strcmp (result, expected4) == 0); + free (result); + } + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%La %d", 3.1416015625L, 33, 44, 55); + static const uint32_t expected1[] = + { '0', 'x', '1', '.', '9', '2', '2', 'p', '+', '1', + ' ', '3', '3', 0 + }; + static const uint32_t expected2[] = + { '0', 'x', '3', '.', '2', '4', '4', 'p', '+', '0', + ' ', '3', '3', 0 + }; + static const uint32_t expected3[] = + { '0', 'x', '6', '.', '4', '8', '8', 'p', '-', '1', + ' ', '3', '3', 0 + }; + static const uint32_t expected4[] = + { '0', 'x', 'c', '.', '9', '1', 'p', '-', '2', ' ', + '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0 + || u32_strcmp (result, expected3) == 0 + || u32_strcmp (result, expected4) == 0); + free (result); + } + + { /* Width. */ + uint32_t *result = + my_xasprintf ("%10La %d", 1.75L, 33, 44, 55); + static const uint32_t expected1[] = + { ' ', ' ', '0', 'x', '1', '.', 'c', 'p', '+', '0', ' ', '3', '3', 0 }; + static const uint32_t expected2[] = + { ' ', ' ', '0', 'x', '3', '.', '8', 'p', '-', '1', ' ', '3', '3', 0 }; + static const uint32_t expected3[] = + { ' ', ' ', ' ', ' ', '0', 'x', '7', 'p', '-', '2', ' ', '3', '3', 0 }; + static const uint32_t expected4[] = + { ' ', ' ', ' ', ' ', '0', 'x', 'e', 'p', '-', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0 + || u32_strcmp (result, expected3) == 0 + || u32_strcmp (result, expected4) == 0); + free (result); + } + + { /* Small precision. */ + uint32_t *result = + my_xasprintf ("%.10La %d", 1.75L, 33, 44, 55); + static const uint32_t expected1[] = + { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3', + 0 + }; + static const uint32_t expected2[] = + { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3', + 0 + }; + static const uint32_t expected3[] = + { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3', + 0 + }; + static const uint32_t expected4[] = + { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0 + || u32_strcmp (result, expected3) == 0 + || u32_strcmp (result, expected4) == 0); + free (result); + } + + { /* Large precision. */ + uint32_t *result = + my_xasprintf ("%.50La %d", 1.75L, 33, 44, 55); + static const uint32_t expected1[] = + { '0', 'x', '1', '.', 'c', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '+', '0', ' ', '3', '3', + 0 + }; + static const uint32_t expected2[] = + { '0', 'x', '3', '.', '8', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '1', ' ', '3', '3', + 0 + }; + static const uint32_t expected3[] = + { '0', 'x', '7', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '2', ' ', '3', '3', + 0 + }; + static const uint32_t expected4[] = + { '0', 'x', 'e', '.', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + '0', '0', '0', '0', 'p', '-', '3', ' ', '3', '3', + 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0 + || u32_strcmp (result, expected3) == 0 + || u32_strcmp (result, expected4) == 0); + free (result); + } + + /* Test the support of the %f format directive. */ + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%f %d", 12.75, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint32_t *result = + my_xasprintf ("%10f %d", 1.75, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint32_t *result = + my_xasprintf ("%.f %d", 1234.0, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '3', '4', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%Lf %d", 12.75L, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint32_t *result = + my_xasprintf ("%10Lf %d", 1.75L, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint32_t *result = + my_xasprintf ("%.Lf %d", 1234.0L, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '3', '4', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %F format directive. */ + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%F %d", 12.75, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint32_t *result = + my_xasprintf ("%.F %d", 1234.0, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '3', '4', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%LF %d", 12.75L, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '.', '7', '5', '0', '0', '0', '0', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint32_t *result = + my_xasprintf ("%.LF %d", 1234.0L, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '3', '4', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %e format directive. */ + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%e %d", 12.75, 33, 44, 55); + static const uint32_t expected1[] = + { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+', + '0', '1', ' ', '3', '3', 0 + }; + static const uint32_t expected2[] = + { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+', + '0', '0', '1', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0); + free (result); + } + + { /* Width. */ + uint32_t *result = + my_xasprintf ("%15e %d", 1.75, 33, 44, 55); + static const uint32_t expected1[] = + { ' ', ' ', ' ', '1', '.', '7', '5', '0', '0', '0', + '0', 'e', '+', '0', '0', ' ', '3', '3', 0 + }; + static const uint32_t expected2[] = + { ' ', ' ', '1', '.', '7', '5', '0', '0', '0', '0', + 'e', '+', '0', '0', '0', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0); + free (result); + } + + { /* Precision. */ + uint32_t *result = + my_xasprintf ("%.e %d", 1234.0, 33, 44, 55); + static const uint32_t expected1[] = + { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 }; + static const uint32_t expected2[] = + { '1', 'e', '+', '0', '0', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0); + free (result); + } + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%Le %d", 12.75L, 33, 44, 55); + static const uint32_t expected[] = + { '1', '.', '2', '7', '5', '0', '0', '0', 'e', '+', + '0', '1', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint32_t *result = + my_xasprintf ("%15Le %d", 1.75L, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', '1', '.', '7', '5', '0', '0', '0', + '0', 'e', '+', '0', '0', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint32_t *result = + my_xasprintf ("%.Le %d", 1234.0L, 33, 44, 55); + static const uint32_t expected[] = + { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %g format directive. */ + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%g %d", 12.75, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '.', '7', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint32_t *result = + my_xasprintf ("%10g %d", 1.75, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', '1', '.', '7', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint32_t *result = + my_xasprintf ("%.g %d", 1234.0, 33, 44, 55); + static const uint32_t expected1[] = + { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 }; + static const uint32_t expected2[] = + { '1', 'e', '+', '0', '0', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected1) == 0 + || u32_strcmp (result, expected2) == 0); + free (result); + } + + { /* A positive number. */ + uint32_t *result = + my_xasprintf ("%Lg %d", 12.75L, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '.', '7', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint32_t *result = + my_xasprintf ("%10Lg %d", 1.75L, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', ' ', '1', '.', '7', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint32_t *result = + my_xasprintf ("%.Lg %d", 1234.0L, 33, 44, 55); + static const uint32_t expected[] = + { '1', 'e', '+', '0', '3', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %n format directive. */ + + { + int count = -1; + uint32_t *result = + my_xasprintf ("%d %n", 123, &count, 33, 44, 55); + static const uint32_t expected[] = + { '1', '2', '3', ' ', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (count == 4); + free (result); + } + + /* Test the support of the POSIX/XSI format strings with positions. */ + + { + uint32_t *result = + my_xasprintf ("%2$d %1$d", 33, 55); + static const uint32_t expected[] = + { '5', '5', ' ', '3', '3', 0 }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the grouping flag. */ + + { + uint32_t *result = + my_xasprintf ("%'d %d", 1234567, 99); + ASSERT (result != NULL); + ASSERT (result[u32_strlen (result) - 1] == '9'); + free (result); + } + + /* Test the support of the 'U' conversion specifier for Unicode strings. */ + + { + static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */ + { + uint32_t *result = + my_xasprintf ("%U %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint32_t *result = + my_xasprintf ("%20U %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint32_t *result = + my_xasprintf ("%-20U %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint32_t *result = + my_xasprintf ("%020U %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint16_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + uint32_t *result = + my_xasprintf ("%lU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint32_t *result = + my_xasprintf ("%20lU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint32_t *result = + my_xasprintf ("%-20lU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint32_t *result = + my_xasprintf ("%020lU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint32_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + uint32_t *result = + my_xasprintf ("%llU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint32_t *result = + my_xasprintf ("%20llU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint32_t *result = + my_xasprintf ("%-20llU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', + 'k', 'o', 'w', 's', 'k', 'i', ' ', ' ', ' ', ' ', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint32_t *result = + my_xasprintf ("%020llU %d", unicode_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', 'R', 'a', 'f', 'a', 0x0142, ' ', + 'M', 'a', 's', 'z', 'k', 'o', 'w', 's', 'k', 'i', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + free (result); + } + } + + /* Test non-ASCII characters in the format string. */ + + { + uint32_t *result = + my_xasprintf ("\304rger", 33, 44, 55); + ASSERT (result == NULL && errno == EINVAL); + } +} diff --git a/tests/unistdio/test-u32-vasnprintf1.c b/tests/unistdio/test-u32-vasnprintf1.c new file mode 100644 index 00000000..959bdab3 --- /dev/null +++ b/tests/unistdio/test-u32-vasnprintf1.c @@ -0,0 +1,88 @@ +/* Test of u32_vasnprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u32-asnprintf1.h" +#include "test-u32-printf1.h" + +static uint32_t * +my_asnprintf (uint32_t *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + uint32_t *ret; + + va_start (args, format); + ret = u32_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static uint32_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + size_t length; + uint32_t *ret; + + va_start (args, format); + ret = u32_vasnprintf (NULL, &length, format, args); + va_end (args); + if (ret != NULL) + ASSERT (length == u32_strlen (ret)); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u32-vasnprintf2.c b/tests/unistdio/test-u32-vasnprintf2.c new file mode 100644 index 00000000..9d8ab007 --- /dev/null +++ b/tests/unistdio/test-u32-vasnprintf2.c @@ -0,0 +1,132 @@ +/* Test of u32_vasnprintf() function in an ISO-8859-1 locale. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +static void +test_function (uint32_t * (*my_asnprintf) (uint32_t *, size_t *, const char *, ...)) +{ + /* Test the support of the 's' conversion specifier for strings. */ + + { + const char *locale_string = "\304rger"; /* Ärger */ + { + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); + static const uint32_t expected[] = + { 0x00c4, 'r', 'g', 'e', 'r', ' ', '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, "%10s %d", locale_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', + ' ', '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, "%-10s %d", locale_string, 33, 44, 55); + static const uint32_t expected[] = + { 0x00c4, 'r', 'g', 'e', 'r', ' ', ' ', ' ', ' ', ' ', + ' ', '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, "%010s %d", locale_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + } +} + +static uint32_t * +my_asnprintf (uint32_t *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + uint32_t *ret; + + va_start (args, format); + ret = u32_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u32-vasnprintf2.sh b/tests/unistdio/test-u32-vasnprintf2.sh new file mode 100755 index 00000000..ae39dd48 --- /dev/null +++ b/tests/unistdio/test-u32-vasnprintf2.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Test whether a traditional french locale is installed. +: ${LOCALE_FR=fr_FR} +if test $LOCALE_FR != none; then + testlocale=$LOCALE_FR +else + if test -f /usr/bin/localedef; then + echo "Skipping test: no traditional french locale is installed" + else + echo "Skipping test: no traditional french locale is supported" + fi + exit 77 +fi + +LC_ALL=$testlocale \ +./test-u32-vasnprintf1${EXEEXT} \ + || exit 1 + +LC_ALL=$testlocale \ +./test-u32-vasnprintf2${EXEEXT} diff --git a/tests/unistdio/test-u32-vasnprintf3.c b/tests/unistdio/test-u32-vasnprintf3.c new file mode 100644 index 00000000..73f7c6c7 --- /dev/null +++ b/tests/unistdio/test-u32-vasnprintf3.c @@ -0,0 +1,132 @@ +/* Test of u32_vasnprintf() function in an UTF-8 locale. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +static void +test_function (uint32_t * (*my_asnprintf) (uint32_t *, size_t *, const char *, ...)) +{ + /* Test the support of the 's' conversion specifier for strings. */ + + { + const char *locale_string = "\303\204rger"; /* Ärger */ + { + size_t length; + uint32_t *result = + my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); + static const uint32_t expected[] = + { 0x00c4, 'r', 'g', 'e', 'r', ' ', '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, "%10s %d", locale_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', + ' ', '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, "%-10s %d", locale_string, 33, 44, 55); + static const uint32_t expected[] = + { 0x00c4, 'r', 'g', 'e', 'r', ' ', ' ', ' ', ' ', ' ', + ' ', '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, "%010s %d", locale_string, 33, 44, 55); + static const uint32_t expected[] = + { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r', + ' ', '3', '3', 0 + }; + ASSERT (result != NULL); + ASSERT (u32_strcmp (result, expected) == 0); + ASSERT (length == u32_strlen (result)); + free (result); + } + } +} + +static uint32_t * +my_asnprintf (uint32_t *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + uint32_t *ret; + + va_start (args, format); + ret = u32_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u32-vasnprintf3.sh b/tests/unistdio/test-u32-vasnprintf3.sh new file mode 100755 index 00000000..d2c31170 --- /dev/null +++ b/tests/unistdio/test-u32-vasnprintf3.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Test whether a french Unicode locale is installed. +: ${LOCALE_FR_UTF8=fr_FR.UTF-8} +if test $LOCALE_FR_UTF8 != none; then + testlocale=$LOCALE_FR_UTF8 +else + if test -f /usr/bin/localedef; then + echo "Skipping test: no french Unicode locale is installed" + else + echo "Skipping test: no french Unicode locale is supported" + fi + exit 77 +fi + +LC_ALL=$testlocale \ +./test-u32-vasnprintf1${EXEEXT} \ + || exit 1 + +LC_ALL=$testlocale \ +./test-u32-vasnprintf3${EXEEXT} diff --git a/tests/unistdio/test-u32-vasprintf1.c b/tests/unistdio/test-u32-vasprintf1.c new file mode 100644 index 00000000..39e68d25 --- /dev/null +++ b/tests/unistdio/test-u32-vasprintf1.c @@ -0,0 +1,75 @@ +/* Test of u32_vasprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u32-printf1.h" + +static uint32_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + uint32_t *result; + int retval; + + va_start (args, format); + retval = u32_vasprintf (&result, format, args); + va_end (args); + if (retval < 0) + return NULL; + ASSERT (result != NULL); + return result; +} + +static void +test_vasprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + test_vasprintf (); + return 0; +} diff --git a/tests/unistdio/test-u32-vsnprintf1.c b/tests/unistdio/test-u32-vsnprintf1.c new file mode 100644 index 00000000..d7727081 --- /dev/null +++ b/tests/unistdio/test-u32-vsnprintf1.c @@ -0,0 +1,84 @@ +/* Test of u32_vsnprintf() function. + Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" +#include "xalloc.h" +#include "progname.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u32-printf1.h" + +static uint32_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + uint32_t buf[1000]; + int retval; + size_t length; + uint32_t *result; + + va_start (args, format); + retval = u32_vsnprintf (buf, sizeof (buf), format, args); + va_end (args); + if (retval < 0 || retval >= (int) sizeof (buf)) + return NULL; + length = u32_strlen (buf); + result = XNMALLOC (length + 1, uint32_t); + u32_cpy (result, buf, length + 1); + return result; +} + +static void +test_vsnprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + set_program_name (argv[0]); + + test_vsnprintf (); + + return 0; +} diff --git a/tests/unistdio/test-u32-vsprintf1.c b/tests/unistdio/test-u32-vsprintf1.c new file mode 100644 index 00000000..8398b274 --- /dev/null +++ b/tests/unistdio/test-u32-vsprintf1.c @@ -0,0 +1,84 @@ +/* Test of u32_vsprintf() function. + Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" +#include "xalloc.h" +#include "progname.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u32-printf1.h" + +static uint32_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + uint32_t buf[1000]; + int retval; + size_t length; + uint32_t *result; + + va_start (args, format); + retval = u32_vsprintf (buf, format, args); + va_end (args); + if (retval < 0 || retval >= (int) sizeof (buf)) + return NULL; + length = u32_strlen (buf); + result = XNMALLOC (length + 1, uint32_t); + u32_cpy (result, buf, length + 1); + return result; +} + +static void +test_vsprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + set_program_name (argv[0]); + + test_vsprintf (); + + return 0; +} diff --git a/tests/unistdio/test-u8-asnprintf1.c b/tests/unistdio/test-u8-asnprintf1.c new file mode 100644 index 00000000..852d76b4 --- /dev/null +++ b/tests/unistdio/test-u8-asnprintf1.c @@ -0,0 +1,59 @@ +/* Test of u8_asnprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u8-asnprintf1.h" + +static void +test_asnprintf () +{ + test_function (u8_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + test_asnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u8-asnprintf1.h b/tests/unistdio/test-u8-asnprintf1.h new file mode 100644 index 00000000..063f145f --- /dev/null +++ b/tests/unistdio/test-u8-asnprintf1.h @@ -0,0 +1,57 @@ +/* Test of u8_[v]asnprintf() function. + Copyright (C) 2007 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +static void +test_function (uint8_t * (*my_asnprintf) (uint8_t *, size_t *, const char *, ...)) +{ + uint8_t buf[8]; + int size; + + /* Test return value convention. */ + + for (size = 0; size <= 8; size++) + { + size_t length = size; + uint8_t *result = my_asnprintf (NULL, &length, "%d", 12345); + static const uint8_t expected[] = "12345"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == 5); + free (result); + } + + for (size = 0; size <= 8; size++) + { + static const uint8_t initializer[] = "DEADBEEF"; + static const uint8_t expected[] = "12345"; + size_t length; + uint8_t *result; + + u8_cpy (buf, initializer, 8); + length = size; + result = my_asnprintf (buf, &length, "%d", 12345); + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == 5); + if (size < 6) + ASSERT (result != buf); + ASSERT (u8_cmp (buf + size, initializer + size, 8 - size) == 0); + if (result != buf) + free (result); + } +} diff --git a/tests/unistdio/test-u8-printf1.h b/tests/unistdio/test-u8-printf1.h new file mode 100644 index 00000000..4ac6573f --- /dev/null +++ b/tests/unistdio/test-u8-printf1.h @@ -0,0 +1,704 @@ +/* Test of u8_v[a]s[n]printf() function. + Copyright (C) 2007 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +static void +test_xfunction (uint8_t * (*my_xasprintf) (const char *, ...)) +{ + /* Test support of size specifiers as in C99. */ + + { + uint8_t *result = + my_xasprintf ("%ju %d", (uintmax_t) 12345671, 33, 44, 55); + static const uint8_t expected[] = "12345671 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { + uint8_t *result = + my_xasprintf ("%zu %d", (size_t) 12345672, 33, 44, 55); + static const uint8_t expected[] = "12345672 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { + uint8_t *result = + my_xasprintf ("%tu %d", (ptrdiff_t) 12345673, 33, 44, 55); + static const uint8_t expected[] = "12345673 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { + uint8_t *result = + my_xasprintf ("%Lg %d", (long double) 1.5, 33, 44, 55); + static const uint8_t expected[] = "1.5 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the 'U' conversion specifier for Unicode strings. */ + + { + static const uint8_t unicode_string[] = "Hello"; + { + uint8_t *result = + my_xasprintf ("%U %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint8_t *result = + my_xasprintf ("%10U %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint8_t *result = + my_xasprintf ("%-10U %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint8_t *result = + my_xasprintf ("%010U %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint16_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 }; + { + uint8_t *result = + my_xasprintf ("%lU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint8_t *result = + my_xasprintf ("%10lU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint8_t *result = + my_xasprintf ("%-10lU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint8_t *result = + my_xasprintf ("%010lU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint32_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 }; + { + uint8_t *result = + my_xasprintf ("%llU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint8_t *result = + my_xasprintf ("%10llU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint8_t *result = + my_xasprintf ("%-10llU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint8_t *result = + my_xasprintf ("%010llU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Hello 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + } + + /* Test the support of the 's' conversion specifier for strings. */ + + { + uint8_t *result = + my_xasprintf ("Mr. %s %d", "Ronald Reagan", 33, 44, 55); + static const uint8_t expected[] = "Mr. Ronald Reagan 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint8_t *result = + my_xasprintf ("Mr. %20s %d", "Ronald Reagan", 33, 44, 55); + static const uint8_t expected[] = "Mr. Ronald Reagan 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* FLAG_LEFT. */ + uint8_t *result = + my_xasprintf ("Mr. %-20s %d", "Ronald Reagan", 33, 44, 55); + static const uint8_t expected[] = "Mr. Ronald Reagan 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* FLAG_ZERO: no effect. */ + uint8_t *result = + my_xasprintf ("Mr. %020s %d", "Ronald Reagan", 33, 44, 55); + static const uint8_t expected[] = "Mr. Ronald Reagan 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal + output of floating-point numbers. */ + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%a %d", 3.1416015625, 33, 44, 55); + static const uint8_t expected1[] = "0x1.922p+1 33"; + static const uint8_t expected2[] = "0x3.244p+0 33"; + static const uint8_t expected3[] = "0x6.488p-1 33"; + static const uint8_t expected4[] = "0xc.91p-2 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0 + || u8_strcmp (result, expected3) == 0 + || u8_strcmp (result, expected4) == 0); + free (result); + } + + { /* Width. */ + uint8_t *result = + my_xasprintf ("%10a %d", 1.75, 33, 44, 55); + static const uint8_t expected1[] = " 0x1.cp+0 33"; + static const uint8_t expected2[] = " 0x3.8p-1 33"; + static const uint8_t expected3[] = " 0x7p-2 33"; + static const uint8_t expected4[] = " 0xep-3 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0 + || u8_strcmp (result, expected3) == 0 + || u8_strcmp (result, expected4) == 0); + free (result); + } + + { /* Small precision. */ + uint8_t *result = + my_xasprintf ("%.10a %d", 1.75, 33, 44, 55); + static const uint8_t expected1[] = "0x1.c000000000p+0 33"; + static const uint8_t expected2[] = "0x3.8000000000p-1 33"; + static const uint8_t expected3[] = "0x7.0000000000p-2 33"; + static const uint8_t expected4[] = "0xe.0000000000p-3 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0 + || u8_strcmp (result, expected3) == 0 + || u8_strcmp (result, expected4) == 0); + free (result); + } + + { /* Large precision. */ + uint8_t *result = + my_xasprintf ("%.50a %d", 1.75, 33, 44, 55); + static const uint8_t expected1[] = "0x1.c0000000000000000000000000000000000000000000000000p+0 33"; + static const uint8_t expected2[] = "0x3.80000000000000000000000000000000000000000000000000p-1 33"; + static const uint8_t expected3[] = "0x7.00000000000000000000000000000000000000000000000000p-2 33"; + static const uint8_t expected4[] = "0xe.00000000000000000000000000000000000000000000000000p-3 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0 + || u8_strcmp (result, expected3) == 0 + || u8_strcmp (result, expected4) == 0); + free (result); + } + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%La %d", 3.1416015625L, 33, 44, 55); + static const uint8_t expected1[] = "0x1.922p+1 33"; + static const uint8_t expected2[] = "0x3.244p+0 33"; + static const uint8_t expected3[] = "0x6.488p-1 33"; + static const uint8_t expected4[] = "0xc.91p-2 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0 + || u8_strcmp (result, expected3) == 0 + || u8_strcmp (result, expected4) == 0); + free (result); + } + + { /* Width. */ + uint8_t *result = + my_xasprintf ("%10La %d", 1.75L, 33, 44, 55); + static const uint8_t expected1[] = " 0x1.cp+0 33"; + static const uint8_t expected2[] = " 0x3.8p-1 33"; + static const uint8_t expected3[] = " 0x7p-2 33"; + static const uint8_t expected4[] = " 0xep-3 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0 + || u8_strcmp (result, expected3) == 0 + || u8_strcmp (result, expected4) == 0); + free (result); + } + + { /* Small precision. */ + uint8_t *result = + my_xasprintf ("%.10La %d", 1.75L, 33, 44, 55); + static const uint8_t expected1[] = "0x1.c000000000p+0 33"; + static const uint8_t expected2[] = "0x3.8000000000p-1 33"; + static const uint8_t expected3[] = "0x7.0000000000p-2 33"; + static const uint8_t expected4[] = "0xe.0000000000p-3 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0 + || u8_strcmp (result, expected3) == 0 + || u8_strcmp (result, expected4) == 0); + free (result); + } + + { /* Large precision. */ + uint8_t *result = + my_xasprintf ("%.50La %d", 1.75L, 33, 44, 55); + static const uint8_t expected1[] = "0x1.c0000000000000000000000000000000000000000000000000p+0 33"; + static const uint8_t expected2[] = "0x3.80000000000000000000000000000000000000000000000000p-1 33"; + static const uint8_t expected3[] = "0x7.00000000000000000000000000000000000000000000000000p-2 33"; + static const uint8_t expected4[] = "0xe.00000000000000000000000000000000000000000000000000p-3 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0 + || u8_strcmp (result, expected3) == 0 + || u8_strcmp (result, expected4) == 0); + free (result); + } + + /* Test the support of the %f format directive. */ + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%f %d", 12.75, 33, 44, 55); + static const uint8_t expected[] = "12.750000 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint8_t *result = + my_xasprintf ("%10f %d", 1.75, 33, 44, 55); + static const uint8_t expected[] = " 1.750000 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint8_t *result = + my_xasprintf ("%.f %d", 1234.0, 33, 44, 55); + static const uint8_t expected[] = "1234 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%Lf %d", 12.75L, 33, 44, 55); + static const uint8_t expected[] = "12.750000 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint8_t *result = + my_xasprintf ("%10Lf %d", 1.75L, 33, 44, 55); + static const uint8_t expected[] = " 1.750000 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint8_t *result = + my_xasprintf ("%.Lf %d", 1234.0L, 33, 44, 55); + static const uint8_t expected[] = "1234 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %F format directive. */ + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%F %d", 12.75, 33, 44, 55); + static const uint8_t expected[] = "12.750000 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint8_t *result = + my_xasprintf ("%.F %d", 1234.0, 33, 44, 55); + static const uint8_t expected[] = "1234 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%LF %d", 12.75L, 33, 44, 55); + static const uint8_t expected[] = "12.750000 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint8_t *result = + my_xasprintf ("%.LF %d", 1234.0L, 33, 44, 55); + static const uint8_t expected[] = "1234 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %e format directive. */ + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%e %d", 12.75, 33, 44, 55); + static const uint8_t expected1[] = "1.275000e+01 33"; + static const uint8_t expected2[] = "1.275000e+001 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0); + free (result); + } + + { /* Width. */ + uint8_t *result = + my_xasprintf ("%15e %d", 1.75, 33, 44, 55); + static const uint8_t expected1[] = " 1.750000e+00 33"; + static const uint8_t expected2[] = " 1.750000e+000 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0); + free (result); + } + + { /* Precision. */ + uint8_t *result = + my_xasprintf ("%.e %d", 1234.0, 33, 44, 55); + static const uint8_t expected1[] = "1e+03 33"; + static const uint8_t expected2[] = "1e+003 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0); + free (result); + } + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%Le %d", 12.75L, 33, 44, 55); + static const uint8_t expected[] = "1.275000e+01 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint8_t *result = + my_xasprintf ("%15Le %d", 1.75L, 33, 44, 55); + static const uint8_t expected[] = " 1.750000e+00 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint8_t *result = + my_xasprintf ("%.Le %d", 1234.0L, 33, 44, 55); + static const uint8_t expected[] = "1e+03 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %g format directive. */ + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%g %d", 12.75, 33, 44, 55); + static const uint8_t expected[] = "12.75 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint8_t *result = + my_xasprintf ("%10g %d", 1.75, 33, 44, 55); + static const uint8_t expected[] = " 1.75 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint8_t *result = + my_xasprintf ("%.g %d", 1234.0, 33, 44, 55); + static const uint8_t expected1[] = "1e+03 33"; + static const uint8_t expected2[] = "1e+003 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected1) == 0 + || u8_strcmp (result, expected2) == 0); + free (result); + } + + { /* A positive number. */ + uint8_t *result = + my_xasprintf ("%Lg %d", 12.75L, 33, 44, 55); + static const uint8_t expected[] = "12.75 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Width. */ + uint8_t *result = + my_xasprintf ("%10Lg %d", 1.75L, 33, 44, 55); + static const uint8_t expected[] = " 1.75 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + { /* Precision. */ + uint8_t *result = + my_xasprintf ("%.Lg %d", 1234.0L, 33, 44, 55); + static const uint8_t expected[] = "1e+03 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the %n format directive. */ + + { + int count = -1; + uint8_t *result = + my_xasprintf ("%d %n", 123, &count, 33, 44, 55); + static const uint8_t expected[] = "123 "; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (count == 4); + free (result); + } + + /* Test the support of the POSIX/XSI format strings with positions. */ + + { + uint8_t *result = + my_xasprintf ("%2$d %1$d", 33, 55); + static const uint8_t expected[] = "55 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + + /* Test the support of the grouping flag. */ + + { + uint8_t *result = + my_xasprintf ("%'d %d", 1234567, 99); + ASSERT (result != NULL); + ASSERT (result[u8_strlen (result) - 1] == '9'); + free (result); + } + + /* Test the support of the 'U' conversion specifier for Unicode strings. */ + + { + static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */ + { + uint8_t *result = + my_xasprintf ("%U %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint8_t *result = + my_xasprintf ("%20U %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint8_t *result = + my_xasprintf ("%-20U %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint8_t *result = + my_xasprintf ("%020U %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint16_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + uint8_t *result = + my_xasprintf ("%lU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint8_t *result = + my_xasprintf ("%20lU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint8_t *result = + my_xasprintf ("%-20lU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint8_t *result = + my_xasprintf ("%020lU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + } + + { + static const uint32_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + uint8_t *result = + my_xasprintf ("%llU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* Width. */ + uint8_t *result = + my_xasprintf ("%20llU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_LEFT. */ + uint8_t *result = + my_xasprintf ("%-20llU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = "Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + uint8_t *result = + my_xasprintf ("%020llU %d", unicode_string, 33, 44, 55); + static const uint8_t expected[] = " Rafa\305\202 Maszkowski 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + free (result); + } + } + + /* Test non-ASCII characters in the format string. */ + + { + uint8_t *result = + my_xasprintf ("\304rger", 33, 44, 55); + ASSERT (result == NULL && errno == EINVAL); + } +} diff --git a/tests/unistdio/test-u8-vasnprintf1.c b/tests/unistdio/test-u8-vasnprintf1.c new file mode 100644 index 00000000..3329df40 --- /dev/null +++ b/tests/unistdio/test-u8-vasnprintf1.c @@ -0,0 +1,88 @@ +/* Test of u8_vasnprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u8-asnprintf1.h" +#include "test-u8-printf1.h" + +static uint8_t * +my_asnprintf (uint8_t *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + uint8_t *ret; + + va_start (args, format); + ret = u8_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static uint8_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + size_t length; + uint8_t *ret; + + va_start (args, format); + ret = u8_vasnprintf (NULL, &length, format, args); + va_end (args); + if (ret != NULL) + ASSERT (length == u8_strlen (ret)); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u8-vasnprintf2.c b/tests/unistdio/test-u8-vasnprintf2.c new file mode 100644 index 00000000..4283be69 --- /dev/null +++ b/tests/unistdio/test-u8-vasnprintf2.c @@ -0,0 +1,122 @@ +/* Test of u8_vasnprintf() function in an ISO-8859-1 locale. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +static void +test_function (uint8_t * (*my_asnprintf) (uint8_t *, size_t *, const char *, ...)) +{ + /* Test the support of the 's' conversion specifier for strings. */ + + { + const char *locale_string = "\304rger"; /* Ärger */ + { + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); + static const uint8_t expected[] = "\303\204rger 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, "%10s %d", locale_string, 33, 44, 55); + static const uint8_t expected[] = " \303\204rger 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, "%-10s %d", locale_string, 33, 44, 55); + static const uint8_t expected[] = "\303\204rger 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, "%010s %d", locale_string, 33, 44, 55); + static const uint8_t expected[] = " \303\204rger 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + } +} + +static uint8_t * +my_asnprintf (uint8_t *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + uint8_t *ret; + + va_start (args, format); + ret = u8_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u8-vasnprintf2.sh b/tests/unistdio/test-u8-vasnprintf2.sh new file mode 100755 index 00000000..ea5fcb08 --- /dev/null +++ b/tests/unistdio/test-u8-vasnprintf2.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Test whether a traditional french locale is installed. +: ${LOCALE_FR=fr_FR} +if test $LOCALE_FR != none; then + testlocale=$LOCALE_FR +else + if test -f /usr/bin/localedef; then + echo "Skipping test: no traditional french locale is installed" + else + echo "Skipping test: no traditional french locale is supported" + fi + exit 77 +fi + +LC_ALL=$testlocale \ +./test-u8-vasnprintf1${EXEEXT} \ + || exit 1 + +LC_ALL=$testlocale \ +./test-u8-vasnprintf2${EXEEXT} diff --git a/tests/unistdio/test-u8-vasnprintf3.c b/tests/unistdio/test-u8-vasnprintf3.c new file mode 100644 index 00000000..fc645bca --- /dev/null +++ b/tests/unistdio/test-u8-vasnprintf3.c @@ -0,0 +1,122 @@ +/* Test of u8_vasnprintf() function in an UTF-8 locale. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +static void +test_function (uint8_t * (*my_asnprintf) (uint8_t *, size_t *, const char *, ...)) +{ + /* Test the support of the 's' conversion specifier for strings. */ + + { + const char *locale_string = "\303\204rger"; /* Ärger */ + { + size_t length; + uint8_t *result = + my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); + static const uint8_t expected[] = "\303\204rger 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, "%10s %d", locale_string, 33, 44, 55); + static const uint8_t expected[] = " \303\204rger 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, "%-10s %d", locale_string, 33, 44, 55); + static const uint8_t expected[] = "\303\204rger 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, "%010s %d", locale_string, 33, 44, 55); + static const uint8_t expected[] = " \303\204rger 33"; + ASSERT (result != NULL); + ASSERT (u8_strcmp (result, expected) == 0); + ASSERT (length == u8_strlen (result)); + free (result); + } + } +} + +static uint8_t * +my_asnprintf (uint8_t *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + uint8_t *ret; + + va_start (args, format); + ret = u8_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-u8-vasnprintf3.sh b/tests/unistdio/test-u8-vasnprintf3.sh new file mode 100755 index 00000000..9df0044a --- /dev/null +++ b/tests/unistdio/test-u8-vasnprintf3.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Test whether a french Unicode locale is installed. +: ${LOCALE_FR_UTF8=fr_FR.UTF-8} +if test $LOCALE_FR_UTF8 != none; then + testlocale=$LOCALE_FR_UTF8 +else + if test -f /usr/bin/localedef; then + echo "Skipping test: no french Unicode locale is installed" + else + echo "Skipping test: no french Unicode locale is supported" + fi + exit 77 +fi + +LC_ALL=$testlocale \ +./test-u8-vasnprintf1${EXEEXT} \ + || exit 1 + +LC_ALL=$testlocale \ +./test-u8-vasnprintf3${EXEEXT} diff --git a/tests/unistdio/test-u8-vasprintf1.c b/tests/unistdio/test-u8-vasprintf1.c new file mode 100644 index 00000000..8a2caae8 --- /dev/null +++ b/tests/unistdio/test-u8-vasprintf1.c @@ -0,0 +1,75 @@ +/* Test of u8_vasprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u8-printf1.h" + +static uint8_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + uint8_t *result; + int retval; + + va_start (args, format); + retval = u8_vasprintf (&result, format, args); + va_end (args); + if (retval < 0) + return NULL; + ASSERT (result != NULL); + return result; +} + +static void +test_vasprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + test_vasprintf (); + return 0; +} diff --git a/tests/unistdio/test-u8-vsnprintf1.c b/tests/unistdio/test-u8-vsnprintf1.c new file mode 100644 index 00000000..6dd7439d --- /dev/null +++ b/tests/unistdio/test-u8-vsnprintf1.c @@ -0,0 +1,84 @@ +/* Test of u8_vsnprintf() function. + Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" +#include "xalloc.h" +#include "progname.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u8-printf1.h" + +static uint8_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + uint8_t buf[1000]; + int retval; + size_t length; + uint8_t *result; + + va_start (args, format); + retval = u8_vsnprintf (buf, sizeof (buf), format, args); + va_end (args); + if (retval < 0 || retval >= (int) sizeof (buf)) + return NULL; + length = u8_strlen (buf); + result = XNMALLOC (length + 1, uint8_t); + u8_cpy (result, buf, length + 1); + return result; +} + +static void +test_vsnprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + set_program_name (argv[0]); + + test_vsnprintf (); + + return 0; +} diff --git a/tests/unistdio/test-u8-vsprintf1.c b/tests/unistdio/test-u8-vsprintf1.c new file mode 100644 index 00000000..9559df0c --- /dev/null +++ b/tests/unistdio/test-u8-vsprintf1.c @@ -0,0 +1,84 @@ +/* Test of u8_vsprintf() function. + Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <errno.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "unistr.h" +#include "xalloc.h" +#include "progname.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-u8-printf1.h" + +static uint8_t * +my_xasprintf (const char *format, ...) +{ + va_list args; + uint8_t buf[1000]; + int retval; + size_t length; + uint8_t *result; + + va_start (args, format); + retval = u8_vsprintf (buf, format, args); + va_end (args); + if (retval < 0 || retval >= (int) sizeof (buf)) + return NULL; + length = u8_strlen (buf); + result = XNMALLOC (length + 1, uint8_t); + u8_cpy (result, buf, length + 1); + return result; +} + +static void +test_vsprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + set_program_name (argv[0]); + + test_vsprintf (); + + return 0; +} diff --git a/tests/unistdio/test-ulc-asnprintf1.c b/tests/unistdio/test-ulc-asnprintf1.c new file mode 100644 index 00000000..591ae262 --- /dev/null +++ b/tests/unistdio/test-ulc-asnprintf1.c @@ -0,0 +1,56 @@ +/* Test of ulc_asnprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-ulc-asnprintf1.h" + +static void +test_asnprintf () +{ + test_function (ulc_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + test_asnprintf (); + return 0; +} diff --git a/tests/unistdio/test-ulc-asnprintf1.h b/tests/unistdio/test-ulc-asnprintf1.h new file mode 100644 index 00000000..285a4062 --- /dev/null +++ b/tests/unistdio/test-ulc-asnprintf1.h @@ -0,0 +1,54 @@ +/* Test of ulc_[v]asnprintf() functions. + Copyright (C) 2007 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +static void +test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) +{ + char buf[8]; + int size; + + /* Test return value convention. */ + + for (size = 0; size <= 8; size++) + { + size_t length = size; + char *result = my_asnprintf (NULL, &length, "%d", 12345); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12345") == 0); + ASSERT (length == 5); + free (result); + } + + for (size = 0; size <= 8; size++) + { + size_t length; + char *result; + + memcpy (buf, "DEADBEEF", 8); + length = size; + result = my_asnprintf (buf, &length, "%d", 12345); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12345") == 0); + ASSERT (length == 5); + if (size < 6) + ASSERT (result != buf); + ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0); + if (result != buf) + free (result); + } +} diff --git a/tests/unistdio/test-ulc-printf1.h b/tests/unistdio/test-ulc-printf1.h new file mode 100644 index 00000000..77516d61 --- /dev/null +++ b/tests/unistdio/test-ulc-printf1.h @@ -0,0 +1,498 @@ +/* Test of ulc_v[a]s[n]printf() functions. + Copyright (C) 2007 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +static void +test_xfunction (char * (*my_xasprintf) (const char *, ...)) +{ + /* Test support of size specifiers as in C99. */ + + { + char *result = + my_xasprintf ("%ju %d", (uintmax_t) 12345671, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12345671 33") == 0); + free (result); + } + + { + char *result = + my_xasprintf ("%zu %d", (size_t) 12345672, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12345672 33") == 0); + free (result); + } + + { + char *result = + my_xasprintf ("%tu %d", (ptrdiff_t) 12345673, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12345673 33") == 0); + free (result); + } + + { + char *result = + my_xasprintf ("%Lg %d", (long double) 1.5, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1.5 33") == 0); + free (result); + } + + /* Test the support of the 'U' conversion specifier for Unicode strings. */ + + { + static const uint8_t unicode_string[] = "Hello"; + { + char *result = + my_xasprintf ("%U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Hello 33") == 0); + free (result); + } + { /* Width. */ + char *result = + my_xasprintf ("%10U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Hello 33") == 0); + free (result); + } + { /* FLAG_LEFT. */ + char *result = + my_xasprintf ("%-10U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Hello 33") == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + char *result = + my_xasprintf ("%010U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Hello 33") == 0); + free (result); + } + } + + { + static const uint16_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 }; + { + char *result = + my_xasprintf ("%lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Hello 33") == 0); + free (result); + } + { /* Width. */ + char *result = + my_xasprintf ("%10lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Hello 33") == 0); + free (result); + } + { /* FLAG_LEFT. */ + char *result = + my_xasprintf ("%-10lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Hello 33") == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + char *result = + my_xasprintf ("%010lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Hello 33") == 0); + free (result); + } + } + + { + static const uint32_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 }; + { + char *result = + my_xasprintf ("%llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Hello 33") == 0); + free (result); + } + { /* Width. */ + char *result = + my_xasprintf ("%10llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Hello 33") == 0); + free (result); + } + { /* FLAG_LEFT. */ + char *result = + my_xasprintf ("%-10llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Hello 33") == 0); + free (result); + } + { /* FLAG_ZERO: no effect. */ + char *result = + my_xasprintf ("%010llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Hello 33") == 0); + free (result); + } + } + + /* Test the support of the 's' conversion specifier for strings. */ + + { + char *result = + my_xasprintf ("Mr. %s %d", "Ronald Reagan", 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Mr. Ronald Reagan 33") == 0); + free (result); + } + + { /* Width. */ + char *result = + my_xasprintf ("Mr. %20s %d", "Ronald Reagan", 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Mr. Ronald Reagan 33") == 0); + free (result); + } + + { /* FLAG_LEFT. */ + char *result = + my_xasprintf ("Mr. %-20s %d", "Ronald Reagan", 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Mr. Ronald Reagan 33") == 0); + free (result); + } + + { /* FLAG_ZERO: no effect. */ + char *result = + my_xasprintf ("Mr. %020s %d", "Ronald Reagan", 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Mr. Ronald Reagan 33") == 0); + free (result); + } + + /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal + output of floating-point numbers. */ + + { /* A positive number. */ + char *result = + my_xasprintf ("%a %d", 3.1416015625, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "0x1.922p+1 33") == 0 + || strcmp (result, "0x3.244p+0 33") == 0 + || strcmp (result, "0x6.488p-1 33") == 0 + || strcmp (result, "0xc.91p-2 33") == 0); + free (result); + } + + { /* Width. */ + char *result = + my_xasprintf ("%10a %d", 1.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 0x1.cp+0 33") == 0 + || strcmp (result, " 0x3.8p-1 33") == 0 + || strcmp (result, " 0x7p-2 33") == 0 + || strcmp (result, " 0xep-3 33") == 0); + free (result); + } + + { /* Small precision. */ + char *result = + my_xasprintf ("%.10a %d", 1.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "0x1.c000000000p+0 33") == 0 + || strcmp (result, "0x3.8000000000p-1 33") == 0 + || strcmp (result, "0x7.0000000000p-2 33") == 0 + || strcmp (result, "0xe.0000000000p-3 33") == 0); + free (result); + } + + { /* Large precision. */ + char *result = + my_xasprintf ("%.50a %d", 1.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "0x1.c0000000000000000000000000000000000000000000000000p+0 33") == 0 + || strcmp (result, "0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0 + || strcmp (result, "0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0 + || strcmp (result, "0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0); + free (result); + } + + { /* A positive number. */ + char *result = + my_xasprintf ("%La %d", 3.1416015625L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "0x1.922p+1 33") == 0 + || strcmp (result, "0x3.244p+0 33") == 0 + || strcmp (result, "0x6.488p-1 33") == 0 + || strcmp (result, "0xc.91p-2 33") == 0); + free (result); + } + + { /* Width. */ + char *result = + my_xasprintf ("%10La %d", 1.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 0x1.cp+0 33") == 0 + || strcmp (result, " 0x3.8p-1 33") == 0 + || strcmp (result, " 0x7p-2 33") == 0 + || strcmp (result, " 0xep-3 33") == 0); + free (result); + } + + { /* Small precision. */ + char *result = + my_xasprintf ("%.10La %d", 1.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "0x1.c000000000p+0 33") == 0 + || strcmp (result, "0x3.8000000000p-1 33") == 0 + || strcmp (result, "0x7.0000000000p-2 33") == 0 + || strcmp (result, "0xe.0000000000p-3 33") == 0); + free (result); + } + + { /* Large precision. */ + char *result = + my_xasprintf ("%.50La %d", 1.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "0x1.c0000000000000000000000000000000000000000000000000p+0 33") == 0 + || strcmp (result, "0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0 + || strcmp (result, "0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0 + || strcmp (result, "0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0); + free (result); + } + + /* Test the support of the %f format directive. */ + + { /* A positive number. */ + char *result = + my_xasprintf ("%f %d", 12.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12.750000 33") == 0); + free (result); + } + + { /* Width. */ + char *result = + my_xasprintf ("%10f %d", 1.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 1.750000 33") == 0); + free (result); + } + + { /* Precision. */ + char *result = + my_xasprintf ("%.f %d", 1234.0, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1234 33") == 0); + free (result); + } + + { /* A positive number. */ + char *result = + my_xasprintf ("%Lf %d", 12.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12.750000 33") == 0); + free (result); + } + + { /* Width. */ + char *result = + my_xasprintf ("%10Lf %d", 1.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 1.750000 33") == 0); + free (result); + } + + { /* Precision. */ + char *result = + my_xasprintf ("%.Lf %d", 1234.0L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1234 33") == 0); + free (result); + } + + /* Test the support of the %F format directive. */ + + { /* A positive number. */ + char *result = + my_xasprintf ("%F %d", 12.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12.750000 33") == 0); + free (result); + } + + { /* Precision. */ + char *result = + my_xasprintf ("%.F %d", 1234.0, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1234 33") == 0); + free (result); + } + + { /* A positive number. */ + char *result = + my_xasprintf ("%LF %d", 12.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12.750000 33") == 0); + free (result); + } + + { /* Precision. */ + char *result = + my_xasprintf ("%.LF %d", 1234.0L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1234 33") == 0); + free (result); + } + + /* Test the support of the %e format directive. */ + + { /* A positive number. */ + char *result = + my_xasprintf ("%e %d", 12.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1.275000e+01 33") == 0 + || strcmp (result, "1.275000e+001 33") == 0); + free (result); + } + + { /* Width. */ + char *result = + my_xasprintf ("%15e %d", 1.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 1.750000e+00 33") == 0 + || strcmp (result, " 1.750000e+000 33") == 0); + free (result); + } + + { /* Precision. */ + char *result = + my_xasprintf ("%.e %d", 1234.0, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1e+03 33") == 0 + || strcmp (result, "1e+003 33") == 0); + free (result); + } + + { /* A positive number. */ + char *result = + my_xasprintf ("%Le %d", 12.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1.275000e+01 33") == 0); + free (result); + } + + { /* Width. */ + char *result = + my_xasprintf ("%15Le %d", 1.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 1.750000e+00 33") == 0); + free (result); + } + + { /* Precision. */ + char *result = + my_xasprintf ("%.Le %d", 1234.0L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1e+03 33") == 0); + free (result); + } + + /* Test the support of the %g format directive. */ + + { /* A positive number. */ + char *result = + my_xasprintf ("%g %d", 12.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12.75 33") == 0); + free (result); + } + + { /* Width. */ + char *result = + my_xasprintf ("%10g %d", 1.75, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 1.75 33") == 0); + free (result); + } + + { /* Precision. */ + char *result = + my_xasprintf ("%.g %d", 1234.0, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1e+03 33") == 0 + || strcmp (result, "1e+003 33") == 0); + free (result); + } + + { /* A positive number. */ + char *result = + my_xasprintf ("%Lg %d", 12.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "12.75 33") == 0); + free (result); + } + + { /* Width. */ + char *result = + my_xasprintf ("%10Lg %d", 1.75L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 1.75 33") == 0); + free (result); + } + + { /* Precision. */ + char *result = + my_xasprintf ("%.Lg %d", 1234.0L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1e+03 33") == 0); + free (result); + } + + /* Test the support of the %n format directive. */ + + { + int count = -1; + char *result = + my_xasprintf ("%d %n", 123, &count, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "123 ") == 0); + ASSERT (count == 4); + free (result); + } + + /* Test the support of the POSIX/XSI format strings with positions. */ + + { + char *result = + my_xasprintf ("%2$d %1$d", 33, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "55 33") == 0); + free (result); + } + + /* Test the support of the grouping flag. */ + + { + char *result = + my_xasprintf ("%'d %d", 1234567, 99); + ASSERT (result != NULL); + ASSERT (result[strlen (result) - 1] == '9'); + free (result); + } +} diff --git a/tests/unistdio/test-ulc-vasnprintf1.c b/tests/unistdio/test-ulc-vasnprintf1.c new file mode 100644 index 00000000..e2f8be6d --- /dev/null +++ b/tests/unistdio/test-ulc-vasnprintf1.c @@ -0,0 +1,85 @@ +/* Test of ulc_vasnprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-ulc-asnprintf1.h" +#include "test-ulc-printf1.h" + +static char * +my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + char *ret; + + va_start (args, format); + ret = ulc_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static char * +my_xasprintf (const char *format, ...) +{ + va_list args; + size_t length; + char *ret; + + va_start (args, format); + ret = ulc_vasnprintf (NULL, &length, format, args); + va_end (args); + if (ret != NULL) + ASSERT (length == strlen (ret)); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-ulc-vasnprintf2.c b/tests/unistdio/test-ulc-vasnprintf2.c new file mode 100644 index 00000000..90229751 --- /dev/null +++ b/tests/unistdio/test-ulc-vasnprintf2.c @@ -0,0 +1,258 @@ +/* Test of ulc_vasnprintf() function in an ISO-8859-1 locale. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +static void +test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) +{ + /* Test the support of the 'U' conversion specifier for Unicode strings. */ + + { + static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */ + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0 + || strcmp (result, "Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%20U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa? Maszkowski 33") == 0 + || strcmp (result, " Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-20U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0 + || strcmp (result, "Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%020U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa? Maszkowski 33") == 0 + || strcmp (result, " Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } + + { + static const uint16_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0 + || strcmp (result, "Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%20lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa? Maszkowski 33") == 0 + || strcmp (result, " Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-20lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0 + || strcmp (result, "Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%020lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa? Maszkowski 33") == 0 + || strcmp (result, " Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } + + { + static const uint32_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0 + || strcmp (result, "Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%20llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa? Maszkowski 33") == 0 + || strcmp (result, " Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-20llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa? Maszkowski 33") == 0 + || strcmp (result, "Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%020llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa? Maszkowski 33") == 0 + || strcmp (result, " Rafal Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } + + /* Test the support of the 's' conversion specifier for strings. */ + + { + const char *locale_string = "\304rger"; /* Ärger */ + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "\304rger 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " \304rger 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "\304rger 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " \304rger 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } +} + +static char * +my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + char *ret; + + va_start (args, format); + ret = ulc_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-ulc-vasnprintf2.sh b/tests/unistdio/test-ulc-vasnprintf2.sh new file mode 100755 index 00000000..39056a63 --- /dev/null +++ b/tests/unistdio/test-ulc-vasnprintf2.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Test whether a traditional french locale is installed. +: ${LOCALE_FR=fr_FR} +if test $LOCALE_FR != none; then + testlocale=$LOCALE_FR +else + if test -f /usr/bin/localedef; then + echo "Skipping test: no traditional french locale is installed" + else + echo "Skipping test: no traditional french locale is supported" + fi + exit 77 +fi + +LC_ALL=$testlocale \ +./test-ulc-vasnprintf1${EXEEXT} \ + || exit 1 + +LC_ALL=$testlocale \ +./test-ulc-vasnprintf2${EXEEXT} diff --git a/tests/unistdio/test-ulc-vasnprintf3.c b/tests/unistdio/test-ulc-vasnprintf3.c new file mode 100644 index 00000000..1fbd925d --- /dev/null +++ b/tests/unistdio/test-ulc-vasnprintf3.c @@ -0,0 +1,246 @@ +/* Test of ulc_vasnprintf() function in an UTF-8 locale. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +static void +test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) +{ + /* Test the support of the 'U' conversion specifier for Unicode strings. */ + + { + static const uint8_t unicode_string[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */ + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%20U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-20U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%020U %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } + + { + static const uint16_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%20lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-20lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%020lU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } + + { + static const uint32_t unicode_string[] = /* Rafał Maszkowski */ + { + 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w', + 's', 'k', 'i', 0 + }; + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%20llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-20llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%020llU %d", unicode_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " Rafa\305\202 Maszkowski 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } + + /* Test the support of the 's' conversion specifier for strings. */ + + { + const char *locale_string = "\303\204rger"; /* Ärger */ + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "\303\204rger 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " \303\204rger 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "\303\204rger 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* FLAG_ZERO: no effect. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " \303\204rger 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + } +} + +static char * +my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...) +{ + va_list args; + char *ret; + + va_start (args, format); + ret = ulc_vasnprintf (resultbuf, lengthp, format, args); + va_end (args); + return ret; +} + +static void +test_vasnprintf () +{ + test_function (my_asnprintf); +} + +int +main (int argc, char *argv[]) +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + test_vasnprintf (); + return 0; +} diff --git a/tests/unistdio/test-ulc-vasnprintf3.sh b/tests/unistdio/test-ulc-vasnprintf3.sh new file mode 100755 index 00000000..caa528a6 --- /dev/null +++ b/tests/unistdio/test-ulc-vasnprintf3.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Test whether a french Unicode locale is installed. +: ${LOCALE_FR_UTF8=fr_FR.UTF-8} +if test $LOCALE_FR_UTF8 != none; then + testlocale=$LOCALE_FR_UTF8 +else + if test -f /usr/bin/localedef; then + echo "Skipping test: no french Unicode locale is installed" + else + echo "Skipping test: no french Unicode locale is supported" + fi + exit 77 +fi + +LC_ALL=$testlocale \ +./test-ulc-vasnprintf1${EXEEXT} \ + || exit 1 + +LC_ALL=$testlocale \ +./test-ulc-vasnprintf3${EXEEXT} diff --git a/tests/unistdio/test-ulc-vasprintf1.c b/tests/unistdio/test-ulc-vasprintf1.c new file mode 100644 index 00000000..d8ed67c2 --- /dev/null +++ b/tests/unistdio/test-ulc-vasprintf1.c @@ -0,0 +1,72 @@ +/* Test of ulc_vasprintf() function. + Copyright (C) 2007-2008 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-ulc-printf1.h" + +static char * +my_xasprintf (const char *format, ...) +{ + va_list args; + char *result; + int retval; + + va_start (args, format); + retval = ulc_vasprintf (&result, format, args); + va_end (args); + if (retval < 0) + return NULL; + ASSERT (result != NULL); + return result; +} + +static void +test_vasprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + test_vasprintf (); + return 0; +} diff --git a/tests/unistdio/test-ulc-vsnprintf1.c b/tests/unistdio/test-ulc-vsnprintf1.c new file mode 100644 index 00000000..7701c21e --- /dev/null +++ b/tests/unistdio/test-ulc-vsnprintf1.c @@ -0,0 +1,77 @@ +/* Test of ulc_vsnprintf() function. + Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "xalloc.h" +#include "progname.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-ulc-printf1.h" + +static char * +my_xasprintf (const char *format, ...) +{ + va_list args; + char result[1000]; + int retval; + + va_start (args, format); + retval = ulc_vsnprintf (result, sizeof (result), format, args); + va_end (args); + if (retval < 0 || retval >= (int) sizeof (result)) + return NULL; + return xstrdup (result); +} + +static void +test_vsnprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + set_program_name (argv[0]); + + test_vsnprintf (); + + return 0; +} diff --git a/tests/unistdio/test-ulc-vsprintf1.c b/tests/unistdio/test-ulc-vsprintf1.c new file mode 100644 index 00000000..76b85b29 --- /dev/null +++ b/tests/unistdio/test-ulc-vsprintf1.c @@ -0,0 +1,77 @@ +/* Test of ulc_vsprintf() function. + Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include "unistdio.h" + +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "xalloc.h" +#include "progname.h" + +#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#include "test-ulc-printf1.h" + +static char * +my_xasprintf (const char *format, ...) +{ + va_list args; + char result[1000]; + int retval; + + va_start (args, format); + retval = ulc_vsprintf (result, format, args); + va_end (args); + if (retval < 0 || retval >= (int) sizeof (result)) + return NULL; + return xstrdup (result); +} + +static void +test_vsprintf () +{ + test_xfunction (my_xasprintf); +} + +int +main (int argc, char *argv[]) +{ + set_program_name (argv[0]); + + test_vsprintf (); + + return 0; +} |