root/maint/gnulib/tests/test-obstack-printf.c

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

DEFINITIONS

This source file includes following definitions.
  1. test_function
  2. my_obstack_printf
  3. test_obstack_vprintf
  4. test_obstack_printf
  5. main

   1 /* Test of obstack_printf() and obstack_vprintf() functions.
   2    Copyright (C) 2008-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 Eric Blake <ebb9@byu.net>, 2008.  */
  18 
  19 #include <config.h>
  20 
  21 #include <stdio.h>
  22 
  23 #include "signature.h"
  24 SIGNATURE_CHECK (obstack_printf, int, (struct obstack *, char const *, ...));
  25 SIGNATURE_CHECK (obstack_vprintf, int, (struct obstack *, char const *,
  26                                         va_list));
  27 
  28 #include "obstack.h"
  29 #include "xalloc.h"
  30 
  31 #include <stdarg.h>
  32 #include <stdlib.h>
  33 #include <string.h>
  34 
  35 #include "macros.h"
  36 
  37 #define obstack_chunk_alloc xmalloc
  38 #define obstack_chunk_free free
  39 
  40 static void
  41 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...))
     /* [previous][next][first][last][top][bottom][index][help] */
  42 {
  43   struct obstack obs;
  44   obstack_init (&obs);
  45   /* In general, be careful that arguments to obstack_* don't have
  46      side effects, as not all compilers evaluate macro arguments only
  47      once.  */
  48 
  49   /* Grow the obstack to near its boundary, then check that short
  50      output longer than the obstack free space grows the obstack.  */
  51   {
  52     char *base = obstack_base (&obs);
  53     char *new_base;
  54     int result;
  55     int room = obstack_room (&obs) - 4;
  56 
  57     obstack_blank_fast (&obs, room);
  58     result = my_obstack_printf (&obs, "%d %s", 123, "456");
  59     ASSERT (result == 7);
  60     ASSERT (result + room == obstack_object_size (&obs));
  61     obstack_1grow (&obs, 0);
  62     new_base = obstack_finish (&obs);
  63     ASSERT (base != new_base);
  64     ASSERT (strcmp (new_base + room, "123 456") == 0);
  65   }
  66 
  67   /* Check that strings shorter than the obstack free space don't
  68      cause a reshuffling of the obstack.  */
  69   {
  70     char *base = obstack_base (&obs);
  71     char *new_base;
  72     int result;
  73     int room = obstack_room (&obs);
  74 
  75     ASSERT (8 < room);
  76     result = my_obstack_printf (&obs, "%d %s", 123, "456");
  77     ASSERT (result == 7);
  78     ASSERT (result == obstack_object_size (&obs));
  79     new_base = obstack_base (&obs);
  80     ASSERT (base == new_base);
  81     ASSERT (strncmp (base, "123 456", result) == 0);
  82     obstack_finish (&obs);
  83   }
  84 
  85   /* Check for generating much more output than a chunk size.  */
  86   {
  87     char *base = obstack_base (&obs);
  88     char *new_base;
  89     int result;
  90     int i;
  91 
  92     ASSERT (obstack_chunk_size (&obs) < 10000);
  93     result = my_obstack_printf (&obs, "%010000d", 0);
  94     ASSERT (result == 10000);
  95     ASSERT (result == obstack_object_size (&obs));
  96     new_base = obstack_base (&obs);
  97     ASSERT (base != new_base);
  98     for (i = 0; i < 10000; i++)
  99       ASSERT (new_base[i] == '0');
 100   }
 101 
 102   obstack_free (&obs, NULL);
 103 }
 104 
 105 static int
 106 my_obstack_printf (struct obstack *obs, const char *format, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108   va_list args;
 109   int ret;
 110 
 111   va_start (args, format);
 112   ret = obstack_vprintf (obs, format, args);
 113   va_end (args);
 114   return ret;
 115 }
 116 
 117 static void
 118 test_obstack_vprintf ()
     /* [previous][next][first][last][top][bottom][index][help] */
 119 {
 120   test_function (my_obstack_printf);
 121 }
 122 
 123 static void
 124 test_obstack_printf ()
     /* [previous][next][first][last][top][bottom][index][help] */
 125 {
 126   test_function (obstack_printf);
 127 }
 128 
 129 int
 130 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
 131 {
 132   test_obstack_vprintf ();
 133   test_obstack_printf ();
 134   return 0;
 135 }

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