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