annotate tests/test-obstack-printf.c @ 17290:44c34bc536ac

Fix typo in comment.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 16 Jan 2013 09:38:28 -0800
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Test of obstack_printf() and obstack_vprintf() functions.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 2008-2013 Free Software Foundation, Inc.
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 (at your option) any later version.
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* Written by Eric Blake <ebb9@byu.net>, 2008. */
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 #include <config.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include <stdio.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22
12489
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
23 #include "signature.h"
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
24 SIGNATURE_CHECK (obstack_printf, int, (struct obstack *, char const *, ...));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
25 SIGNATURE_CHECK (obstack_vprintf, int, (struct obstack *, char const *,
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
26 va_list));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
27
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28 #include "obstack.h"
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29 #include "xalloc.h"
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31 #include <stdarg.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32 #include <stdlib.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
33 #include <string.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34
11040
cee1ca4b9566 Use module progname correctly in the tests that need it.
Bruno Haible <bruno@clisp.org>
parents: 10207
diff changeset
35 #include "progname.h"
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12489
diff changeset
36 #include "macros.h"
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
37
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
38 #define obstack_chunk_alloc xmalloc
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
39 #define obstack_chunk_free free
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
41 static void
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...))
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
44 struct obstack obs;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
45 obstack_init (&obs);
10207
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
46 /* In general, be careful that arguments to obstack_* don't have
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
47 side effects, as not all compilers evaluate macro arguments only
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
48 once. */
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
49
10207
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
50 /* Grow the obstack to near its boundary, then check that short
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
51 output longer than the obstack free space grows the obstack. */
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
52 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
53 char *base = obstack_base (&obs);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
54 char *new_base;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
55 int result;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
56 int room = obstack_room (&obs) - 4;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
57
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
58 obstack_blank_fast (&obs, room);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
59 result = my_obstack_printf (&obs, "%d %s", 123, "456");
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 ASSERT (result == 7);
10207
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
61 ASSERT (result + room == obstack_object_size (&obs));
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
62 obstack_1grow (&obs, 0);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
63 new_base = obstack_finish (&obs);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
64 ASSERT (base != new_base);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
65 ASSERT (strcmp (new_base + room, "123 456") == 0);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
66 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
67
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
68 /* Check that strings shorter than the obstack free space don't
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
69 cause a reshuffling of the obstack. */
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
70 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
71 char *base = obstack_base (&obs);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
72 char *new_base;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
73 int result;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
74 int room = obstack_room (&obs);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
75
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
76 ASSERT (8 < room);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
77 result = my_obstack_printf (&obs, "%d %s", 123, "456");
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
78 ASSERT (result == 7);
10207
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
79 ASSERT (result == obstack_object_size (&obs));
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
80 new_base = obstack_base (&obs);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
81 ASSERT (base == new_base);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
82 ASSERT (strncmp (base, "123 456", result) == 0);
10207
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
83 obstack_finish (&obs);
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
84 }
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
85
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
86 /* Check for generating much more output than a chunk size. */
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
87 {
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
88 char *base = obstack_base (&obs);
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
89 char *new_base;
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
90 int result;
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
91 int i;
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
92
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
93 ASSERT (obstack_chunk_size (&obs) < 10000);
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
94 result = my_obstack_printf (&obs, "%010000d", 0);
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
95 ASSERT (result == 10000);
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
96 ASSERT (result == obstack_object_size (&obs));
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
97 new_base = obstack_base (&obs);
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
98 ASSERT (base != new_base);
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
99 for (i = 0; i < 10000; i++)
e027c5e42aba Improve obstack-printf test code.
Eric Blake <ebb9@byu.net>
parents: 10205
diff changeset
100 ASSERT (new_base[i] == '0');
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
101 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
102
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
103 obstack_free (&obs, NULL);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
104 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
105
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
106 static int
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
107 my_obstack_printf (struct obstack *obs, const char *format, ...)
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
108 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
109 va_list args;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
110 int ret;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
111
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
112 va_start (args, format);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
113 ret = obstack_vprintf (obs, format, args);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
114 va_end (args);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
115 return ret;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
116 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
117
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
118 static void
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
119 test_obstack_vprintf ()
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
120 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
121 test_function (my_obstack_printf);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
122 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
123
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
124 static void
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
125 test_obstack_printf ()
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
126 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
127 test_function (obstack_printf);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
128 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
129
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
130 int
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
131 main (int argc, char *argv[])
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
132 {
11040
cee1ca4b9566 Use module progname correctly in the tests that need it.
Bruno Haible <bruno@clisp.org>
parents: 10207
diff changeset
133 set_program_name (argv[0]);
cee1ca4b9566 Use module progname correctly in the tests that need it.
Bruno Haible <bruno@clisp.org>
parents: 10207
diff changeset
134
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
135 test_obstack_vprintf ();
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
136 test_obstack_printf ();
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
137 return 0;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
138 }