Restore #include "progname.h"
[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 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "vasnprintf.h"
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 static char *
43 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
44 {
45   va_list args;
46   char *ret;
47
48   va_start (args, format);
49   ret = vasnprintf (resultbuf, lengthp, format, args);
50   va_end (args);
51   return ret;
52 }
53
54 static void
55 test_vasnprintf ()
56 {
57   char buf[8];
58   int size;
59
60   for (size = 0; size <= 8; size++)
61     {
62       size_t length = size;
63       char *result = my_asnprintf (NULL, &length, "%d", 12345);
64       ASSERT (result != NULL);
65       ASSERT (strcmp (result, "12345") == 0);
66       ASSERT (length == 5);
67       free (result);
68     }
69
70   for (size = 0; size <= 8; size++)
71     {
72       size_t length;
73       char *result;
74
75       memcpy (buf, "DEADBEEF", 8);
76       length = size;
77       result = my_asnprintf (buf, &length, "%d", 12345);
78       ASSERT (result != NULL);
79       ASSERT (strcmp (result, "12345") == 0);
80       ASSERT (length == 5);
81       if (size < 6)
82         ASSERT (result != buf);
83       ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0);
84       if (result != buf)
85         free (result);
86     }
87 }
88
89 static void
90 test_asnprintf ()
91 {
92   char buf[8];
93   int size;
94
95   for (size = 0; size <= 8; size++)
96     {
97       size_t length = size;
98       char *result = asnprintf (NULL, &length, "%d", 12345);
99       ASSERT (result != NULL);
100       ASSERT (strcmp (result, "12345") == 0);
101       ASSERT (length == 5);
102       free (result);
103     }
104
105   for (size = 0; size <= 8; size++)
106     {
107       size_t length;
108       char *result;
109
110       memcpy (buf, "DEADBEEF", 8);
111       length = size;
112       result = asnprintf (buf, &length, "%d", 12345);
113       ASSERT (result != NULL);
114       ASSERT (strcmp (result, "12345") == 0);
115       ASSERT (length == 5);
116       if (size < 6)
117         ASSERT (result != buf);
118       ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0);
119       if (result != buf)
120         free (result);
121     }
122 }
123
124 int
125 main (int argc, char *argv[])
126 {
127   test_vasnprintf ();
128   test_asnprintf ();
129   return 0;
130 }