test-vasprintf.c

Go to the documentation of this file.
00001 /* Test of vasprintf() and asprintf() functions.
00002    Copyright (C) 2007-2008 Free Software Foundation, Inc.
00003 
00004    This program is free software: you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00016 
00017 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
00018 
00019 #include <config.h>
00020 
00021 #include <stdio.h>
00022 
00023 #include <stdarg.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #define ASSERT(expr) \
00028   do                                                                         \
00029     {                                                                        \
00030       if (!(expr))                                                           \
00031         {                                                                    \
00032           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
00033           fflush (stderr);                                                   \
00034           abort ();                                                          \
00035         }                                                                    \
00036     }                                                                        \
00037   while (0)
00038 
00039 static int
00040 my_asprintf (char **result, const char *format, ...)
00041 {
00042   va_list args;
00043   int ret;
00044 
00045   va_start (args, format);
00046   ret = vasprintf (result, format, args);
00047   va_end (args);
00048   return ret;
00049 }
00050 
00051 static void
00052 test_vasprintf ()
00053 {
00054   int repeat;
00055 
00056   for (repeat = 0; repeat <= 8; repeat++)
00057     {
00058       char *result;
00059       int retval = my_asprintf (&result, "%d", 12345);
00060       ASSERT (retval == 5);
00061       ASSERT (result != NULL);
00062       ASSERT (strcmp (result, "12345") == 0);
00063       free (result);
00064     }
00065 }
00066 
00067 static void
00068 test_asprintf ()
00069 {
00070   int repeat;
00071 
00072   for (repeat = 0; repeat <= 8; repeat++)
00073     {
00074       char *result;
00075       int retval = asprintf (&result, "%d", 12345);
00076       ASSERT (retval == 5);
00077       ASSERT (result != NULL);
00078       ASSERT (strcmp (result, "12345") == 0);
00079       free (result);
00080     }
00081 }
00082 
00083 int
00084 main (int argc, char *argv[])
00085 {
00086   test_vasprintf ();
00087   test_asprintf ();
00088   return 0;
00089 }

Generated on Tue Oct 21 18:28:19 2008 for gsasl by  doxygen 1.5.6