test-vasnprintf.c

Go to the documentation of this file.
00001 /* Test of vasnprintf() and asnprintf() 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 "vasnprintf.h"
00022 
00023 #include <stdarg.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 #define ASSERT(expr) \
00029   do                                                                         \
00030     {                                                                        \
00031       if (!(expr))                                                           \
00032         {                                                                    \
00033           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
00034           fflush (stderr);                                                   \
00035           abort ();                                                          \
00036         }                                                                    \
00037     }                                                                        \
00038   while (0)
00039 
00040 static char *
00041 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
00042 {
00043   va_list args;
00044   char *ret;
00045 
00046   va_start (args, format);
00047   ret = vasnprintf (resultbuf, lengthp, format, args);
00048   va_end (args);
00049   return ret;
00050 }
00051 
00052 static void
00053 test_vasnprintf ()
00054 {
00055   char buf[8];
00056   int size;
00057 
00058   for (size = 0; size <= 8; size++)
00059     {
00060       size_t length = size;
00061       char *result = my_asnprintf (NULL, &length, "%d", 12345);
00062       ASSERT (result != NULL);
00063       ASSERT (strcmp (result, "12345") == 0);
00064       ASSERT (length == 5);
00065       free (result);
00066     }
00067 
00068   for (size = 0; size <= 8; size++)
00069     {
00070       size_t length;
00071       char *result;
00072 
00073       memcpy (buf, "DEADBEEF", 8);
00074       length = size;
00075       result = my_asnprintf (buf, &length, "%d", 12345);
00076       ASSERT (result != NULL);
00077       ASSERT (strcmp (result, "12345") == 0);
00078       ASSERT (length == 5);
00079       if (size < 6)
00080         ASSERT (result != buf);
00081       ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0);
00082       if (result != buf)
00083         free (result);
00084     }
00085 }
00086 
00087 static void
00088 test_asnprintf ()
00089 {
00090   char buf[8];
00091   int size;
00092 
00093   for (size = 0; size <= 8; size++)
00094     {
00095       size_t length = size;
00096       char *result = asnprintf (NULL, &length, "%d", 12345);
00097       ASSERT (result != NULL);
00098       ASSERT (strcmp (result, "12345") == 0);
00099       ASSERT (length == 5);
00100       free (result);
00101     }
00102 
00103   for (size = 0; size <= 8; size++)
00104     {
00105       size_t length;
00106       char *result;
00107 
00108       memcpy (buf, "DEADBEEF", 8);
00109       length = size;
00110       result = asnprintf (buf, &length, "%d", 12345);
00111       ASSERT (result != NULL);
00112       ASSERT (strcmp (result, "12345") == 0);
00113       ASSERT (length == 5);
00114       if (size < 6)
00115         ASSERT (result != buf);
00116       ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0);
00117       if (result != buf)
00118         free (result);
00119     }
00120 }
00121 
00122 int
00123 main (int argc, char *argv[])
00124 {
00125   test_vasnprintf ();
00126   test_asnprintf ();
00127   return 0;
00128 }

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