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, don't invoke obstack_* functions inside ASSERT, as
51 not all compilers can avoid multiple side effects. */
53 /* Grow the obstack to near its boundary, then check that output
54 longer than the obstack free space grows the obstack. */
56 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 size = obstack_object_size (&obs);
66 ASSERT (result + room == size);
67 obstack_1grow (&obs, 0);
68 new_base = obstack_finish (&obs);
69 ASSERT (base != new_base);
70 ASSERT (strcmp (new_base + room, "123 456") == 0);
73 /* Check that strings shorter than the obstack free space don't
74 cause a reshuffling of the obstack. */
76 char *base = obstack_base (&obs);
80 int room = obstack_room (&obs);
83 result = my_obstack_printf (&obs, "%d %s", 123, "456");
85 size = obstack_object_size (&obs);
86 ASSERT (result == size);
87 new_base = obstack_base (&obs);
88 ASSERT (base == new_base);
89 ASSERT (strncmp (base, "123 456", result) == 0);
92 obstack_free (&obs, NULL);
96 my_obstack_printf (struct obstack *obs, const char *format, ...)
101 va_start (args, format);
102 ret = obstack_vprintf (obs, format, args);
108 test_obstack_vprintf ()
110 test_function (my_obstack_printf);
114 test_obstack_printf ()
116 test_function (obstack_printf);
120 main (int argc, char *argv[])
122 test_obstack_vprintf ();
123 test_obstack_printf ();