root/maint/gnulib/tests/test-string-buffer.c

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

DEFINITIONS

This source file includes following definitions.
  1. my_appendf
  2. main

   1 /* Test of buffer that accumulates a string by piecewise concatenation.
   2    Copyright (C) 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 2, or (at your option)
   7    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>, 2021.  */
  18 
  19 #include <config.h>
  20 
  21 #include "string-buffer.h"
  22 
  23 #include <string.h>
  24 
  25 #include "macros.h"
  26 
  27 static int
  28 my_appendf (struct string_buffer *buffer, const char *formatstring, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30   va_list args;
  31 
  32   va_start (args, formatstring);
  33   int ret = sb_appendvf (buffer, formatstring, args);
  34   va_end (args);
  35 
  36   return ret;
  37 }
  38 
  39 char invalid_format_string_1[] = "%&";
  40 char invalid_format_string_2[] = "%^";
  41 
  42 int
  43 main ()
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45   /* Test simple string concatenation.  */
  46   {
  47     struct string_buffer buffer;
  48 
  49     sb_init (&buffer);
  50     char *s = sb_dupfree (&buffer);
  51     ASSERT (s != NULL && strcmp (s, "") == 0);
  52     free (s);
  53   }
  54 
  55   {
  56     struct string_buffer buffer;
  57 
  58     sb_init (&buffer);
  59     sb_append (&buffer, "abc");
  60     sb_append (&buffer, "");
  61     sb_append (&buffer, "defg");
  62     char *s = sb_dupfree (&buffer);
  63     ASSERT (s != NULL && strcmp (s, "abcdefg") == 0);
  64     free (s);
  65   }
  66 
  67   /* Test printf-like formatting.  */
  68   {
  69     struct string_buffer buffer;
  70 
  71     sb_init (&buffer);
  72     sb_append (&buffer, "<");
  73     sb_appendf (&buffer, "%x", 3735928559U);
  74     sb_append (&buffer, ">");
  75     char *s = sb_dupfree (&buffer);
  76     ASSERT (s != NULL && strcmp (s, "<deadbeef>") == 0);
  77     free (s);
  78   }
  79 
  80   /* Test vprintf-like formatting.  */
  81   {
  82     struct string_buffer buffer;
  83 
  84     sb_init (&buffer);
  85     sb_append (&buffer, "<");
  86     my_appendf (&buffer, "%x", 3735928559U);
  87     sb_append (&buffer, ">");
  88     char *s = sb_dupfree (&buffer);
  89     ASSERT (s != NULL && strcmp (s, "<deadbeef>") == 0);
  90     free (s);
  91   }
  92 
  93   /* Test printf-like formatting failure.
  94      On all systems except AIX, trying to convert the wide-character 0x76543210
  95      to a multibyte string (in the "C" locale) fails.
  96      On all systems where REPLACE_VSNPRINTF=1 (this includes AIX), i.e. where
  97      the Gnulib implementation of vsnprintf() is used), invalid format
  98      directives make the *printf call fail.  */
  99   {
 100     struct string_buffer buffer;
 101 
 102     sb_init (&buffer);
 103     sb_append (&buffer, "<");
 104     sb_appendf (&buffer, "%lc", 0x76543210);
 105     sb_append (&buffer, "|");
 106     sb_appendf (&buffer, invalid_format_string_1, 1);
 107     sb_append (&buffer, "|");
 108     sb_appendf (&buffer, invalid_format_string_2, 2);
 109     sb_append (&buffer, ">");
 110     char *s = sb_dupfree (&buffer);
 111     ASSERT (s == NULL);
 112   }
 113 
 114   return 0;
 115 }

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