Formatted output functions for Unicode strings.
[gnulib.git] / tests / unistdio / test-u16-vsnprintf1.c
1 /* Test of u16_vsnprintf() function.
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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include "unistdio.h"
23
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "unistr.h"
33 #include "xalloc.h"
34
35 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
36 #define ASSERT(expr) \
37   do                                                                         \
38     {                                                                        \
39       if (!(expr))                                                           \
40         {                                                                    \
41           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42           abort ();                                                          \
43         }                                                                    \
44     }                                                                        \
45   while (0)
46
47 #include "test-u16-printf1.h"
48
49 static uint16_t *
50 my_xasprintf (const char *format, ...)
51 {
52   va_list args;
53   uint16_t buf[1000];
54   int retval;
55   size_t length;
56   uint16_t *result;
57
58   va_start (args, format);
59   retval = u16_vsnprintf (buf, sizeof (buf), format, args);
60   va_end (args);
61   if (retval < 0 || retval >= (int) sizeof (buf))
62     return NULL;
63   length = u16_strlen (buf);
64   result = XNMALLOC (length + 1, uint16_t);
65   u16_cpy (result, buf, length + 1);
66   return result;
67 }
68
69 static void
70 test_vsnprintf ()
71 {
72   test_xfunction (my_xasprintf);
73 }
74
75 int
76 main (int argc, char *argv[])
77 {
78   test_vsnprintf ();
79   return 0;
80 }