Unconditionally include <config.h> in unit tests.
[gnulib.git] / tests / test-vasnprintf.c
1 /* Test of 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 <stdarg.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 static char *
41 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
42 {
43   va_list args;
44   char *ret;
45
46   va_start (args, format);
47   ret = vasnprintf (resultbuf, lengthp, format, args);
48   va_end (args);
49   return ret;
50 }
51
52 static void
53 test_vasnprintf ()
54 {
55   char buf[8];
56   int size;
57
58   for (size = 0; size <= 8; size++)
59     {
60       size_t length = size;
61       char *result = my_asnprintf (NULL, &length, "%d", 12345);
62       ASSERT (result != NULL);
63       ASSERT (strcmp (result, "12345") == 0);
64       ASSERT (length == 5);
65       free (result);
66     }
67
68   for (size = 0; size <= 8; size++)
69     {
70       size_t length;
71       char *result;
72
73       memcpy (buf, "DEADBEEF", 8);
74       length = size;
75       result = my_asnprintf (buf, &length, "%d", 12345);
76       ASSERT (result != NULL);
77       ASSERT (strcmp (result, "12345") == 0);
78       ASSERT (length == 5);
79       if (size < 6)
80         ASSERT (result != buf);
81       ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0);
82       if (result != buf)
83         free (result);
84     }
85 }
86
87 static void
88 test_asnprintf ()
89 {
90   char buf[8];
91   int size;
92
93   for (size = 0; size <= 8; size++)
94     {
95       size_t length = size;
96       char *result = asnprintf (NULL, &length, "%d", 12345);
97       ASSERT (result != NULL);
98       ASSERT (strcmp (result, "12345") == 0);
99       ASSERT (length == 5);
100       free (result);
101     }
102
103   for (size = 0; size <= 8; size++)
104     {
105       size_t length;
106       char *result;
107
108       memcpy (buf, "DEADBEEF", 8);
109       length = size;
110       result = asnprintf (buf, &length, "%d", 12345);
111       ASSERT (result != NULL);
112       ASSERT (strcmp (result, "12345") == 0);
113       ASSERT (length == 5);
114       if (size < 6)
115         ASSERT (result != buf);
116       ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0);
117       if (result != buf)
118         free (result);
119     }
120 }
121
122 int
123 main (int argc, char *argv[])
124 {
125   test_vasnprintf ();
126   test_asnprintf ();
127   return 0;
128 }