ca07f47524d942ec35123860162694ba7501d988
[gnulib.git] / tests / test-obstack-printf.c
1 /* Test of obstack_printf() and obstack_vprintf() functions.
2    Copyright (C) 2008-2009 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 <http://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 "progname.h"
36
37 #define ASSERT(expr) \
38   do                                                                         \
39     {                                                                        \
40       if (!(expr))                                                           \
41         {                                                                    \
42           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43           fflush (stderr);                                                   \
44           abort ();                                                          \
45         }                                                                    \
46     }                                                                        \
47   while (0)
48
49 #define obstack_chunk_alloc xmalloc
50 #define obstack_chunk_free free
51
52 static void
53 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...))
54 {
55   struct obstack obs;
56   obstack_init (&obs);
57   /* In general, be careful that arguments to obstack_* don't have
58      side effects, as not all compilers evaluate macro arguments only
59      once.  */
60
61   /* Grow the obstack to near its boundary, then check that short
62      output longer than the obstack free space grows the obstack.  */
63   {
64     char *base = obstack_base (&obs);
65     char *new_base;
66     int result;
67     int room = obstack_room (&obs) - 4;
68
69     obstack_blank_fast (&obs, room);
70     result = my_obstack_printf (&obs, "%d %s", 123, "456");
71     ASSERT (result == 7);
72     ASSERT (result + room == obstack_object_size (&obs));
73     obstack_1grow (&obs, 0);
74     new_base = obstack_finish (&obs);
75     ASSERT (base != new_base);
76     ASSERT (strcmp (new_base + room, "123 456") == 0);
77   }
78
79   /* Check that strings shorter than the obstack free space don't
80      cause a reshuffling of the obstack.  */
81   {
82     char *base = obstack_base (&obs);
83     char *new_base;
84     int result;
85     int room = obstack_room (&obs);
86
87     ASSERT (8 < room);
88     result = my_obstack_printf (&obs, "%d %s", 123, "456");
89     ASSERT (result == 7);
90     ASSERT (result == obstack_object_size (&obs));
91     new_base = obstack_base (&obs);
92     ASSERT (base == new_base);
93     ASSERT (strncmp (base, "123 456", result) == 0);
94     obstack_finish (&obs);
95   }
96
97   /* Check for generating much more output than a chunk size.  */
98   {
99     char *base = obstack_base (&obs);
100     char *new_base;
101     int result;
102     int i;
103
104     ASSERT (obstack_chunk_size (&obs) < 10000);
105     result = my_obstack_printf (&obs, "%010000d", 0);
106     ASSERT (result == 10000);
107     ASSERT (result == obstack_object_size (&obs));
108     new_base = obstack_base (&obs);
109     ASSERT (base != new_base);
110     for (i = 0; i < 10000; i++)
111       ASSERT (new_base[i] == '0');
112   }
113
114   obstack_free (&obs, NULL);
115 }
116
117 static int
118 my_obstack_printf (struct obstack *obs, const char *format, ...)
119 {
120   va_list args;
121   int ret;
122
123   va_start (args, format);
124   ret = obstack_vprintf (obs, format, args);
125   va_end (args);
126   return ret;
127 }
128
129 static void
130 test_obstack_vprintf ()
131 {
132   test_function (my_obstack_printf);
133 }
134
135 static void
136 test_obstack_printf ()
137 {
138   test_function (obstack_printf);
139 }
140
141 int
142 main (int argc, char *argv[])
143 {
144   set_program_name (argv[0]);
145
146   test_obstack_vprintf ();
147   test_obstack_printf ();
148   return 0;
149 }