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