1 /* Test of obstack_printf() and obstack_vprintf() functions.
2 Copyright (C) 2008-2010 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2008. */
23 #include "signature.h"
24 SIGNATURE_CHECK (obstack_printf, int, (struct obstack *, char const *, ...));
25 SIGNATURE_CHECK (obstack_vprintf, int, (struct obstack *, char const *,
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
42 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...))
46 /* In general, be careful that arguments to obstack_* don't have
47 side effects, as not all compilers evaluate macro arguments only
50 /* Grow the obstack to near its boundary, then check that short
51 output longer than the obstack free space grows the obstack. */
53 char *base = obstack_base (&obs);
56 int room = obstack_room (&obs) - 4;
58 obstack_blank_fast (&obs, room);
59 result = my_obstack_printf (&obs, "%d %s", 123, "456");
61 ASSERT (result + room == obstack_object_size (&obs));
62 obstack_1grow (&obs, 0);
63 new_base = obstack_finish (&obs);
64 ASSERT (base != new_base);
65 ASSERT (strcmp (new_base + room, "123 456") == 0);
68 /* Check that strings shorter than the obstack free space don't
69 cause a reshuffling of the obstack. */
71 char *base = obstack_base (&obs);
74 int room = obstack_room (&obs);
77 result = my_obstack_printf (&obs, "%d %s", 123, "456");
79 ASSERT (result == obstack_object_size (&obs));
80 new_base = obstack_base (&obs);
81 ASSERT (base == new_base);
82 ASSERT (strncmp (base, "123 456", result) == 0);
83 obstack_finish (&obs);
86 /* Check for generating much more output than a chunk size. */
88 char *base = obstack_base (&obs);
93 ASSERT (obstack_chunk_size (&obs) < 10000);
94 result = my_obstack_printf (&obs, "%010000d", 0);
95 ASSERT (result == 10000);
96 ASSERT (result == obstack_object_size (&obs));
97 new_base = obstack_base (&obs);
98 ASSERT (base != new_base);
99 for (i = 0; i < 10000; i++)
100 ASSERT (new_base[i] == '0');
103 obstack_free (&obs, NULL);
107 my_obstack_printf (struct obstack *obs, const char *format, ...)
112 va_start (args, format);
113 ret = obstack_vprintf (obs, format, args);
119 test_obstack_vprintf ()
121 test_function (my_obstack_printf);
125 test_obstack_printf ()
127 test_function (obstack_printf);
131 main (int argc, char *argv[])
133 set_program_name (argv[0]);
135 test_obstack_vprintf ();
136 test_obstack_printf ();