Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / tests / unistdio / test-u8-vasnprintf3.c
1 /* Test of u8_vasnprintf() function in an UTF-8 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 (uint8_t * (*my_asnprintf) (uint8_t *, size_t *, const char *, ...))
46 {
47   /* Test the support of the 's' conversion specifier for strings.  */
48
49   {
50     const char *locale_string = "\303\204rger"; /* Ã„rger */
51     {
52       size_t length;
53       uint8_t *result =
54         my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55);
55       static const uint8_t expected[] = "\303\204rger 33";
56       ASSERT (result != NULL);
57       ASSERT (u8_strcmp (result, expected) == 0);
58       ASSERT (length == u8_strlen (result));
59       free (result);
60     }
61     { /* Width.  */
62       size_t length;
63       uint8_t *result =
64         my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55);
65       static const uint8_t expected[] = "     \303\204rger 33";
66       ASSERT (result != NULL);
67       ASSERT (u8_strcmp (result, expected) == 0);
68       ASSERT (length == u8_strlen (result));
69       free (result);
70     }
71     { /* FLAG_LEFT.  */
72       size_t length;
73       uint8_t *result =
74         my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55);
75       static const uint8_t expected[] = "\303\204rger      33";
76       ASSERT (result != NULL);
77       ASSERT (u8_strcmp (result, expected) == 0);
78       ASSERT (length == u8_strlen (result));
79       free (result);
80     }
81     { /* FLAG_ZERO: no effect.  */
82       size_t length;
83       uint8_t *result =
84         my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55);
85       static const uint8_t expected[] = "     \303\204rger 33";
86       ASSERT (result != NULL);
87       ASSERT (u8_strcmp (result, expected) == 0);
88       ASSERT (length == u8_strlen (result));
89       free (result);
90     }
91   }
92 }
93
94 static uint8_t *
95 my_asnprintf (uint8_t *resultbuf, size_t *lengthp, const char *format, ...)
96 {
97   va_list args;
98   uint8_t *ret;
99
100   va_start (args, format);
101   ret = u8_vasnprintf (resultbuf, lengthp, format, args);
102   va_end (args);
103   return ret;
104 }
105
106 static void
107 test_vasnprintf ()
108 {
109   test_function (my_asnprintf);
110 }
111
112 int
113 main (int argc, char *argv[])
114 {
115   /* configure should already have checked that the locale is supported.  */
116   if (setlocale (LC_ALL, "") == NULL)
117     return 1;
118
119   test_vasnprintf ();
120   return 0;
121 }