1 /* Test of obstack_printf() and obstack_vprintf() functions.
2 Copyright (C) 2008 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. */
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42 #define obstack_chunk_alloc xmalloc
43 #define obstack_chunk_free free
46 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...))
50 /* In general, be careful that arguments to obstack_* don't have
51 side effects, as not all compilers evaluate macro arguments only
54 /* Grow the obstack to near its boundary, then check that short
55 output longer than the obstack free space grows the obstack. */
57 char *base = obstack_base (&obs);
60 int room = obstack_room (&obs) - 4;
62 obstack_blank_fast (&obs, room);
63 result = my_obstack_printf (&obs, "%d %s", 123, "456");
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);
72 /* Check that strings shorter than the obstack free space don't
73 cause a reshuffling of the obstack. */
75 char *base = obstack_base (&obs);
78 int room = obstack_room (&obs);
81 result = my_obstack_printf (&obs, "%d %s", 123, "456");
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);
90 /* Check for generating much more output than a chunk size. */
92 char *base = obstack_base (&obs);
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');
107 obstack_free (&obs, NULL);
111 my_obstack_printf (struct obstack *obs, const char *format, ...)
116 va_start (args, format);
117 ret = obstack_vprintf (obs, format, args);
123 test_obstack_vprintf ()
125 test_function (my_obstack_printf);
129 test_obstack_printf ()
131 test_function (obstack_printf);
135 main (int argc, char *argv[])
137 test_obstack_vprintf ();
138 test_obstack_printf ();