maint: update copyright
[gnulib.git] / tests / unistdio / test-u32-vasnprintf2.c
1 /* Test of u32_vasnprintf() function in an ISO-8859-1 locale.
2    Copyright (C) 2007-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>, 2007.  */
18
19 #include <config.h>
20
21 #include "unistdio.h"
22
23 #include <locale.h>
24 #include <stdarg.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "unistr.h"
30 #include "macros.h"
31
32 static void
33 test_function (uint32_t * (*my_asnprintf) (uint32_t *, size_t *, const char *, ...))
34 {
35   /* Test the support of the 's' conversion specifier for strings.  */
36
37   {
38     const char *locale_string = "\304rger"; /* Ã„rger */
39     {
40       size_t length;
41       uint32_t *result =
42         my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55);
43       static const uint32_t expected[] =
44         { 0x00c4, 'r', 'g', 'e', 'r', ' ', '3', '3', 0 };
45       ASSERT (result != NULL);
46       ASSERT (u32_strcmp (result, expected) == 0);
47       ASSERT (length == u32_strlen (result));
48       free (result);
49     }
50     { /* Width.  */
51       size_t length;
52       uint32_t *result =
53         my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55);
54       static const uint32_t expected[] =
55         { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r',
56           ' ', '3', '3', 0
57         };
58       ASSERT (result != NULL);
59       ASSERT (u32_strcmp (result, expected) == 0);
60       ASSERT (length == u32_strlen (result));
61       free (result);
62     }
63     { /* FLAG_LEFT.  */
64       size_t length;
65       uint32_t *result =
66         my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55);
67       static const uint32_t expected[] =
68         { 0x00c4, 'r', 'g', 'e', 'r', ' ', ' ', ' ', ' ', ' ',
69           ' ', '3', '3', 0
70         };
71       ASSERT (result != NULL);
72       ASSERT (u32_strcmp (result, expected) == 0);
73       ASSERT (length == u32_strlen (result));
74       free (result);
75     }
76     { /* FLAG_ZERO: no effect.  */
77       size_t length;
78       uint32_t *result =
79         my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55);
80       static const uint32_t expected[] =
81         { ' ', ' ', ' ', ' ', ' ', 0x00c4, 'r', 'g', 'e', 'r',
82           ' ', '3', '3', 0
83         };
84       ASSERT (result != NULL);
85       ASSERT (u32_strcmp (result, expected) == 0);
86       ASSERT (length == u32_strlen (result));
87       free (result);
88     }
89   }
90 }
91
92 static uint32_t *
93 my_asnprintf (uint32_t *resultbuf, size_t *lengthp, const char *format, ...)
94 {
95   va_list args;
96   uint32_t *ret;
97
98   va_start (args, format);
99   ret = u32_vasnprintf (resultbuf, lengthp, format, args);
100   va_end (args);
101   return ret;
102 }
103
104 static void
105 test_vasnprintf ()
106 {
107   test_function (my_asnprintf);
108 }
109
110 int
111 main (int argc, char *argv[])
112 {
113   /* configure should already have checked that the locale is supported.  */
114   if (setlocale (LC_ALL, "") == NULL)
115     return 1;
116
117   test_vasnprintf ();
118   return 0;
119 }