openat: don’t close (-1)
[gnulib.git] / tests / test-vasnwprintf-posix3.c
blob53923bab77bd7af3dee2ac23c44c1e8766ff99f5
1 /* Test of POSIX compatible vasnwprintf() and asnwprintf() functions.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2010. */
19 #include <config.h>
21 #include "vasnwprintf.h"
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <wchar.h>
28 #include "macros.h"
30 /* glibc >= 2.2 supports the 'I' flag, and in glibc >= 2.2.3 the fa_IR
31 locale defines the 'outdigits' to be U+06F0..U+06F9.
32 So we test for glibc >= 2.3. */
33 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__
35 static void
36 test_function (wchar_t * (*my_asnwprintf) (wchar_t *, size_t *, const wchar_t *, ...))
38 /* Test that the 'I' flag is supported. */
40 size_t length;
41 wchar_t *result =
42 my_asnwprintf (NULL, &length, L"%Id %d", 1234567, 99);
43 static const wchar_t expected[] = /* "۱۲۳۴۵۶۷ 99" */
45 0x06F1, 0x06F2, 0x06F3, 0x06F4, 0x06F5, 0x06F6, 0x06F7, L' ',
46 L'9', L'9', L'\0'
48 ASSERT (result != NULL);
49 ASSERT (wcscmp (result, expected) == 0);
50 ASSERT (length == wcslen (result));
51 free (result);
55 static wchar_t *
56 my_asnwprintf (wchar_t *resultbuf, size_t *lengthp, const wchar_t *format, ...)
58 va_list args;
59 wchar_t *ret;
61 va_start (args, format);
62 ret = vasnwprintf (resultbuf, lengthp, format, args);
63 va_end (args);
64 return ret;
67 static void
68 test_vasnwprintf ()
70 test_function (my_asnwprintf);
73 static void
74 test_asnwprintf ()
76 test_function (asnwprintf);
79 #endif
81 int
82 main (int argc, char *argv[])
84 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__
85 /* Select a locale with Arabic 'outdigits'. */
86 if (setlocale (LC_ALL, "fa_IR.UTF-8") == NULL
87 && setlocale (LC_ALL, "fa_IR") == NULL)
89 fprintf (stderr, "Skipping test: no Iranian locale is installed\n");
90 return 77;
93 test_vasnwprintf ();
94 test_asnwprintf ();
96 return test_exit_status;
97 #else
98 fprintf (stderr, "Skipping test: not a glibc >= 2.3 system\n");
99 return 77;
100 #endif