Unconditionally include <config.h> in unit tests.
[gnulib.git] / tests / test-vasnprintf-posix2.c
1 /* Test of POSIX compatible vasnprintf() and asnprintf() functions.
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 "vasnprintf.h"
23
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 int
41 main (int argc, char *argv[])
42 {
43   /* configure should already have checked that the locale is supported.  */
44   if (setlocale (LC_ALL, "") == NULL)
45     return 1;
46
47   /* Test that a locale dependent decimal point is used.  */
48   {
49     size_t length;
50     char *result = asnprintf (NULL, &length, "%.1a", 1.0);
51     ASSERT (result != NULL);
52     ASSERT (strcmp (result, "0x1,0p+0") == 0
53             || strcmp (result, "0x2,0p-1") == 0
54             || strcmp (result, "0x4,0p-2") == 0
55             || strcmp (result, "0x8,0p-3") == 0);
56     ASSERT (length == strlen (result));
57     free (result);
58   }
59
60   /* Test that a locale dependent decimal point is used.  */
61   {
62     size_t length;
63     char *result = asnprintf (NULL, &length, "%.1La", 1.0L);
64     ASSERT (result != NULL);
65     ASSERT (strcmp (result, "0x1,0p+0") == 0
66             || strcmp (result, "0x2,0p-1") == 0
67             || strcmp (result, "0x4,0p-2") == 0
68             || strcmp (result, "0x8,0p-3") == 0);
69     ASSERT (length == strlen (result));
70     free (result);
71   }
72
73   return 0;
74 }