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