maint: update copyright
[gnulib.git] / tests / test-vasnprintf-posix3.c
1 /* Test of POSIX compatible vasnprintf() and asnprintf() functions.
2    Copyright (C) 2010-2014 Free Software Foundation, Inc.
3
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.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2010.  */
18
19 #include <config.h>
20
21 #include "vasnprintf.h"
22
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "macros.h"
28
29 static void
30 test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
31 {
32   /* glibc >= 2.2 supports the 'I' flag, and in glibc >= 2.2.3 the fa_IR
33      locale defines the 'outdigits' to be U+06F0..U+06F9.
34      So we test for glibc >= 2.3.  */
35 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__
36   /* Test that the 'I' flag is supported.  */
37   {
38     size_t length;
39     char *result =
40       my_asnprintf (NULL, &length, "%Id %d", 1234567, 99);
41     static const char expected[] = /* "۱۲۳۴۵۶۷ 99" */
42       "\xDB\xB1\xDB\xB2\xDB\xB3\xDB\xB4\xDB\xB5\xDB\xB6\xDB\xB7 99";
43     ASSERT (result != NULL);
44     ASSERT (strcmp (result, expected) == 0);
45     ASSERT (length == strlen (result));
46     free (result);
47   }
48 #endif
49 }
50
51 static char *
52 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
53 {
54   va_list args;
55   char *ret;
56
57   va_start (args, format);
58   ret = vasnprintf (resultbuf, lengthp, format, args);
59   va_end (args);
60   return ret;
61 }
62
63 static void
64 test_vasnprintf ()
65 {
66   test_function (my_asnprintf);
67 }
68
69 static void
70 test_asnprintf ()
71 {
72   test_function (asnprintf);
73 }
74
75 int
76 main (int argc, char *argv[])
77 {
78 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__
79   /* Select a locale with Arabic 'outdigits'.  */
80   if (setlocale (LC_ALL, "fa_IR.UTF-8") == NULL)
81     {
82       fprintf (stderr, "Skipping test: no Iranian locale is installed\n");
83       return 77;
84     }
85
86   test_vasnprintf ();
87   test_asnprintf ();
88
89   return 0;
90 #else
91   fprintf (stderr, "Skipping test: not a glibc >= 2.3 system\n");
92   return 77;
93 #endif
94 }