fba7887c703fd3b193d347f957321c27de3781be
[gnulib.git] / tests / test-obstack-printf.c
1 /* Test of obstack_printf() and obstack_vprintf() functions.
2    Copyright (C) 2008 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 "obstack.h"
24 #include "xalloc.h"
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 #define obstack_chunk_alloc xmalloc
43 #define obstack_chunk_free free
44
45 static void
46 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...))
47 {
48   struct obstack obs;
49   obstack_init (&obs);
50   /* In general, don't invoke obstack_* functions inside ASSERT, as
51      not all compilers can avoid multiple side effects.  */
52
53   /* Grow the obstack to near its boundary, then check that output
54      longer than the obstack free space grows the obstack.  */
55   {
56     char *base = obstack_base (&obs);
57     char *new_base;
58     int result;
59     int size;
60     int room = obstack_room (&obs) - 4;
61
62     obstack_blank_fast (&obs, room);
63     result = my_obstack_printf (&obs, "%d %s", 123, "456");
64     ASSERT (result == 7);
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);
71   }
72
73   /* Check that strings shorter than the obstack free space don't
74      cause a reshuffling of the obstack.  */
75   {
76     char *base = obstack_base (&obs);
77     char *new_base;
78     int result;
79     int size;
80     int room = obstack_room (&obs);
81
82     ASSERT (8 < room);
83     result = my_obstack_printf (&obs, "%d %s", 123, "456");
84     ASSERT (result == 7);
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);
90   }
91
92   obstack_free (&obs, NULL);
93 }
94
95 static int
96 my_obstack_printf (struct obstack *obs, const char *format, ...)
97 {
98   va_list args;
99   int ret;
100
101   va_start (args, format);
102   ret = obstack_vprintf (obs, format, args);
103   va_end (args);
104   return ret;
105 }
106
107 static void
108 test_obstack_vprintf ()
109 {
110   test_function (my_obstack_printf);
111 }
112
113 static void
114 test_obstack_printf ()
115 {
116   test_function (obstack_printf);
117 }
118
119 int
120 main (int argc, char *argv[])
121 {
122   test_obstack_vprintf ();
123   test_obstack_printf ();
124   return 0;
125 }