Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / tests / test-xvasprintf.c
1 /* Test of xvasprintf() and xasprintf() 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 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           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 static char *
42 my_xasprintf (const char *format, ...)
43 {
44   va_list args;
45   char *ret;
46
47   va_start (args, format);
48   ret = xvasprintf (format, args);
49   va_end (args);
50   return ret;
51 }
52
53 static void
54 test_xvasprintf ()
55 {
56   int repeat;
57
58   for (repeat = 0; repeat <= 8; repeat++)
59     {
60       char *result = my_xasprintf ("%d", 12345);
61       ASSERT (result != NULL);
62       ASSERT (strcmp (result, "12345") == 0);
63       free (result);
64     }
65 }
66
67 static void
68 test_xasprintf ()
69 {
70   int repeat;
71
72   for (repeat = 0; repeat <= 8; repeat++)
73     {
74       char *result = xasprintf ("%d", 12345);
75       ASSERT (result != NULL);
76       ASSERT (strcmp (result, "12345") == 0);
77       free (result);
78     }
79 }
80
81 int
82 main (int argc, char *argv[])
83 {
84   set_program_name (argv[0]);
85
86   test_xvasprintf ();
87   test_xasprintf ();
88
89   return 0;
90 }