Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-xvasprintf.c
1 /* Test of xvasprintf() and xasprintf() functions.
2    Copyright (C) 2007-2009 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 "xvasprintf.h"
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "progname.h"
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 static char *
43 my_xasprintf (const char *format, ...)
44 {
45   va_list args;
46   char *ret;
47
48   va_start (args, format);
49   ret = xvasprintf (format, args);
50   va_end (args);
51   return ret;
52 }
53
54 static void
55 test_xvasprintf (void)
56 {
57   int repeat;
58   char *result;
59
60   for (repeat = 0; repeat <= 8; repeat++)
61     {
62       result = my_xasprintf ("%d", 12345);
63       ASSERT (result != NULL);
64       ASSERT (strcmp (result, "12345") == 0);
65       free (result);
66     }
67
68   {
69     /* Silence gcc warning about zero-length format string.  */
70     char *empty = "";
71     result = my_xasprintf (empty);
72     ASSERT (result != NULL);
73     ASSERT (strcmp (result, "") == 0);
74     free (result);
75   }
76
77   result = my_xasprintf ("%s", "foo");
78   ASSERT (result != NULL);
79   ASSERT (strcmp (result, "foo") == 0);
80   free (result);
81
82   result = my_xasprintf ("%s%s", "foo", "bar");
83   ASSERT (result != NULL);
84   ASSERT (strcmp (result, "foobar") == 0);
85   free (result);
86
87   result = my_xasprintf ("%s%sbaz", "foo", "bar");
88   ASSERT (result != NULL);
89   ASSERT (strcmp (result, "foobarbaz") == 0);
90   free (result);
91 }
92
93 static void
94 test_xasprintf ()
95 {
96   int repeat;
97   char *result;
98
99   for (repeat = 0; repeat <= 8; repeat++)
100     {
101       result = xasprintf ("%d", 12345);
102       ASSERT (result != NULL);
103       ASSERT (strcmp (result, "12345") == 0);
104       free (result);
105     }
106
107   {
108     /* Silence gcc warning about zero-length format string.  */
109     char *empty = "";
110     result = xasprintf (empty);
111     ASSERT (result != NULL);
112     ASSERT (strcmp (result, "") == 0);
113     free (result);
114   }
115
116   result = xasprintf ("%s", "foo");
117   ASSERT (result != NULL);
118   ASSERT (strcmp (result, "foo") == 0);
119   free (result);
120
121   result = xasprintf ("%s%s", "foo", "bar");
122   ASSERT (result != NULL);
123   ASSERT (strcmp (result, "foobar") == 0);
124   free (result);
125
126   result = my_xasprintf ("%s%sbaz", "foo", "bar");
127   ASSERT (result != NULL);
128   ASSERT (strcmp (result, "foobarbaz") == 0);
129   free (result);
130 }
131
132 int
133 main (int argc _UNUSED_PARAMETER_, char *argv[])
134 {
135   set_program_name (argv[0]);
136
137   test_xvasprintf ();
138   test_xasprintf ();
139
140   return 0;
141 }