maint: update copyright
[gnulib.git] / tests / test-obstack-printf.c
1 /* Test of obstack_printf() and obstack_vprintf() functions.
2    Copyright (C) 2008-2014 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2008.  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (obstack_printf, int, (struct obstack *, char const *, ...));
25 SIGNATURE_CHECK (obstack_vprintf, int, (struct obstack *, char const *,
26                                         va_list));
27
28 #include "obstack.h"
29 #include "xalloc.h"
30
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "progname.h"
36 #include "macros.h"
37
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
40
41 static void
42 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...))
43 {
44   struct obstack obs;
45   obstack_init (&obs);
46   /* In general, be careful that arguments to obstack_* don't have
47      side effects, as not all compilers evaluate macro arguments only
48      once.  */
49
50   /* Grow the obstack to near its boundary, then check that short
51      output longer than the obstack free space grows the obstack.  */
52   {
53     char *base = obstack_base (&obs);
54     char *new_base;
55     int result;
56     int room = obstack_room (&obs) - 4;
57
58     obstack_blank_fast (&obs, room);
59     result = my_obstack_printf (&obs, "%d %s", 123, "456");
60     ASSERT (result == 7);
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);
66   }
67
68   /* Check that strings shorter than the obstack free space don't
69      cause a reshuffling of the obstack.  */
70   {
71     char *base = obstack_base (&obs);
72     char *new_base;
73     int result;
74     int room = obstack_room (&obs);
75
76     ASSERT (8 < room);
77     result = my_obstack_printf (&obs, "%d %s", 123, "456");
78     ASSERT (result == 7);
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);
84   }
85
86   /* Check for generating much more output than a chunk size.  */
87   {
88     char *base = obstack_base (&obs);
89     char *new_base;
90     int result;
91     int i;
92
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');
101   }
102
103   obstack_free (&obs, NULL);
104 }
105
106 static int
107 my_obstack_printf (struct obstack *obs, const char *format, ...)
108 {
109   va_list args;
110   int ret;
111
112   va_start (args, format);
113   ret = obstack_vprintf (obs, format, args);
114   va_end (args);
115   return ret;
116 }
117
118 static void
119 test_obstack_vprintf ()
120 {
121   test_function (my_obstack_printf);
122 }
123
124 static void
125 test_obstack_printf ()
126 {
127   test_function (obstack_printf);
128 }
129
130 int
131 main (int argc, char *argv[])
132 {
133   set_program_name (argv[0]);
134
135   test_obstack_vprintf ();
136   test_obstack_printf ();
137   return 0;
138 }