1 /* Test of obstack_printf() and obstack_vprintf() functions.
2 Copyright (C) 2008-2009 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. */
32 #define ASSERT(expr) \
37 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
44 #define obstack_chunk_alloc xmalloc
45 #define obstack_chunk_free free
48 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...))
52 /* In general, be careful that arguments to obstack_* don't have
53 side effects, as not all compilers evaluate macro arguments only
56 /* Grow the obstack to near its boundary, then check that short
57 output longer than the obstack free space grows the obstack. */
59 char *base = obstack_base (&obs);
62 int room = obstack_room (&obs) - 4;
64 obstack_blank_fast (&obs, room);
65 result = my_obstack_printf (&obs, "%d %s", 123, "456");
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);
74 /* Check that strings shorter than the obstack free space don't
75 cause a reshuffling of the obstack. */
77 char *base = obstack_base (&obs);
80 int room = obstack_room (&obs);
83 result = my_obstack_printf (&obs, "%d %s", 123, "456");
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);
92 /* Check for generating much more output than a chunk size. */
94 char *base = obstack_base (&obs);
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');
109 obstack_free (&obs, NULL);
113 my_obstack_printf (struct obstack *obs, const char *format, ...)
118 va_start (args, format);
119 ret = obstack_vprintf (obs, format, args);
125 test_obstack_vprintf ()
127 test_function (my_obstack_printf);
131 test_obstack_printf ()
133 test_function (obstack_printf);
137 main (int argc, char *argv[])
139 set_program_name (argv[0]);
141 test_obstack_vprintf ();
142 test_obstack_printf ();