test-vasprintf.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 }