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