root/maint/gnulib/tests/test-xvasprintf.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. my_xasprintf
  2. test_xvasprintf
  3. test_xasprintf
  4. main

   1 /* Test of xvasprintf() and xasprintf() functions.
   2    Copyright (C) 2007-2021 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 <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
  18 
  19 /* Tell GCC not to warn about the specific edge cases tested here.  */
  20 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
  21 # pragma GCC diagnostic ignored "-Wformat-zero-length"
  22 # pragma GCC diagnostic ignored "-Wformat-nonliteral"
  23 # pragma GCC diagnostic ignored "-Wformat-security"
  24 #endif
  25 
  26 #include <config.h>
  27 
  28 #include "xvasprintf.h"
  29 
  30 #include <stdarg.h>
  31 #include <stdlib.h>
  32 #include <string.h>
  33 
  34 #include "macros.h"
  35 
  36 static char *
  37 my_xasprintf (const char *format, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39   va_list args;
  40   char *ret;
  41 
  42   va_start (args, format);
  43   ret = xvasprintf (format, args);
  44   va_end (args);
  45   return ret;
  46 }
  47 
  48 static void
  49 test_xvasprintf (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51   int repeat;
  52   char *result;
  53 
  54   for (repeat = 0; repeat <= 8; repeat++)
  55     {
  56       result = my_xasprintf ("%d", 12345);
  57       ASSERT (result != NULL);
  58       ASSERT (strcmp (result, "12345") == 0);
  59       free (result);
  60     }
  61 
  62   {
  63     /* Silence gcc warning about zero-length format string.  */
  64     const char *empty = "";
  65     result = my_xasprintf (empty);
  66     ASSERT (result != NULL);
  67     ASSERT (strcmp (result, "") == 0);
  68     free (result);
  69   }
  70 
  71   result = my_xasprintf ("%s", "foo");
  72   ASSERT (result != NULL);
  73   ASSERT (strcmp (result, "foo") == 0);
  74   free (result);
  75 
  76   result = my_xasprintf ("%s%s", "foo", "bar");
  77   ASSERT (result != NULL);
  78   ASSERT (strcmp (result, "foobar") == 0);
  79   free (result);
  80 
  81   result = my_xasprintf ("%s%sbaz", "foo", "bar");
  82   ASSERT (result != NULL);
  83   ASSERT (strcmp (result, "foobarbaz") == 0);
  84   free (result);
  85 }
  86 
  87 static void
  88 test_xasprintf (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90   int repeat;
  91   char *result;
  92 
  93   for (repeat = 0; repeat <= 8; repeat++)
  94     {
  95       result = xasprintf ("%d", 12345);
  96       ASSERT (result != NULL);
  97       ASSERT (strcmp (result, "12345") == 0);
  98       free (result);
  99     }
 100 
 101   {
 102     /* Silence gcc warning about zero-length format string,
 103        and about "format not a string literal and no format"
 104        (whatever that means) .  */
 105     const char *empty = "";
 106     result = xasprintf (empty, empty);
 107     ASSERT (result != NULL);
 108     ASSERT (strcmp (result, "") == 0);
 109     free (result);
 110   }
 111 
 112   result = xasprintf ("%s", "foo");
 113   ASSERT (result != NULL);
 114   ASSERT (strcmp (result, "foo") == 0);
 115   free (result);
 116 
 117   result = xasprintf ("%s%s", "foo", "bar");
 118   ASSERT (result != NULL);
 119   ASSERT (strcmp (result, "foobar") == 0);
 120   free (result);
 121 
 122   result = my_xasprintf ("%s%sbaz", "foo", "bar");
 123   ASSERT (result != NULL);
 124   ASSERT (strcmp (result, "foobarbaz") == 0);
 125   free (result);
 126 }
 127 
 128 int
 129 main (_GL_UNUSED int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131   test_xvasprintf ();
 132   test_xasprintf ();
 133 
 134   return 0;
 135 }

/* [previous][next][first][last][top][bottom][index][help] */